2 // secd-155-otrnegotiationmonitor.m
5 // Created by Michelle Auricchio on 6/5/17.
8 #import <Foundation/Foundation.h>
9 #include "keychain/SecureObjectSync/SOSAccount.h"
10 #include <Security/SecureObjectSync/SOSCloudCircle.h>
11 #include "keychain/SecureObjectSync/SOSInternal.h"
12 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
13 #include "keychain/SecureObjectSync/SOSTransport.h"
14 #include <Security/SecureObjectSync/SOSViews.h>
15 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
16 #include "keychain/SecureObjectSync/SOSTransportMessage.h"
17 #include "keychain/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 <Security/SecRecoveryKey.h>
31 #include <utilities/SecCFWrappers.h>
32 #include <Security/SecKeyPriv.h>
34 #include "keychain/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 "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
43 static void tests(void)
45 CFErrorRef error = NULL;
47 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
48 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
49 CFStringRef cfaccount = CFSTR("test@test.org");
51 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("ak"));
52 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("ak"));
54 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
56 // Bob wins writing at this point, feed the changes back to alice.
57 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
59 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
60 CFReleaseNull(cfpassword);
63 ok(NULL != alice_account, "Alice Created");
64 ok(NULL != bob_account, "Bob Created");
66 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
69 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
71 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
74 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
77 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
79 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
80 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
82 CFReleaseNull(applicants);
85 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
87 accounts_agree("bob&alice pair", bob_account, alice_account);
89 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
90 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
93 SOSDataSourceRef alice_ds = SOSDataSourceFactoryCreateDataSource(alice_account.factory, CFSTR("ak"), NULL);
94 SOSEngineRef alice_engine = alice_ds ? SOSDataSourceGetSharedEngine(alice_ds, NULL) : (SOSEngineRef) NULL;
96 ok(false == SOSEngineHandleCodedMessage(alice_account, alice_engine, (__bridge CFStringRef)bob_account.peerID, NULL, NULL));
98 SOSDataSourceRef bob_ds = SOSDataSourceFactoryCreateDataSource(bob_account.factory, CFSTR("ak"), NULL);
99 SOSEngineRef bob_engine = bob_ds ? SOSDataSourceGetSharedEngine(bob_ds, NULL) : (SOSEngineRef) NULL;
101 ok(false == SOSEngineHandleCodedMessage(bob_account, bob_engine, (__bridge CFStringRef)alice_account.peerID, NULL, NULL));
103 /* new routines to test */
104 __block NSString *bobID = bob_account.peerID;
105 __block NSString *aliceID = alice_account.peerID;
107 SOSEngineWithPeerID(bob_engine, (__bridge CFStringRef)aliceID, NULL, ^(SOSPeerRef alice_peer, SOSCoderRef coder, SOSDataSourceRef dataSource, SOSTransactionRef txn, bool *forceSaveState) {
108 ok(false == SOSPeerOTRTimerHaveReachedMaxRetryAllowance(bob_account , aliceID));
109 ok(false == SOSPeerTimerForPeerExist(alice_peer));
110 ok(false == SOSPeerOTRTimerHaveAnRTTAvailable(bob_account, aliceID));
113 SOSEngineWithPeerID(alice_engine, (__bridge CFStringRef)bobID, NULL, ^(SOSPeerRef bob_peer, SOSCoderRef coder, SOSDataSourceRef dataSource, SOSTransactionRef txn, bool *forceSaveState) {
114 ok(false == SOSPeerOTRTimerHaveReachedMaxRetryAllowance(alice_account , bobID));
115 ok(false == SOSPeerTimerForPeerExist(bob_peer));
116 ok(false == SOSPeerOTRTimerHaveAnRTTAvailable(alice_account, bobID));
119 //creating test devices
122 // Optionally prefix each peer with name to make them more unique.
123 CFArrayRef deviceIDs = CFArrayCreateForCFTypes(kCFAllocatorDefault,alice_account.peerID, bob_account.peerID, NULL);
124 CFSetRef views = SOSViewsCopyTestV2Default();
125 CFMutableArrayRef peerMetas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
126 CFStringRef deviceID;
127 CFArrayForEachC(deviceIDs, deviceID) {
128 SOSPeerMetaRef peerMeta = SOSPeerMetaCreateWithComponents(deviceID, views, NULL);
129 CFArrayAppendValue(peerMetas, peerMeta);
130 CFReleaseNull(peerMeta);
133 CFReleaseNull(views);
134 CFArrayForEachC(deviceIDs, deviceID) {
135 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
136 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
138 if([alice_account.peerID isEqual: (__bridge id)(deviceID)]){
139 alice_account.factory = device->dsf;
140 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
143 bob_account.factory = device->dsf;
144 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
147 CFReleaseNull(device);
149 CFReleaseNull(deviceIDs);
150 CFReleaseNull(peerMetas);
152 SOSUnregisterAllTransportMessages();
153 CFArrayRemoveAllValues(message_transports);
155 ok(SOSAccountEnsurePeerRegistration(alice_account, NULL), "ensure peer registration - alice");
157 ok(SOSAccountEnsurePeerRegistration(bob_account, NULL), "ensure peer registration - bob");
159 // ids_test_sync(alice_account, bob_account);
162 int secd_155_otr_negotiation_monitor(int argc, char *const *argv)
166 secd_test_setup_temp_keychain(__FUNCTION__, NULL);