2 // secd-155-otrnegotiationmonitor.m
5 // Created by Michelle Auricchio on 6/5/17.
8 #import <Foundation/Foundation.h>
9 #include <Security/SecureObjectSync/SOSAccount.h>
10 #include <Security/SecureObjectSync/SOSCloudCircle.h>
11 #include <Security/SecureObjectSync/SOSInternal.h>
12 #include <Security/SecureObjectSync/SOSUserKeygen.h>
13 #include <Security/SecureObjectSync/SOSTransport.h>
14 #include <Security/SecureObjectSync/SOSViews.h>
15 #include <Security/SecureObjectSync/SOSAccountTrustClassic+Expansion.h>
16 #include <Security/SecureObjectSync/SOSTransportMessage.h>
17 #include <Security/SecureObjectSync/SOSPeerOTRTimer.h>
19 #import "SOSAccountTesting.h"
20 #import "SOSTransportTestTransports.h"
25 #include "secd_regressions.h"
26 #include "SOSTestDataSource.h"
28 #include "SOSRegressionUtilities.h"
29 #include "SecRecoveryKey.h"
31 #include <utilities/SecCFWrappers.h>
32 #include <Security/SecKeyPriv.h>
34 #include <securityd/SOSCloudCircleServer.h>
35 #include "SecdTestKeychainUtilities.h"
36 #include "SOSAccountTesting.h"
37 #import "SOSTransportTestTransports.h"
38 #include "SOSTestDevice.h"
39 #include "SOSTestDataSource.h"
40 #include <Security/SecureObjectSync/SOSAccountTrustClassic+Circle.h>
41 static int kTestTestCount = 114;
44 static void tests(void)
46 CFErrorRef error = NULL;
48 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
49 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
50 CFStringRef cfaccount = CFSTR("test@test.org");
52 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("ak"));
53 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("ak"));
55 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
57 // Bob wins writing at this point, feed the changes back to alice.
58 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
60 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
61 CFReleaseNull(cfpassword);
64 ok(NULL != alice_account, "Alice Created");
65 ok(NULL != bob_account, "Bob Created");
67 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
70 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
72 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
75 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
78 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
80 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
81 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
83 CFReleaseNull(applicants);
86 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
88 accounts_agree("bob&alice pair", bob_account, alice_account);
90 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
91 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
94 SOSDataSourceRef alice_ds = SOSDataSourceFactoryCreateDataSource(alice_account.factory, CFSTR("ak"), NULL);
95 SOSEngineRef alice_engine = alice_ds ? SOSDataSourceGetSharedEngine(alice_ds, NULL) : (SOSEngineRef) NULL;
97 ok(false == SOSEngineHandleCodedMessage(alice_account, alice_engine, (__bridge CFStringRef)bob_account.peerID, NULL, NULL));
99 SOSDataSourceRef bob_ds = SOSDataSourceFactoryCreateDataSource(bob_account.factory, CFSTR("ak"), NULL);
100 SOSEngineRef bob_engine = bob_ds ? SOSDataSourceGetSharedEngine(bob_ds, NULL) : (SOSEngineRef) NULL;
102 ok(false == SOSEngineHandleCodedMessage(bob_account, bob_engine, (__bridge CFStringRef)alice_account.peerID, NULL, NULL));
104 /* new routines to test */
105 __block NSString *bobID = bob_account.peerID;
106 __block NSString *aliceID = alice_account.peerID;
108 SOSEngineWithPeerID(bob_engine, (__bridge CFStringRef)aliceID, NULL, ^(SOSPeerRef alice_peer, SOSCoderRef coder, SOSDataSourceRef dataSource, SOSTransactionRef txn, bool *forceSaveState) {
109 ok(false == SOSPeerOTRTimerHaveReachedMaxRetryAllowance(bob_account , aliceID));
110 ok(false == SOSPeerTimerForPeerExist(alice_peer));
111 ok(false == SOSPeerOTRTimerHaveAnRTTAvailable(bob_account, aliceID));
114 SOSEngineWithPeerID(alice_engine, (__bridge CFStringRef)bobID, NULL, ^(SOSPeerRef bob_peer, SOSCoderRef coder, SOSDataSourceRef dataSource, SOSTransactionRef txn, bool *forceSaveState) {
115 ok(false == SOSPeerOTRTimerHaveReachedMaxRetryAllowance(alice_account , bobID));
116 ok(false == SOSPeerTimerForPeerExist(bob_peer));
117 ok(false == SOSPeerOTRTimerHaveAnRTTAvailable(alice_account, bobID));
120 //creating test devices
123 // Optionally prefix each peer with name to make them more unique.
124 CFArrayRef deviceIDs = CFArrayCreateForCFTypes(kCFAllocatorDefault,alice_account.peerID, bob_account.peerID, NULL);
125 CFSetRef views = SOSViewsCopyTestV2Default();
126 CFMutableArrayRef peerMetas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
127 CFStringRef deviceID;
128 CFArrayForEachC(deviceIDs, deviceID) {
129 SOSPeerMetaRef peerMeta = SOSPeerMetaCreateWithComponents(deviceID, views, NULL);
130 CFArrayAppendValue(peerMetas, peerMeta);
131 CFReleaseNull(peerMeta);
134 CFReleaseNull(views);
135 CFArrayForEachC(deviceIDs, deviceID) {
136 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
137 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
139 if([alice_account.peerID isEqual: (__bridge id)(deviceID)]){
140 alice_account.factory = device->dsf;
141 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
144 bob_account.factory = device->dsf;
145 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
148 CFReleaseNull(device);
150 CFReleaseNull(deviceIDs);
151 CFReleaseNull(peerMetas);
153 SOSUnregisterAllTransportMessages();
154 CFArrayRemoveAllValues(message_transports);
156 ok(SOSAccountEnsurePeerRegistration(alice_account, NULL), "ensure peer registration - alice");
158 ok(SOSAccountEnsurePeerRegistration(bob_account, NULL), "ensure peer registration - bob");
160 // ids_test_sync(alice_account, bob_account);
163 int secd_155_otr_negotiation_monitor(int argc, char *const *argv)
165 plan_tests(kTestTestCount);
167 secd_test_setup_temp_keychain(__FUNCTION__, NULL);