File: OLMKitTests.m

package info (click to toggle)
olm 3.2.16%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,208 kB
  • sloc: cpp: 15,245; ansic: 10,894; java: 3,244; objc: 2,291; javascript: 1,882; python: 1,839; makefile: 439; sh: 245; asm: 7; xml: 1
file content (266 lines) | stat: -rw-r--r-- 10,316 bytes parent folder | download | duplicates (3)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
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.
*/

#import <XCTest/XCTest.h>
#import <OLMKit/OLMKit.h>

@interface OLMKitTests : XCTestCase

@end

@implementation OLMKitTests

- (void)testAliceAndBob {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateOneTimeKeys:5];
    
    [self _testAliceAndBob:bob withBobKeys:bob.oneTimeKeys];
}

- (void)testAliceAndBobFallbackKey {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateFallbackKey];
    
    [self _testAliceAndBob:bob withBobKeys:bob.unpublishedFallbackKey];

}

- (void)testMarkAsPublishedFallbackKey {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateFallbackKey];
    
    
    NSDictionary *unpublished = bob.unpublishedFallbackKey;
    __block NSString *bobKeyValue = ((NSDictionary *) unpublished[@"curve25519"]).allValues.lastObject;
    
    XCTAssertNotNil(bobKeyValue);
    
    [bob markOneTimeKeysAsPublished];
    
    NSDictionary *unpublishedAfter = bob.unpublishedFallbackKey;
    
    __block NSString *bobKeyValueAfter = ((NSDictionary *) unpublishedAfter[@"curve25519"]).allValues.lastObject;
    
    XCTAssertNil(bobKeyValueAfter);
}

- (void)_testAliceAndBob:(OLMAccount *)bob withBobKeys:(NSDictionary *)bobKeys {
    XCTAssertNotNil(bob);
    XCTAssertNotNil(bobKeys);
    
    NSError *error;
    
    NSString *bobIdKey = bob.identityKeys[@"curve25519"];
    XCTAssertNotNil(bobIdKey);
    
    __block NSString *bobKeyValue = nil;
    [bobKeys[@"curve25519"] enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        bobKeyValue = obj;
    }];
    XCTAssert([bobKeyValue isKindOfClass:[NSString class]]);
    
    OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
    
    OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobKeyValue error:nil];
    NSString *message = @"Hello!";
    OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
    XCTAssertNil(error);
    
    OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
    NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:&error];
    XCTAssertEqualObjects(message, plaintext);
    XCTAssertNil(error);

    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"]);

    BOOL success = [bob removeOneTimeKeysForSession:bobSession];
    XCTAssertTrue(success);
}

- (void)testBackAndForthWithOneTimeKeys {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateOneTimeKeys:1];
    
    [self _testBackAndForthWithBob:bob andBobKeys:bob.oneTimeKeys];
}

- (void)testBackAndForthWithFallbackKey {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateFallbackKey];
    
    [self _testBackAndForthWithBob:bob andBobKeys:bob.unpublishedFallbackKey];
}

- (void)_testBackAndForthWithBob:(OLMAccount *)bob andBobKeys:(NSDictionary *)bobKeys {
    XCTAssertNotNil(bob);
    XCTAssertNotNil(bobKeys);
    
    NSString *bobIdKey = bob.identityKeys[@"curve25519"];
    XCTAssertNotNil(bobIdKey);

    __block NSString *bobKeyValue = nil;
    [bobKeys[@"curve25519"] enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        bobKeyValue = obj;
    }];
    XCTAssert([bobKeyValue isKindOfClass:[NSString class]]);
    
    OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
    
    OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobKeyValue error:nil];
    NSString *message = @"Hello I'm Alice!";
    OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:nil];
    
    OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
    NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:nil];
    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.";
    
    OLMMessage *eMsg1 = [bobSession encryptMessage:msg1 error:nil];
    OLMMessage *eMsg2 = [bobSession encryptMessage:msg2 error:nil];
    OLMMessage *eMsg3 = [bobSession encryptMessage:msg3 error:nil];
    
    NSString *dMsg1 = [aliceSession decryptMessage:eMsg1 error:nil];
    NSString *dMsg2 = [aliceSession decryptMessage:eMsg2 error:nil];
    NSString *dMsg3 = [aliceSession decryptMessage:eMsg3 error:nil];
    XCTAssertEqualObjects(msg1, dMsg1);
    XCTAssertEqualObjects(msg2, dMsg2);
    XCTAssertEqualObjects(msg3, dMsg3);
}

