aboutsummaryrefslogtreecommitdiffstats
path: root/utils/binary/read_test.go
blob: 22867c259ac7cd594c4f5a7f760d961f14556437 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package binary

import (
	"bufio"
	"bytes"
	"encoding/binary"
	"testing"

	. "gopkg.in/check.v1"
	"gopkg.in/src-d/go-git.v4/plumbing"
)

func Test(t *testing.T) { TestingT(t) }

type BinarySuite struct{}

var _ = Suite(&BinarySuite{})

func (s *BinarySuite) TestRead(c *C) {
	buf := bytes.NewBuffer(nil)
	err := binary.Write(buf, binary.BigEndian, int64(42))
	c.Assert(err, IsNil)
	err = binary.Write(buf, binary.BigEndian, int32(42))
	c.Assert(err, IsNil)

	var i64 int64
	var i32 int32
	err = Read(buf, &i64, &i32)
	c.Assert(err, IsNil)
	c.Assert(i64, Equals, int64(42))
	c.Assert(i32, Equals, int32(42))
}

func (s *BinarySuite) TestReadUntil(c *C) {
	buf := bytes.NewBuffer([]byte("foo bar"))

	b, err := ReadUntil(buf, ' ')
	c.Assert(err, IsNil)
	c.Assert(b, HasLen, 3)
	c.Assert(string(b), Equals, "foo")
}

func (s *BinarySuite) TestReadUntilFromBufioReader(c *C) {
	buf := bufio.NewReader(bytes.NewBuffer([]byte("foo bar")))

	b, err := ReadUntilFromBufioReader(buf, ' ')
	c.Assert(err, IsNil)
	c.Assert(b, HasLen, 3)
	c.Assert(string(b), Equals, "foo")
}

func (s *BinarySuite) TestReadVariableWidthInt(c *C) {
	buf := bytes.NewBuffer([]byte{129, 110})

	i, err := ReadVariableWidthInt(buf)
	c.Assert(err, IsNil)
	c.Assert(i, Equals, int64(366))
}

func (s *BinarySuite) TestReadVariableWidthIntShort(c *C) {
	buf := bytes.NewBuffer([]byte{19})

	i, err := ReadVariableWidthInt(buf)
	c.Assert(err, IsNil)
	c.Assert(i, Equals, int64(19))
}

func (s *BinarySuite) TestReadUint32(c *C) {
	buf := bytes.NewBuffer(nil)
	err := binary.Write(buf, binary.BigEndian, uint32(42))
	c.Assert(err, IsNil)

	i32, err := ReadUint32(buf)
	c.Assert(err, IsNil)
	c.Assert(i32, Equals, uint32(42))
}

func (s *BinarySuite) TestReadUint16(c *C) {
	buf := bytes.NewBuffer(nil)
	err := binary.Write(buf, binary.BigEndian, uint16(42))
	c.Assert(err, IsNil)

	i32, err := ReadUint16(buf)
	c.Assert(err, IsNil)
	c.Assert(i32, Equals, uint16(42))
}

func (s *BinarySuite) TestReadHash(c *C) {
	expected := plumbing.NewHash("43aec75c611f22c73b27ece2841e6ccca592f285")
	buf := bytes.NewBuffer(nil)
	err := binary.Write(buf, binary.BigEndian, expected)
	c.Assert(err, IsNil)

	hash, err := ReadHash(buf)
	c.Assert(err, IsNil)
	c.Assert(hash.String(), Equals, expected.String())
}

func (s *BinarySuite) TestIsBinary(c *C) {
	buf := bytes.NewBuffer(nil)
	buf.Write(bytes.Repeat([]byte{'A'}, sniffLen))
	buf.Write([]byte{0})
	ok, err := IsBinary(buf)
	c.Assert(err, IsNil)
	c.Assert(ok, Equals, false)

	buf.Reset()

	buf.Write(bytes.Repeat([]byte{'A'}, sniffLen-1))
	buf.Write([]byte{0})
	ok, err = IsBinary(buf)
	c.Assert(err, IsNil)
	c.Assert(ok, Equals, true)

	buf.Reset()

	buf.Write(bytes.Repeat([]byte{'A'}, 10))
	ok, err = IsBinary(buf)
	c.Assert(err, IsNil)
	c.Assert(ok, Equals, false)
}