-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpacket-v1_test.go
99 lines (90 loc) · 2.34 KB
/
packet-v1_test.go
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
package macaroon
import (
"strconv"
"strings"
"testing"
"unicode"
qt "github.com/frankban/quicktest"
)
func TestAppendPacket(t *testing.T) {
c := qt.New(t)
data, ok := appendPacketV1(nil, "field", []byte("some data"))
c.Assert(ok, qt.Equals, true)
c.Assert(string(data), qt.Equals, "0014field some data\n")
data, ok = appendPacketV1(data, "otherfield", []byte("more and more data"))
c.Assert(ok, qt.Equals, true)
c.Assert(string(data), qt.Equals, "0014field some data\n0022otherfield more and more data\n")
}
func TestAppendPacketTooBig(t *testing.T) {
c := qt.New(t)
data, ok := appendPacketV1(nil, "field", make([]byte, 65532))
c.Assert(ok, qt.Equals, false)
c.Assert(data, qt.IsNil)
}
var parsePacketV1Tests = []struct {
data string
expect packetV1
expectErr string
}{{
expectErr: "packet too short",
}, {
data: "0014field some data\n",
expect: packetV1{
fieldName: []byte("field"),
data: []byte("some data"),
totalLen: 20,
},
}, {
data: "0015field some data\n",
expectErr: "packet size too big",
}, {
data: "0003a\n",
expectErr: "packet size too small",
}, {
data: "0014fieldwithoutanyspaceordata\n",
expectErr: "cannot parse field name",
}, {
data: "fedcsomefield " + strings.Repeat("x", 0xfedc-len("0000somefield \n")) + "\n",
expect: packetV1{
fieldName: []byte("somefield"),
data: []byte(strings.Repeat("x", 0xfedc-len("0000somefield \n"))),
totalLen: 0xfedc,
},
}, {
data: "zzzzbadpacketsizenomacaroon",
expectErr: "cannot parse size",
}}
func TestParsePacketV1(t *testing.T) {
c := qt.New(t)
for i, test := range parsePacketV1Tests {
c.Logf("test %d: %q", i, truncate(test.data))
p, err := parsePacketV1([]byte(test.data))
if test.expectErr != "" {
c.Assert(err, qt.ErrorMatches, test.expectErr)
c.Assert(p, packetEquals, packetV1{})
continue
}
c.Assert(err, qt.Equals, nil)
c.Assert(p, packetEquals, test.expect)
}
}
func truncate(d string) string {
if len(d) > 50 {
return d[0:50] + "..."
}
return d
}
func TestAsciiHex(t *testing.T) {
c := qt.New(t)
for b := 0; b < 256; b++ {
n, err := strconv.ParseInt(string(b), 16, 8)
value, ok := asciiHex(byte(b))
if err != nil || unicode.IsUpper(rune(b)) {
c.Assert(ok, qt.Equals, false)
c.Assert(value, qt.Equals, 0)
} else {
c.Assert(ok, qt.Equals, true)
c.Assert(value, qt.Equals, int(n))
}
}
}