From 1ebb82f98715b672474c5c0f27d8b18ec740fced Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 7 Nov 2016 19:23:52 -0500 Subject: [PATCH] Contacts don't have safety numbers until they've exchanged keys. // FREEBIE --- .../TSKitiOSTestApp.xcodeproj/project.pbxproj | 8 ++++ src/Contacts/TSThread.h | 2 + src/Contacts/TSThread.m | 5 +++ src/Contacts/Threads/TSContactThread.m | 6 +++ src/Security/OWSFingerprint.m | 3 ++ src/Storage/TSYapDatabaseObject.h | 4 ++ src/Storage/TSYapDatabaseObject.m | 12 +++++- tests/Contacts/TSContactThreadTest.m | 38 +++++++++++++++++++ tests/Contacts/TSGroupThreadTest.m | 23 +++++++++++ 9 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/Contacts/TSContactThreadTest.m create mode 100644 tests/Contacts/TSGroupThreadTest.m diff --git a/Example/TSKitiOSTestApp/TSKitiOSTestApp.xcodeproj/project.pbxproj b/Example/TSKitiOSTestApp/TSKitiOSTestApp.xcodeproj/project.pbxproj index 331095f8b..39cba8f6e 100644 --- a/Example/TSKitiOSTestApp/TSKitiOSTestApp.xcodeproj/project.pbxproj +++ b/Example/TSKitiOSTestApp/TSKitiOSTestApp.xcodeproj/project.pbxproj @@ -10,6 +10,8 @@ 308D7DFA789594CEA62740D9 /* libPods-TSKitiOSTestAppTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C0DC1A83C39CBC09FB2405A3 /* libPods-TSKitiOSTestAppTests.a */; }; 45046FE01D95A6130015EFF2 /* TSMessagesManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 45046FDF1D95A6130015EFF2 /* TSMessagesManagerTest.m */; }; 450E3C9A1D96DD2600BF4EB6 /* OWSDisappearingMessagesJobTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 450E3C991D96DD2600BF4EB6 /* OWSDisappearingMessagesJobTest.m */; }; + 4516E3E81DD153CC00DC4206 /* TSGroupThreadTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4516E3E71DD153CC00DC4206 /* TSGroupThreadTest.m */; }; + 4516E3EA1DD1542300DC4206 /* TSContactThreadTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4516E3E91DD1542300DC4206 /* TSContactThreadTest.m */; }; 452EE6CF1D4A754C00E934BA /* TSThreadTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 452EE6CE1D4A754C00E934BA /* TSThreadTest.m */; }; 452EE6D51D4AC43300E934BA /* OWSOrphanedDataCleanerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 452EE6D41D4AC43300E934BA /* OWSOrphanedDataCleanerTest.m */; }; 453E1FCF1DA8313100DDD7B7 /* OWSMessageSenderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 453E1FCE1DA8313100DDD7B7 /* OWSMessageSenderTest.m */; }; @@ -59,6 +61,8 @@ 36DA6C703F99948D553F4E3F /* Pods-TSKitiOSTestAppTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TSKitiOSTestAppTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-TSKitiOSTestAppTests/Pods-TSKitiOSTestAppTests.debug.xcconfig"; sourceTree = ""; }; 45046FDF1D95A6130015EFF2 /* TSMessagesManagerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TSMessagesManagerTest.m; path = ../../../tests/Messages/TSMessagesManagerTest.m; sourceTree = ""; }; 450E3C991D96DD2600BF4EB6 /* OWSDisappearingMessagesJobTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OWSDisappearingMessagesJobTest.m; path = ../../../tests/Messages/OWSDisappearingMessagesJobTest.m; sourceTree = ""; }; + 4516E3E71DD153CC00DC4206 /* TSGroupThreadTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TSGroupThreadTest.m; path = ../../../tests/Contacts/TSGroupThreadTest.m; sourceTree = ""; }; + 4516E3E91DD1542300DC4206 /* TSContactThreadTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TSContactThreadTest.m; path = ../../../tests/Contacts/TSContactThreadTest.m; sourceTree = ""; }; 452EE6CE1D4A754C00E934BA /* TSThreadTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TSThreadTest.m; path = ../../../tests/Contacts/TSThreadTest.m; sourceTree = ""; }; 452EE6D41D4AC43300E934BA /* OWSOrphanedDataCleanerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSOrphanedDataCleanerTest.m; sourceTree = ""; }; 453E1FCE1DA8313100DDD7B7 /* OWSMessageSenderTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OWSMessageSenderTest.m; path = ../../../tests/Messages/OWSMessageSenderTest.m; sourceTree = ""; }; @@ -201,6 +205,8 @@ 459850C01D22C6F2006FFEDB /* PhoneNumberTest.m */, 452EE6CE1D4A754C00E934BA /* TSThreadTest.m */, 45B700961D9841E400269FFD /* OWSDisappearingMessagesConfigurationTest.m */, + 4516E3E71DD153CC00DC4206 /* TSGroupThreadTest.m */, + 4516E3E91DD1542300DC4206 /* TSContactThreadTest.m */, ); name = Contacts; sourceTree = ""; @@ -535,6 +541,7 @@ 45458B751CC342B600A02153 /* SignedPreKeyDeletionTests.m in Sources */, 45458B7B1CC342B600A02153 /* CryptographyTests.m in Sources */, 45D7243F1D67899F00E0CA54 /* OWSDeviceProvisionerTest.m in Sources */, + 4516E3E81DD153CC00DC4206 /* TSGroupThreadTest.m in Sources */, 45458B791CC342B600A02153 /* TSStoragePreKeyStoreTests.m in Sources */, 452EE6D51D4AC43300E934BA /* OWSOrphanedDataCleanerTest.m in Sources */, 450E3C9A1D96DD2600BF4EB6 /* OWSDisappearingMessagesJobTest.m in Sources */, @@ -547,6 +554,7 @@ 453E1FDB1DA83EFB00DDD7B7 /* OWSFakeContactsUpdater.m in Sources */, 453E1FD81DA83E1000DDD7B7 /* OWSFakeContactsManager.m in Sources */, 454021ED1D960ABF00F2126D /* OWSDisappearingMessageFinderTest.m in Sources */, + 4516E3EA1DD1542300DC4206 /* TSContactThreadTest.m in Sources */, 45458B771CC342B600A02153 /* TSMessageStorageTests.m in Sources */, 45046FE01D95A6130015EFF2 /* TSMessagesManagerTest.m in Sources */, 45458B7C1CC342B600A02153 /* MessagePaddingTests.m in Sources */, diff --git a/src/Contacts/TSThread.h b/src/Contacts/TSThread.h index e51640d8a..06e21fdbb 100644 --- a/src/Contacts/TSThread.h +++ b/src/Contacts/TSThread.h @@ -64,6 +64,8 @@ NS_ASSUME_NONNULL_BEGIN */ - (BOOL)hasUnreadMessages; +- (BOOL)hasSafetyNumbers; + - (void)markAllAsRead; - (void)markAllAsReadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction; diff --git a/src/Contacts/TSThread.m b/src/Contacts/TSThread.m index e2b446534..9b4e94b71 100644 --- a/src/Contacts/TSThread.m +++ b/src/Contacts/TSThread.m @@ -84,6 +84,11 @@ NS_ASSUME_NONNULL_BEGIN return nil; } +- (BOOL)hasSafetyNumbers +{ + return NO; +} + #pragma mark Interactions /** diff --git a/src/Contacts/Threads/TSContactThread.m b/src/Contacts/Threads/TSContactThread.m index f4e97c9af..fe68e6363 100644 --- a/src/Contacts/Threads/TSContactThread.m +++ b/src/Contacts/Threads/TSContactThread.m @@ -3,6 +3,7 @@ #import "TSContactThread.h" #import "ContactsUpdater.h" +#import "TSStorageManager+identityKeyStore.h" #import "TextSecureKitEnv.h" #import #import @@ -74,6 +75,11 @@ NS_ASSUME_NONNULL_BEGIN return false; } +- (BOOL)hasSafetyNumbers +{ + return !![self.storageManager identityKeyForRecipientId:self.contactIdentifier]; +} + - (NSString *)name { return [[TextSecureKitEnv sharedEnv].contactsManager nameStringForPhoneIdentifier:self.contactIdentifier]; diff --git a/src/Security/OWSFingerprint.m b/src/Security/OWSFingerprint.m index e845081dc..eb61eb937 100644 --- a/src/Security/OWSFingerprint.m +++ b/src/Security/OWSFingerprint.m @@ -29,6 +29,9 @@ static uint32_t const OWSFingerprintDefaultHashIterations = 5200; theirIdentityKey:(NSData *)theirIdentityKeyWithoutKeyType hashIterations:(uint32_t)hashIterations { + NSParameterAssert(theirIdentityKeyWithoutKeyType.length == 32); + NSParameterAssert(myIdentityKeyWithoutKeyType.length == 32); + self = [super init]; if (!self) { return self; diff --git a/src/Storage/TSYapDatabaseObject.h b/src/Storage/TSYapDatabaseObject.h index 638ab93e8..c20cf3b51 100644 --- a/src/Storage/TSYapDatabaseObject.h +++ b/src/Storage/TSYapDatabaseObject.h @@ -3,6 +3,7 @@ #import +@class TSStorageManager; @class YapDatabaseConnection; @class YapDatabaseReadTransaction; @class YapDatabaseReadWriteTransaction; @@ -61,6 +62,9 @@ - (YapDatabaseConnection *)dbConnection; + (YapDatabaseConnection *)dbConnection; +- (TSStorageManager *)storageManager; ++ (TSStorageManager *)storageManager; + /** * Fetches the object with the provided identifier * diff --git a/src/Storage/TSYapDatabaseObject.m b/src/Storage/TSYapDatabaseObject.m index edd07c2c8..591bf7deb 100644 --- a/src/Storage/TSYapDatabaseObject.m +++ b/src/Storage/TSYapDatabaseObject.m @@ -65,11 +65,21 @@ return [[self class] dbConnection]; } +- (TSStorageManager *)storageManager +{ + return [[self class] storageManager]; +} + #pragma mark Class Methods + (YapDatabaseConnection *)dbConnection { - return [TSStorageManager sharedManager].dbConnection; + return [self storageManager].dbConnection; +} + ++ (TSStorageManager *)storageManager +{ + return [TSStorageManager sharedManager]; } + (NSString *)collection diff --git a/tests/Contacts/TSContactThreadTest.m b/tests/Contacts/TSContactThreadTest.m new file mode 100644 index 000000000..6498cd4d2 --- /dev/null +++ b/tests/Contacts/TSContactThreadTest.m @@ -0,0 +1,38 @@ +// Created by Michael Kirk on 11/7/16. +// Copyright © 2016 Open Whisper Systems. All rights reserved. + +#import "TSContactThread.h" +#import "TSStorageManager+identityKeyStore.h" +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface TSContactThreadTest : XCTestCase + +@property (nonatomic) TSContactThread *contactThread; + +@end + +@implementation TSContactThreadTest + +- (void)setUp +{ + self.contactThread = [TSContactThread getOrCreateThreadWithContactId:@"fake-contact-id"]; + [self.contactThread.storageManager removeIdentityKeyForRecipient:self.contactThread.contactIdentifier]; +} + +- (void)testHasSafetyNumbersWithoutRemoteIdentity +{ + XCTAssertFalse(self.contactThread.hasSafetyNumbers); +} + +- (void)testHasSafetyNumbersWithRemoteIdentity +{ + [self.contactThread.storageManager saveRemoteIdentity:[NSData new] + recipientId:self.contactThread.contactIdentifier]; + XCTAssert(self.contactThread.hasSafetyNumbers); +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/tests/Contacts/TSGroupThreadTest.m b/tests/Contacts/TSGroupThreadTest.m new file mode 100644 index 000000000..9dfe91d90 --- /dev/null +++ b/tests/Contacts/TSGroupThreadTest.m @@ -0,0 +1,23 @@ +// Created by Michael Kirk on 11/7/16. +// Copyright © 2016 Open Whisper Systems. All rights reserved. + +#import "TSGroupThread.h" +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface TSGroupThreadTest : XCTestCase + +@end + +@implementation TSGroupThreadTest + +- (void)testHasSafetyNumbers +{ + TSGroupThread *groupThread = [TSGroupThread new]; + XCTAssertFalse(groupThread.hasSafetyNumbers); +} + +@end + +NS_ASSUME_NONNULL_END