1package binary
2
3import (
4 "bytes"
5 "encoding/binary"
6 "testing"
7
8 . "gopkg.in/check.v1"
9 "gopkg.in/src-d/go-git.v4/plumbing"
10)
11
12func Test(t *testing.T) { TestingT(t) }
13
14type BinarySuite struct{}
15
16var _ = Suite(&BinarySuite{})
17
18func (s *BinarySuite) TestRead(c *C) {
19 buf := bytes.NewBuffer(nil)
20 err := binary.Write(buf, binary.BigEndian, int64(42))
21 c.Assert(err, IsNil)
22 err = binary.Write(buf, binary.BigEndian, int32(42))
23 c.Assert(err, IsNil)
24
25 var i64 int64
26 var i32 int32
27 err = Read(buf, &i64, &i32)
28 c.Assert(err, IsNil)
29 c.Assert(i64, Equals, int64(42))
30 c.Assert(i32, Equals, int32(42))
31}
32
33func (s *BinarySuite) TestReadUntil(c *C) {
34 buf := bytes.NewBuffer([]byte("foo bar"))
35
36 b, err := ReadUntil(buf, ' ')
37 c.Assert(err, IsNil)
38 c.Assert(b, HasLen, 3)
39 c.Assert(string(b), Equals, "foo")
40}
41
42func (s *BinarySuite) TestReadVariableWidthInt(c *C) {
43 buf := bytes.NewBuffer([]byte{129, 110})
44
45 i, err := ReadVariableWidthInt(buf)
46 c.Assert(err, IsNil)
47 c.Assert(i, Equals, int64(366))
48}
49
50func (s *BinarySuite) TestReadVariableWidthIntShort(c *C) {
51 buf := bytes.NewBuffer([]byte{19})
52
53 i, err := ReadVariableWidthInt(buf)
54 c.Assert(err, IsNil)
55 c.Assert(i, Equals, int64(19))
56}
57
58func (s *BinarySuite) TestReadUint32(c *C) {
59 buf := bytes.NewBuffer(nil)
60 err := binary.Write(buf, binary.BigEndian, uint32(42))
61 c.Assert(err, IsNil)
62
63 i32, err := ReadUint32(buf)
64 c.Assert(err, IsNil)
65 c.Assert(i32, Equals, uint32(42))
66}
67
68func (s *BinarySuite) TestReadUint16(c *C) {
69 buf := bytes.NewBuffer(nil)
70 err := binary.Write(buf, binary.BigEndian, uint16(42))
71 c.Assert(err, IsNil)
72
73 i32, err := ReadUint16(buf)
74 c.Assert(err, IsNil)
75 c.Assert(i32, Equals, uint16(42))
76}
77
78func (s *BinarySuite) TestReadHash(c *C) {
79 expected := plumbing.NewHash("43aec75c611f22c73b27ece2841e6ccca592f285")
80 buf := bytes.NewBuffer(nil)
81 err := binary.Write(buf, binary.BigEndian, expected)
82 c.Assert(err, IsNil)
83
84 hash, err := ReadHash(buf)
85 c.Assert(err, IsNil)
86 c.Assert(hash.String(), Equals, expected.String())
87}
88
89func (s *BinarySuite) TestIsBinary(c *C) {
90 buf := bytes.NewBuffer(nil)
91 buf.Write(bytes.Repeat([]byte{'A'}, sniffLen))
92 buf.Write([]byte{0})
93 ok, err := IsBinary(buf)
94 c.Assert(err, IsNil)
95 c.Assert(ok, Equals, false)
96
97 buf.Reset()
98
99 buf.Write(bytes.Repeat([]byte{'A'}, sniffLen-1))
100 buf.Write([]byte{0})
101 ok, err = IsBinary(buf)
102 c.Assert(err, IsNil)
103 c.Assert(ok, Equals, true)
104
105 buf.Reset()
106
107 buf.Write(bytes.Repeat([]byte{'A'}, 10))
108 ok, err = IsBinary(buf)
109 c.Assert(err, IsNil)
110 c.Assert(ok, Equals, false)
111}