-
Notifications
You must be signed in to change notification settings - Fork 46
/
util_test.go
178 lines (159 loc) · 4.27 KB
/
util_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package weave
import (
"encoding/hex"
"encoding/json"
"strings"
"testing"
"github.com/iov-one/weave/errors"
"github.com/iov-one/weave/weavetest/assert"
)
func TestHex(t *testing.T) {
cases := map[string]struct {
orig []byte
ser string
invalid string
}{
"decimal": {[]byte{01, 02}, `"0102"`, `"012"`},
"hex": {[]byte{0xFF, 0x14, 0x56}, `"FF1456"`, `FF1456`},
"empty": {[]byte{}, `""`, `"`},
}
unmarshalHex := func(bz []byte, out *[]byte) (err error) {
var s string
err = json.Unmarshal(bz, &s)
if err != nil {
return errors.Wrap(err, "parse string")
}
// and interpret that string as hex
val, err := hex.DecodeString(s)
if err != nil {
return err
}
// only update object on success
*out = val
return nil
}
marshalHex := func(bytes []byte) ([]byte, error) {
s := strings.ToUpper(hex.EncodeToString(bytes))
return json.Marshal(s)
}
for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
// marshal as expected
bz, err := marshalHex(tc.orig)
assert.Nil(t, err)
ser := []byte(tc.ser)
assert.Equal(t, ser, bz)
// properly parse
in := []byte{}
err = unmarshalHex(ser, &in)
assert.Nil(t, err)
assert.Equal(t, tc.orig, in)
// failure returns error and doesn't affect input
err = unmarshalHex([]byte(tc.invalid), &in)
assert.Equal(t, true, err != nil)
assert.Equal(t, tc.orig, in)
})
}
}
func TestAddress(t *testing.T) {
bad := Address{1, 3, 5}
assert.Equal(t, true, bad.Validate() != nil)
// creating address
bz := []byte("bling")
addr := NewAddress(bz)
assert.Nil(t, addr.Validate())
assert.Equal(t, false, addr.Equals(bz))
assert.Equal(t, false, addr.Equals(bad))
// marshalling
foo := addr.String()
assert.Equal(t, 2*AddressLength, len(foo))
ser, err := addr.MarshalJSON()
assert.Nil(t, err)
addr3 := Address{}
err = addr3.UnmarshalJSON(ser)
assert.Nil(t, err)
assert.Equal(t, true, addr.Equals(addr3))
}
func TestCondition(t *testing.T) {
other := NewCondition("some", "such", []byte("data"))
failure, err := hex.DecodeString("736967732F656432353531392F16E290A51B2B136C2C213884D03B8BAE483D6133F0A3D110FED3890E0A5A4E18")
assert.Nil(t, err)
data, err := hex.DecodeString("16E290A51B2B136C2C213884D03B8BAE483D6133F0A3D110FED3890E0A5A4E18")
assert.Nil(t, err)
cases := map[string]struct {
perm Condition
isError bool
ext string
typ string
data []byte
serial string
}{
"bad format without data separator": {
[]byte("fo6/ds2qa"), true, "", "", nil, "",
},
"invalid ext format": {
NewCondition("a.b", "dfr", []byte{34}), true, "", "", nil, "",
},
"good format": {
[]byte("Foo/B4r/BZZ"),
false,
"Foo",
"B4r",
[]byte("BZZ"),
"Foo/B4r/425A5A",
},
"non-ascii data": {
NewCondition("help", "W1N", []byte{0xCA, 0xFE}),
false,
"help",
"W1N",
[]byte{0xCA, 0xFE},
"help/W1N/CAFE",
},
// some weird failure from random test case
// turns out to do with 0xa (newline) character in data
"including newline character": {
failure,
false,
"sigs",
"ed25519",
data,
"sigs/ed25519/16E290A51B2B136C2C213884D03B8BAE483D6133F0A3D110FED3890E0A5A4E18",
},
}
for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
ext, typ, data, err := tc.perm.Parse()
if tc.isError {
assert.Equal(t, true, err != nil)
assert.Equal(t, true, tc.perm.Validate() != nil)
return
}
// make sure parse matches
assert.Nil(t, err)
assert.Nil(t, tc.perm.Validate())
assert.Equal(t, tc.ext, ext)
assert.Equal(t, tc.typ, typ)
assert.Equal(t, tc.data, data)
// equal should pass with proper bytes
cp := NewCondition(ext, typ, data)
assert.Equal(t, true, tc.perm.Equals(cp))
// doesn't match arbitrary other permission
assert.Equal(t, false, tc.perm.Equals(other))
addr := tc.perm.Address()
assert.Nil(t, addr.Validate())
assert.Equal(t, false, other.Address().Equals(addr))
// make sure we get expected string
assert.Equal(t, tc.serial, tc.perm.String())
})
}
}
func TestEmpty(t *testing.T) {
var addr Address
var perm Condition
badPerm := Condition{0xFA, 0xDE}
assert.Equal(t, "(nil)", addr.String())
assert.Nil(t, perm.Address())
assert.Equal(t, "Invalid Condition: FADE", badPerm.String())
assert.Equal(t, "Invalid Condition: ", perm.String())
}