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
34 self.name = CFRetainSafe(n);
35 self.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
37 self.circleName = CFRetainSafe(cN);
40 key_transports = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
41 CFArrayAppendValue(key_transports, (__bridge CFTypeRef)((CKKeyParameter*)self));
43 SOSRegisterTransportKeyParameter((CKKeyParameter*)self);
50 CFReleaseNull(self->_changes);
51 CFReleaseNull(self->_circleName);
55 - (void)setChanges:(CFMutableDictionaryRef)changes
57 CFRetainAssign(self->_changes, changes);
60 -(bool) SOSTransportKeyParameterHandleKeyParameterChanges:(CKKeyParameterTest*) transport data:(CFDataRef) data err:(CFErrorRef) error
62 SOSAccount* acct = transport.account;
63 return SOSAccountHandleParametersChange(acct, data, &error);
67 -(void) SOSTransportKeyParameterHandleNewAccount:(CKKeyParameterTest*) transport acct:(SOSAccount*) acct
71 CFArrayRemoveAllValue(key_transports, (__bridge CFTypeRef)(acct.key_transport));
73 if(message_transports){
74 CFArrayRemoveAllValue(message_transports, (__bridge CFTypeRef)acct.kvs_message_transport);
77 CFArrayRemoveAllValue(circle_transports, (__bridge CFTypeRef)(acct.circle_transport));
79 SOSAccountSetToNew(acct);
80 SOSAccountResetToTest(acct, transport.name);
83 CFStringRef SOSTransportKeyParameterTestGetName(CKKeyParameterTest* transport){
84 return transport.name;
87 void SOSTransportKeyParameterTestSetName(CKKeyParameterTest* transport, CFStringRef accountName){
88 transport.name = accountName;
91 SOSAccount* SOSTransportKeyParameterTestGetAccount(CKKeyParameterTest* transport){
92 return ((CKKeyParameter*)transport).account;
95 CFMutableDictionaryRef SOSTransportKeyParameterTestGetChanges(CKKeyParameterTest* transport){
96 return transport.changes;
99 void SOSTransportKeyParameterTestClearChanges(CKKeyParameterTest* transport){
100 transport.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
103 -(bool) SOSTransportKeyParameterPublishCloudParameters:(CKKeyParameterTest*) transport data:(CFDataRef)newParameters err:(CFErrorRef*) error
105 if(!transport.changes)
106 transport.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
108 CFDictionarySetValue(transport.changes, kSOSKVSKeyParametersKey, newParameters);
116 //MARK: Test Circle Transport
118 @implementation SOSCircleStorageTransportTest
119 @synthesize accountName = accountName;
126 CFStringRef SOSTransportCircleTestGetName(SOSCircleStorageTransportTest* transport){
127 return (__bridge CFStringRef)(transport.accountName);
129 void SOSTransportCircleTestSetName(SOSCircleStorageTransportTest* transport, CFStringRef name){
130 transport.accountName = nil;
131 transport.accountName = (__bridge NSString *)(name);
134 -(CFMutableDictionaryRef) SOSTransportCircleTestGetChanges
136 return (__bridge CFMutableDictionaryRef)(self.pending_changes);
139 void SOSTransportCircleTestClearChanges(SOSCircleStorageTransportTest* transport){
140 transport.pending_changes = [NSMutableDictionary dictionary];
143 -(id) initWithAccount:(SOSAccount *)acct andWithAccountName:(CFStringRef)acctName andCircleName:(CFStringRef)cName
148 self.accountName = (__bridge NSString *)(acctName);
149 self.circleName = (__bridge NSString*)cName;
150 self.pending_changes = [NSMutableDictionary dictionary];
151 if(!circle_transports)
152 circle_transports = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
153 CFArrayAppendValue(circle_transports, (__bridge CFTypeRef)self);
155 SOSRegisterTransportCircle((SOSCircleStorageTransportTest*)self);
160 -(bool) kvsRingFlushChanges:(CFErrorRef*) error
165 -(bool) kvsRingPostRing:(CFStringRef) ringName ring:(CFDataRef) ring err:(CFErrorRef *)error
167 CFStringRef ringKey = SOSRingKeyCreateWithName(ringName, error);
168 CFMutableDictionaryRef changes = [self SOSTransportCircleTestGetChanges];
169 CFDictionaryAddValue(changes, ringKey, ring);
170 CFReleaseNull(ringKey);
174 -(bool) kvssendDebugInfo:(CFStringRef) type debug:(CFTypeRef) debugInfo err:(CFErrorRef *)error
176 CFMutableDictionaryRef changes = [self SOSTransportCircleTestGetChanges];
177 CFDictionaryAddValue(changes, type, debugInfo);
181 -(bool) postRetirement:(CFStringRef)cName peer:(SOSPeerInfoRef)peer err:(CFErrorRef *)error
183 CFStringRef retirement_key = SOSRetirementKeyCreateWithCircleNameAndPeer(cName, SOSPeerInfoGetPeerID(peer));
184 CFDataRef retirement_data = SOSPeerInfoCopyEncodedData(peer, kCFAllocatorDefault, error);
187 [self testAddToChanges:retirement_key data:retirement_data];
189 CFReleaseNull(retirement_key);
190 CFReleaseNull(retirement_data);
194 -(bool) flushChanges:(CFErrorRef *)error
199 -(void) testAddToChanges:(CFStringRef) message_key data:(CFDataRef) message_data
201 if (self.pending_changes == NULL) {
202 self.pending_changes = [NSMutableDictionary dictionary];
204 if (message_data == NULL) {
205 [self.pending_changes setObject:[NSNull null] forKey:(__bridge NSString*)(message_key)];
207 [self.pending_changes setObject:(__bridge NSData*)message_data forKey:(__bridge NSString*)message_key];
209 secnotice("circle-changes", "Adding circle change %@ %@->%@", self.accountName, message_key, message_data);
212 -(void) SOSTransportCircleTestAddBulkToChanges:(CFDictionaryRef) updates
214 if (self.pending_changes == NULL) {
215 self.pending_changes = [[NSMutableDictionary alloc]initWithDictionary:(__bridge NSDictionary * _Nonnull)(updates)];
219 CFDictionaryForEach(updates, ^(const void *key, const void *value) {
220 [self.pending_changes setObject:(__bridge id _Nonnull)value forKey:(__bridge id _Nonnull)key];
226 -(bool) expireRetirementRecords:(CFDictionaryRef) retirements err:(CFErrorRef *)error
229 CFMutableDictionaryRef keysToWrite = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
231 CFDictionaryForEach(retirements, ^(const void *key, const void *value) {
232 if (isString(key) && isArray(value)) {
233 CFStringRef circle_name = (CFStringRef) key;
234 CFArrayRef retirees = (CFArrayRef) value;
236 CFArrayForEach(retirees, ^(const void *value) {
237 if (isString(value)) {
238 CFStringRef retiree_id = (CFStringRef) value;
240 CFStringRef kvsKey = SOSRetirementKeyCreateWithCircleNameAndPeer(circle_name, retiree_id);
242 CFDictionaryAddValue(keysToWrite, kvsKey, kCFNull);
244 CFReleaseSafe(kvsKey);
250 if(CFDictionaryGetCount(keysToWrite)) {
251 [self SOSTransportCircleTestAddBulkToChanges:keysToWrite];
253 CFReleaseNull(keysToWrite);
258 bool SOSTransportCircleTestRemovePendingChange(SOSCircleStorageTransportTest* transport, CFStringRef circleName, CFErrorRef *error){
259 CFStringRef circle_key = SOSCircleKeyCreateWithName(circleName, error);
261 [transport.pending_changes removeObjectForKey:(__bridge NSString*)circle_key];
262 CFReleaseNull(circle_key);
266 -(bool) postCircle:(CFStringRef)cName circleData:(CFDataRef)circle_data err:(CFErrorRef *)error
268 CFStringRef circle_key = SOSCircleKeyCreateWithName(cName, error);
270 [self testAddToChanges:circle_key data:circle_data];
271 CFReleaseNull(circle_key);
276 -(CFDictionaryRef)handleRetirementMessages:(CFMutableDictionaryRef) circle_retirement_messages_table err:(CFErrorRef *)error
278 return SOSAccountHandleRetirementMessages(self.account, circle_retirement_messages_table, error);
281 -(CFArrayRef)CF_RETURNS_RETAINED handleCircleMessagesAndReturnHandledCopy:(CFMutableDictionaryRef) circle_circle_messages_table err:(CFErrorRef *)error
283 CFMutableArrayRef handledKeys = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
284 CFDictionaryForEach(circle_circle_messages_table, ^(const void *key, const void *value) {
285 CFErrorRef circleMessageError = NULL;
286 if (!SOSAccountHandleCircleMessage(self.account, key, value, &circleMessageError)) {
287 secerror("Error handling circle message %@ (%@): %@", key, value, circleMessageError);
290 CFStringRef circle_id = (CFStringRef) key;
291 CFArrayAppendValue(handledKeys, circle_id);
293 CFReleaseNull(circleMessageError);
299 SOSAccount* SOSTransportCircleTestGetAccount(SOSCircleStorageTransportTest* transport) {
300 return transport.account;
306 //MARK KVS Message Test Transport
311 @implementation SOSMessageKVSTest
314 -(id) initWithAccount:(SOSAccount*)acct andName:(CFStringRef)n andCircleName:(CFStringRef) cN
318 self.engine = SOSDataSourceFactoryGetEngineForDataSourceName(acct.factory, cN, NULL);
320 self.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
321 self.name = CFRetainSafe(n);
322 self.circleName = (__bridge NSString*)cN;
324 if(!message_transports)
325 message_transports = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
326 CFArrayAppendValue(message_transports, (__bridge const void *)((SOSMessageKVSTest*)self));
327 SOSRegisterTransportMessage((SOSMessageKVSTest*)self);
333 - (void)setChanges:(CFMutableDictionaryRef)changes
335 CFRetainAssign(self->_changes, changes);
338 -(CFIndex) SOSTransportMessageGetTransportType
342 -(CFStringRef) SOSTransportMessageGetCircleName
344 return (__bridge CFStringRef)circleName;
346 -(CFTypeRef) SOSTransportMessageGetEngine
350 -(SOSAccount*) SOSTransportMessageGetAccount
355 void SOSTransportMessageKVSTestSetName(SOSMessageKVSTest* transport, CFStringRef n)
360 CFStringRef SOSTransportMessageKVSTestGetName(SOSMessageKVSTest* transport)
362 return transport.name;
365 CFMutableDictionaryRef SOSTransportMessageKVSTestGetChanges(SOSMessageKVSTest* transport)
367 return transport.changes;
370 void SOSTransportMessageTestClearChanges(SOSMessageKVSTest* transport)
372 transport.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
375 static void SOSTransportMessageTestAddBulkToChanges(SOSMessageKVSTest* transport, CFDictionaryRef updates)
377 #ifndef __clang_analyzer__ // The analyzer thinks transport.changes is a leak, but I don't know why.
378 if (transport.changes == NULL) {
379 transport.changes = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, CFDictionaryGetCount(updates), updates);
382 CFDictionaryForEach(updates, ^(const void *key, const void *value) {
383 CFDictionarySetValue(transport.changes, key, value);
386 #endif // __clang_analyzer__
389 static void SOSTransportMessageTestAddToChanges(SOSMessageKVSTest* transport, CFStringRef message_key, CFDataRef message_data)
391 if (transport.changes == NULL) {
392 transport.changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
394 if (message_data == NULL) {
395 CFDictionarySetValue(transport.changes, message_key, kCFNull);
397 CFDictionarySetValue(transport.changes, message_key, message_data);
401 -(bool) SOSTransportMessageCleanupAfterPeerMessages:(SOSMessageKVSTest*) transport peers:(CFDictionaryRef)circle_to_peer_ids err:(CFErrorRef*) error
403 if(!transport.engine)
405 CFArrayRef enginePeers = SOSEngineGetPeerIDs((SOSEngineRef)transport.engine);
407 CFDictionaryForEach(circle_to_peer_ids, ^(const void *key, const void *value) {
408 if (isString(key) && isArray(value)) {
409 CFStringRef circle_name = (CFStringRef) key;
410 CFArrayRef peers_to_cleanup_after = (CFArrayRef) value;
412 CFArrayForEach(peers_to_cleanup_after, ^(const void *value) {
413 if (isString(value)) {
414 CFStringRef cleanup_id = (CFStringRef) value;
415 // TODO: Since the enginePeers list is not authorative (the Account is) this could inadvertently clean up active peers or leave behind stale peers
416 if (enginePeers) CFArrayForEach(enginePeers, ^(const void *value) {
417 if (isString(value)) {
418 CFStringRef in_circle_id = (CFStringRef) value;
420 CFStringRef kvsKey = SOSMessageKeyCreateWithCircleNameAndPeerNames(circle_name, cleanup_id, in_circle_id);
421 SOSTransportMessageTestAddToChanges(transport, kvsKey, NULL);
422 CFReleaseSafe(kvsKey);
424 kvsKey = SOSMessageKeyCreateWithCircleNameAndPeerNames(circle_name, in_circle_id, cleanup_id);
425 SOSTransportMessageTestAddToChanges(transport, kvsKey, NULL);
426 CFReleaseSafe(kvsKey);
435 return [transport SOSTransportMessageFlushChanges:transport err:error];
439 static bool sendToPeer(SOSMessageKVSTest* transport, CFStringRef circleName, CFStringRef peerID, CFDataRef message, CFErrorRef *error) {
441 NSString* myID = transport.account.peerID;
442 CFStringRef message_to_peer_key = SOSMessageKeyCreateFromTransportToPeer((SOSMessage*)transport, (__bridge CFStringRef) myID, peerID);
443 CFDictionaryRef a_message_to_a_peer = CFDictionaryCreateForCFTypes(NULL, message_to_peer_key, message, NULL);
445 SOSTransportMessageTestAddBulkToChanges(transport, a_message_to_a_peer);
446 CFReleaseNull(a_message_to_a_peer);
447 CFReleaseNull(message_to_peer_key);
452 -(bool) SOSTransportMessageSyncWithPeers:(SOSMessageKVSTest*) transport p:(CFSetRef) peers err:(CFErrorRef *)error
454 // Each entry is keyed by circle name and contains a list of peerIDs
456 __block bool result = true;
458 CFSetForEach(peers, ^(const void *value) {
459 CFStringRef peerID = asString(value, NULL);
462 SOSEngineWithPeerID((SOSEngineRef)transport.engine, peerID, error, ^(SOSPeerRef peer, SOSCoderRef coder, SOSDataSourceRef dataSource, SOSTransactionRef txn, bool *forceSaveState) {
463 SOSEnginePeerMessageSentCallback* sentCallback = NULL;
464 CFDataRef message_to_send = NULL;
465 bool ok = SOSPeerCoderSendMessageIfNeeded([transport SOSTransportMessageGetAccount], (SOSEngineRef)transport.engine, txn, peer, coder, &message_to_send, peerID, false, &sentCallback, error);
466 if (message_to_send) {
467 CFDictionaryRef peer_dict = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, peerID, message_to_send, NULL);
468 CFDictionarySetValue(SOSTransportMessageKVSTestGetChanges(transport), (__bridge CFStringRef)self->circleName, peer_dict);
469 SOSEngineMessageCallCallback(sentCallback, ok);
470 CFReleaseSafe(peer_dict);
473 SOSEngineFreeMessageCallback(sentCallback);
474 CFReleaseSafe(message_to_send);
481 -(bool) SOSTransportMessageSendMessages:(SOSMessageKVSTest*) transport pm:(CFDictionaryRef) peer_messages err:(CFErrorRef *)error
483 __block bool result = true;
485 CFDictionaryForEach(peer_messages, ^(const void *key, const void *value) {
486 if (isString(key) && isData(value)) {
487 CFStringRef peerID = (CFStringRef) key;
488 CFDataRef message = (CFDataRef) value;
489 bool rx = sendToPeer(transport, (__bridge CFStringRef)transport->circleName, peerID, message, error);
497 -(bool) SOSTransportMessageFlushChanges:(SOSMessageKVSTest*) transport err:(CFErrorRef *)error
502 SOSAccount* SOSTransportMessageKVSTestGetAccount(SOSMessageKVSTest* transport) {
503 return transport.account;
508 void SOSAccountUpdateTestTransports(SOSAccount* account, CFDictionaryRef gestalt){
509 CFStringRef new_name = (CFStringRef)CFDictionaryGetValue(gestalt, kPIUserDefinedDeviceNameKey);
511 SOSTransportKeyParameterTestSetName((CKKeyParameterTest*)account.key_transport, new_name);
512 SOSTransportCircleTestSetName((SOSCircleStorageTransportTest*)account.circle_transport, new_name);
513 SOSTransportMessageKVSTestSetName((SOSMessageKVSTest*)account.kvs_message_transport, new_name);
516 static CF_RETURNS_RETAINED SOSCircleRef SOSAccountEnsureCircleTest(SOSAccount* a, CFStringRef name, CFStringRef accountName)
518 CFErrorRef localError = NULL;
519 SOSAccountTrustClassic *trust = a.trust;
521 SOSCircleRef circle = CFRetainSafe([a.trust getCircle:&localError]);
522 if(!circle || isSOSErrorCoded(localError, kSOSErrorIncompatibleCircle)){
523 secnotice("circle", "Error retrieving the circle: %@", localError);
524 CFReleaseNull(localError);
525 CFReleaseNull(circle);
527 circle = SOSCircleCreate(kCFAllocatorDefault, name, &localError);
529 [trust setTrustedCircle:circle];
532 secnotice("circle", "Could not create circle: %@", localError);
534 CFReleaseNull(localError);
537 if(![trust ensureFullPeerAvailable:(__bridge CFDictionaryRef)(a.gestalt) deviceID:(__bridge CFStringRef)(a.deviceID) backupKey:(__bridge CFDataRef)(a.backup_key) err:&localError])
539 secnotice("circle", "had an error building full peer: %@", localError);
540 CFReleaseNull(localError);
544 CFReleaseNull(localError);
548 bool SOSAccountEnsureFactoryCirclesTest(SOSAccount* a, CFStringRef accountName)
555 CFStringRef circle_name = SOSDataSourceFactoryCopyName(a.factory);
558 CFReleaseSafe(SOSAccountEnsureCircleTest(a, (CFStringRef)circle_name, accountName));
560 CFReleaseNull(circle_name);
566 bool SOSAccountInflateTestTransportsForCircle(SOSAccount* account, CFStringRef circleName, CFStringRef accountName, CFErrorRef *error){
567 bool success = false;
569 if(account.key_transport == nil){
570 account.key_transport = (CKKeyParameter*)[[CKKeyParameterTest alloc] initWithAccount:account andName:accountName andCircleName:circleName];
571 require_quiet(account.key_transport, fail);
574 if(account.circle_transport == nil){
575 account.circle_transport = (SOSKVSCircleStorageTransport*)[[SOSCircleStorageTransportTest alloc] initWithAccount:account andWithAccountName:accountName andCircleName:circleName];
576 require_quiet(account.circle_transport, fail);
578 if(account.kvs_message_transport == nil){
579 account.kvs_message_transport = (SOSMessageKVS*)[[SOSMessageKVSTest alloc] initWithAccount:account andName:accountName andCircleName:circleName];