OLMKit: Replaced NSAsserts by NSErrors

This commit is contained in:
manuroe 2016-11-14 16:54:51 +01:00
parent 27a8c28da4
commit cf66af6f2e
11 changed files with 211 additions and 83 deletions

View file

@ -151,7 +151,7 @@
size_t result = olm_remove_one_time_keys(self.account, session.session);
if (result == olm_error()) {
const char *error = olm_account_last_error(_account);
NSAssert(NO, @"olm_remove_one_time_keys error: %s", error);
NSLog(@"olm_remove_one_time_keys error: %s", error);
return NO;
}
return YES;
@ -174,7 +174,7 @@
NSParameterAssert(serializedData.length > 0);
if (key.length == 0 || serializedData.length == 0) {
if (error) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
}
return nil;
}
@ -184,7 +184,7 @@
const char *olm_error = olm_account_last_error(_account);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && errorString) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}
@ -201,7 +201,7 @@
const char *olm_error = olm_account_last_error(_account);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && errorString) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}

View file

@ -19,11 +19,11 @@
@interface OLMInboundGroupSession : NSObject <OLMSerializable, NSSecureCoding>
- (instancetype) initInboundGroupSessionWithSessionKey:(NSString*)sessionKey;
- (instancetype) initInboundGroupSessionWithSessionKey:(NSString*)sessionKey error:(NSError**)error;
- (NSString*)sessionIdentifier;
/** base64 ciphertext -> UTF-8 plaintext */
- (NSString*)decryptMessage:(NSString*)message messageIndex:(NSUInteger*)messageIndex;
- (NSString*)decryptMessage:(NSString*)message messageIndex:(NSUInteger*)messageIndex error:(NSError**)error;
@end

View file

@ -49,14 +49,25 @@
return self;
}
- (instancetype)initInboundGroupSessionWithSessionKey:(NSString *)sessionKey {
- (instancetype)initInboundGroupSessionWithSessionKey:(NSString *)sessionKey error:(NSError**)error {
self = [self init];
if (self) {
NSData *sessionKeyData = [sessionKey dataUsingEncoding:NSUTF8StringEncoding];
size_t result = olm_init_inbound_group_session(session, sessionKeyData.bytes, sessionKeyData.length);
if (result == olm_error()) {
const char *error = olm_inbound_group_session_last_error(session);
NSAssert(NO, @"olm_init_inbound_group_session error: %s", error);
const char *olm_error = olm_inbound_group_session_last_error(session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_init_inbound_group_session error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_init_inbound_group_session error: %@", errorString]
}];
}
return nil;
}
}
@ -72,14 +83,14 @@
size_t result = olm_inbound_group_session_id(session, idData.mutableBytes, idData.length);
if (result == olm_error()) {
const char *error = olm_inbound_group_session_last_error(session);
NSAssert(NO, @"olm_inbound_group_session_id error: %s", error);
NSLog(@"olm_inbound_group_session_id error: %s", error);
return nil;
}
NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding];
return idString;
}
- (NSString *)decryptMessage:(NSString *)message messageIndex:(NSUInteger*)messageIndex
- (NSString *)decryptMessage:(NSString *)message messageIndex:(NSUInteger*)messageIndex error:(NSError**)error
{
NSParameterAssert(message != nil);
NSData *messageData = [message dataUsingEncoding:NSUTF8StringEncoding];
@ -89,8 +100,19 @@
NSMutableData *mutMessage = messageData.mutableCopy;
size_t maxPlaintextLength = olm_group_decrypt_max_plaintext_length(session, mutMessage.mutableBytes, mutMessage.length);
if (maxPlaintextLength == olm_error()) {
const char *error = olm_inbound_group_session_last_error(session);
NSAssert(NO, @"olm_group_decrypt_max_plaintext_length error: %s", error);
const char *olm_error = olm_inbound_group_session_last_error(session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_group_decrypt_max_plaintext_length error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_decrypt_max_plaintext_length error: %@", errorString]
}];
}
return nil;
}
// message buffer is destroyed by olm_group_decrypt_max_plaintext_length
@ -98,8 +120,19 @@
NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength];
size_t plaintextLength = olm_group_decrypt(session, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length, messageIndex);
if (plaintextLength == olm_error()) {
const char *error = olm_inbound_group_session_last_error(session);
NSAssert(NO, @"olm_group_decrypt error: %s", error);
const char *olm_error = olm_inbound_group_session_last_error(session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_group_decrypt error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_decrypt error: %@", errorString]
}];
}
return nil;
}
plaintextData.length = plaintextLength;
@ -120,7 +153,7 @@
NSParameterAssert(serializedData.length > 0);
if (key.length == 0 || serializedData.length == 0) {
if (error) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
}
return nil;
}
@ -130,7 +163,7 @@
const char *olm_error = olm_inbound_group_session_last_error(session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && errorString) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}
@ -147,7 +180,7 @@
const char *olm_error = olm_inbound_group_session_last_error(session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && errorString) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}

