-
Notifications
You must be signed in to change notification settings - Fork 14
/
query_test.go
192 lines (152 loc) · 5.82 KB
/
query_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
package otr3
import (
"testing"
)
func Test_receiveQueryMessage_ignoreVersion1(t *testing.T) {
queryMsg := []byte("?OTR?")
c := &Conversation{Policies: policies(allowV2 | allowV3)}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
msg, err := c.receiveQueryMessage(queryMsg)
expError := OtrError{msg: "unsupported OTR version", conflict: false}
assertEquals(t, err, expError)
assertNil(t, msg)
}
func Test_receiveQueryMessage_ignoreVersion1AndSupportVersion2(t *testing.T) {
queryMsg := []byte("?OTR?v2?")
c := &Conversation{Policies: policies(allowV2 | allowV3)}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
msg, err := c.receiveQueryMessage(queryMsg)
assertNil(t, err)
assertEquals(t, c.ake.state, authStateAwaitingDHKey{})
assertDeepEquals(t, dhMsgType(msg[0]), msgTypeDHCommit)
assertDeepEquals(t, dhMsgVersion(msg[0]), uint16(2))
}
func Test_receiveQueryMessage_ignoreBizarreClaim(t *testing.T) {
queryMsg := []byte("?OTRv?")
c := &Conversation{Policies: policies(allowV2 | allowV3)}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
msg, err := c.receiveQueryMessage(queryMsg)
expError := OtrError{msg: "unsupported OTR version", conflict: false}
assertEquals(t, err, expError)
assertNil(t, msg)
}
func Test_receiveQueryMessage_ignoreAdditionalText(t *testing.T) {
queryMsg := []byte("?OTRv2? I like number 3")
c := &Conversation{Policies: policies(allowV2 | allowV3)}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
msg, err := c.receiveQueryMessage(queryMsg)
assertNil(t, err)
assertEquals(t, c.ake.state, authStateAwaitingDHKey{})
assertDeepEquals(t, dhMsgType(msg[0]), msgTypeDHCommit)
assertDeepEquals(t, dhMsgVersion(msg[0]), uint16(2))
}
func Test_receiveQueryMessage_sendDHCommitv3AndTransitToStateAwaitingDHKey(t *testing.T) {
queryMsg := []byte("?OTRv23?")
c := &Conversation{Policies: policies(allowV3)}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
msg, err := c.receiveQueryMessage(queryMsg)
assertNil(t, err)
assertEquals(t, c.ake.state, authStateAwaitingDHKey{})
assertDeepEquals(t, dhMsgType(msg[0]), msgTypeDHCommit)
assertDeepEquals(t, dhMsgVersion(msg[0]), uint16(3))
}
func Test_receiveQueryMessageV2_sendDHCommitv2(t *testing.T) {
queryMsg := []byte("?OTRvx23?")
c := &Conversation{Policies: policies(allowV2)}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
msg, err := c.receiveQueryMessage(queryMsg)
assertNil(t, err)
assertEquals(t, c.ake.state, authStateAwaitingDHKey{})
assertDeepEquals(t, dhMsgType(msg[0]), msgTypeDHCommit)
assertDeepEquals(t, dhMsgVersion(msg[0]), uint16(2))
}
func Test_receiveQueryMessageV2V3_sendDHCommitv3WhenV2AndV3AreAllowed(t *testing.T) {
queryMsg := []byte("?OTRvx23?")
c := &Conversation{Policies: policies(allowV2 | allowV3)}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
msg, err := c.receiveQueryMessage(queryMsg)
assertNil(t, err)
assertEquals(t, c.ake.state, authStateAwaitingDHKey{})
assertDeepEquals(t, dhMsgType(msg[0]), msgTypeDHCommit)
assertDeepEquals(t, dhMsgVersion(msg[0]), uint16(3))
}
func Test_receiveQueryMessage_StoresRAndXAndGx(t *testing.T) {
fixture := fixtureConversation()
_, _ = fixture.dhCommitMessage()
cxt := &Conversation{
version: otrV3{},
Rand: fixtureRand(),
}
cxt.SetOurKeys([]PrivateKey{bobPrivateKey})
_, err := cxt.sendDHCommit()
assertNil(t, err)
assertDeepEquals(t, cxt.ake.r, fixture.ake.r)
assertDeepEquals(t, cxt.ake.secretExponent, fixture.ake.secretExponent)
assertDeepEquals(t, cxt.ake.ourPublicValue, fixture.ake.ourPublicValue)
}
func Test_receiveQueryMessage_signalsMessageEventOnFailure(t *testing.T) {
queryMsg := []byte("?OTRv3?")
c := newConversation(nil, fixedRand([]string{"ABCD"}))
c.SetOurKeys([]PrivateKey{bobPrivateKey})
c.Policies.add(allowV3)
c.expectMessageEvent(t, func() {
_, _ = c.receiveQueryMessage(queryMsg)
}, MessageEventSetupError, nil, errShortRandomRead)
}
func Test_receiveQueryMessage_returnsErrorIfNoCompatibleVersionCouldBeFound(t *testing.T) {
c := &Conversation{Policies: policies(allowV3)}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
_, err := c.receiveQueryMessage([]byte("?OTRv?2?"))
assertEquals(t, err, errUnsupportedOTRVersion)
}
func Test_receiveQueryMessage_returnsErrorIfDhCommitMessageGeneratesError(t *testing.T) {
c := &Conversation{
Policies: policies(allowV2),
Rand: fixedRand([]string{"ABCDABCD"}),
}
c.SetOurKeys([]PrivateKey{bobPrivateKey})
_, err := c.receiveQueryMessage([]byte("?OTRv2?"))
assertEquals(t, err, errShortRandomRead)
}
func Test_parseOTRQueryMessage(t *testing.T) {
var exp = map[string][]int{
"?OTR?": {1},
"?OTRv2?": {2},
"?OTRv23?": {2, 3},
"?OTR?v2": {1, 2},
"?OTRv248?": {2, 4, 8},
"?OTR?v?": {1},
"?OTRv?": {},
}
for queryMsg, versions := range exp {
m := []byte(queryMsg)
assertDeepEquals(t, parseOTRQueryMessage(m), versions)
}
}
func Test_extractVersionsFromQueryMessage_returnsNilForUnsupportedVersions(t *testing.T) {
p := policies(0)
msg := []byte("?OTR?")
versions := extractVersionsFromQueryMessage(p, msg)
assertEquals(t, versions, 0)
}
func Test_extractVersionsFromQueryMessage_acceptsBothV2AndV3IfThePolicyAllows(t *testing.T) {
msg := []byte("?OTRv32?")
p := policies(allowV2 | allowV3)
versions := extractVersionsFromQueryMessage(p, msg)
assertEquals(t, versions, 1<<2|1<<3)
}
func Test_extractVersionsFromQueryMessage_acceptsOTRV2IfHasOnlyAllowV2Policy(t *testing.T) {
msg := []byte("?OTRv32?")
p := policies(allowV2)
versions := extractVersionsFromQueryMessage(p, msg)
assertEquals(t, versions, 1<<2)
}
func Test_QueryMessage_returnsARegularQueryMessage(t *testing.T) {
c := &Conversation{Policies: policies(allowV3)}
assertEquals(t, string(c.QueryMessage()), "?OTRv3?")
}
func Test_QueryMessage_returnsAQueryMessageWithExtraMessage(t *testing.T) {
c := &Conversation{Policies: policies(allowV3)}
c.SetFriendlyQueryMessage("hello foobarium")
assertEquals(t, string(c.QueryMessage()), "?OTRv3? hello foobarium")
}