1 #include <CoreFoundation/CoreFoundation.h>
2 #include <CoreFoundation/CFRuntime.h>
4 #include "keychain/SecureObjectSync/SOSAccount.h"
5 #include "keychain/SecureObjectSync/SOSAccountPriv.h"
6 #include "keychain/SecureObjectSync/SOSTransport.h"
7 #import "keychain/SecureObjectSync/SOSTransportKeyParameter.h"
8 #import "keychain/SecureObjectSync/SOSTransportCircleKVS.h"
9 #import "keychain/SecureObjectSync/SOSTransportMessageKVS.h"
10 #include "keychain/SecureObjectSync/SOSKVSKeys.h"
11 #include "keychain/SecureObjectSync/SOSPeerCoder.h"
12 #include <utilities/SecCFWrappers.h>
13 #include "keychain/SecureObjectSync/SOSPeerInfoV2.h"
14 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
15 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Identity.h"
17 #include "SOSTransportTestTransports.h"
18 #include "SOSAccountTesting.h"
20 CFMutableArrayRef key_transports = NULL;
21 CFMutableArrayRef circle_transports = NULL;
22 CFMutableArrayRef message_transports = NULL;
25 //Mark Test Key Parameter Transport
28 @implementation CKKeyParameterTest
30 -(id) initWithAccount:(SOSAccount*) acct andName:(CFStringRef) n andCircleName:(CFStringRef) cN
32 if ((self = [super init])) {
33 self.name = CFRetainSafe(n);
34 self.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
36 self.circleName = CFRetainSafe(cN);
39 key_transports = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
40 CFArrayAppendValue(key_transports, (__bridge CFTypeRef)((CKKeyParameter*)self));
42 SOSRegisterTransportKeyParameter((CKKeyParameter*)self);
49 CFReleaseNull(self->_changes);
50 CFReleaseNull(self->_circleName);
54 - (void)setChanges:(CFMutableDictionaryRef)changes
56 CFRetainAssign(self->_changes, changes);
59 -(bool) SOSTransportKeyParameterHandleKeyParameterChanges:(CKKeyParameterTest*) transport data:(CFDataRef) data err:(CFErrorRef) error
61 SOSAccount* acct = transport.account;
62 return SOSAccountHandleParametersChange(acct, data, &error);
66 -(void) SOSTransportKeyParameterHandleNewAccount:(CKKeyParameterTest*) transport acct:(SOSAccount*) acct
70 CFArrayRemoveAllValue(key_transports, (__bridge CFTypeRef)(acct.key_transport));
72 if(message_transports){
73 CFArrayRemoveAllValue(message_transports, (__bridge CFTypeRef)acct.kvs_message_transport);
76 CFArrayRemoveAllValue(circle_transports, (__bridge CFTypeRef)(acct.circle_transport));
78 SOSAccountSetToNew(acct);
79 SOSAccountResetToTest(acct, transport.name);
82 CFStringRef SOSTransportKeyParameterTestGetName(CKKeyParameterTest* transport){
83 return transport.name;
86 void SOSTransportKeyParameterTestSetName(CKKeyParameterTest* transport, CFStringRef accountName){
87 transport.name = accountName;
90 SOSAccount* SOSTransportKeyParameterTestGetAccount(CKKeyParameterTest* transport){
91 return ((CKKeyParameter*)transport).account;
94 CFMutableDictionaryRef SOSTransportKeyParameterTestGetChanges(CKKeyParameterTest* transport){
95 return transport.changes;
98 void SOSTransportKeyParameterTestClearChanges(CKKeyParameterTest* transport){
99 transport.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
102 -(bool) SOSTransportKeyParameterPublishCloudParameters:(CKKeyParameterTest*) transport data:(CFDataRef)newParameters err:(CFErrorRef*) error
104 if(!transport.changes)
105 transport.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
107 CFDictionarySetValue(transport.changes, kSOSKVSKeyParametersKey, newParameters);
115 //MARK: Test Circle Transport
117 @implementation SOSCircleStorageTransportTest
118 @synthesize accountName = accountName;
125 CFStringRef SOSTransportCircleTestGetName(SOSCircleStorageTransportTest* transport){
126 return (__bridge CFStringRef)(transport.accountName);
128 void SOSTransportCircleTestSetName(SOSCircleStorageTransportTest* transport, CFStringRef name){
129 transport.accountName = nil;
130 transport.accountName = (__bridge NSString *)(name);
133 -(CFMutableDictionaryRef) SOSTransportCircleTestGetChanges
135 return (__bridge CFMutableDictionaryRef)(self.pending_changes);
138 void SOSTransportCircleTestClearChanges(SOSCircleStorageTransportTest* transport){
139 transport.pending_changes = [NSMutableDictionary dictionary];
142 -(id) initWithAccount:(SOSAccount *)acct andWithAccountName:(CFStringRef)acctName andCircleName:(CFStringRef)cName
144 if ((self = [super init])) {
146 self.accountName = (__bridge NSString *)(acctName);
147 self.circleName = (__bridge NSString*)cName;
148 self.pending_changes = [NSMutableDictionary dictionary];
149 if(!circle_transports)
150 circle_transports = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
151 CFArrayAppendValue(circle_transports, (__bridge CFTypeRef)self);
153 SOSRegisterTransportCircle((SOSCircleStorageTransportTest*)self);
158 -(bool) kvsRingFlushChanges:(CFErrorRef*) error
163 -(bool) kvsRingPostRing:(CFStringRef) ringName ring:(CFDataRef) ring err:(CFErrorRef *)error
165 CFStringRef ringKey = SOSRingKeyCreateWithName(ringName, error);
166 CFMutableDictionaryRef changes = [self SOSTransportCircleTestGetChanges];
167 CFDictionaryAddValue(changes, ringKey, ring);
168 CFReleaseNull(ringKey);
172 -(bool) kvssendDebugInfo:(CFStringRef) type debug:(CFTypeRef) debugInfo err:(CFErrorRef *)error
174 CFMutableDictionaryRef changes = [self SOSTransportCircleTestGetChanges];
175 CFDictionaryAddValue(changes, type, debugInfo);
179 -(bool) postRetirement:(CFStringRef)cName peer:(SOSPeerInfoRef)peer err:(CFErrorRef *)error
181 CFStringRef retirement_key = SOSRetirementKeyCreateWithCircleNameAndPeer(cName, SOSPeerInfoGetPeerID(peer));
182 CFDataRef retirement_data = SOSPeerInfoCopyEncodedData(peer, kCFAllocatorDefault, error);
185 [self testAddToChanges:retirement_key data:retirement_data];
187 CFReleaseNull(retirement_key);
188 CFReleaseNull(retirement_data);
192 -(bool) flushChanges:(CFErrorRef *)error
197 -(void) testAddToChanges:(CFStringRef) message_key data:(CFDataRef) message_data
199 if (self.pending_changes == NULL) {
200 self.pending_changes = [NSMutableDictionary dictionary];
202 if (message_data == NULL) {
203 [self.pending_changes setObject:[NSNull null] forKey:(__bridge NSString*)(message_key)];
205 [self.pending_changes setObject:(__bridge NSData*)message_data forKey:(__bridge NSString*)message_key];
207 secnotice("circle-changes", "Adding circle change %@ %@->%@", self.accountName, message_key, message_data);
210 -(void) SOSTransportCircleTestAddBulkToChanges:(CFDictionaryRef) updates
212 if (self.pending_changes == NULL) {
213 self.pending_changes = [[NSMutableDictionary alloc]initWithDictionary:(__bridge NSDictionary * _Nonnull)(updates)];
217 CFDictionaryForEach(updates, ^(const void *key, const void *value) {
218 [self.pending_changes setObject:(__bridge id _Nonnull)value forKey:(__bridge id _Nonnull)key];
224 -(bool) expireRetirementRecords:(CFDictionaryRef) retirements err:(CFErrorRef *)error
227 CFMutableDictionaryRef keysToWrite = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
229 CFDictionaryForEach(retirements, ^(const void *key, const void *value) {
230 if (isString(key) && isArray(value)) {
231 CFStringRef circle_name = (CFStringRef) key;
232 CFArrayRef retirees = (CFArrayRef) value;
234 CFArrayForEach(retirees, ^(const void *value) {
235 if (isString(value)) {
236 CFStringRef retiree_id = (CFStringRef) value;
238 CFStringRef kvsKey = SOSRetirementKeyCreateWithCircleNameAndPeer(circle_name, retiree_id);
240 CFDictionaryAddValue(keysToWrite, kvsKey, kCFNull);
242 CFReleaseSafe(kvsKey);
248 if(CFDictionaryGetCount(keysToWrite)) {
249 [self SOSTransportCircleTestAddBulkToChanges:keysToWrite];
251 CFReleaseNull(keysToWrite);
256 bool SOSTransportCircleTestRemovePendingChange(SOSCircleStorageTransportTest* transport, CFStringRef circleName, CFErrorRef *error){
257 CFStringRef circle_key = SOSCircleKeyCreateWithName(circleName, error);
259 [transport.pending_changes removeObjectForKey:(__bridge NSString*)circle_key];
260 CFReleaseNull(circle_key);
264 -(bool) postCircle:(CFStringRef)cName circleData:(CFDataRef)circle_data err:(CFErrorRef *)error
266 CFStringRef circle_key = SOSCircleKeyCreateWithName(cName, error);
268 [self testAddToChanges:circle_key data:circle_data];
269 CFReleaseNull(circle_key);
274 -(CFDictionaryRef)handleRetirementMessages:(CFMutableDictionaryRef) circle_retirement_messages_table err:(CFErrorRef *)error
276 return SOSAccountHandleRetirementMessages(self.account, circle_retirement_messages_table, error);
279 -(CFArrayRef)CF_RETURNS_RETAINED handleCircleMessagesAndReturnHandledCopy:(CFMutableDictionaryRef) circle_circle_messages_table err:(CFErrorRef *)error
281 CFMutableArrayRef handledKeys = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
282 CFDictionaryForEach(circle_circle_messages_table, ^(const void *key, const void *value) {
283 CFErrorRef circleMessageError = NULL;
284 if (!SOSAccountHandleCircleMessage(self.account, key, value, &circleMessageError)) {
285 secerror("Error handling circle message %@ (%@): %@", key, value, circleMessageError);
288 CFStringRef circle_id = (CFStringRef) key;
289 CFArrayAppendValue(handledKeys, circle_id);
291 CFReleaseNull(circleMessageError);
297 SOSAccount* SOSTransportCircleTestGetAccount(SOSCircleStorageTransportTest* transport) {
298 return transport.account;
304 //MARK KVS Message Test Transport
309 @implementation SOSMessageKVSTest
312 -(id) initWithAccount:(SOSAccount*)acct andName:(CFStringRef)n andCircleName:(CFStringRef) cN
314 if ((self = [super init])) {
315 self.engine = SOSDataSourceFactoryGetEngineForDataSourceName(acct.factory, cN, NULL);
317 self.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
318 self.name = CFRetainSafe(n);
319 self.circleName = (__bridge NSString*)cN;
321 if(!message_transports)
322 message_transports = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
323 CFArrayAppendValue(message_transports, (__bridge const void *)((SOSMessageKVSTest*)self));
324 SOSRegisterTransportMessage((SOSMessageKVSTest*)self);
330 - (void)setChanges:(CFMutableDictionaryRef)changes
332 CFRetainAssign(self->_changes, changes);
335 -(CFIndex) SOSTransportMessageGetTransportType
339 -(CFStringRef) SOSTransportMessageGetCircleName
341 return (__bridge CFStringRef)circleName;
343 -(CFTypeRef) SOSTransportMessageGetEngine
347 -(SOSAccount*) SOSTransportMessageGetAccount
352 void SOSTransportMessageKVSTestSetName(SOSMessageKVSTest* transport, CFStringRef n)
357 CFStringRef SOSTransportMessageKVSTestGetName(SOSMessageKVSTest* transport)
359 return transport.name;
362 CFMutableDictionaryRef SOSTransportMessageKVSTestGetChanges(SOSMessageKVSTest* transport)
364 return transport.changes;
367 void SOSTransportMessageTestClearChanges(SOSMessageKVSTest* transport)
369 transport.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
372 static void SOSTransportMessageTestAddBulkToChanges(SOSMessageKVSTest* transport, CFDictionaryRef updates)
374 #ifndef __clang_analyzer__ // The analyzer thinks transport.changes is a leak, but I don't know why.
375 if (transport.changes == NULL) {
376 transport.changes = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, CFDictionaryGetCount(updates), updates);
379 CFDictionaryForEach(updates, ^(const void *key, const void *value) {
380 CFDictionarySetValue(transport.changes, key, value);
383 #endif // __clang_analyzer__
386 static void SOSTransportMessageTestAddToChanges(SOSMessageKVSTest* transport, CFStringRef message_key, CFDataRef message_data)
388 if (transport.changes == NULL) {
389 transport.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
391 if (message_data == NULL) {
392 CFDictionarySetValue(transport.changes, message_key, kCFNull);
394 CFDictionarySetValue(transport.changes, message_key, message_data);
398 -(bool) SOSTransportMessageCleanupAfterPeerMessages:(SOSMessageKVSTest*) transport peers:(CFDictionaryRef)circle_to_peer_ids err:(CFErrorRef*) error
400 if(!transport.engine)
402 CFArrayRef enginePeers = SOSEngineGetPeerIDs((SOSEngineRef)transport.engine);
404 CFDictionaryForEach(circle_to_peer_ids, ^(const void *key, const void *value) {
405 if (isString(key) && isArray(value)) {
406 CFStringRef circle_name = (CFStringRef) key;
407 CFArrayRef peers_to_cleanup_after = (CFArrayRef) value;
409 CFArrayForEach(peers_to_cleanup_after, ^(const void *value) {
410 if (isString(value)) {
411 CFStringRef cleanup_id = (CFStringRef) value;
412 // TODO: Since the enginePeers list is not authorative (the Account is) this could inadvertently clean up active peers or leave behind stale peers
413 if (enginePeers) CFArrayForEach(enginePeers, ^(const void *value) {
414 if (isString(value)) {
415 CFStringRef in_circle_id = (CFStringRef) value;
417 CFStringRef kvsKey = SOSMessageKeyCreateWithCircleNameAndPeerNames(circle_name, cleanup_id, in_circle_id);
418 SOSTransportMessageTestAddToChanges(transport, kvsKey, NULL);
419 CFReleaseSafe(kvsKey);
421 kvsKey = SOSMessageKeyCreateWithCircleNameAndPeerNames(circle_name, in_circle_id, cleanup_id);
422 SOSTransportMessageTestAddToChanges(transport, kvsKey, NULL);
423 CFReleaseSafe(kvsKey);
432 return [transport SOSTransportMessageFlushChanges:transport err:error];
436 static bool sendToPeer(SOSMessageKVSTest* transport, CFStringRef circleName, CFStringRef peerID, CFDataRef message, CFErrorRef *error) {
438 NSString* myID = transport.account.peerID;
439 CFStringRef message_to_peer_key = SOSMessageKeyCreateFromTransportToPeer((SOSMessage*)transport, (__bridge CFStringRef) myID, peerID);
440 CFDictionaryRef a_message_to_a_peer = CFDictionaryCreateForCFTypes(NULL, message_to_peer_key, message, NULL);
442 SOSTransportMessageTestAddBulkToChanges(transport, a_message_to_a_peer);
443 CFReleaseNull(a_message_to_a_peer);
444 CFReleaseNull(message_to_peer_key);
449 -(bool) SOSTransportMessageSyncWithPeers:(SOSMessageKVSTest*) transport p:(CFSetRef) peers err:(CFErrorRef *)error
451 // Each entry is keyed by circle name and contains a list of peerIDs
453 __block bool result = true;
455 CFSetForEach(peers, ^(const void *value) {
456 CFStringRef peerID = asString(value, NULL);
459 SOSEngineWithPeerID((SOSEngineRef)transport.engine, peerID, error, ^(SOSPeerRef peer, SOSCoderRef coder, SOSDataSourceRef dataSource, SOSTransactionRef txn, bool *forceSaveState) {
460 SOSEnginePeerMessageSentCallback* sentCallback = NULL;
461 CFDataRef message_to_send = NULL;
462 bool ok = SOSPeerCoderSendMessageIfNeeded([transport SOSTransportMessageGetAccount], (SOSEngineRef)transport.engine, txn, peer, coder, &message_to_send, peerID, false, &sentCallback, error);
463 if (message_to_send) {
464 CFDictionaryRef peer_dict = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, peerID, message_to_send, NULL);
465 CFDictionarySetValue(SOSTransportMessageKVSTestGetChanges(transport), (__bridge CFStringRef)self->circleName, peer_dict);
466 SOSEngineMessageCallCallback(sentCallback, ok);
467 CFReleaseSafe(peer_dict);
470 SOSEngineFreeMessageCallback(sentCallback);
471 CFReleaseSafe(message_to_send);
478 -(bool) SOSTransportMessageSendMessages:(SOSMessageKVSTest*) transport pm:(CFDictionaryRef) peer_messages err:(CFErrorRef *)error
480 __block bool result = true;
482 CFDictionaryForEach(peer_messages, ^(const void *key, const void *value) {
483 if (isString(key) && isData(value)) {
484 CFStringRef peerID = (CFStringRef) key;
485 CFDataRef message = (CFDataRef) value;
486 bool rx = sendToPeer(transport, (__bridge CFStringRef)transport->circleName, peerID, message, error);
494 -(bool) SOSTransportMessageFlushChanges:(SOSMessageKVSTest*) transport err:(CFErrorRef *)error
499 SOSAccount* SOSTransportMessageKVSTestGetAccount(SOSMessageKVSTest* transport) {
500 return transport.account;
505 void SOSAccountUpdateTestTransports(SOSAccount* account, CFDictionaryRef gestalt){
506 CFStringRef new_name = (CFStringRef)CFDictionaryGetValue(gestalt, kPIUserDefinedDeviceNameKey);
508 SOSTransportKeyParameterTestSetName((CKKeyParameterTest*)account.key_transport, new_name);
509 SOSTransportCircleTestSetName((SOSCircleStorageTransportTest*)account.circle_transport, new_name);
510 SOSTransportMessageKVSTestSetName((SOSMessageKVSTest*)account.kvs_message_transport, new_name);
513 static CF_RETURNS_RETAINED SOSCircleRef SOSAccountEnsureCircleTest(SOSAccount* account, CFStringRef name, CFStringRef accountName)
515 CFErrorRef localError = NULL;
516 SOSAccountTrustClassic *trust = account.trust;
518 SOSCircleRef circle = CFRetainSafe([account.trust getCircle:&localError]);
519 if(!circle || isSOSErrorCoded(localError, kSOSErrorIncompatibleCircle)){
520 secnotice("circle", "Error retrieving the circle: %@", localError);
521 CFReleaseNull(localError);
522 CFReleaseNull(circle);
524 circle = SOSCircleCreate(kCFAllocatorDefault, name, &localError);
526 [trust setTrustedCircle:circle];
529 secnotice("circle", "Could not create circle: %@", localError);
531 CFReleaseNull(localError);
534 if(![trust ensureFullPeerAvailable:account err:&localError])
536 secnotice("circle", "had an error building full peer: %@", localError);
537 CFReleaseNull(localError);
541 CFReleaseNull(localError);
545 bool SOSAccountEnsureFactoryCirclesTest(SOSAccount* a, CFStringRef accountName)
552 CFStringRef circle_name = SOSDataSourceFactoryCopyName(a.factory);
555 CFReleaseSafe(SOSAccountEnsureCircleTest(a, (CFStringRef)circle_name, accountName));
557 CFReleaseNull(circle_name);
563 bool SOSAccountInflateTestTransportsForCircle(SOSAccount* account, CFStringRef circleName, CFStringRef accountName, CFErrorRef *error){
564 bool success = false;
566 if(account.key_transport == nil){
567 account.key_transport = (CKKeyParameter*)[[CKKeyParameterTest alloc] initWithAccount:account andName:accountName andCircleName:circleName];
568 require_quiet(account.key_transport, fail);
571 if(account.circle_transport == nil){
572 account.circle_transport = (SOSKVSCircleStorageTransport*)[[SOSCircleStorageTransportTest alloc] initWithAccount:account andWithAccountName:accountName andCircleName:circleName];
573 require_quiet(account.circle_transport, fail);
575 if(account.kvs_message_transport == nil){
576 account.kvs_message_transport = (SOSMessageKVS*)[[SOSMessageKVSTest alloc] initWithAccount:account andName:accountName andCircleName:circleName];