View file

@ -26,6 +26,6 @@
- (NSString*)sessionKey;
/** UTF-8 plaintext -> base64 ciphertext */
- (NSString*)encryptMessage:(NSString*)message;
- (NSString*)encryptMessage:(NSString*)message error:(NSError**)error;
@end

View file

@ -56,7 +56,7 @@
size_t result = olm_init_outbound_group_session(session, random.mutableBytes, random.length);
if (result == olm_error()) {
const char *error = olm_outbound_group_session_last_error(session);
NSAssert(NO, @"olm_init_outbound_group_session error: %s", error);
NSLog(@"olm_init_outbound_group_session error: %s", error);
return nil;
}
}
@ -72,7 +72,7 @@
size_t result = olm_outbound_group_session_id(session, idData.mutableBytes, idData.length);
if (result == olm_error()) {
const char *error = olm_outbound_group_session_last_error(session);
NSAssert(NO, @"olm_outbound_group_session_id error: %s", error);
NSLog(@"olm_outbound_group_session_id error: %s", error);
return nil;
}
NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding];
@ -92,14 +92,14 @@
size_t result = olm_outbound_group_session_key(session, sessionKeyData.mutableBytes, sessionKeyData.length);
if (result == olm_error()) {
const char *error = olm_outbound_group_session_last_error(session);
NSAssert(NO, @"olm_outbound_group_session_key error: %s", error);
NSLog(@"olm_outbound_group_session_key error: %s", error);
return nil;
}
NSString *sessionKey = [[NSString alloc] initWithData:sessionKeyData encoding:NSUTF8StringEncoding];
return sessionKey;
}
- (NSString *)encryptMessage:(NSString *)message {
- (NSString *)encryptMessage:(NSString *)message error:(NSError**)error {
NSData *plaintextData = [message dataUsingEncoding:NSUTF8StringEncoding];
size_t ciphertextLength = olm_group_encrypt_message_length(session, plaintextData.length);
NSMutableData *ciphertext = [NSMutableData dataWithLength:ciphertextLength];
@ -108,8 +108,19 @@
}
size_t result = olm_group_encrypt(session, plaintextData.bytes, plaintextData.length, ciphertext.mutableBytes, ciphertext.length);
if (result == olm_error()) {
const char *error = olm_outbound_group_session_last_error(session);
NSAssert(NO, @"olm_group_encrypt error: %s", error);
const char *olm_error = olm_outbound_group_session_last_error(session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_group_encrypt error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_encrypt error: %@", errorString]
}];
}
return nil;
}
return [[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding];
@ -127,7 +138,7 @@
NSParameterAssert(serializedData.length > 0);
if (key.length == 0 || serializedData.length == 0) {
if (error) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
}
return nil;
}
@ -137,7 +148,7 @@
const char *olm_error = olm_outbound_group_session_last_error(session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && errorString) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}
@ -154,7 +165,7 @@
const char *olm_error = olm_outbound_group_session_last_error(session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && errorString) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}

View file

@ -13,11 +13,11 @@
@interface OLMSession : NSObject <OLMSerializable, NSSecureCoding>
- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey;
- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey error:(NSError**)error;
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage;
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error;
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage;
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error;
- (NSString*) sessionIdentifier;
@ -26,9 +26,9 @@
- (BOOL) matchesInboundSessionFrom:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString *)oneTimeKeyMessage;
/** UTF-8 plaintext -> base64 ciphertext */
- (OLMMessage*) encryptMessage:(NSString*)message;
- (OLMMessage*) encryptMessage:(NSString*)message error:(NSError**)error;
/** base64 ciphertext -> UTF-8 plaintext */
- (NSString*) decryptMessage:(OLMMessage*)message;
- (NSString*) decryptMessage:(OLMMessage*)message error:(NSError**)error;
@end

View file

