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/SOSAccount.h>
31#include <SecureObjectSync/SOSCircle.h>
32#include <SecureObjectSync/SOSPeerInfo.h>
33#include <SecureObjectSync/SOSInternal.h>
34#include <SecureObjectSync/SOSKVSKeys.h>
35#include <utilities/SecCFWrappers.h>
36
37#include <CoreFoundation/CoreFoundation.h>
38
39#include <stdlib.h>
40#include <unistd.h>
41
42#include "SOSCircle_regressions.h"
43
44#include "SOSRegressionUtilities.h"
45
46
47static int kTestTestCount = 15;
48static void tests(void)
49{
50    SecKeyRef publicKey = NULL;
51
52    CFErrorRef error = NULL;
53    SOSCircleRef circle = SOSCircleCreate(NULL, CFSTR("Test Circle"), &error);
54
55    CFStringRef circle_key = SOSCircleKeyCreateWithCircle(circle, NULL);
56
57    CFStringRef circle_name = NULL;
58    ok(circle_key, "Circle key created");
59    is(SOSKVSKeyGetKeyType(circle_key), kCircleKey, "Is circle key");
60    is(SOSKVSKeyGetKeyTypeAndParse(circle_key, &circle_name, NULL, NULL), kCircleKey, "Is circle key, extract name");
61    ok(circle_name, "Circle name extracted");
62    ok(CFEqualSafe(circle_name, SOSCircleGetName(circle)), "Circle name matches '%@' '%@'", circle_name, SOSCircleGetName(circle));
63
64    CFReleaseSafe(circle_key);
65
66    SOSPeerInfoRef pi = SOSCreatePeerInfoFromName(CFSTR("Test Peer"), &publicKey, &error);
67
68    CFStringRef other_peer_id = CFSTR("OTHER PEER");
69
70    CFStringRef messageKey = SOSMessageKeyCreateWithCircleAndPeerNames(circle, SOSPeerInfoGetPeerID(pi), other_peer_id);
71
72    ok(messageKey, "Getting message key '%@'", messageKey);
73
74    CFStringRef message_circle_name = NULL;
75    CFStringRef message_from_peer_id = NULL;
76    CFStringRef message_to_peer_id = NULL;
77
78    is(SOSKVSKeyGetKeyType(messageKey), kMessageKey, "Is message key");
79    is(SOSKVSKeyGetKeyTypeAndParse(messageKey,
80                                   &message_circle_name,
81                                   &message_from_peer_id,
82                                   &message_to_peer_id), kMessageKey, "Is message key, extract parts");
83
84
85    ok(CFEqualSafe(SOSCircleGetName(circle), message_circle_name), "circle key matches in message (%@ v %@)",SOSCircleGetName(circle), message_circle_name);
86
87
88    ok(CFEqualSafe(SOSPeerInfoGetPeerID(pi), message_from_peer_id), "from peer set correctly (%@ v %@)", SOSPeerInfoGetPeerID(pi), message_from_peer_id);
89
90    ok(CFEqualSafe(other_peer_id, message_to_peer_id), "to peer set correctly (%@ v %@)", other_peer_id, message_to_peer_id);
91
92    CFStringRef retirementKey = SOSRetirementKeyCreateWithCircleAndPeer(circle, SOSPeerInfoGetPeerID(pi));
93    CFStringRef retirement_circle_name = NULL;
94    CFStringRef retirement_peer_id = NULL;
95
96    is(SOSKVSKeyGetKeyType(retirementKey), kRetirementKey, "Is retirement key");
97    is(SOSKVSKeyGetKeyTypeAndParse(retirementKey,
98                                   &retirement_circle_name,
99                                   &retirement_peer_id,
100                                   NULL), kRetirementKey, "Is retirement key, extract parts");
101    CFReleaseSafe(retirementKey);
102    ok(CFEqualSafe(SOSCircleGetName(circle), retirement_circle_name), "circle key matches in retirement (%@ v %@)",
103       SOSCircleGetName(circle), retirement_circle_name);
104    ok(CFEqualSafe(SOSPeerInfoGetPeerID(pi), retirement_peer_id), "retirement peer set correctly (%@ v %@)",
105       SOSPeerInfoGetPeerID(pi), retirement_peer_id);
106
107    CFReleaseNull(publicKey);
108    CFReleaseNull(circle);
109    CFReleaseNull(error);
110    CFReleaseNull(pi);
111    CFReleaseNull(messageKey);
112    CFReleaseNull(message_circle_name);
113    CFReleaseNull(message_from_peer_id);
114    CFReleaseNull(message_to_peer_id);
115}
116
117int sc_20_keynames(int argc, char *const *argv)
118{
119    plan_tests(kTestTestCount);
120
121    tests();
122
123	return 0;
124}
125