1/*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26
27#include <Security/SecBase.h>
28#include <Security/SecItem.h>
29
30#include <CoreFoundation/CFDictionary.h>
31
32#include <SecureObjectSync/SOSAccount.h>
33#include <SecureObjectSync/SOSCloudCircle.h>
34#include <SecureObjectSync/SOSInternal.h>
35#include <SecureObjectSync/SOSUserKeygen.h>
36#include <SecureObjectSync/SOSTransport.h>
37
38#include <stdlib.h>
39#include <unistd.h>
40
41#include "secd_regressions.h"
42#include "SOSTestDataSource.h"
43
44#include "SOSRegressionUtilities.h"
45#include <utilities/SecCFWrappers.h>
46#include <Security/SecKeyPriv.h>
47
48#include <securityd/SOSCloudCircleServer.h>
49
50#include "SOSAccountTesting.h"
51
52static int kTestTestCount = 145;
53
54
55
56static void tests(void)
57{
58    CFErrorRef error = NULL;
59    CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
60    CFStringRef cfaccount = CFSTR("test@test.org");
61
62    CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
63
64    SOSAccountRef alice_account = CreateAccountForLocalChanges( CFSTR("Alice"), CFSTR("TestSource"));
65    SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
66    SOSAccountRef carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
67
68    ok(SOSAccountAssertUserCredentials(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
69
70    // Bob wins writing at this point, feed the changes back to alice.
71    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
72
73    ok(SOSAccountAssertUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
74    CFReleaseNull(error);
75
76    ok(SOSAccountAssertUserCredentials(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
77    CFReleaseNull(error);
78    CFReleaseNull(cfpassword);
79    ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
80    CFReleaseNull(error);
81
82    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
83
84    ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
85    CFReleaseNull(error);
86    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
87
88    {
89        CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
90
91        ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
92        ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
93        CFReleaseNull(error);
94        CFReleaseNull(applicants);
95    }
96
97    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
98
99    accounts_agree("bob&alice pair", bob_account, alice_account);
100
101    /*----- normal join after restore -----*/
102
103    ok(SOSAccountJoinCirclesAfterRestore(carole_account, &error), "Carole cloud identity joins (%@)", error);
104    CFReleaseNull(error);
105    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
106
107    is(countApplicants(alice_account), 0, "See no applicants");
108
109    is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
110
111    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
112
113    accounts_agree_internal("Carole's in", bob_account, alice_account, false);
114    accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
115
116    ok(SOSAccountLeaveCircles(carole_account, &error), "Carol Leaves again");
117    CFReleaseNull(error);
118
119    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
120
121    /*----- join - join after restore -----*/
122
123    ok(SOSAccountJoinCircles(carole_account, &error), "Carole normally joins (%@)", error);
124    CFReleaseNull(error);
125
126    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
127
128    is(countApplicants(alice_account), 1, "See one applicant");
129
130    ok(SOSAccountJoinCirclesAfterRestore(carole_account, &error), "Carole cloud identity joins (%@)", error);
131    CFReleaseNull(error);
132
133    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
134
135
136    is(countApplicants(alice_account), 0, "See no applicants");
137
138    is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
139
140    is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
141
142    accounts_agree_internal("Carole's in", bob_account, alice_account, false);
143    accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
144
145    CFReleaseNull(bob_account);
146    CFReleaseNull(alice_account);
147    CFReleaseNull(carole_account);
148
149    SOSUnregisterAllTransportMessages();
150    SOSUnregisterAllTransportCircles();
151    SOSUnregisterAllTransportKeyParameters();
152    CFArrayRemoveAllValues(key_transports);
153    CFArrayRemoveAllValues(circle_transports);
154    CFArrayRemoveAllValues(message_transports);
155
156}
157
158int secd_60_account_cloud_identity(int argc, char *const *argv)
159{
160    plan_tests(kTestTestCount);
161
162    tests();
163
164    return 0;
165}
166