]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Tool/keychain_sync_test.m
Security-57740.60.18.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Tool / keychain_sync_test.m
1 //
2 // keychain_sync_test.c
3 // sec
4 //
5 // Created by Mitch Adler on 7/8/16.
6 //
7 //
8
9 #include "keychain_sync_test.h"
10
11 #include "secToolFileIO.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <getopt.h>
16
17 #include <utilities/SecCFWrappers.h>
18 #include <utilities/SecCFRelease.h>
19
20 #import <Foundation/Foundation.h>
21
22 #include <Security/SecureObjectSync/SOSCloudCircle.h>
23
24 #import "NSFileHandle+Formatting.h"
25
26 static char boolToChars(bool val, char truechar, char falsechar) {
27 return val? truechar: falsechar;
28 }
29
30 int
31 keychain_sync_test(int argc, char * const *argv)
32 {
33 NSFileHandle *fhout = [NSFileHandle fileHandleWithStandardOutput];
34 NSFileHandle *fherr = [NSFileHandle fileHandleWithStandardError];
35 /*
36 "Keychain Syncing test"
37
38 */
39 int result = 0;
40 NSError* error = nil;
41 __block CFErrorRef cfError = NULL;
42
43 static int verbose_flag = 0;
44 bool dump_pending = false;
45
46 static struct option long_options[] =
47 {
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'},
55 {0, 0, 0, 0}
56 };
57 static const char * params = "p:m";
58
59 /* getopt_long stores the option index here. */
60 int option_index = 0;
61
62 NSArray<NSString*>* viewList = nil;
63
64 int opt_result = 0;
65 while (opt_result != -1) {
66 opt_result = getopt_long (argc, argv, params, long_options, &option_index);
67 switch (opt_result) {
68 case 'p': {
69 NSString* parameter = [NSString stringWithCString: optarg encoding:NSUTF8StringEncoding];
70
71 viewList = [parameter componentsSeparatedByString:@","];
72
73 }
74 break;
75 case 'm':
76 dump_pending = true;
77 break;
78 case -1:
79 break;
80 default:
81 return 2;
82 }
83
84 }
85
86 if (viewList) {
87 CFBooleanRef result = SOSCCPeersHaveViewsEnabled((__bridge CFArrayRef) viewList, &cfError);
88 if (result != NULL) {
89 [fhout writeFormat: @"Views: %@\n", viewList];
90 [fhout writeFormat: @"Enabled on other peers: %@\n", CFBooleanGetValue(result) ? @"yes" : @"no"];
91 }
92 }
93
94 if (dump_pending) {
95 CFArrayRef peers = SOSCCCopyPeerPeerInfo(&cfError);
96 [fhout writeFormat: @"Dumping state for %ld peers\n", CFArrayGetCount(peers)];
97
98 CFArrayForEach(peers, ^(const void *value) {
99 SOSPeerInfoRef thisPeer = (SOSPeerInfoRef) value;
100 if (thisPeer) {
101 CFReleaseNull(cfError);
102 bool message = SOSCCMessageFromPeerIsPending(thisPeer, &cfError);
103 if (!message && cfError != NULL) {
104 [fherr writeFormat: @"Error from SOSCCMessageFromPeerIsPending: %@\n", cfError];
105 }
106 CFReleaseNull(cfError);
107 bool send = SOSCCSendToPeerIsPending(thisPeer, &cfError);
108 if (!message && cfError != NULL) {
109 [fherr writeFormat: @"Error from SOSCCSendToPeerIsPending: %@\n", cfError];
110 }
111 CFReleaseNull(cfError);
112
113 [fhout writeFormat: @"Peer: %c%c %@\n", boolToChars(message, 'M', 'm'), boolToChars(send, 'S', 's'), thisPeer];
114 } else {
115 [fherr writeFormat: @"Non SOSPeerInfoRef in array: %@\n", value];
116 }
117 });
118 }
119
120 if (cfError != NULL) {
121 [fherr writeFormat: @"Error: %@\n", cfError];
122 }
123
124 if (error != NULL) {
125 [fherr writeFormat: @"Error: %@\n", error];
126 }
127
128 return result;
129 }