@ -59,7 +59,7 @@
return self;
}
- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey {
- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey error:(NSError**)error {
self = [self initWithAccount:account];
if (!self) {
return nil;
@ -69,14 +69,25 @@
NSData *otKey = [theirOneTimeKey dataUsingEncoding:NSUTF8StringEncoding];
size_t result = olm_create_outbound_session(_session, account.account, idKey.bytes, idKey.length, otKey.bytes, otKey.length, random.mutableBytes, random.length);
if (result == olm_error()) {
const char *error = olm_session_last_error(_session);
NSAssert(NO, @"olm_create_outbound_session error: %s", error);
const char *olm_error = olm_session_last_error(_session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_create_outbound_session error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_outbound_session error: %@", errorString]
}];
}
return nil;
}
return self;
}
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage {
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error {
self = [self initWithAccount:account];
if (!self) {
return nil;
@ -84,14 +95,25 @@
NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]];
size_t result = olm_create_inbound_session(_session, account.account, otk.mutableBytes, oneTimeKeyMessage.length);
if (result == olm_error()) {
const char *error = olm_session_last_error(_session);
NSAssert(NO, @"olm_create_inbound_session error: %s", error);
const char *olm_error = olm_session_last_error(_session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_create_inbound_session error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_inbound_session error: %@", errorString]
}];
}
return nil;
}
return self;
}
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage {
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error {
self = [self initWithAccount:account];
if (!self) {
return nil;
@ -100,8 +122,19 @@
NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]];
size_t result = olm_create_inbound_session_from(_session, account.account, idKey.bytes, idKey.length, otk.mutableBytes, otk.length);
if (result == olm_error()) {
const char *error = olm_session_last_error(_session);
NSAssert(NO, @"olm_create_inbound_session_from error: %s", error);
const char *olm_error = olm_session_last_error(_session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_create_inbound_session_from error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_inbound_session_from error: %@", errorString]
}];
}
return nil;
}
return self;
@ -116,14 +149,14 @@
size_t result = olm_session_id(_session, idData.mutableBytes, idData.length);
if (result == olm_error()) {
const char *error = olm_session_last_error(_session);
NSAssert(NO, @"olm_session_id error: %s", error);
NSLog(@"olm_session_id error: %s", error);
return nil;
}
NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding];
return idString;
}
- (OLMMessage*) encryptMessage:(NSString*)message {
- (OLMMessage*) encryptMessage:(NSString*)message error:(NSError**)error {
size_t messageType = olm_encrypt_message_type(_session);
size_t randomLength = olm_encrypt_random_length(_session);
NSMutableData *random = [OLMUtility randomBytesOfLength:randomLength];
@ -135,8 +168,19 @@
}
size_t result = olm_encrypt(_session, plaintextData.bytes, plaintextData.length, random.mutableBytes, random.length, ciphertext.mutableBytes, ciphertext.length);
if (result == olm_error()) {
const char *error = olm_session_last_error(_session);
NSAssert(NO, @"olm_encrypt error: %s", error);
const char *olm_error = olm_session_last_error(_session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_encrypt error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_encrypt error: %@", errorString]
}];
}
return nil;
}
NSString *ciphertextString = [[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding];
@ -144,7 +188,7 @@
return encryptedMessage;
}
- (NSString*) decryptMessage:(OLMMessage*)message {
- (NSString*) decryptMessage:(OLMMessage*)message error:(NSError**)error {
NSParameterAssert(message != nil);
NSData *messageData = [message.ciphertext dataUsingEncoding:NSUTF8StringEncoding];
if (!messageData) {
@ -153,8 +197,19 @@
NSMutableData *mutMessage = messageData.mutableCopy;
size_t maxPlaintextLength = olm_decrypt_max_plaintext_length(_session, message.type, mutMessage.mutableBytes, mutMessage.length);
if (maxPlaintextLength == olm_error()) {
const char *error = olm_session_last_error(_session);
NSAssert(NO, @"olm_decrypt_max_plaintext_length error: %s", error);
const char *olm_error = olm_session_last_error(_session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_decrypt_max_plaintext_length error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_decrypt_max_plaintext_length error: %@", errorString]
}];
}
return nil;
}
// message buffer is destroyed by olm_decrypt_max_plaintext_length
@ -162,8 +217,19 @@
NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength];
size_t plaintextLength = olm_decrypt(_session, message.type, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length);
if (plaintextLength == olm_error()) {
const char *error = olm_session_last_error(_session);
NSAssert(NO, @"olm_decrypt error: %s", error);
const char *olm_error = olm_session_last_error(_session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
NSLog(@"olm_decrypt error: %@", errorString);
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_decrypt error: %@", errorString]
}];
}
return nil;
}
plaintextData.length = plaintextLength;
@ -183,7 +249,7 @@
NSParameterAssert(serializedData.length > 0);
if (key.length == 0 || serializedData.length == 0) {
if (error) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
}
return nil;
}
@ -193,7 +259,7 @@
const char *olm_error = olm_session_last_error(_session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && errorString) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}
@ -210,7 +276,7 @@
const char *olm_error = olm_session_last_error(_session);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && errorString) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
*error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}

