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 <SecureObjectSync/SOSAccount.h>
30#include <SecureObjectSync/SOSCloudCircle.h>
31#include <SecureObjectSync/SOSInternal.h>
32#include <SecureObjectSync/SOSUserKeygen.h>
33#include <stdlib.h>
34#include <unistd.h>
35
36#include "secd_regressions.h"
37#include "SOSTestDataSource.h"
38
39#include "SOSRegressionUtilities.h"
40#include <utilities/SecCFWrappers.h>
41
42#include <securityd/SOSCloudCircleServer.h>
43#include "SecdTestKeychainUtilities.h"
44#include "SOSAccountTesting.h"
45
46static int kTestTestCount = 10 + kSecdTestSetupTestCount;
47static void tests(void)
48{
49    secd_test_setup_temp_keychain("secd_50_account", ^{
50    });
51
52    CFErrorRef error = NULL;
53    CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
54    CFStringRef cfaccount = CFSTR("test@test.org");
55
56    SOSDataSourceFactoryRef test_factory = SOSTestDataSourceFactoryCreate();
57    SOSDataSourceRef test_source = SOSTestDataSourceCreate();
58    SOSTestDataSourceFactoryAddDataSource(test_factory, CFSTR("TestType"), test_source);
59
60    SOSAccountRef account = CreateAccountForLocalChanges(CFSTR("Test Device"),CFSTR("TestType") );
61
62    ok(SOSAccountAssertUserCredentials(account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
63    CFReleaseNull(error);
64    CFReleaseNull(cfpassword);
65
66    ok(NULL != account, "Created");
67
68    ok(1 == SOSAccountCountCircles(account), "Has one circle");
69
70    size_t size = SOSAccountGetDEREncodedSize(account, &error);
71    CFReleaseNull(error);
72    uint8_t buffer[size];
73    uint8_t* start = SOSAccountEncodeToDER(account, &error, buffer, buffer + sizeof(buffer));
74    CFReleaseNull(error);
75
76    ok(start, "successful encoding");
77    ok(start == buffer, "Used whole buffer");
78
79    const uint8_t *der = buffer;
80    SOSAccountRef inflated = SOSAccountCreateFromDER(kCFAllocatorDefault, test_factory,
81                                                     &error, &der, buffer + sizeof(buffer));
82
83    SOSAccountEnsureFactoryCirclesTest(inflated, CFSTR("Test Device"));
84    ok(inflated, "inflated");
85    ok(CFEqual(inflated, account), "Compares");
86
87    CFDictionaryRef new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("New Device"));
88    ok(SOSAccountResetToOffering(account, &error), "Reset to Offering  (%@)", error);
89    CFReleaseNull(error);
90    is(SOSAccountIsInCircles(account, &error), kSOSCCInCircle, "Was in Circle  (%@)", error);
91    CFReleaseNull(error);
92
93    SOSAccountUpdateGestalt(account, new_gestalt);
94
95    is(SOSAccountIsInCircles(account, &error), kSOSCCInCircle, "Still in Circle  (%@)", error);
96    CFReleaseNull(error);
97
98    CFReleaseNull(new_gestalt);
99    CFReleaseNull(account);
100
101    SOSUnregisterAllTransportMessages();
102    SOSUnregisterAllTransportCircles();
103    SOSUnregisterAllTransportKeyParameters();
104
105    CFArrayRemoveAllValues(key_transports);
106    CFArrayRemoveAllValues(circle_transports);
107    CFArrayRemoveAllValues(message_transports);
108
109}
110
111int secd_50_account(int argc, char *const *argv)
112{
113    plan_tests(kTestTestCount);
114
115    tests();
116
117    return 0;
118}
119