Signal-iOS/SignalServiceKit/tests/Storage/TSStorageIdentityKeyStoreTests.m
Michael Kirk ccb4a88742 Import SSK (and history) into Signal-iOS
git remote add ssk ../SignalServiceKit
git remote update
git merge -s ours --allow-unrelated-histories --no-commit ssk/master
git read-tree --prefix=SignalServiceKit -u ssk/master
git commit
2017-07-21 13:55:01 -04:00

76 lines
2.8 KiB
Objective-C

//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <25519/Curve25519.h>
#import "OWSUnitTestEnvironment.h"
#import "SecurityUtils.h"
#import "OWSIdentityManager.h"
#import "OWSRecipientIdentity.h"
#import "TSStorageManager.h"
#import "TextSecureKitEnv.h"
@interface TSStorageIdentityKeyStoreTests : XCTestCase
@end
@implementation TSStorageIdentityKeyStoreTests
- (void)setUp {
[super setUp];
[[TSStorageManager sharedManager] purgeCollection:@"TSStorageManagerTrustedKeysCollection"];
[OWSRecipientIdentity removeAllObjectsInCollection];
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testNewEmptyKey {
NSData *newKey = [SecurityUtils generateRandomBytes:32];
NSString *recipientId = @"test@gmail.com";
XCTAssert([[OWSIdentityManager sharedManager] isTrustedIdentityKey:newKey recipientId:recipientId direction:TSMessageDirectionOutgoing]);
XCTAssert([[OWSIdentityManager sharedManager] isTrustedIdentityKey:newKey recipientId:recipientId direction:TSMessageDirectionIncoming]);
}
- (void)testAlreadyRegisteredKey {
NSData *newKey = [SecurityUtils generateRandomBytes:32];
NSString *recipientId = @"test@gmail.com";
[[OWSIdentityManager sharedManager] saveRemoteIdentity:newKey recipientId:recipientId];
XCTAssert([[OWSIdentityManager sharedManager] isTrustedIdentityKey:newKey recipientId:recipientId direction:TSMessageDirectionOutgoing]);
XCTAssert([[OWSIdentityManager sharedManager] isTrustedIdentityKey:newKey recipientId:recipientId direction:TSMessageDirectionIncoming]);
}
- (void)testChangedKey
{
NSData *originalKey = [SecurityUtils generateRandomBytes:32];
NSString *recipientId = @"test@protonmail.com";
[[OWSIdentityManager sharedManager] saveRemoteIdentity:originalKey recipientId:recipientId];
XCTAssert([[OWSIdentityManager sharedManager] isTrustedIdentityKey:originalKey recipientId:recipientId direction:TSMessageDirectionOutgoing]);
XCTAssert([[OWSIdentityManager sharedManager] isTrustedIdentityKey:originalKey recipientId:recipientId direction:TSMessageDirectionIncoming]);
NSData *otherKey = [SecurityUtils generateRandomBytes:32];
XCTAssertFalse([[OWSIdentityManager sharedManager] isTrustedIdentityKey:otherKey recipientId:recipientId direction:TSMessageDirectionOutgoing]);
XCTAssert([[OWSIdentityManager sharedManager] isTrustedIdentityKey:otherKey recipientId:recipientId direction:TSMessageDirectionIncoming]);
}
- (void)testIdentityKey {
[[OWSIdentityManager sharedManager] generateNewIdentityKey];
XCTAssert([[[OWSIdentityManager sharedManager] identityKeyPair].publicKey length] == 32);
}
@end