2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <SecureObjectSync/SOSPeer.h>
27 #include "SOSCircle_regressions.h"
29 #include <corecrypto/ccsha2.h>
31 #include <Security/SecBase64.h>
33 #include <utilities/SecCFWrappers.h>
37 static int kTestTestCount
= 13;
39 static void tests(void)
42 const unsigned kSOSPeerVersion
= 0;
44 CFErrorRef error
= NULL
;
47 /* Create peer test. */
48 CFStringRef peer_id
= CFSTR("peer 60");
50 __block
unsigned msg_count
= 0;
51 uint8_t msg_digest_buffer
[CCSHA256_OUTPUT_SIZE
];
52 uint8_t *msg_digest
= msg_digest_buffer
;
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) {
60 ccdigest(sha256
, msglen
, msg
, msg_digest
);
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
);
69 size_t encmaxlen
= SecBase64Encode(msg
, msglen
, NULL
, 0);
70 CFMutableDataRef encoded
= CFDataCreateMutable(NULL
, encmaxlen
);
71 CFDataSetLength(encoded
, encmaxlen
);
73 char *enc
= (char *)CFDataGetMutableBytePtr(encoded
);
77 SecBase64Encode2(msg
, msglen
,
79 encmaxlen
, kSecB64_F_LINE_LEN_USE_PARAM
,
81 assert(enclen
< INT32_MAX
);
83 // printf("=== BEGIN SOSMESSAGE ===\n%.*s\n=== END SOSMESSAGE ===\n", (int)enclen, enc);
90 ok(peer
= SOSPeerCreateSimple(peer_id
, kSOSPeerVersion
, &error
),
91 "create peer: %@", error
);
94 /* Send a test message. */
95 is((int)msg_count
, 0, "no message sent yet");
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
);
106 /* Check the peer's version. */
107 is(SOSPeerGetVersion(peer
), kSOSPeerVersion
, "version is correct");
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
);
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
);
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
);
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
);
134 SOSPeerRef reinflated_peer
= NULL
;
135 ok(reinflated_peer
= SOSPeerCreateSimple(peer_id
, kSOSPeerVersion
, &error
),
136 "create peer: %@", error
);
137 CFReleaseNull(error
);
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
);
146 CFReleaseSafe(reinflated_peer
);
150 int sc_60_peer(int argc
, char *const *argv
)
152 plan_tests(kTestTestCount
);