]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-155-otr-negotiation-monitor.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-155-otr-negotiation-monitor.m
1 //
2 // secd-155-otrnegotiationmonitor.m
3 // secdtests_ios
4 //
5 // Created by Michelle Auricchio on 6/5/17.
6 //
7
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"
18
19 #import "SOSAccountTesting.h"
20 #import "SOSTransportTestTransports.h"
21
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "secd_regressions.h"
26 #include "SOSTestDataSource.h"
27
28 #include "SOSRegressionUtilities.h"
29 #include <Security/SecRecoveryKey.h>
30
31 #include <utilities/SecCFWrappers.h>
32 #include <Security/SecKeyPriv.h>
33
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"
41 #if SOS_ENABLED
42
43
44 static void tests(void)
45 {
46 CFErrorRef error = NULL;
47
48 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
49 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
50 CFStringRef cfaccount = CFSTR("test@test.org");
51
52 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("ak"));
53 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("ak"));
54
55 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
56
57 // Bob wins writing at this point, feed the changes back to alice.
58 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
59
60 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
61 CFReleaseNull(cfpassword);
62 CFReleaseNull(error);
63
64 ok(NULL != alice_account, "Alice Created");
65 ok(NULL != bob_account, "Bob Created");
66
67 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
68 CFReleaseNull(error);
69
70 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
71
72 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
73 CFReleaseNull(error);
74
75 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
76
77 {
78 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
79
80 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
81 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
82 CFReleaseNull(error);
83 CFReleaseNull(applicants);
84 }
85
86 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
87
88 accounts_agree("bob&alice pair", bob_account, alice_account);
89
90 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
91 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
92 CFReleaseNull(peers);
93
94 SOSDataSourceRef alice_ds = SOSDataSourceFactoryCreateDataSource(alice_account.factory, CFSTR("ak"), NULL);
95 SOSEngineRef alice_engine = alice_ds ? SOSDataSourceGetSharedEngine(alice_ds, NULL) : (SOSEngineRef) NULL;
96
97 ok(false == SOSEngineHandleCodedMessage(alice_account, alice_engine, (__bridge CFStringRef)bob_account.peerID, NULL, NULL));
98
99 SOSDataSourceRef bob_ds = SOSDataSourceFactoryCreateDataSource(bob_account.factory, CFSTR("ak"), NULL);
100 SOSEngineRef bob_engine = bob_ds ? SOSDataSourceGetSharedEngine(bob_ds, NULL) : (SOSEngineRef) NULL;
101
102 ok(false == SOSEngineHandleCodedMessage(bob_account, bob_engine, (__bridge CFStringRef)alice_account.peerID, NULL, NULL));
103
104 /* new routines to test */
105 __block NSString *bobID = bob_account.peerID;
106 __block NSString *aliceID = alice_account.peerID;
107
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));
112 });
113
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));
118 });
119
120 //creating test devices
121 CFIndex version = 0;
122
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);
132 }
133
134 CFReleaseNull(views);
135 CFArrayForEachC(deviceIDs, deviceID) {
136 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
137 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
138
139 if([alice_account.peerID isEqual: (__bridge id)(deviceID)]){
140 alice_account.factory = device->dsf;
141 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
142 }
143 else{
144 bob_account.factory = device->dsf;
145 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
146 }
147
148 CFReleaseNull(device);
149 }
150 CFReleaseNull(deviceIDs);
151 CFReleaseNull(peerMetas);
152
153 SOSUnregisterAllTransportMessages();
154 CFArrayRemoveAllValues(message_transports);
155
156 ok(SOSAccountEnsurePeerRegistration(alice_account, NULL), "ensure peer registration - alice");
157
158 ok(SOSAccountEnsurePeerRegistration(bob_account, NULL), "ensure peer registration - bob");
159
160 // ids_test_sync(alice_account, bob_account);
161 }
162 #endif
163
164 int secd_155_otr_negotiation_monitor(int argc, char *const *argv)
165 {
166 #if SOS_ENABLED
167 plan_tests(44);
168 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
169 tests();
170 #else
171 plan_tests(0);
172 #endif
173 return 0;
174 }