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