| 1 |
package binary |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"encoding/binary" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/plumbing" |
| 10 |
|
| 11 |
. "gopkg.in/check.v1" |
| 12 |
) |
| 13 |
|
| 14 |
func Test(t *testing.T) { TestingT(t) } |
| 15 |
|
| 16 |
type BinarySuite struct{} |
| 17 |
|
| 18 |
var _ = Suite(&BinarySuite{}) |
| 19 |
|
| 20 |
func (s *BinarySuite) TestRead(c *C) { |
| 21 |
buf := bytes.NewBuffer(nil) |
| 22 |
err := binary.Write(buf, binary.BigEndian, int64(42)) |
| 23 |
c.Assert(err, IsNil) |
| 24 |
err = binary.Write(buf, binary.BigEndian, int32(42)) |
| 25 |
c.Assert(err, IsNil) |
| 26 |
|
| 27 |
var i64 int64 |
| 28 |
var i32 int32 |
| 29 |
err = Read(buf, &i64, &i32) |
| 30 |
c.Assert(err, IsNil) |
| 31 |
c.Assert(i64, Equals, int64(42)) |
| 32 |
c.Assert(i32, Equals, int32(42)) |
| 33 |
} |
| 34 |
|
| 35 |
func (s *BinarySuite) TestReadUntil(c *C) { |
| 36 |
buf := bytes.NewBuffer([]byte("foo bar")) |
| 37 |
|
| 38 |
b, err := ReadUntil(buf, ' ') |
| 39 |
c.Assert(err, IsNil) |
| 40 |
c.Assert(b, HasLen, 3) |
| 41 |
c.Assert(string(b), Equals, "foo") |
| 42 |
} |
| 43 |
|
| 44 |
func (s *BinarySuite) TestReadUntilFromBufioReader(c *C) { |
| 45 |
buf := bufio.NewReader(bytes.NewBuffer([]byte("foo bar"))) |
| 46 |
|
| 47 |
b, err := ReadUntilFromBufioReader(buf, ' ') |
| 48 |
c.Assert(err, IsNil) |
| 49 |
c.Assert(b, HasLen, 3) |
| 50 |
c.Assert(string(b), Equals, "foo") |
| 51 |
} |
| 52 |
|
| 53 |
func (s *BinarySuite) TestReadVariableWidthInt(c *C) { |
| 54 |
buf := bytes.NewBuffer([]byte{129, 110}) |
| 55 |
|
| 56 |
i, err := ReadVariableWidthInt(buf) |
| 57 |
c.Assert(err, IsNil) |
| 58 |
c.Assert(i, Equals, int64(366)) |
| 59 |
} |
| 60 |
|
| 61 |
func (s *BinarySuite) TestReadVariableWidthIntShort(c *C) { |
| 62 |
buf := bytes.NewBuffer([]byte{19}) |
| 63 |
|
| 64 |
i, err := ReadVariableWidthInt(buf) |
| 65 |
c.Assert(err, IsNil) |
| 66 |
c.Assert(i, Equals, int64(19)) |
| 67 |
} |
| 68 |
|
| 69 |
func (s *BinarySuite) TestReadVariableWidthIntOverflow(c *C) { |
| 70 |
// A continuation byte every iteration accumulates 7 bits and a +1 |
| 71 |
// adjustment per byte; eleven such bytes pushes the running int64 |
| 72 |
// past its bound and the decoder must reject the input. |
| 73 |
buf := bytes.NewBuffer(bytes.Repeat([]byte{0xFF}, 11)) |
| 74 |
|
| 75 |
_, err := ReadVariableWidthInt(buf) |
| 76 |
c.Assert(err, Equals, ErrIntegerOverflow) |
| 77 |
} |
| 78 |
|
| 79 |
func (s *BinarySuite) TestReadVariableWidthIntBoundary(c *C) { |
| 80 |
// Crafted input that drives the running accumulator to exactly |
| 81 |
// (math.MaxInt64-127)>>7 — the largest pre-increment value for |
| 82 |
// which the next iteration would still fit in int64. Seven 0xFE |
| 83 |
// bytes followed by 0xFF land v at exactly the bound; an eighth |
| 84 |
// continuation byte forces the next iteration. With a strict |
| 85 |
// "greater than" check the bound was off by one and the |
| 86 |
// subsequent v++ <<7 wrapped through MinInt64. |
| 87 |
buf := bytes.NewBuffer([]byte{ |
| 88 |
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0x00, |
| 89 |
}) |
| 90 |
|
| 91 |
_, err := ReadVariableWidthInt(buf) |
| 92 |
c.Assert(err, Equals, ErrIntegerOverflow) |
| 93 |
} |
| 94 |
|
| 95 |
func (s *BinarySuite) TestReadUint32(c *C) { |
| 96 |
buf := bytes.NewBuffer(nil) |
| 97 |
err := binary.Write(buf, binary.BigEndian, uint32(42)) |
| 98 |
c.Assert(err, IsNil) |
| 99 |
|
| 100 |
i32, err := ReadUint32(buf) |
| 101 |
c.Assert(err, IsNil) |
| 102 |
c.Assert(i32, Equals, uint32(42)) |
| 103 |
} |
| 104 |
|
| 105 |
func (s *BinarySuite) TestReadUint16(c *C) { |
| 106 |
buf := bytes.NewBuffer(nil) |
| 107 |
err := binary.Write(buf, binary.BigEndian, uint16(42)) |
| 108 |
c.Assert(err, IsNil) |
| 109 |
|
| 110 |
i32, err := ReadUint16(buf) |
| 111 |
c.Assert(err, IsNil) |
| 112 |
c.Assert(i32, Equals, uint16(42)) |
| 113 |
} |
| 114 |
|
| 115 |
func (s *BinarySuite) TestReadHash(c *C) { |
| 116 |
expected := plumbing.NewHash("43aec75c611f22c73b27ece2841e6ccca592f285") |
| 117 |
buf := bytes.NewBuffer(nil) |
| 118 |
err := binary.Write(buf, binary.BigEndian, expected) |
| 119 |
c.Assert(err, IsNil) |
| 120 |
|
| 121 |
hash, err := ReadHash(buf) |
| 122 |
c.Assert(err, IsNil) |
| 123 |
c.Assert(hash.String(), Equals, expected.String()) |
| 124 |
} |
| 125 |
|
| 126 |
func (s *BinarySuite) TestIsBinary(c *C) { |
| 127 |
buf := bytes.NewBuffer(nil) |
| 128 |
buf.Write(bytes.Repeat([]byte{'A'}, sniffLen)) |
| 129 |
buf.Write([]byte{0}) |
| 130 |
ok, err := IsBinary(buf) |
| 131 |
c.Assert(err, IsNil) |
| 132 |
c.Assert(ok, Equals, false) |
| 133 |
|
| 134 |
buf.Reset() |
| 135 |
|
| 136 |
buf.Write(bytes.Repeat([]byte{'A'}, sniffLen-1)) |
| 137 |
buf.Write([]byte{0}) |
| 138 |
ok, err = IsBinary(buf) |
| 139 |
c.Assert(err, IsNil) |
| 140 |
c.Assert(ok, Equals, true) |
| 141 |
|
| 142 |
buf.Reset() |
| 143 |
|
| 144 |
buf.Write(bytes.Repeat([]byte{'A'}, 10)) |
| 145 |
ok, err = IsBinary(buf) |
| 146 |
c.Assert(err, IsNil) |
| 147 |
c.Assert(ok, Equals, false) |
| 148 |
} |