2 * Copyright (c) 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@
25 #include <Security/SecBase.h>
26 #include <Security/SecItem.h>
28 #include <CoreFoundation/CFDictionary.h>
30 #include <Security/SecureObjectSync/SOSAccount.h>
31 #include <Security/SecureObjectSync/SOSCloudCircle.h>
32 #include <Security/SecureObjectSync/SOSInternal.h>
33 #include <Security/SecureObjectSync/SOSUserKeygen.h>
34 #include <Security/SecureObjectSync/SOSTransport.h>
39 #include "secd_regressions.h"
40 #include "SOSTestDataSource.h"
42 #include "SOSRegressionUtilities.h"
43 #include <utilities/SecCFWrappers.h>
44 #include <Security/SecKeyPriv.h>
46 #include <securityd/SOSCloudCircleServer.h>
48 #include "SOSAccountTesting.h"
50 #include "SecdTestKeychainUtilities.h"
52 static int kTestTestCount
= 63;
54 static void tests(void)
56 CFErrorRef error
= NULL
;
58 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
59 CFStringRef cfaccount
= CFSTR("test@test.org");
61 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
63 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
64 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
66 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
68 // Bob wins writing at this point, feed the changes back to alice.
69 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
71 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
74 CFReleaseNull(cfpassword
);
77 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
80 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
82 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
85 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
87 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
89 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicants %@ (%@)", applicants
, error
);
91 CFReleaseSafe(applicants
);
94 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
95 CFMutableStringRef timeDescription
= CFStringCreateMutableCopy(kCFAllocatorDefault
, 0, CFSTR("["));
96 CFAbsoluteTime currentTimeAndDate
= CFAbsoluteTimeGetCurrent();
98 withStringOfAbsoluteTime(currentTimeAndDate
, ^(CFStringRef decription
) {
99 CFStringAppend(timeDescription
, decription
);
101 CFStringAppend(timeDescription
, CFSTR("]"));
105 CFNumberRef attempts
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberLongLongType
, (const void*)&tries
);
107 CFMutableArrayRef escrowTimeAndTries
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
108 CFArrayAppendValue(escrowTimeAndTries
, timeDescription
);
109 CFArrayAppendValue(escrowTimeAndTries
, attempts
);
110 CFDictionaryRef escrowRecord
= CFDictionaryCreateForCFTypes(kCFAllocatorDefault
, CFSTR("account label"), escrowTimeAndTries
, NULL
);
112 CFMutableDictionaryRef record
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
113 CFDictionaryAddValue(record
, CFSTR("12345"), escrowRecord
);
115 SOSFullPeerInfoRef alice_fpi
= SOSAccountGetMyFullPeerInfo(alice_account
);
116 ok(SOSFullPeerInfoAddEscrowRecord(alice_fpi
, CFSTR("12345"), escrowRecord
, &error
), "Adding Escrow records to Alice FPI(%@)", error
);
117 CFDictionaryRef fpi_escrow
= SOSPeerInfoCopyEscrowRecord(SOSFullPeerInfoGetPeerInfo(alice_fpi
));
118 ok(CFEqualSafe(CFDictionaryGetValue(fpi_escrow
, CFSTR("12345")), escrowRecord
), "Alice's FPI has escrow (%@)", error
);
120 ok(SOSAccountAddEscrowRecords(bob_account
, CFSTR("12345"), escrowRecord
, &error
), "Adding escrow to Bob's account (%@)", error
);
121 CFReleaseNull(fpi_escrow
);
123 fpi_escrow
= (CFDictionaryRef
)SOSAccountGetValue(bob_account
, kSOSEscrowRecord
, NULL
);
124 ok(CFEqualSafe(CFDictionaryGetValue(fpi_escrow
, CFSTR("12345")), escrowRecord
), "Bob has escrow records in account (%@)", error
);
126 ok(SOSAccountResetToEmpty(alice_account
, &error
), "Reset to offering (%@)", error
);
127 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
129 ok(SOSAccountResetToEmpty(bob_account
, &error
), "Reset to offering (%@)", error
);
130 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
132 ok(SOSAccountAddEscrowRecords(bob_account
, CFSTR("12345"), escrowRecord
, &error
), "Adding escrow to Bob's account (%@)", error
);
134 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
135 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
137 CFDictionaryRef bob_fpi_escrow
= SOSPeerInfoCopyEscrowRecord(SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
));
138 ok(bob_fpi_escrow
== NULL
, "Bob's FPI escrow should be null");
139 CFReleaseNull(bob_fpi_escrow
);
141 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
142 bob_fpi_escrow
= SOSPeerInfoCopyEscrowRecord(SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
));
143 ok(CFEqualSafe(CFDictionaryGetValue(bob_fpi_escrow
, CFSTR("12345")), escrowRecord
), "Bob has escrow records in account (%@)", error
);
144 CFReleaseNull(bob_fpi_escrow
);
146 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
149 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
150 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicants %@ (%@)", applicants
, error
);
151 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Accept bob into the fold");
152 CFReleaseNull(error
);
153 CFReleaseNull(applicants
);
155 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
157 fpi_escrow
= (CFDictionaryRef
)SOSAccountGetValue(bob_account
, kSOSEscrowRecord
, NULL
);
158 ok(isNull(fpi_escrow
), "Bob's escrow records in the account object should be gone");
160 CFReleaseNull(record
);
161 CFReleaseNull(escrowRecord
);
162 CFReleaseNull(timeDescription
);
163 CFReleaseNull(attempts
);
166 int secd_95_escrow_persistence(int argc
, char *const *argv
)
168 plan_tests(kTestTestCount
);
170 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);