| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package chunkstream |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "fmt" |
| 10 "io" |
| 11 "testing" |
| 12 |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 ) |
| 15 |
| 16 func TestReader(t *testing.T) { |
| 17 Convey(`An empty Buffer with generation 42`, t, func() { |
| 18 b := Buffer{ |
| 19 gen: 42, |
| 20 } |
| 21 |
| 22 for _, chunks := range [][]*testChunk{ |
| 23 []*testChunk(nil), |
| 24 {tc()}, |
| 25 {tc(0)}, |
| 26 {tc(0, 0, 1)}, |
| 27 {tc(0), tc(0), tc(0)}, |
| 28 {tc(1, 2, 3, 0)}, |
| 29 {tc(1, 2), tc(3, 0, 4, 0), tc(0, 5)}, |
| 30 } { |
| 31 Convey(fmt.Sprintf(`With Chunks %v`, chunks), func() { |
| 32 aggregate := []byte{} |
| 33 size := int64(0) |
| 34 for _, c := range chunks { |
| 35 aggregate = append(aggregate, c.Bytes().
..) |
| 36 size += int64(c.Len()) |
| 37 b.Append(c) |
| 38 } |
| 39 |
| 40 // This is pretty stupid to do, but for testing
it's a nice edge case |
| 41 // to hammer. |
| 42 Convey(`A Reader with limit 0`, func() { |
| 43 br := b.ReaderLimit(0) |
| 44 |
| 45 So(br.Remaining(), ShouldEqual, 0) |
| 46 So(br.Consumed(), ShouldEqual, 0) |
| 47 |
| 48 Convey(`Read() returns EOF.`, func() { |
| 49 buf := make([]byte, 16) |
| 50 a, err := br.Read(buf) |
| 51 So(err, ShouldEqual, io.EOF) |
| 52 So(a, ShouldEqual, 0) |
| 53 }) |
| 54 |
| 55 Convey(`ReadByte() returns EOF.`, func()
{ |
| 56 _, err := br.ReadByte() |
| 57 So(err, ShouldEqual, io.EOF) |
| 58 }) |
| 59 |
| 60 Convey(`Skip() panics.`, func() { |
| 61 So(func() { br.Skip(1) }, Should
Panic) |
| 62 }) |
| 63 |
| 64 Convey(`Index() returns -1.`, func() { |
| 65 So(br.Index([]byte{0}), ShouldEq
ual, -1) |
| 66 }) |
| 67 }) |
| 68 |
| 69 Convey(`An unlimited Reader`, func() { |
| 70 br := b.Reader() |
| 71 So(br.Remaining(), ShouldEqual, b.Len()) |
| 72 So(br.Consumed(), ShouldEqual, 0) |
| 73 |
| 74 Convey(`Can Read() the full block of dat
a.`, func() { |
| 75 buf := make([]byte, len(aggregat
e)) |
| 76 amt, err := br.Read(buf) |
| 77 So(amt, ShouldEqual, len(aggrega
te)) |
| 78 So(err, ShouldEqual, io.EOF) |
| 79 So(buf[:amt], ShouldResemble, ag
gregate) |
| 80 |
| 81 Convey(`Subsequent Read() will r
eturn io.EOF.`, func() { |
| 82 amt, err := br.Read(buf) |
| 83 So(amt, ShouldEqual, 0) |
| 84 So(err, ShouldEqual, io.
EOF) |
| 85 }) |
| 86 }) |
| 87 |
| 88 Convey(`Can Read() the full block of dat
a byte-by-byte.`, func() { |
| 89 buf := make([]byte, 1) |
| 90 for i, d := range aggregate { |
| 91 amt, err := br.Read(buf) |
| 92 if i == len(aggregate)-1
{ |
| 93 So(err, ShouldEq
ual, io.EOF) |
| 94 } else { |
| 95 So(err, ShouldBe
Nil) |
| 96 } |
| 97 |
| 98 So(amt, ShouldEqual, 1) |
| 99 So(buf[0], ShouldEqual,
d) |
| 100 } |
| 101 |
| 102 Convey(`Subsequent Read() will r
eturn io.EOF.`, func() { |
| 103 amt, err := br.Read(buf) |
| 104 So(amt, ShouldEqual, 0) |
| 105 So(err, ShouldEqual, io.
EOF) |
| 106 }) |
| 107 }) |
| 108 |
| 109 Convey(`Can ReadByte() the full block of
data.`, func() { |
| 110 for _, d := range aggregate { |
| 111 b, err := br.ReadByte() |
| 112 So(err, ShouldBeNil) |
| 113 So(b, ShouldEqual, d) |
| 114 } |
| 115 |
| 116 Convey(`Subsequent ReadByte() wi
ll return io.EOF.`, func() { |
| 117 _, err := br.ReadByte() |
| 118 So(err, ShouldEqual, io.
EOF) |
| 119 }) |
| 120 }) |
| 121 |
| 122 Convey(`Will panic if the Buffer generat
ion changes.`, func() { |
| 123 b.gen++ |
| 124 So(func() { br.ReadByte() }, Sho
uldPanic) |
| 125 }) |
| 126 |
| 127 for _, needle := range [][]byte{ |
| 128 {0}, |
| 129 {0, 0}, |
| 130 {0, 0, 0, 0}, |
| 131 } { |
| 132 expected := bytes.Index(aggregat
e, needle) |
| 133 Convey(fmt.Sprintf(`Index() of %
v returns %v.`, needle, expected), func() { |
| 134 So(br.Index(needle), Sho
uldEqual, expected) |
| 135 }) |
| 136 } |
| 137 }) |
| 138 }) |
| 139 } |
| 140 |
| 141 Convey(`An unlimited Reader`, func() { |
| 142 br := b.Reader() |
| 143 |
| 144 Convey(`Has chunksRemaining() value of 0.`, func() { |
| 145 So(br.chunkRemaining(), ShouldEqual, 0) |
| 146 }) |
| 147 }) |
| 148 |
| 149 Convey(`With chunks [{0x01, 0x02, 0x00}, {0x00, 0x03}, {0x00}, {
0x00, 0x00}, {0x04}]`, func() { |
| 150 for _, c := range []Chunk{tc(1, 2, 0), tc(0, 3), tc(0),
tc(0, 0), tc(4)} { |
| 151 b.Append(c) |
| 152 } |
| 153 |
| 154 Convey(`An unlimited Reader`, func() { |
| 155 br := b.Reader() |
| 156 |
| 157 Convey(`Should have Remaining() value of 9.`, fu
nc() { |
| 158 So(br.Remaining(), ShouldEqual, 9) |
| 159 }) |
| 160 |
| 161 Convey(`Can spawn a limited clone.`, func() { |
| 162 buf := bytes.Buffer{} |
| 163 _, err := buf.ReadFrom(br.CloneLimit(7)) |
| 164 So(err, ShouldBeNil) |
| 165 So(buf.Bytes(), ShouldResemble, []byte{0
x01, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00}) |
| 166 }) |
| 167 |
| 168 for _, s := range []struct { |
| 169 needle []byte |
| 170 index int64 |
| 171 }{ |
| 172 {[]byte(nil), 0}, |
| 173 {[]byte{0x01}, 0}, |
| 174 {[]byte{0x01, 0x02}, 0}, |
| 175 {[]byte{0x02, 0x00}, 1}, |
| 176 {[]byte{0x00}, 2}, |
| 177 {[]byte{0x00, 0x00}, 2}, |
| 178 {[]byte{0x00, 0x00, 0x00}, 5}, |
| 179 {[]byte{0x03, 0x00, 0x00}, 4}, |
| 180 } { |
| 181 Convey(fmt.Sprintf(`Has Index %v for nee
dle %v`, s.index, s.needle), func() { |
| 182 So(br.Index(s.needle), ShouldEqu
al, s.index) |
| 183 }) |
| 184 } |
| 185 }) |
| 186 |
| 187 Convey(`A Reader with a limit of 6`, func() { |
| 188 br := b.ReaderLimit(6) |
| 189 |
| 190 Convey(`Should have Remaining() value of 6.`, fu
nc() { |
| 191 So(br.Remaining(), ShouldEqual, 6) |
| 192 }) |
| 193 |
| 194 Convey(`Has index of -1 for needle [0x00, 0x04]`
, func() { |
| 195 So(br.Index([]byte{0x00, 0x04}), ShouldE
qual, -1) |
| 196 }) |
| 197 }) |
| 198 |
| 199 Convey(`A Reader with a limit of 20`, func() { |
| 200 br := b.ReaderLimit(20) |
| 201 |
| 202 Convey(`Should have Remaining() value of 9.`, fu
nc() { |
| 203 So(br.Remaining(), ShouldEqual, 9) |
| 204 }) |
| 205 }) |
| 206 }) |
| 207 |
| 208 Convey(`With chunks [{0x0F}..{0x00}]`, func() { |
| 209 for i := 0x0F; i >= 0x00; i-- { |
| 210 b.Append(tc(byte(i))) |
| 211 } |
| 212 br := b.Reader() |
| 213 |
| 214 Convey(`Has index of -1 for needle [0x0f, 0x10]`, func()
{ |
| 215 So(br.Index([]byte{0x0f, 0x10}), ShouldEqual, -1
) |
| 216 }) |
| 217 |
| 218 for _, s := range []struct { |
| 219 needle []byte |
| 220 index int64 |
| 221 }{ |
| 222 {[]byte{0x0F}, 0}, |
| 223 {[]byte{0x04, 0x03, 0x02}, 11}, |
| 224 {[]byte{0x01, 0x00}, 14}, |
| 225 {[]byte{0x00}, 15}, |
| 226 {[]byte{0x00, 0xFF}, -1}, |
| 227 } { |
| 228 Convey(fmt.Sprintf(`Has Index %v for needle %v`,
s.index, s.needle), func() { |
| 229 So(br.Index(s.needle), ShouldEqual, s.in
dex) |
| 230 }) |
| 231 } |
| 232 }) |
| 233 }) |
| 234 } |
| OLD | NEW |