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@
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
29 #include "keychain/SecureObjectSync/SOSAccount.h"
30 #include <Security/SecureObjectSync/SOSCloudCircle.h>
31 #include "keychain/SecureObjectSync/SOSInternal.h"
32 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
33 #include "keychain/SecureObjectSync/SOSAccountTrustClassic.h"
34 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
38 #include "secd_regressions.h"
39 #include "SOSTestDataSource.h"
41 #include "SOSRegressionUtilities.h"
42 #include <utilities/SecCFWrappers.h>
44 #include "keychain/securityd/SOSCloudCircleServer.h"
45 #include "SecdTestKeychainUtilities.h"
46 #include "SOSAccountTesting.h"
50 static int kTestTestCount = 9 + kSecdTestSetupTestCount;
51 static void tests(void)
54 CFErrorRef cfError = NULL;
55 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
56 CFStringRef cfaccount = CFSTR("test@test.org");
58 SOSDataSourceFactoryRef test_factory = SOSTestDataSourceFactoryCreate();
59 SOSDataSourceRef test_source = SOSTestDataSourceCreate();
60 SOSTestDataSourceFactorySetDataSource(test_factory, CFSTR("TestType"), test_source);
62 SOSAccount* account = CreateAccountForLocalChanges(CFSTR("Test Device"),CFSTR("TestType") );
64 ok(SOSAccountAssertUserCredentialsAndUpdate(account, cfaccount, cfpassword, &cfError), "Credential setting (%@)", cfError);
65 CFReleaseNull(cfError);
66 CFReleaseNull(cfpassword);
68 ok(NULL != account, "Created");
70 size_t size = [account.trust getDEREncodedSize:account err:&error];
75 uint8_t* start = [account.trust encodeToDER:account err:&error start:buffer end:buffer + sizeof(buffer)];
78 ok(start, "successful encoding");
79 ok(start == buffer, "Used whole buffer");
81 const uint8_t *der = buffer;
82 SOSAccount* inflated = [SOSAccount accountFromDER:&der end:buffer + sizeof(buffer)
83 factory:test_factory error:&error];
85 ok(inflated, "inflated %@", error);
86 ok([inflated isEqual:account], "Compares");
90 CFDictionaryRef new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("New Device"));
91 ok(SOSAccountResetToOffering_wTxn(account, &cfError), "Reset to Offering (%@)", error);
92 CFReleaseNull(cfError);
94 is([account getCircleStatus:&cfError], kSOSCCInCircle, "Was in Circle (%@)", error);
95 CFReleaseNull(cfError);
97 [account.trust updateGestalt:account newGestalt:new_gestalt];
98 is([account getCircleStatus:&cfError], kSOSCCInCircle, "Still in Circle (%@)", error);
99 CFReleaseNull(cfError);
101 SecKeyRef userKey = SOSAccountCopyStashedUserPrivateKey(account, &cfError);
102 ok(userKey, "retrieved userKey");
103 CFReleaseNull(userKey);
105 SecKeyRef deviceKey = SOSAccountCopyDevicePrivateKey(account, &cfError);
106 ok(deviceKey, "retrieved deviceKey");
107 CFReleaseNull(deviceKey);
110 CFReleaseNull(new_gestalt);
112 SOSDataSourceFactoryRelease(test_factory);
113 SOSDataSourceRelease(test_source, NULL);
123 int secd_50_account(int argc, char *const *argv)
126 plan_tests(kTestTestCount);
127 secd_test_setup_temp_keychain(__FUNCTION__, NULL);