OLMKitTests.m 8.52 KB
Newer Older
manuroe's avatar
manuroe committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
Copyright 2016 Chris Ballinger
Copyright 2016 OpenMarket Ltd
Copyright 2016 Vector Creations Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Chris Ballinger's avatar
Chris Ballinger committed
18
19

#import <XCTest/XCTest.h>
manuroe's avatar
manuroe committed
20
#import <OLMKit/OLMKit.h>
Chris Ballinger's avatar
Chris Ballinger committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

@interface OLMKitTests : XCTestCase

@end

@implementation OLMKitTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

38
- (void)testAliceAndBob {
39
40
    NSError *error;

Chris Ballinger's avatar
Chris Ballinger committed
41
42
43
    OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateOneTimeKeys:5];
Chris Ballinger's avatar
Chris Ballinger committed
44
45
46
47
48
49
50
51
52
53
54
55
    NSDictionary *bobIdKeys = bob.identityKeys;
    NSString *bobIdKey = bobIdKeys[@"curve25519"];
    NSDictionary *bobOneTimeKeys = bob.oneTimeKeys;
    NSParameterAssert(bobIdKey != nil);
    NSParameterAssert(bobOneTimeKeys != nil);
    __block NSString *bobOneTimeKey = nil;
    NSDictionary *bobOtkCurve25519 = bobOneTimeKeys[@"curve25519"];
    [bobOtkCurve25519 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        bobOneTimeKey = obj;
    }];
    XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
    
56
    OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
Chris Ballinger's avatar
Chris Ballinger committed
57
    NSString *message = @"Hello!";
58
59
    OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
    XCTAssertNil(error);
Chris Ballinger's avatar
Chris Ballinger committed
60
    
61
62
    OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
    NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:&error];
Chris Ballinger's avatar
Chris Ballinger committed
63
    XCTAssertEqualObjects(message, plaintext);
64
    XCTAssertNil(error);
65
66
67
68
69
70
71
72
73

    XCTAssert([bobSession matchesInboundSession:aliceToBobMsg.ciphertext]);
    XCTAssertFalse([aliceSession matchesInboundSession:@"ARandomOtkMessage"]);

    NSString *aliceIdKey = alice.identityKeys[@"curve25519"];
    XCTAssert([bobSession matchesInboundSessionFrom:aliceIdKey oneTimeKeyMessage:aliceToBobMsg.ciphertext]);
    XCTAssertFalse([bobSession matchesInboundSessionFrom:@"ARandomIdKey" oneTimeKeyMessage:aliceToBobMsg.ciphertext]);
    XCTAssertFalse([bobSession matchesInboundSessionFrom:aliceIdKey oneTimeKeyMessage:@"ARandomOtkMessage"]);

74
    BOOL success = [bob removeOneTimeKeysForSession:bobSession];
Chris Ballinger's avatar
Chris Ballinger committed
75
    XCTAssertTrue(success);
Chris Ballinger's avatar
Chris Ballinger committed
76
77
}

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
- (void) testBackAndForth {
    OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateOneTimeKeys:1];
    NSDictionary *bobIdKeys = bob.identityKeys;
    NSString *bobIdKey = bobIdKeys[@"curve25519"];
    NSDictionary *bobOneTimeKeys = bob.oneTimeKeys;
    NSParameterAssert(bobIdKey != nil);
    NSParameterAssert(bobOneTimeKeys != nil);
    __block NSString *bobOneTimeKey = nil;
    NSDictionary *bobOtkCurve25519 = bobOneTimeKeys[@"curve25519"];
    [bobOtkCurve25519 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        bobOneTimeKey = obj;
    }];
    XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
    
94
    OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
95
    NSString *message = @"Hello I'm Alice!";
96
    OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:nil];
97
    
98
99
    OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
    NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:nil];
100
101
102
103
104
105
106
107
    XCTAssertEqualObjects(message, plaintext);
    BOOL success = [bob removeOneTimeKeysForSession:bobSession];
    XCTAssertTrue(success);
    
    NSString *msg1 = @"Hello I'm Bob!";
    NSString *msg2 = @"Isn't life grand?";
    NSString *msg3 = @"Let's go to the opera.";
    
108
109
110
    OLMMessage *eMsg1 = [bobSession encryptMessage:msg1 error:nil];
    OLMMessage *eMsg2 = [bobSession encryptMessage:msg2 error:nil];
    OLMMessage *eMsg3 = [bobSession encryptMessage:msg3 error:nil];
