193
199
if !bob.IsEncrypted() {
194
200
t.Error("Bob doesn't believe that the conversation is secure")
205
firstRoundTrip = iota
210
func roundTrip(t *testing.T, alice, bob *Conversation, message []byte, macKeyCheck int) {
211
alicesMessage, err := alice.Send(message)
213
t.Errorf("Error from Alice sending message: %s", err)
216
if len(alice.oldMACs) != 0 {
217
t.Errorf("Alice has not revealed all MAC keys")
220
for i, msg := range alicesMessage {
221
out, encrypted, _, _, err := bob.Receive(msg)
224
t.Errorf("Error generated while processing test message: %s", err.Error())
227
if i != len(alicesMessage)-1 {
228
t.Fatal("Bob produced a message while processing a fragment of Alice's")
231
t.Errorf("Message was not marked as encrypted")
233
if !bytes.Equal(out, message) {
234
t.Errorf("Message corrupted: got %x, want %x", out, message)
241
if len(bob.oldMACs) != 0 {
242
t.Errorf("Bob should not have MAC keys to reveal")
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))
250
bobsMessage, err := bob.Send(message)
252
t.Errorf("Error from Bob sending message: %s", err)
255
if len(bob.oldMACs) != 0 {
256
t.Errorf("Bob has not revealed all MAC keys")
259
for i, msg := range bobsMessage {
260
out, encrypted, _, _, err := alice.Receive(msg)
263
t.Errorf("Error generated while processing test message: %s", err.Error())
266
if i != len(bobsMessage)-1 {
267
t.Fatal("Alice produced a message while processing a fragment of Bob's")
270
t.Errorf("Message was not marked as encrypted")
272
if !bytes.Equal(out, message) {
273
t.Errorf("Message corrupted: got %x, want %x", out, message)
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))
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))
290
func TestConversation(t *testing.T) {
291
alice, bob := setupConversation(t)
197
293
var testMessages = [][]byte{
198
294
[]byte("hello"), []byte("bye"),
201
for j, testMessage := range testMessages {
202
alicesMessage, err = alice.Send(testMessage)
204
if len(alice.oldMACs) != 0 {
205
t.Errorf("Alice has not revealed all MAC keys")
208
for i, msg := range alicesMessage {
209
out, encrypted, _, _, err := bob.Receive(msg)
212
t.Errorf("Error generated while processing test message: %s", err.Error())
215
if i != len(alicesMessage)-1 {
216
t.Fatal("Bob produced a message while processing a fragment of Alice's")
219
t.Errorf("Message was not marked as encrypted")
221
if !bytes.Equal(out, testMessage) {
222
t.Errorf("Message corrupted: got %x, want %x", out, testMessage)
228
if len(bob.oldMACs) != 0 {
229
t.Errorf("Bob should not have MAC keys to reveal")
231
} else if len(bob.oldMACs) != 40 {
232
t.Errorf("Bob does not have MAC keys to reveal")
235
bobsMessage, err = bob.Send(testMessage)
237
if len(bob.oldMACs) != 0 {
238
t.Errorf("Bob has not revealed all MAC keys")
241
for i, msg := range bobsMessage {
242
out, encrypted, _, _, err := alice.Receive(msg)
245
t.Errorf("Error generated while processing test message: %s", err.Error())
248
if i != len(bobsMessage)-1 {
249
t.Fatal("Alice produced a message while processing a fragment of Bob's")
252
t.Errorf("Message was not marked as encrypted")
254
if !bytes.Equal(out, testMessage) {
255
t.Errorf("Message corrupted: got %x, want %x", out, testMessage)
261
if len(alice.oldMACs) != 20 {
262
t.Errorf("Alice does not have MAC keys to reveal")
264
} else if len(alice.oldMACs) != 40 {
265
t.Errorf("Alice does not have MAC keys to reveal")
297
roundTripType := firstRoundTrip
299
for _, testMessage := range testMessages {
300
roundTrip(t, alice, bob, testMessage, roundTripType)
301
roundTripType = subsequentRoundTrip
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)
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