]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-50-account.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-50-account.m
1 /*
2 * Copyright (c) 2012-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 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28
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"
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 #include "secd_regressions.h"
39 #include "SOSTestDataSource.h"
40
41 #include "SOSRegressionUtilities.h"
42 #include <utilities/SecCFWrappers.h>
43
44 #include "keychain/securityd/SOSCloudCircleServer.h"
45 #include "SecdTestKeychainUtilities.h"
46 #include "SOSAccountTesting.h"
47
48 #if SOS_ENABLED
49
50 static int kTestTestCount = 9 + kSecdTestSetupTestCount;
51 static void tests(void)
52 {
53 NSError* error = nil;
54 CFErrorRef cfError = NULL;
55 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
56 CFStringRef cfaccount = CFSTR("test@test.org");
57
58 SOSDataSourceFactoryRef test_factory = SOSTestDataSourceFactoryCreate();
59 SOSDataSourceRef test_source = SOSTestDataSourceCreate();
60 SOSTestDataSourceFactorySetDataSource(test_factory, CFSTR("TestType"), test_source);
61
62 SOSAccount* account = CreateAccountForLocalChanges(CFSTR("Test Device"),CFSTR("TestType") );
63
64 ok(SOSAccountAssertUserCredentialsAndUpdate(account, cfaccount, cfpassword, &cfError), "Credential setting (%@)", cfError);
65 CFReleaseNull(cfError);
66 CFReleaseNull(cfpassword);
67
68 ok(NULL != account, "Created");
69
70 size_t size = [account.trust getDEREncodedSize:account err:&error];
71
72 error = nil;
73 uint8_t buffer[size];
74
75 uint8_t* start = [account.trust encodeToDER:account err:&error start:buffer end:buffer + sizeof(buffer)];
76 error = nil;
77
78 ok(start, "successful encoding");
79 ok(start == buffer, "Used whole buffer");
80
81 const uint8_t *der = buffer;
82 SOSAccount* inflated = [SOSAccount accountFromDER:&der end:buffer + sizeof(buffer)
83 factory:test_factory error:&error];
84
85 ok(inflated, "inflated %@", error);
86 ok([inflated isEqual:account], "Compares");
87
88 error = nil;
89
90 CFDictionaryRef new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("New Device"));
91 ok(SOSAccountResetToOffering_wTxn(account, &cfError), "Reset to Offering (%@)", error);
92 CFReleaseNull(cfError);
93
94 is([account getCircleStatus:&cfError], kSOSCCInCircle, "Was in Circle (%@)", error);
95 CFReleaseNull(cfError);
96
97 [account.trust updateGestalt:account newGestalt:new_gestalt];
98 is([account getCircleStatus:&cfError], kSOSCCInCircle, "Still in Circle (%@)", error);
99 CFReleaseNull(cfError);
100
101 SecKeyRef userKey = SOSAccountCopyStashedUserPrivateKey(account, &cfError);
102 ok(userKey, "retrieved userKey");
103 CFReleaseNull(userKey);
104
105 SecKeyRef deviceKey = SOSAccountCopyDevicePrivateKey(account, &cfError);
106 ok(deviceKey, "retrieved deviceKey");
107 CFReleaseNull(deviceKey);
108
109
110 CFReleaseNull(new_gestalt);
111
112 SOSDataSourceFactoryRelease(test_factory);
113 SOSDataSourceRelease(test_source, NULL);
114
115 account = nil;
116 inflated = nil;
117
118 SOSTestCleanup();
119 }
120
121 #endif
122
123 int secd_50_account(int argc, char *const *argv)
124 {
125 #if SOS_ENABLED
126 plan_tests(kTestTestCount);
127 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
128 tests();
129 #else
130 plan_tests(0);
131 #endif
132 return 0;
133 }