111
    
112
113
114
    NSString *dMsg1 = [aliceSession decryptMessage:eMsg1 error:nil];
    NSString *dMsg2 = [aliceSession decryptMessage:eMsg2 error:nil];
    NSString *dMsg3 = [aliceSession decryptMessage:eMsg3 error:nil];
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    XCTAssertEqualObjects(msg1, dMsg1);
    XCTAssertEqualObjects(msg2, dMsg2);
    XCTAssertEqualObjects(msg3, dMsg3);
}

- (void) testAccountSerialization {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateOneTimeKeys:5];
    NSDictionary *bobIdKeys = bob.identityKeys;
    NSDictionary *bobOneTimeKeys = bob.oneTimeKeys;
    
    NSData *bobData = [NSKeyedArchiver archivedDataWithRootObject:bob];
    
    OLMAccount *bob2 = [NSKeyedUnarchiver unarchiveObjectWithData:bobData];
    NSDictionary *bobIdKeys2 = bob2.identityKeys;
manuroe's avatar
manuroe committed
130
    NSDictionary *bobOneTimeKeys2 = bob2.oneTimeKeys;
131
132
133
134
135
136
    
    XCTAssertEqualObjects(bobIdKeys, bobIdKeys2);
    XCTAssertEqualObjects(bobOneTimeKeys, bobOneTimeKeys2);
}

- (void) testSessionSerialization {
137
138
    NSError *error;

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateOneTimeKeys:1];
    NSDictionary *bobIdKeys = bob.identityKeys;
    NSString *bobIdKey = bobIdKeys[@"curve25519"];
    NSDictionary *bobOneTimeKeys = bob.oneTimeKeys;
    NSParameterAssert(bobIdKey != nil);
    NSParameterAssert(bobOneTimeKeys != nil);
    __block NSString *bobOneTimeKey = nil;
    NSDictionary *bobOtkCurve25519 = bobOneTimeKeys[@"curve25519"];
    [bobOtkCurve25519 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        bobOneTimeKey = obj;
    }];
    XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
    
154
    OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
155
    NSString *message = @"Hello I'm Alice!";
156
157
158
    OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
    XCTAssertNil(error);

159
    
160
161
    OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
    NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:nil];
162
163
164
165
166
167
168
169
    XCTAssertEqualObjects(message, plaintext);
    BOOL success = [bob removeOneTimeKeysForSession:bobSession];
    XCTAssertTrue(success);
    
    NSString *msg1 = @"Hello I'm Bob!";
    NSString *msg2 = @"Isn't life grand?";
    NSString *msg3 = @"Let's go to the opera.";
    
170
171
172
    OLMMessage *eMsg1 = [bobSession encryptMessage:msg1 error:nil];
    OLMMessage *eMsg2 = [bobSession encryptMessage:msg2 error:nil];
    OLMMessage *eMsg3 = [bobSession encryptMessage:msg3 error:nil];
173
174
175
176
    
    NSData *aliceData = [NSKeyedArchiver archivedDataWithRootObject:aliceSession];
    OLMSession *alice2 = [NSKeyedUnarchiver unarchiveObjectWithData:aliceData];
    
177
178
179
    NSString *dMsg1 = [alice2 decryptMessage:eMsg1 error:nil];
    NSString *dMsg2 = [alice2 decryptMessage:eMsg2 error:nil];
    NSString *dMsg3 = [alice2 decryptMessage:eMsg3 error:nil];
180
181
182
183
184
    XCTAssertEqualObjects(msg1, dMsg1);
    XCTAssertEqualObjects(msg2, dMsg2);
    XCTAssertEqualObjects(msg3, dMsg3);
}

manuroe's avatar
manuroe committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
- (void)testEd25519Signing {

    OLMUtility *olmUtility = [[OLMUtility alloc] init];
    OLMAccount *alice = [[OLMAccount alloc] initNewAccount];

    NSDictionary *aJSON = @{
                            @"key1": @"value1",
                            @"key2": @"value2"
                            };
    NSData *message = [NSKeyedArchiver archivedDataWithRootObject:aJSON];
    NSString *signature = [alice signMessage:message];


    NSString *aliceEd25519Key = alice.identityKeys[@"ed25519"];

    NSError *error;
    BOOL result = [olmUtility verifyEd25519Signature:signature key:aliceEd25519Key message:message error:&error];
    XCTAssert(result);
    XCTAssertNil(error);
}
Chris Ballinger's avatar
Chris Ballinger committed
205
206

@end