View file

@ -8,6 +8,8 @@
#import <Foundation/Foundation.h>
FOUNDATION_EXPORT NSString *const OLMErrorDomain;
@interface OLMUtility : NSObject
/**

View file

@ -10,6 +10,8 @@
#include "olm/olm.h"
NSString *const OLMErrorDomain = @"org.matrix.olm";
@interface OLMUtility()
@property (nonatomic) OlmUtility *utility;
@ -61,7 +63,7 @@
size_t result = olm_sha256(_utility, message.bytes, message.length, shaData.mutableBytes, shaData.length);
if (result == olm_error()) {
const char *error = olm_utility_last_error(_utility);
NSAssert(NO, @"olm_sha256 error: %s", error);
NSLog(@"olm_sha256 error: %s", error);
return nil;
}

View file

@ -37,6 +37,7 @@
}
- (void)testAliceAndBob {
NSError *error;
OLMOutboundGroupSession *aliceSession = [[OLMOutboundGroupSession alloc] initOutboundGroupSession];
XCTAssertGreaterThan(aliceSession.sessionIdentifier.length, 0);
@ -47,18 +48,23 @@
NSString *sessionKey = aliceSession.sessionKey;
NSString *message = @"Hello!";
NSString *aliceToBobMsg = [aliceSession encryptMessage:message];
NSString *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
XCTAssertEqual(aliceSession.messageIndex, 1);
XCTAssertGreaterThanOrEqual(aliceToBobMsg.length, 0);
XCTAssertNil(error);
OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:sessionKey];
OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:sessionKey error:&error];
XCTAssertEqualObjects(aliceSession.sessionIdentifier, bobSession.sessionIdentifier);
XCTAssertNil(error);
NSUInteger messageIndex;
NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg messageIndex:&messageIndex];
NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg messageIndex:&messageIndex error:&error];
XCTAssertEqualObjects(message, plaintext);
XCTAssertEqual(messageIndex, 0);
XCTAssertNil(error);
}
- (void)testOutboundGroupSessionSerialization {
@ -76,7 +82,7 @@
OLMOutboundGroupSession *aliceSession = [[OLMOutboundGroupSession alloc] initOutboundGroupSession];
OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:aliceSession.sessionKey];
OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:aliceSession.sessionKey error:nil];
NSData *bobData = [NSKeyedArchiver archivedDataWithRootObject:bobSession];
OLMInboundGroupSession *bobSession2 = [NSKeyedUnarchiver unarchiveObjectWithData:bobData];

View file

@ -26,6 +26,8 @@
}
- (void)testAliceAndBob {
NSError *error;
OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
[bob generateOneTimeKeys:5];
@ -41,13 +43,15 @@
}];
XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey];
OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
NSString *message = @"Hello!";
OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message];
OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
XCTAssertNil(error);
OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext];
NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg];
OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:&error];
XCTAssertEqualObjects(message, plaintext);
XCTAssertNil(error);
BOOL success = [bob removeOneTimeKeysForSession:bobSession];
XCTAssertTrue(success);
}
@ -68,12 +72,12 @@
}];
XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey];
OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
NSString *message = @"Hello I'm Alice!";
OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message];
OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:nil];
OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext];
NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg];
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);
@ -82,13 +86,13 @@
NSString *msg2 = @"Isn't life grand?";
NSString *msg3 = @"Let's go to the opera.";
OLMMessage *eMsg1 = [bobSession encryptMessage:msg1];
OLMMessage *eMsg2 = [bobSession encryptMessage:msg2];
OLMMessage *eMsg3 = [bobSession encryptMessage:msg3];
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];
NSString *dMsg2 = [aliceSession decryptMessage:eMsg2];
NSString *dMsg3 = [aliceSession decryptMessage:eMsg3];
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);
@ -113,6 +117,8 @@
}
- (void) testSessionSerialization {
NSError *error;
OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
[bob generateOneTimeKeys:1];
@ -128,12 +134,14 @@
}];
XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey];
OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
NSString *message = @"Hello I'm Alice!";
OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message];
OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
XCTAssertNil(error);
OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext];
NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg];
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);
@ -142,16 +150,16 @@
NSString *msg2 = @"Isn't life grand?";
NSString *msg3 = @"Let's go to the opera.";
OLMMessage *eMsg1 = [bobSession encryptMessage:msg1];
OLMMessage *eMsg2 = [bobSession encryptMessage:msg2];
OLMMessage *eMsg3 = [bobSession encryptMessage:msg3];
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];
NSString *dMsg2 = [alice2 decryptMessage:eMsg2];
NSString *dMsg3 = [alice2 decryptMessage:eMsg3];
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);