]> git.saurik.com Git - apple/security.git/blob - Security/sec/SOSCircle/Regressions/sc-40-circle.c
Security-57031.1.35.tar.gz
[apple/security.git] / Security / sec / SOSCircle / Regressions / sc-40-circle.c
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 #include <Security/SecKey.h>
29
30 #include <SecureObjectSync/SOSCircle.h>
31 #include <SecureObjectSync/SOSCloudCircle.h>
32 #include <SecureObjectSync/SOSPeerInfo.h>
33 #include <SecureObjectSync/SOSInternal.h>
34 #include <SecureObjectSync/SOSUserKeygen.h>
35
36 #include <utilities/SecCFWrappers.h>
37
38 #include <CoreFoundation/CoreFoundation.h>
39
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 #include <securityd/SOSCloudCircleServer.h>
44
45 #include "SOSCircle_regressions.h"
46
47 #include "SOSRegressionUtilities.h"
48
49 static int kTestTestCount = 18;
50 static void tests(void)
51 {
52 SOSCircleRef circle = SOSCircleCreate(NULL, CFSTR("TEST DOMAIN"), NULL);
53
54 ok(NULL != circle, "Circle creation");
55
56 ok(0 == SOSCircleCountPeers(circle), "Zero peers");
57
58 //SecKeyRef publicKey = NULL;
59 SecKeyRef dev_a_key = NULL;
60 SecKeyRef dev_b_key = NULL;
61 CFErrorRef error = NULL;
62 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
63
64 ok(cfpassword, "no password");
65
66 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
67 ok(parameters, "No parameters!");
68 ok(error == NULL, "Error: (%@)", error);
69 CFReleaseNull(error);
70
71 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
72 CFReleaseNull(parameters);
73
74 SOSFullPeerInfoRef peer_a_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer A"), &dev_a_key, NULL);
75
76 SOSFullPeerInfoRef peer_b_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer B"), &dev_b_key, NULL);
77
78 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
79 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
80 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
81
82 ok(SOSCircleAcceptRequest(circle, user_privkey, peer_a_full_info, SOSFullPeerInfoGetPeerInfo(peer_a_full_info), NULL));
83
84 ok(!SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
85 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_b_full_info, NULL));
86
87 ok(SOSCircleCountPeers(circle) == 1, "Peer count");
88
89 size_t size = SOSCircleGetDEREncodedSize(circle, &error);
90 uint8_t buffer[size];
91 uint8_t* start = SOSCircleEncodeToDER(circle, &error, buffer, buffer + sizeof(buffer));
92
93 ok(start, "successful encoding");
94 ok(start == buffer, "Used whole buffer");
95
96 const uint8_t *der = buffer;
97 SOSCircleRef inflated = SOSCircleCreateFromDER(NULL, &error, &der, buffer + sizeof(buffer));
98
99 ok(inflated, "inflated");
100 ok(CFEqualSafe(inflated, circle), "Compares");
101
102
103 ok(SOSCircleRemovePeer(circle, user_privkey, peer_a_full_info, SOSFullPeerInfoGetPeerInfo(peer_a_full_info), NULL));
104 ok(SOSCircleCountPeers(circle) == 0, "Peer count");
105
106 CFReleaseNull(dev_a_key);
107 CFReleaseNull(cfpassword);
108 }
109
110 int sc_40_circle(int argc, char *const *argv)
111 {
112 plan_tests(kTestTestCount);
113
114 tests();
115
116 return 0;
117 }