-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhelpers_test.go
194 lines (161 loc) · 4.84 KB
/
helpers_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package otr3
import (
"encoding/hex"
"io"
"math/big"
"reflect"
"regexp"
"testing"
)
func assertEquals(t *testing.T, actual, expected interface{}) {
if actual != expected {
t.Errorf("Expected:\n%#v \n"+
"to equal:\n%#v\n", actual, expected)
}
}
func assertNotEquals(t *testing.T, actual, expected interface{}) {
if actual == expected {
t.Errorf("Expected:\n%#v \n"+
"to not equal:\n%#v\n", actual, expected)
}
}
func isNil(actual interface{}) bool {
val := reflect.ValueOf(actual)
switch val.Kind() {
case reflect.Invalid:
return true
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return val.IsNil()
default:
return actual == nil
}
}
func assertNil(t *testing.T, actual interface{}) {
if !isNil(actual) {
t.Errorf("Expected:\n%#v \n"+
"to be nil\n", actual)
}
}
func assertTrue(t *testing.T, actual bool) {
if !actual {
t.Errorf("Expected: %#v to be true\n", actual)
}
}
func assertFalse(t *testing.T, actual bool) {
if actual {
t.Errorf("Expected: %#v to be false\n", actual)
}
}
func assertNotNil(t *testing.T, actual interface{}) {
if isNil(actual) {
t.Errorf("Expected:\n%#v \n"+
"to not be nil\n", actual)
}
}
func assertDeepEquals(t *testing.T, actual, expected interface{}) {
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected:\n%#v \n"+
"to equal:\n%#v\n", actual, expected)
}
}
func assertMatches(t *testing.T, actual, expected string) {
matches, err := regexp.MatchString("^"+expected+"$", actual)
if err != nil {
t.Errorf("Expected:\n%#v \n"+
"to match:\n%#v - Can't compile regex: %s\n", actual, expected, err.Error())
}
if !matches {
t.Errorf("Expected:\n%#v \n"+
"to match:\n%#v\n", actual, expected)
}
}
func dhMsgType(msg []byte) byte {
return msg[2]
}
func dhMsgVersion(msg []byte) uint16 {
_, protocolVersion, _ := ExtractShort(msg)
return protocolVersion
}
func bytesFromHex(s string) []byte {
val, _ := hex.DecodeString(s)
return val
}
// bnFromHex is a test utility that doesn't take into account possible errors. Thus, make sure to only call it with valid hexadecimal strings (of even length)
func bnFromHex(s string) *big.Int {
res, _ := new(big.Int).SetString(s, 16)
return res
}
// parseIntoPrivateKey is a test utility that doesn't take into account possible errors. Thus, make sure to only call it with valid values
// it only parses DSA keys right now
func parseIntoPrivateKey(hexString string) PrivateKey {
b, _ := hex.DecodeString(hexString)
pk := new(DSAPrivateKey)
pk.Parse(b)
return pk
}
func newConversation(v otrVersion, rand io.Reader) *Conversation {
var p policy
switch v {
case otrV3{}:
p = allowV3
case otrV2{}:
p = allowV2
}
akeNotStarted := new(ake)
akeNotStarted.state = authStateNone{}
return &Conversation{
version: v,
Rand: rand,
smp: smp{
state: smpStateExpect1{},
},
ake: akeNotStarted,
Policies: policies(p),
fragmentSize: 65535, //we are not testing fragmentation by default
ourInstanceTag: 0x101, //every conversation should be able to talk to each other
theirInstanceTag: 0x101,
}
}
func (c *Conversation) expectMessageEvent(t *testing.T, f func(), expectedEvent MessageEvent, expectedMessage []byte, expectedError error) {
called := false
c.messageEventHandler = dynamicMessageEventHandler{func(event MessageEvent, message []byte, err error, trace ...interface{}) {
assertDeepEquals(t, event, expectedEvent)
assertDeepEquals(t, message, expectedMessage)
assertDeepEquals(t, err, expectedError)
called = true
}}
f()
assertEquals(t, called, true)
}
func (c *Conversation) doesntExpectMessageEvent(t *testing.T, f func()) {
c.messageEventHandler = dynamicMessageEventHandler{func(event MessageEvent, message []byte, err error, trace ...interface{}) {
t.Errorf("Didn't expect a message event, but got: %v with msg %v and error %#v", event, message, err)
}}
f()
}
func (c *Conversation) expectSMPEvent(t *testing.T, f func(), expectedEvent SMPEvent, expectedProgress int, expectedQuestion string) {
called := false
c.smpEventHandler = dynamicSMPEventHandler{func(event SMPEvent, progressPercent int, question string) {
assertEquals(t, event, expectedEvent)
assertEquals(t, progressPercent, expectedProgress)
assertEquals(t, question, expectedQuestion)
called = true
}}
f()
assertEquals(t, called, true)
}
func (c *Conversation) expectSecurityEvent(t *testing.T, f func(), expectedEvent SecurityEvent) {
called := false
c.securityEventHandler = dynamicSecurityEventHandler{func(event SecurityEvent) {
assertEquals(t, event, expectedEvent)
called = true
}}
f()
assertEquals(t, called, true)
}
func (c *Conversation) doesntExpectSecurityEvent(t *testing.T, f func()) {
c.securityEventHandler = dynamicSecurityEventHandler{func(event SecurityEvent) {
t.Errorf("Didn't expect a security event, but got: %v", event)
}}
f()
}