aboutsummaryrefslogtreecommitdiff
path: root/xcode/OLMKitTests
diff options
context:
space:
mode:
authorRichard van der Hoff <github@rvanderhoff.org.uk>2016-12-22 14:43:01 +0000
committerGitHub <noreply@github.com>2016-12-22 14:43:01 +0000
commit90b36130532d2d8040b39e310d8d0d5ee22aea0a (patch)
tree26eabdea9bc04077b1621deb34b5d0cbe669dedd /xcode/OLMKitTests
parentfb91b1f18269c5b681c972dc4448858db0966c3a (diff)
parent46ad79517ec8e005bd2d1de767d3cd59ec038fe2 (diff)
Merge pull request #36 from matrix-org/manuroe/olmkit
OLMKit
Diffstat (limited to 'xcode/OLMKitTests')
-rw-r--r--xcode/OLMKitTests/Info.plist22
-rw-r--r--xcode/OLMKitTests/OLMKitGroupTests.m94
-rw-r--r--xcode/OLMKitTests/OLMKitTests.m206
3 files changed, 322 insertions, 0 deletions
diff --git a/xcode/OLMKitTests/Info.plist b/xcode/OLMKitTests/Info.plist
new file mode 100644
index 0000000..6c6c23c
--- /dev/null
+++ b/xcode/OLMKitTests/Info.plist
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>en</string>
+ <key>CFBundleExecutable</key>
+ <string>$(EXECUTABLE_NAME)</string>
+ <key>CFBundleIdentifier</key>
+ <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundleName</key>
+ <string>$(PRODUCT_NAME)</string>
+ <key>CFBundlePackageType</key>
+ <string>BNDL</string>
+ <key>CFBundleShortVersionString</key>
+ <string>1.0</string>
+ <key>CFBundleVersion</key>
+ <string>1</string>
+</dict>
+</plist>
diff --git a/xcode/OLMKitTests/OLMKitGroupTests.m b/xcode/OLMKitTests/OLMKitGroupTests.m
new file mode 100644
index 0000000..ea82295
--- /dev/null
+++ b/xcode/OLMKitTests/OLMKitGroupTests.m
@@ -0,0 +1,94 @@
+/*
+ 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>
+
+#include "olm/olm.h"
+
+@interface OLMKitGroupTests : XCTestCase
+
+@end
+
+@implementation OLMKitGroupTests
+
+- (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];
+}
+
+- (void)testAliceAndBob {
+ NSError *error;
+
+ OLMOutboundGroupSession *aliceSession = [[OLMOutboundGroupSession alloc] initOutboundGroupSession];
+ XCTAssertGreaterThan(aliceSession.sessionIdentifier.length, 0);
+ XCTAssertGreaterThan(aliceSession.sessionKey.length, 0);
+ XCTAssertEqual(aliceSession.messageIndex, 0);
+
+ // Store the session key before starting encrypting
+ NSString *sessionKey = aliceSession.sessionKey;
+
+ NSString *message = @"Hello!";
+ NSString *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
+
+ XCTAssertEqual(aliceSession.messageIndex, 1);
+ XCTAssertGreaterThanOrEqual(aliceToBobMsg.length, 0);
+ XCTAssertNil(error);
+
+ OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:sessionKey error:&error];
+ XCTAssertEqualObjects(aliceSession.sessionIdentifier, bobSession.sessionIdentifier);
+ XCTAssertNil(error);
+
+ NSUInteger messageIndex;
+
+ NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg messageIndex:&messageIndex error:&error];
+ XCTAssertEqualObjects(message, plaintext);
+
+ XCTAssertEqual(messageIndex, 0);
+ XCTAssertNil(error);
+}
+
+- (void)testOutboundGroupSessionSerialization {
+
+ OLMOutboundGroupSession *aliceSession = [[OLMOutboundGroupSession alloc] initOutboundGroupSession];
+
+ NSData *aliceData = [NSKeyedArchiver archivedDataWithRootObject:aliceSession];
+ OLMOutboundGroupSession *aliceSession2 = [NSKeyedUnarchiver unarchiveObjectWithData:aliceData];
+
+ XCTAssertEqualObjects(aliceSession2.sessionKey, aliceSession.sessionKey);
+ XCTAssertEqualObjects(aliceSession2.sessionIdentifier, aliceSession.sessionIdentifier);
+}
+
+- (void)testInboundGroupSessionSerialization {
+
+ OLMOutboundGroupSession *aliceSession = [[OLMOutboundGroupSession alloc] initOutboundGroupSession];
+
+ OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:aliceSession.sessionKey error:nil];
+
+ NSData *bobData = [NSKeyedArchiver archivedDataWithRootObject:bobSession];
+ OLMInboundGroupSession *bobSession2 = [NSKeyedUnarchiver unarchiveObjectWithData:bobData];
+
+ XCTAssertEqualObjects(bobSession2.sessionIdentifier, aliceSession.sessionIdentifier);
+}
+
+@end
diff --git a/xcode/OLMKitTests/OLMKitTests.m b/xcode/OLMKitTests/OLMKitTests.m
new file mode 100644
index 0000000..ee02420
--- /dev/null
+++ b/xcode/OLMKitTests/OLMKitTests.m
@@ -0,0 +1,206 @@
+/*
+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)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];
+}
+
+- (void)testAliceAndBob {
+ NSError *error;
+
+ OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
+ OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
+ [bob generateOneTimeKeys:5];
+ 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]]);
+
+ OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey 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) 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]]);
+
+ OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey 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];
+ NSDictionary *bobIdKeys = bob.identityKeys;
+ NSDictionary *bobOneTimeKeys = bob.oneTimeKeys;
+
+ NSData *bobData = [NSKeyedArchiver archivedDataWithRootObject:bob];
+
+ OLMAccount *bob2 = [NSKeyedUnarchiver unarchiveObjectWithData:bobData];
+ NSDictionary *bobIdKeys2 = bob2.identityKeys;
+ NSDictionary *bobOneTimeKeys2 = bob2.oneTimeKeys;
+
+ XCTAssertEqualObjects(bobIdKeys, bobIdKeys2);
+ XCTAssertEqualObjects(bobOneTimeKeys, bobOneTimeKeys2);
+}
+
+- (void) testSessionSerialization {
+ NSError *error;
+
+ 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]]);
+
+ OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey 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];
+ OLMSession *alice2 = [NSKeyedUnarchiver unarchiveObjectWithData:aliceData];
+
+ 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 {
+
+ 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);
+}
+
+@end