| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package recordio | 5 package recordio |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "encoding/binary" | 9 "encoding/binary" |
| 10 "errors" | 10 "errors" |
| 11 "fmt" |
| 11 "io" | 12 "io" |
| 12 "io/ioutil" | 13 "io/ioutil" |
| 13 "testing" | 14 "testing" |
| 14 | 15 |
| 15 . "github.com/smartystreets/goconvey/convey" | 16 . "github.com/smartystreets/goconvey/convey" |
| 16 ) | 17 ) |
| 17 | 18 |
| 18 // plainReader implements the io.Reader interface on top of a bytes.Buffer; | 19 // plainReader implements the io.Reader interface on top of a bytes.Buffer; |
| 19 // however, it does not also implement io.ByteReader. | 20 // however, it does not also implement io.ByteReader. |
| 20 type plainReader struct { | 21 type plainReader struct { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 51 return 0, r.readByteErr | 52 return 0, r.readByteErr |
| 52 } | 53 } |
| 53 | 54 |
| 54 b, err = r.buf.ReadByte() | 55 b, err = r.buf.ReadByte() |
| 55 if err == nil { | 56 if err == nil { |
| 56 r.readBytes++ | 57 r.readBytes++ |
| 57 } | 58 } |
| 58 return | 59 return |
| 59 } | 60 } |
| 60 | 61 |
| 62 func btos(b ...[]byte) []string { |
| 63 s := make([]string, len(b)) |
| 64 for i, v := range b { |
| 65 s[i] = string(v) |
| 66 } |
| 67 return s |
| 68 } |
| 69 |
| 61 // TestReader tests the default Reader implementation, "reader". | 70 // TestReader tests the default Reader implementation, "reader". |
| 62 func TestReader(t *testing.T) { | 71 func TestReader(t *testing.T) { |
| 63 t.Parallel() | 72 t.Parallel() |
| 64 | 73 |
| 65 Convey(`A frame reader with max size 1MB using a plain io.Reader`, t, fu
nc() { | 74 Convey(`A frame reader with max size 1MB using a plain io.Reader`, t, fu
nc() { |
| 66 maxSize := int64(1024 * 1024) | 75 maxSize := int64(1024 * 1024) |
| 67 tr := plainReader{} | 76 tr := plainReader{} |
| 68 r := NewReader(&tr, maxSize) | 77 r := NewReader(&tr, maxSize) |
| 69 | 78 |
| 70 Convey(`Will return io.EOF with an empty reader.`, func() { | 79 Convey(`Will return io.EOF with an empty reader.`, func() { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 207 |
| 199 // Have "ReadByte()" calls ignore the configured error.
This will cause | 208 // Have "ReadByte()" calls ignore the configured error.
This will cause |
| 200 // the frame size to be read without incident, but the f
rame data to still | 209 // the frame size to be read without incident, but the f
rame data to still |
| 201 // return an error. | 210 // return an error. |
| 202 tr.err = errors.New("test: test-induced error") | 211 tr.err = errors.New("test: test-induced error") |
| 203 data, err := r.ReadFrameAll() | 212 data, err := r.ReadFrameAll() |
| 204 So(err, ShouldEqual, tr.err) | 213 So(err, ShouldEqual, tr.err) |
| 205 }) | 214 }) |
| 206 }) | 215 }) |
| 207 } | 216 } |
| 217 |
| 218 func TestSplit(t *testing.T) { |
| 219 t.Parallel() |
| 220 |
| 221 Convey(`Testing Split`, t, func() { |
| 222 for _, v := range [][]string{ |
| 223 {}, |
| 224 {""}, |
| 225 {"", "foo", ""}, |
| 226 {"foo", "bar", "baz"}, |
| 227 } { |
| 228 Convey(fmt.Sprintf(`Can Split stream: %#v`, v), func() { |
| 229 // Write frames to "buf". |
| 230 var buf bytes.Buffer |
| 231 for _, s := range v { |
| 232 _, err := WriteFrame(&buf, []byte(s)) |
| 233 if err != nil { |
| 234 panic(err) |
| 235 } |
| 236 } |
| 237 |
| 238 // Confirm that Split works. |
| 239 sp, err := Split(buf.Bytes()) |
| 240 So(err, ShouldBeNil) |
| 241 So(btos(sp...), ShouldResemble, v) |
| 242 }) |
| 243 } |
| 244 |
| 245 Convey(`Will refuse to split a frame that is too large.`, func()
{ |
| 246 _, err := Split([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF
F, 0xFF, 0xFF, 0x00}) |
| 247 So(err, ShouldEqual, ErrFrameTooLarge) |
| 248 }) |
| 249 |
| 250 Convey(`Will fail to split if there aren't enough bytes.`, func(
) { |
| 251 sp, err := Split([]byte{0x01, 0xAA, 0x02}) // 1-byte {0x
AA}, 2-bytes ... EOF! |
| 252 So(sp, ShouldResemble, [][]byte{{0xAA}}) |
| 253 So(err, ShouldEqual, ErrFrameTooLarge) |
| 254 }) |
| 255 }) |
| 256 } |
| OLD | NEW |