~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/golang.org/x/crypto/otr/otr_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
121
121
        }
122
122
}
123
123
 
124
 
func TestConversation(t *testing.T) {
 
124
func setupConversation(t *testing.T) (alice, bob *Conversation) {
125
125
        alicePrivateKey, _ := hex.DecodeString(alicePrivateKeyHex)
126
126
        bobPrivateKey, _ := hex.DecodeString(bobPrivateKeyHex)
127
127
 
128
 
        var alice, bob Conversation
 
128
        alice, bob = new(Conversation), new(Conversation)
 
129
 
129
130
        alice.PrivateKey = new(PrivateKey)
130
131
        bob.PrivateKey = new(PrivateKey)
131
132
        alice.PrivateKey.Parse(alicePrivateKey)
133
134
        alice.FragmentSize = 100
134
135
        bob.FragmentSize = 100
135
136
 
 
137
        if alice.IsEncrypted() {
 
138
                t.Error("Alice believes that the conversation is secure before we've started")
 
139
        }
 
140
        if bob.IsEncrypted() {
 
141
                t.Error("Bob believes that the conversation is secure before we've started")
 
142
        }
 
143
 
 
144
        performHandshake(t, alice, bob)
 
145
        return alice, bob
 
146
}
 
147
 
 
148
func performHandshake(t *testing.T, alice, bob *Conversation) {
136
149
        var alicesMessage, bobsMessage [][]byte
137
150
        var out []byte
138
151
        var aliceChange, bobChange SecurityChange
139
152
        var err error
140
153
        alicesMessage = append(alicesMessage, []byte(QueryMessage))
141
154
 
142
 
        if alice.IsEncrypted() {
143
 
                t.Error("Alice believes that the conversation is secure before we've started")
144
 
        }
145
 
        if bob.IsEncrypted() {
146
 
                t.Error("Bob believes that the conversation is secure before we've started")
147
 
        }
148
 
 
149
155
        for round := 0; len(alicesMessage) > 0 || len(bobsMessage) > 0; round++ {
150
156
                bobsMessage = nil
151
157
                for i, msg := range alicesMessage {
193
199
        if !bob.IsEncrypted() {
194
200
                t.Error("Bob doesn't believe that the conversation is secure")
195
201
        }
 
202
}
 
203
 
 
204
const (
 
205
        firstRoundTrip = iota
 
206
        subsequentRoundTrip
 
207
        noMACKeyCheck
 
208
)
 
209
 
 
210
func roundTrip(t *testing.T, alice, bob *Conversation, message []byte, macKeyCheck int) {
 
211
        alicesMessage, err := alice.Send(message)
 
212
        if err != nil {
 
213
                t.Errorf("Error from Alice sending message: %s", err)
 
214
        }
 
215
 
 
216
        if len(alice.oldMACs) != 0 {
 
217
                t.Errorf("Alice has not revealed all MAC keys")
 
218
        }
 
219
 
 
220
        for i, msg := range alicesMessage {
 
221
                out, encrypted, _, _, err := bob.Receive(msg)
 
222
 
 
223
                if err != nil {
 
224
                        t.Errorf("Error generated while processing test message: %s", err.Error())
 
225
                }
 
226
                if len(out) > 0 {
 
227
                        if i != len(alicesMessage)-1 {
 
228
                                t.Fatal("Bob produced a message while processing a fragment of Alice's")
 
229
                        }
 
230
                        if !encrypted {
 
231
                                t.Errorf("Message was not marked as encrypted")
 
232
                        }
 
233
                        if !bytes.Equal(out, message) {
 
234
                                t.Errorf("Message corrupted: got %x, want %x", out, message)
 
235
                        }
 
236
                }
 
237
        }
 
238
 
 
239
        switch macKeyCheck {
 
240
        case firstRoundTrip:
 
241
                if len(bob.oldMACs) != 0 {
 
242
                        t.Errorf("Bob should not have MAC keys to reveal")
 
243
                }
 
244
        case subsequentRoundTrip:
 
245
                if len(bob.oldMACs) != 40 {
 
246
                        t.Errorf("Bob has %d bytes of MAC keys to reveal, but should have 40", len(bob.oldMACs))
 
247
                }
 
248
        }
 
249
 
 
250
        bobsMessage, err := bob.Send(message)
 
251
        if err != nil {
 
252
                t.Errorf("Error from Bob sending message: %s", err)
 
253
        }
 
254
 
 
255
        if len(bob.oldMACs) != 0 {
 
256
                t.Errorf("Bob has not revealed all MAC keys")
 
257
        }
 
258
 
 
259
        for i, msg := range bobsMessage {
 
260
                out, encrypted, _, _, err := alice.Receive(msg)
 
261
 
 
262
                if err != nil {
 
263
                        t.Errorf("Error generated while processing test message: %s", err.Error())
 
264
                }
 
265
                if len(out) > 0 {
 
266
                        if i != len(bobsMessage)-1 {
 
267
                                t.Fatal("Alice produced a message while processing a fragment of Bob's")
 
268
                        }
 
269
                        if !encrypted {
 
270
                                t.Errorf("Message was not marked as encrypted")
 
271
                        }
 
272
                        if !bytes.Equal(out, message) {
 
273
                                t.Errorf("Message corrupted: got %x, want %x", out, message)
 
274
                        }
 
275
                }
 
276
        }
 
277
 
 
278
        switch macKeyCheck {
 
279
        case firstRoundTrip:
 
280
                if len(alice.oldMACs) != 20 {
 
281
                        t.Errorf("Alice has %d bytes of MAC keys to reveal, but should have 20", len(alice.oldMACs))
 
282
                }
 
283
        case subsequentRoundTrip:
 
284
                if len(alice.oldMACs) != 40 {
 
285
                        t.Errorf("Alice has %d bytes of MAC keys to reveal, but should have 40", len(alice.oldMACs))
 
286
                }
 
287
        }
 
288
}
 
289
 
 
290
func TestConversation(t *testing.T) {
 
291
        alice, bob := setupConversation(t)
196
292
 
197
293
        var testMessages = [][]byte{
198
294
                []byte("hello"), []byte("bye"),
199
295
        }
200
296
 
201
 
        for j, testMessage := range testMessages {
202
 
                alicesMessage, err = alice.Send(testMessage)
203
 
 
204
 
                if len(alice.oldMACs) != 0 {
205
 
                        t.Errorf("Alice has not revealed all MAC keys")
206
 
                }
207
 
 
208
 
                for i, msg := range alicesMessage {
209
 
                        out, encrypted, _, _, err := bob.Receive(msg)
210
 
 
211
 
                        if err != nil {
212
 
                                t.Errorf("Error generated while processing test message: %s", err.Error())
213
 
                        }
214
 
                        if len(out) > 0 {
215
 
                                if i != len(alicesMessage)-1 {
216
 
                                        t.Fatal("Bob produced a message while processing a fragment of Alice's")
217
 
                                }
218
 
                                if !encrypted {
219
 
                                        t.Errorf("Message was not marked as encrypted")
220
 
                                }
221
 
                                if !bytes.Equal(out, testMessage) {
222
 
                                        t.Errorf("Message corrupted: got %x, want %x", out, testMessage)
223
 
                                }
224
 
                        }
225
 
                }
226
 
 
227
 
                if j == 0 {
228
 
                        if len(bob.oldMACs) != 0 {
229
 
                                t.Errorf("Bob should not have MAC keys to reveal")
230
 
                        }
231
 
                } else if len(bob.oldMACs) != 40 {
232
 
                        t.Errorf("Bob does not have MAC keys to reveal")
233
 
                }
234
 
 
235
 
                bobsMessage, err = bob.Send(testMessage)
236
 
 
237
 
                if len(bob.oldMACs) != 0 {
238
 
                        t.Errorf("Bob has not revealed all MAC keys")
239
 
                }
240
 
 
241
 
                for i, msg := range bobsMessage {
242
 
                        out, encrypted, _, _, err := alice.Receive(msg)
243
 
 
244
 
                        if err != nil {
245
 
                                t.Errorf("Error generated while processing test message: %s", err.Error())
246
 
                        }
247
 
                        if len(out) > 0 {
248
 
                                if i != len(bobsMessage)-1 {
249
 
                                        t.Fatal("Alice produced a message while processing a fragment of Bob's")
250
 
                                }
251
 
                                if !encrypted {
252
 
                                        t.Errorf("Message was not marked as encrypted")
253
 
                                }
254
 
                                if !bytes.Equal(out, testMessage) {
255
 
                                        t.Errorf("Message corrupted: got %x, want %x", out, testMessage)
256
 
                                }
257
 
                        }
258
 
                }
259
 
 
260
 
                if j == 0 {
261
 
                        if len(alice.oldMACs) != 20 {
262
 
                                t.Errorf("Alice does not have MAC keys to reveal")
263
 
                        }
264
 
                } else if len(alice.oldMACs) != 40 {
265
 
                        t.Errorf("Alice does not have MAC keys to reveal")
266
 
                }
 
297
        roundTripType := firstRoundTrip
 
298
 
 
299
        for _, testMessage := range testMessages {
 
300
                roundTrip(t, alice, bob, testMessage, roundTripType)
 
301
                roundTripType = subsequentRoundTrip
267
302
        }
268
303
}
269
304
 
348
383
        }
349
384
}
350
385
 
 
386
func TestRehandshaking(t *testing.T) {
 
387
        alice, bob := setupConversation(t)
 
388
        roundTrip(t, alice, bob, []byte("test"), firstRoundTrip)
 
389
        roundTrip(t, alice, bob, []byte("test 2"), subsequentRoundTrip)
 
390
        roundTrip(t, alice, bob, []byte("test 3"), subsequentRoundTrip)
 
391
        roundTrip(t, alice, bob, []byte("test 4"), subsequentRoundTrip)
 
392
        roundTrip(t, alice, bob, []byte("test 5"), subsequentRoundTrip)
 
393
        roundTrip(t, alice, bob, []byte("test 6"), subsequentRoundTrip)
 
394
        roundTrip(t, alice, bob, []byte("test 7"), subsequentRoundTrip)
 
395
        roundTrip(t, alice, bob, []byte("test 8"), subsequentRoundTrip)
 
396
        performHandshake(t, alice, bob)
 
397
        roundTrip(t, alice, bob, []byte("test"), noMACKeyCheck)
 
398
        roundTrip(t, alice, bob, []byte("test 2"), noMACKeyCheck)
 
399
}
 
400
 
351
401
func TestAgainstLibOTR(t *testing.T) {
352
402
        // This test requires otr.c.test to be built as /tmp/a.out.
353
403
        // If enabled, this tests runs forever performing OTR handshakes in a
400
450
                if change == NewKeys {
401
451
                        alicesMessage, err := alice.Send([]byte("Go -> libotr test message"))
402
452
                        if err != nil {
403
 
                                t.Errorf("error sending message: %s", err.Error())
 
453
                                t.Fatalf("error sending message: %s", err.Error())
404
454
                        } else {
405
455
                                for _, msg := range alicesMessage {
406
456
                                        out.Write(msg)
410
460
                }
411
461
                if len(text) > 0 {
412
462
                        if !bytes.Equal(text, expectedText) {
413
 
                                t.Errorf("expected %x, but got %x", expectedText, text)
 
463
                                t.Fatalf("expected %x, but got %x", expectedText, text)
414
464
                        }
415
465
                        if !encrypted {
416
 
                                t.Error("message wasn't encrypted")
 
466
                                t.Fatal("message wasn't encrypted")
417
467
                        }
418
468
                }
419
469
        }