2 * Copyright (c) 2013-2016 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@
24 // secd-200-logstate.c
33 #include <Security/SecBase.h>
34 #include <Security/SecItem.h>
36 #include <CoreFoundation/CFDictionary.h>
38 #include <Security/SecureObjectSync/SOSAccount.h>
39 #include <Security/SecureObjectSync/SOSCloudCircle.h>
40 #include <Security/SecureObjectSync/SOSInternal.h>
41 #include <Security/SecureObjectSync/SOSUserKeygen.h>
42 #include <Security/SecureObjectSync/SOSTransport.h>
47 #include "secd_regressions.h"
48 #include "SOSTestDataSource.h"
50 #include "SOSRegressionUtilities.h"
51 #include <utilities/SecCFWrappers.h>
52 #include <Security/SecKeyPriv.h>
54 #include <securityd/SOSCloudCircleServer.h>
56 #include "SOSAccountTesting.h"
58 #include "SecdTestKeychainUtilities.h"
60 #define HOW_MANY_MINIONS 4
62 static int kTestTestCount
= (5+(HOW_MANY_MINIONS
+1)*20);
65 static bool SOSArrayForEachAccount(CFArrayRef accounts
, bool (^operation
)(SOSAccountRef account
)) {
66 __block
bool retval
= true;
67 CFArrayForEach(accounts
, ^(const void *value
) {
68 SOSAccountRef account
= (SOSAccountRef
) value
;
69 retval
&= operation(account
);
75 static inline void FeedChangesToMasterMinions(CFMutableDictionaryRef changes
, SOSAccountRef master_account
, CFArrayRef minion_accounts
) {
76 FeedChangesTo(changes
, master_account
);
77 SOSArrayForEachAccount(minion_accounts
, ^bool(SOSAccountRef account
) {
78 FeedChangesTo(changes
, account
);
81 FeedChangesTo(changes
, master_account
);
86 static inline bool ProcessChangesOnceMasterMinions(CFMutableDictionaryRef changes
, SOSAccountRef master_account
, CFArrayRef minion_accounts
) {
87 bool result
= FillAllChanges(changes
);
88 FeedChangesToMasterMinions(changes
, master_account
, minion_accounts
);
92 static inline int ProcessChangesForMasterAndMinions(CFMutableDictionaryRef changes
, SOSAccountRef master_account
, CFArrayRef minion_accounts
) {
94 bool new_data
= false;
96 new_data
= ProcessChangesOnceMasterMinions(changes
, master_account
, minion_accounts
);
102 static bool MakeTheBigCircle(CFMutableDictionaryRef changes
, SOSAccountRef master_account
, CFArrayRef minion_accounts
, CFErrorRef
*error
) {
103 bool retval
= SOSAccountResetToOffering_wTxn(master_account
, error
);
104 require_quiet(retval
, errOut
);
105 ProcessChangesForMasterAndMinions(changes
, master_account
, minion_accounts
);
106 retval
= SOSArrayForEachAccount(minion_accounts
, ^bool(SOSAccountRef account
) {
107 bool localret
= SOSAccountJoinCircles_wTxn(account
, error
);
108 ProcessChangesForMasterAndMinions(changes
, master_account
, minion_accounts
);
111 require_quiet(retval
, errOut
);
112 CFArrayRef applicants
= SOSAccountCopyApplicants(master_account
, error
);
113 retval
= SOSAccountAcceptApplicants(master_account
, applicants
, error
);
114 ProcessChangesForMasterAndMinions(changes
, master_account
, minion_accounts
);
120 static CFArrayRef
CreateManyAccountsForLocalChanges(CFStringRef namefmt
, CFStringRef data_source_name
, size_t howmany
) {
121 CFMutableArrayRef accounts
= CFArrayCreateMutable(kCFAllocatorDefault
, howmany
, &kCFTypeArrayCallBacks
);
123 for(size_t i
= 0; i
< howmany
; i
++) {
124 CFStringRef tmpname
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, namefmt
, i
);
125 SOSAccountRef tmp
= CreateAccountForLocalChanges(tmpname
, CFSTR("TestSource"));
126 CFArraySetValueAtIndex(accounts
, i
, tmp
);
127 CFReleaseNull(tmpname
);
133 static bool AssertAllCredentialsAndUpdate(CFMutableDictionaryRef changes
, SOSAccountRef master_account
, CFArrayRef minion_accounts
, CFStringRef user_account
, CFDataRef user_password
, CFErrorRef
*error
) {
134 __block
bool retval
= SOSAccountAssertUserCredentialsAndUpdate(master_account
, user_account
, user_password
, error
);
135 ProcessChangesForMasterAndMinions(changes
, master_account
, minion_accounts
);
136 retval
&= SOSArrayForEachAccount(minion_accounts
, ^bool(SOSAccountRef account
) {
137 CFReleaseNull(*error
);
138 return SOSAccountAssertUserCredentialsAndUpdate(account
, user_account
, user_password
, error
);
140 CFReleaseNull(*error
);
145 static void tests(void)
147 CFErrorRef error
= NULL
;
148 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
149 CFStringRef cfaccount
= CFSTR("test@test.org");
151 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
153 SOSAccountRef master_account
= CreateAccountForLocalChanges(CFSTR("master"), CFSTR("TestSource"));
154 CFArrayRef minion_accounts
= CreateManyAccountsForLocalChanges(CFSTR("minion%d"), CFSTR("TestSource"), HOW_MANY_MINIONS
);
156 ok(AssertAllCredentialsAndUpdate(changes
, master_account
, minion_accounts
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
159 SOSAccountLogState(master_account
);
162 ok(MakeTheBigCircle(changes
, master_account
, minion_accounts
, &error
), "Get Everyone into the circle %@", error
);
166 SOSAccountLogState(master_account
);
167 SOSAccountLogViewState(master_account
);
170 CFDataRef acctData
= SOSAccountCopyEncodedData(master_account
, kCFAllocatorDefault
, &error
);
171 diag("Account DER Size is %d for %d peers", CFDataGetLength(acctData
), HOW_MANY_MINIONS
+1);
172 CFReleaseNull(acctData
);
173 CFReleaseNull(error
);
175 CFDataRef circleData
= SOSCircleCopyEncodedData(master_account
->trusted_circle
, kCFAllocatorDefault
, &error
);
176 diag("Circle DER Size is %d for %d peers", CFDataGetLength(circleData
), HOW_MANY_MINIONS
+1);
177 CFReleaseNull(circleData
);
178 CFReleaseNull(error
);
180 CFDataRef peerData
= SOSPeerInfoCopyEncodedData(SOSAccountGetMyPeerInfo(master_account
), kCFAllocatorDefault
, &error
);
181 diag("Peer DER Size is %d", CFDataGetLength(peerData
));
182 CFReleaseNull(peerData
);
183 CFReleaseNull(error
);
185 CFReleaseNull(error
);
186 CFReleaseNull(master_account
);
187 CFReleaseNull(minion_accounts
);
193 int secd_200_logstate(int argc
, char *const *argv
)
195 plan_tests(kTestTestCount
);
197 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);