- (void)testAccountSerialization {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateOneTimeKeys:5];
    [bob generateFallbackKey];
    NSDictionary *bobIdKeys = bob.identityKeys;
    NSDictionary *bobOneTimeKeys = bob.oneTimeKeys;
    NSDictionary *bobFallbackKey = bob.unpublishedFallbackKey;
    
    NSError *error;
    NSData *bobData = [NSKeyedArchiver archivedDataWithRootObject:bob requiringSecureCoding:NO error:&error];
    XCTAssertNil(error);
    
    OLMAccount *bob2 = [NSKeyedUnarchiver unarchivedObjectOfClass:[OLMAccount class] fromData:bobData error:&error];
    XCTAssertNil(error);
    
    NSDictionary *bobIdKeys2 = bob2.identityKeys;
    NSDictionary *bobOneTimeKeys2 = bob2.oneTimeKeys;
    NSDictionary *bobFallbackKey2 = bob2.unpublishedFallbackKey;
    
    XCTAssertEqualObjects(bobIdKeys, bobIdKeys2);
    XCTAssertEqualObjects(bobOneTimeKeys, bobOneTimeKeys2);
    XCTAssertEqualObjects(bobFallbackKey, bobFallbackKey2);
}

- (void)testSessionSerializationWithOneTimeKey {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateOneTimeKeys:1];
    
    [self _testSessionSerializationWithBob:bob bobKeys:bob.oneTimeKeys];
}

- (void)testSessionSerializationWithFallbackKey {
    OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
    [bob generateFallbackKey];
    
    [self _testSessionSerializationWithBob:bob bobKeys:bob.unpublishedFallbackKey];
}

- (void)_testSessionSerializationWithBob:(OLMAccount *)bob bobKeys:(NSDictionary *)bobKeys {
    NSError *error;
    
    NSString *bobIdKey = bob.identityKeys[@"curve25519"];
    XCTAssertNotNil(bobIdKey);
    
    __block NSString *bobKeyValue = nil;
    [bobKeys[@"curve25519"] enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        bobKeyValue = obj;
    }];
    XCTAssert([bobKeyValue isKindOfClass:[NSString class]]);
    
    OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
    
    OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobKeyValue error:nil];
    NSString *message = @"Hello I'm Alice!";
    
    OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
    XCTAssertNil(error);
    
    OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
    NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:nil];
    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.";
    
    OLMMessage *eMsg1 = [bobSession encryptMessage:msg1 error:nil];
    OLMMessage *eMsg2 = [bobSession encryptMessage:msg2 error:nil];
    OLMMessage *eMsg3 = [bobSession encryptMessage:msg3 error:nil];
    
    NSData *aliceData = [NSKeyedArchiver archivedDataWithRootObject:aliceSession requiringSecureCoding:NO error:&error];
    XCTAssertNil(error);
    
    OLMSession *alice2 = [NSKeyedUnarchiver unarchivedObjectOfClass:[OLMSession class] fromData:aliceData error:&error];
    XCTAssertNil(error);
    
    NSString *dMsg1 = [alice2 decryptMessage:eMsg1 error:nil];
    NSString *dMsg2 = [alice2 decryptMessage:eMsg2 error:nil];
    NSString *dMsg3 = [alice2 decryptMessage:eMsg3 error:nil];
    XCTAssertEqualObjects(msg1, dMsg1);
    XCTAssertEqualObjects(msg2, dMsg2);
    XCTAssertEqualObjects(msg3, dMsg3);
}

- (void)testEd25519Signing {
    NSError *error;

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

    NSDictionary *aJSON = @{
                            @"key1": @"value1",
                            @"key2": @"value2"
                            };
    NSData *message = [NSKeyedArchiver archivedDataWithRootObject:aJSON requiringSecureCoding:NO error:&error];
    XCTAssertNil(error);
    
    NSString *signature = [alice signMessage:message];

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

    BOOL result = [olmUtility verifyEd25519Signature:signature key:aliceEd25519Key message:message error:&error];
    XCTAssert(result);
    XCTAssertNil(error);
}

@end