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"
40 __block CFErrorRef cfError = NULL;
42 static int verbose_flag = 0;
43 bool dump_pending = false;
45 static struct option long_options[] =
47 /* These options set a flag. */
48 {"verbose", no_argument, &verbose_flag, 1},
49 {"brief", no_argument, &verbose_flag, 0},
50 /* These options don’t set a flag.
51 We distinguish them by their indices. */
52 {"enabled-peer-views", required_argument, 0, 'p'},
53 {"message-pending-state", no_argument, 0, 'm'},
56 static const char * params = "p:m";
58 /* getopt_long stores the option index here. */
61 NSArray<NSString*>* viewList = nil;
64 while (opt_result != -1) {
65 opt_result = getopt_long (argc, argv, params, long_options, &option_index);
68 NSString* parameter = [NSString stringWithCString: optarg encoding:NSUTF8StringEncoding];
70 viewList = [parameter componentsSeparatedByString:@","];
80 return SHOW_USAGE_MESSAGE;
86 CFBooleanRef result = SOSCCPeersHaveViewsEnabled((__bridge CFArrayRef) viewList, &cfError);
88 [fhout writeFormat: @"Views: %@\n", viewList];
89 [fhout writeFormat: @"Enabled on other peers: %@\n", CFBooleanGetValue(result) ? @"yes" : @"no"];
94 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];
121 if (cfError != NULL) {
122 [fherr writeFormat: @"Error: %@\n", cfError];