2 // keychain_sync_test.c
5 // Created by Mitch Adler on 7/8/16.
9 #include "keychain_sync_test.h"
11 #include "secToolFileIO.h"
17 #include <utilities/SecCFWrappers.h>
18 #include <utilities/SecCFRelease.h>
20 #import <Foundation/Foundation.h>
22 #include <Security/SecureObjectSync/SOSCloudCircle.h>
24 #import "NSFileHandle+Formatting.h"
26 static char boolToChars(bool val, char truechar, char falsechar) {
27 return val? truechar: falsechar;
31 keychain_sync_test(int argc, char * const *argv)
33 NSFileHandle *fhout = [NSFileHandle fileHandleWithStandardOutput];
34 NSFileHandle *fherr = [NSFileHandle fileHandleWithStandardError];
36 "Keychain Syncing test"
41 __block CFErrorRef cfError = NULL;
43 static int verbose_flag = 0;
44 bool dump_pending = false;
46 static struct option long_options[] =
48 /* These options set a flag. */
49 {"verbose", no_argument, &verbose_flag, 1},
50 {"brief", no_argument, &verbose_flag, 0},
51 /* These options don’t set a flag.
52 We distinguish them by their indices. */
53 {"enabled-peer-views", required_argument, 0, 'p'},
54 {"message-pending-state", no_argument, 0, 'm'},
57 static const char * params = "p:m";
59 /* getopt_long stores the option index here. */
62 NSArray<NSString*>* viewList = nil;
65 while (opt_result != -1) {
66 opt_result = getopt_long (argc, argv, params, long_options, &option_index);
69 NSString* parameter = [NSString stringWithCString: optarg encoding:NSUTF8StringEncoding];
71 viewList = [parameter componentsSeparatedByString:@","];
87 CFBooleanRef result = SOSCCPeersHaveViewsEnabled((__bridge CFArrayRef) viewList, &cfError);
89 [fhout writeFormat: @"Views: %@\n", viewList];
90 [fhout writeFormat: @"Enabled on other peers: %@\n", CFBooleanGetValue(result) ? @"yes" : @"no"];
95 CFArrayRef peers = SOSCCCopyPeerPeerInfo(&cfError);
96 [fhout writeFormat: @"Dumping state for %ld peers\n", CFArrayGetCount(peers)];
98 CFArrayForEach(peers, ^(const void *value) {
99 SOSPeerInfoRef thisPeer = (SOSPeerInfoRef) value;
101 CFReleaseNull(cfError);
102 bool message = SOSCCMessageFromPeerIsPending(thisPeer, &cfError);
103 if (!message && cfError != NULL) {
104 [fherr writeFormat: @"Error from SOSCCMessageFromPeerIsPending: %@\n", cfError];
106 CFReleaseNull(cfError);
107 bool send = SOSCCSendToPeerIsPending(thisPeer, &cfError);
108 if (!message && cfError != NULL) {
109 [fherr writeFormat: @"Error from SOSCCSendToPeerIsPending: %@\n", cfError];
111 CFReleaseNull(cfError);
113 [fhout writeFormat: @"Peer: %c%c %@\n", boolToChars(message, 'M', 'm'), boolToChars(send, 'S', 's'), thisPeer];
115 [fherr writeFormat: @"Non SOSPeerInfoRef in array: %@\n", value];
120 if (cfError != NULL) {
121 [fherr writeFormat: @"Error: %@\n", cfError];
125 [fherr writeFormat: @"Error: %@\n", error];