]>
Commit | Line | Data |
---|---|---|
fa7225c8 A |
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 | ||
6b200bc3 A |
17 | #include <utilities/SecCFWrappers.h> |
18 | #include <utilities/SecCFRelease.h> | |
19 | ||
fa7225c8 A |
20 | #import <Foundation/Foundation.h> |
21 | ||
22 | #include <Security/SecureObjectSync/SOSCloudCircle.h> | |
23 | ||
24 | #import "NSFileHandle+Formatting.h" | |
25 | ||
6b200bc3 A |
26 | static char boolToChars(bool val, char truechar, char falsechar) { |
27 | return val? truechar: falsechar; | |
28 | } | |
29 | ||
fa7225c8 A |
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; | |
6b200bc3 | 40 | __block CFErrorRef cfError = NULL; |
fa7225c8 A |
41 | |
42 | static int verbose_flag = 0; | |
6b200bc3 A |
43 | bool dump_pending = false; |
44 | ||
fa7225c8 A |
45 | static struct option long_options[] = |
46 | { | |
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'}, | |
6b200bc3 | 53 | {"message-pending-state", no_argument, 0, 'm'}, |
fa7225c8 A |
54 | {0, 0, 0, 0} |
55 | }; | |
6b200bc3 | 56 | static const char * params = "p:m"; |
fa7225c8 A |
57 | |
58 | /* getopt_long stores the option index here. */ | |
59 | int option_index = 0; | |
60 | ||
61 | NSArray<NSString*>* viewList = nil; | |
62 | ||
63 | int opt_result = 0; | |
64 | while (opt_result != -1) { | |
65 | opt_result = getopt_long (argc, argv, params, long_options, &option_index); | |
66 | switch (opt_result) { | |
67 | case 'p': { | |
68 | NSString* parameter = [NSString stringWithCString: optarg encoding:NSUTF8StringEncoding]; | |
69 | ||
70 | viewList = [parameter componentsSeparatedByString:@","]; | |
71 | ||
72 | } | |
73 | break; | |
6b200bc3 A |
74 | case 'm': |
75 | dump_pending = true; | |
76 | break; | |
fa7225c8 A |
77 | case -1: |
78 | break; | |
79 | default: | |
80 | return 2; | |
81 | } | |
82 | ||
83 | } | |
84 | ||
85 | if (viewList) { | |
86 | CFBooleanRef result = SOSCCPeersHaveViewsEnabled((__bridge CFArrayRef) viewList, &cfError); | |
87 | if (result != NULL) { | |
88 | [fhout writeFormat: @"Views: %@\n", viewList]; | |
89 | [fhout writeFormat: @"Enabled on other peers: %@\n", CFBooleanGetValue(result) ? @"yes" : @"no"]; | |
90 | } | |
91 | } | |
92 | ||
6b200bc3 A |
93 | if (dump_pending) { |
94 | CFArrayRef peers = SOSCCCopyPeerPeerInfo(&cfError); | |
866f8763 A |
95 | if (peers != NULL) { |
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]; | |
6b200bc3 | 116 | } |
866f8763 A |
117 | }); |
118 | } | |
6b200bc3 A |
119 | } |
120 | ||
fa7225c8 A |
121 | if (cfError != NULL) { |
122 | [fherr writeFormat: @"Error: %@\n", cfError]; | |
123 | } | |
124 | ||
fa7225c8 A |
125 | return result; |
126 | } |