]> git.saurik.com Git - apple/security.git/blob - Security/sec/SOSCircle/Regressions/sc-60-peer.c
Security-57031.10.10.tar.gz
[apple/security.git] / Security / sec / SOSCircle / Regressions / sc-60-peer.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 #include <SecureObjectSync/SOSPeer.h>
26
27 #include "SOSCircle_regressions.h"
28
29 #include <corecrypto/ccsha2.h>
30
31 #include <Security/SecBase64.h>
32
33 #include <utilities/SecCFWrappers.h>
34
35 #include <stdint.h>
36
37 static int kTestTestCount = 13;
38
39 static void tests(void)
40 {
41 #if 0
42 const unsigned kSOSPeerVersion = 0;
43
44 CFErrorRef error = NULL;
45 SOSPeerRef peer;
46
47 /* Create peer test. */
48 CFStringRef peer_id = CFSTR("peer 60");
49
50 __block unsigned msg_count = 0;
51 uint8_t msg_digest_buffer[CCSHA256_OUTPUT_SIZE];
52 uint8_t *msg_digest = msg_digest_buffer;
53
54 SOSPeerSendBlock sendBlock = ^bool (CFDataRef message, CFErrorRef *error) {
55 size_t msglen = CFDataGetLength(message);
56 const uint8_t *msg = CFDataGetBytePtr(message);
57 const struct ccdigest_info *sha256 = ccsha256_di();
58 if (msg_count++ == 0) {
59 /* message n=0 */
60 ccdigest(sha256, msglen, msg, msg_digest);
61 } else {
62 /* message n=n+1 */
63 ccdigest_di_decl(sha256, sha256_ctx);
64 ccdigest_init(sha256, sha256_ctx);
65 ccdigest_update(sha256, sha256_ctx, sizeof(msg_digest_buffer), msg_digest);
66 ccdigest_update(sha256, sha256_ctx, msglen, msg);
67 ccdigest_final(sha256, sha256_ctx, msg_digest);
68 }
69 size_t encmaxlen = SecBase64Encode(msg, msglen, NULL, 0);
70 CFMutableDataRef encoded = CFDataCreateMutable(NULL, encmaxlen);
71 CFDataSetLength(encoded, encmaxlen);
72 SecBase64Result rc;
73 char *enc = (char *)CFDataGetMutableBytePtr(encoded);
74 #ifndef NDEBUG
75 size_t enclen =
76 #endif
77 SecBase64Encode2(msg, msglen,
78 enc,
79 encmaxlen, kSecB64_F_LINE_LEN_USE_PARAM,
80 64, &rc);
81 assert(enclen < INT32_MAX);
82
83 // printf("=== BEGIN SOSMESSAGE ===\n%.*s\n=== END SOSMESSAGE ===\n", (int)enclen, enc);
84
85 CFRelease(encoded);
86 return true;
87 };
88
89
90 ok(peer = SOSPeerCreateSimple(peer_id, kSOSPeerVersion, &error),
91 "create peer: %@", error);
92 CFReleaseNull(error);
93
94 /* Send a test message. */
95 is((int)msg_count, 0, "no message sent yet");
96 size_t msglen = 10;
97 uint8_t msg[msglen];
98 memcpy(msg, "0123456789", msglen);
99 CFDataRef message = CFDataCreate(NULL, msg, msglen);
100 ok(SOSPeerSendMessage(peer, message, &error, sendBlock),
101 "send message to peer: %@", error);
102 CFReleaseNull(error);
103 is((int)msg_count, 1, "We sent %d/1 messages", msg_count);
104 CFRelease(message);
105
106 /* Check the peer's version. */
107 is(SOSPeerGetVersion(peer), kSOSPeerVersion, "version is correct");
108
109 //SOSPeerManifestType mfType = kSOSPeerProposedManifest;
110 SOSPeerManifestType mfType = kSOSPeerConfirmedManifest;
111 /* Get the peer's manifest. */
112 SOSManifestRef manifest = SOSPeerGetManifest(peer, mfType, &error);
113 ok(manifest == NULL, "No manifest yet for this peer: %@", error);
114 CFReleaseNull(error);
115
116 /* Get the peer's manifest digest. */
117 CFDataRef digest = SOSManifestGetDigest(manifest, &error);
118 ok(digest == NULL, "No digest yet for this peer's manifest: %@", error);
119 CFReleaseNull(error);
120
121 ok(manifest = SOSManifestCreateWithBytes(NULL, 0, &error), "Create empty manifest: %@", error);
122 CFReleaseNull(error);
123 ok(SOSPeerSetManifest(peer, mfType, manifest, &error), "Set empty manifest on peer: %@", error);
124 CFReleaseNull(error);
125
126 /* Get the peer's empty manifest digest. */
127 digest = SOSManifestGetDigest(manifest, &error);
128 ok(digest, "Got a digest: %@ this peer's manifest: %@", digest, error);
129 CFReleaseNull(error);
130
131 /* Clean up. */
132 CFReleaseSafe(peer);
133
134 SOSPeerRef reinflated_peer = NULL;
135 ok(reinflated_peer = SOSPeerCreateSimple(peer_id, kSOSPeerVersion, &error),
136 "create peer: %@", error);
137 CFReleaseNull(error);
138
139 manifest = SOSPeerGetManifest(reinflated_peer, mfType, &error);
140 ok(manifest != NULL, "Got NULL manifest after reinflate (%@)", error);
141 CFDataRef digestAfterReinflate = SOSManifestGetDigest(manifest, &error);
142 ok(digestAfterReinflate != NULL, "Got NULL digest after reinflate (%@)", error);
143 ok(digestAfterReinflate && CFEqual(digest, digestAfterReinflate), "Compare digest after reinflate, before: %@ after: %@", digest, digestAfterReinflate);
144 CFReleaseNull(error);
145
146 CFReleaseSafe(reinflated_peer);
147 #endif
148 }
149
150 int sc_60_peer(int argc, char *const *argv)
151 {
152 plan_tests(kTestTestCount);
153
154 tests();
155
156 return 0;
157 }