2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
30 #include <CoreFoundation/CFDictionary.h>
32 #include <Security/SecureObjectSync/SOSAccount.h>
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSUserKeygen.h>
36 #include <Security/SecureObjectSync/SOSTransport.h>
37 #include <Security/SecureObjectSync/SOSViews.h>
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
49 #include <securityd/SOSCloudCircleServer.h>
51 #if !TARGET_IPHONE_SIMULATOR
52 #include "SOSAccountTesting.h"
54 #include "SecdTestKeychainUtilities.h"
56 #if !TARGET_IPHONE_SIMULATOR
58 static CFDataRef
CopyBackupKeyForString(CFStringRef string
, CFErrorRef
*error
)
60 __block CFDataRef result
= NULL
;
61 CFStringPerformWithUTF8CFData(string
, ^(CFDataRef stringAsData
) {
62 result
= SOSCopyDeviceBackupPublicKey(stringAsData
, error
);
67 static int kTestTestCount
= 106;
69 static int kTestTestCount
= 1;
72 static void tests(void)
74 #if !TARGET_IPHONE_SIMULATOR
76 __block CFErrorRef error
= NULL
;
77 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
78 CFStringRef cfaccount
= CFSTR("test@test.org");
80 CFStringRef kTestView1
= CFSTR("TestView1");
81 CFStringRef kTestView2
= CFSTR("TestView2");
83 CFMutableSetRef testViews
= CFSetCreateMutableForCFTypes(kCFAllocatorDefault
);
84 CFSetAddValue(testViews
, kTestView1
);
85 //CFSetAddValue(testViews, kTestView2);
87 SOSViewsSetTestViewsSet(testViews
);
89 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
90 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
91 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
93 CFDataRef alice_backup_key
= CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error
);
94 CFDataRef bob_backup_key
= CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error
);
96 // Bob wins writing at this point, feed the changes back to alice.
97 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
99 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
100 CFReleaseNull(error
);
102 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
103 CFReleaseNull(error
);
105 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
107 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
108 CFReleaseNull(error
);
110 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
112 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
113 CFReleaseNull(error
);
115 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
118 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
120 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
121 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
122 CFReleaseNull(error
);
123 CFReleaseNull(applicants
);
126 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
128 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
129 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
130 CFReleaseNull(peers
);
132 is(SOSAccountUpdateView(alice_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
133 CFReleaseNull(error
);
135 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
136 CFReleaseNull(error
);
139 skip("Labotomzed Public Key setting", 73, ENABLE_V2_BACKUP
); // When we disable, these don't work.
141 ok(SOSAccountSetBackupPublicKey(alice_account
, alice_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
142 CFReleaseNull(error
);
144 ok(SOSAccountSetBackupPublicKey(bob_account
, bob_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
145 CFReleaseNull(error
);
147 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Is alice is in backup before sync?");
149 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Is bob in the backup after sync? - 1");
151 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 4, "updates");
153 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Is alice is in backup after sync?");
155 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "IS bob in the backup after sync");
158 //Bob leaves the circle
160 ok(SOSAccountLeaveCircle(bob_account
, &error
), "Bob Leaves (%@)", error
);
161 CFReleaseNull(error
);
163 //Alice should kick Bob out of the backup!
164 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
166 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Bob left the circle, Alice is not in the backup");
168 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is still in the backup!");
170 //Bob gets back into the circle
171 ok(SOSAccountJoinCircles(bob_account
, &error
));
172 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
174 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
176 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
177 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
178 CFReleaseNull(error
);
179 CFReleaseNull(applicants
);
182 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
186 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
187 CFReleaseNull(error
);
189 ok(SOSAccountSetBackupPublicKey(bob_account
, bob_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
190 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
194 //removing backup key for bob account
198 ok(SOSAccountRemoveBackupPublickey(bob_account
, &error
), "Removing Bob's backup key (%@)", error
);
199 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
201 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key is in the backup - should not be so!");
202 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is up to date in the backup!");
205 // Setting new backup public key for Bob
208 ok(SOSAccountSetBackupPublicKey(bob_account
, bob_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
209 CFReleaseNull(error
);
211 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
212 ok(SOSAccountStartNewBackup(bob_account
, kTestView1
, &error
), "Setting new backup public key for bob account failed: (%@)", error
);
214 //bob is in his own backup
215 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key is not in the backup");
216 //alice does not have bob in her backup
217 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is up to date in the backup - should not be so!");
219 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 5, "updates");
221 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key should be in the backup");
222 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Alice is in the backup");
225 CFReleaseNull(bob_account
);
226 CFReleaseNull(alice_account
);
227 CFReleaseNull(cfpassword
);
229 SOSViewsSetTestViewsSet(NULL
);
231 SOSUnregisterAllTransportMessages();
232 SOSUnregisterAllTransportCircles();
233 SOSUnregisterAllTransportKeyParameters();
234 CFArrayRemoveAllValues(key_transports
);
235 CFArrayRemoveAllValues(circle_transports
);
236 CFArrayRemoveAllValues(message_transports
);
238 CFReleaseNull(testViews
);
239 CFReleaseNull(kTestView1
);
240 CFReleaseNull(kTestView2
);
245 int secd_62_account_backup(int argc
, char *const *argv
)
247 plan_tests(kTestTestCount
);
249 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);