]> git.saurik.com Git - apple/configd.git/blob - sctest/genSCTestOptions.c
configd-888.51.2.tar.gz
[apple/configd.git] / sctest / genSCTestOptions.c
1 /*
2 * Copyright (c) 2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <mach/boolean.h>
30 #include <getopt.h>
31
32 typedef struct {
33 const char *scope; // Either "global" or for a particular command
34 const char *optionString; // The option
35 const char *optionKey; // String representation of the key to be used to access this option's value from the "options" dictionary.
36 int hasArg; // no_argument, required_argument, optional_argument
37 const char *usageString; // help string
38 } SCTestOption;
39
40 // Add the options here and then once finished, go to ${SRCROOT}/sctest/ in Terminal and type "make".
41 // The options should then be ready to use, through the "options" dictionary.
42 SCTestOption testOptions[] = {
43 {"global", "cpu", "kSCTestGlobalOptionCPU", no_argument, "Prints the CPU usage after the test completes"},
44 {"global", "help", "kSCTestGlobalOptionHelp", no_argument, "Prints this very useful help!"},
45 {"global", "time", "kSCTestGlobalOptionTime", no_argument, "Prints the time elapsed since the test was launched"},
46 {"global", "verbose", "kSCTestGlobalOptionVerbose", no_argument, "Enables verbose mode"},
47 {"global", "wait", "kSCTestGlobalOptionWait", no_argument, "Results in a wait for 'sctest'"},
48
49 {"dynamic_store", "dns", "kSCTestDynamicStoreOptionDNS", no_argument, "Prints the global DNS information from the SCDynamicStore"},
50 {"dynamic_store", "ipv4", "kSCTestDynamicStoreOptionIPv4", no_argument, "Prints the global IPv4 information from the SCDynamicStore"},
51 {"dynamic_store", "ipv6", "kSCTestDynamicStoreOptionIPv6", no_argument, "Prints the global IPv6 information from the SCDynamicStore"},
52 {"dynamic_store", "proxies", "kSCTestDynamicStoreOptionProxies", no_argument, "Prints the global Proxy information from the SCDynamicStore"},
53
54 {"preferences", "service_list", "kSCTestPreferencesServiceList", no_argument, "Prints the Network Services list from the preferences"},
55 {"preferences", "service_order", "kSCTestPreferencesServiceOrder", no_argument, "Prints the Network Service order from the preferences"},
56
57 {"config_agent", "dns_domain", "kSCTestConfigAgentDNSDomains", required_argument, "Configures the DNS Servers for certain domains. A comma-separated list of domains can be specified. Default is 'apple.com'"},
58 {"config_agent", "dns_servers", "kSCTestConfigAgentDNSServers", required_argument, "Configures the specified DNS Servers. A comma-separated list of IP Addresses can be specified"},
59 {"config_agent", "remove_dns", "kSCTestConfigAgentRemoveDNS", no_argument, "Remove a dns configuration, previously configured via 'sctest'"},
60
61 {"config_agent", "ftp_proxy", "kSCTestConfigAgentFTPProxy", required_argument, "Add a proxy agent with FTP proxy. Format of the argument is 'server:port'"},
62 {"config_agent", "gopher_proxy", "kSCTestConfigAgentGopherProxy", required_argument, "Add a proxy agent with Gopher proxy. Format of the argument is 'server:port'"},
63 {"config_agent", "http_proxy", "kSCTestConfigAgentHTTPProxy", required_argument, "Add a proxy agent with HTTP proxy. Format of the argument is 'server:port'"},
64 {"config_agent", "https_proxy", "kSCTestConfigAgentHTTPSProxy", required_argument, "Add a proxy agent with HTTPS proxy. Format of the argument is 'server:port'"},
65 {"config_agent", "proxy_match_domain", "kSCTestConfigAgentProxyMatchDomain", required_argument, "Add a proxy agent for a match domain. If this option is not specified, 'apple.com' will be used"},
66 {"config_agent", "remove_proxy", "kSCTestConfigAgentRemoveProxy", no_argument, "Remove a proxy configuration, previously configured via 'sctest'"},
67 {"config_agent", "socks_proxy", "kSCTestConfigAgentSOCKSProxy", required_argument, "Add a proxy agent with SOCKS proxy. Format of the argument is 'server:port'"},
68
69 {"reachability", "address", "kSCTestReachabilityAddress", required_argument, "Determine reachability to this address"},
70 {"reachability", "host", "kSCTestReachabilityHost", required_argument, "Determine reachability to this host"},
71 {"reachability", "interface", "kSCTestReachabilityInterface", required_argument, "Determine reachability when scoped to this interface"},
72 {"reachability", "watch", "kSCTestReachabilityWatch", no_argument, "Watch for reachability changes"},
73
74 {"unit_test", "command", "kSCTestUnitTestCommand", required_argument, "Run a unit test for a specific command. If this option is not specified, unit-tests for all commands will be run"},
75 {"unit_test", "list_tests", "kSCTestUnitTestListTests", no_argument, "List the test commands in a JSON format. This is for NPT compliance"},
76 {"unit_test", "test_method", "kSCTestUnitTestTestMethod", required_argument, "Runs a specific unit test. List can be obtained by using the 'test_method_list' option"},
77 {"unit_test", "test_method_list", "kSCTestUnitTestTestMethodList", no_argument, "Lists all the unit tests. A specific one can be run using the 'test_method' option"},
78 };
79
80 static int testOptionsCount = (sizeof(testOptions) / sizeof(testOptions[0]));
81
82 void
83 printDeclarations()
84 {
85 // Prints to SCTestOptions.h
86
87 const char *keyTemplate = "extern const NSString * const";
88 for (int i = 0; i < testOptionsCount; i++) {
89 char buffer[256] = {0};
90 snprintf(buffer, sizeof(buffer), "%s %s;", keyTemplate, testOptions[i].optionKey);
91 printf("%s\n", buffer);
92 }
93 }
94
95 void
96 printOptionEntries()
97 {
98 // Prints to SCTestOptions.h
99
100 const char *entryTemplate = "#define kSCTestOptionEntries";
101 printf("\n%s \\\n", entryTemplate);
102 for (int i = 0; i < testOptionsCount; i++) {
103 printf("\t\t{\"%s\", %d, NULL, 0}, \\", testOptions[i].optionString, testOptions[i].hasArg);
104 printf("\n");
105 }
106 }
107
108 void
109 printUsage()
110 {
111 // Prints to SCTestOptions.h
112
113 const char *usageTemplate = "#define kSCTestOptionHelp";
114 printf("\n%s \\\n", usageTemplate);
115 const char *last = "";
116 for (int i = 0; i < testOptionsCount; i++) {
117 if (strcmp(last, testOptions[i].scope)) {
118 last = testOptions[i].scope;
119 printf("\t\t\"\\n============== %s options =============\\n\"\\\n", testOptions[i].scope);
120 }
121
122 printf("\t\t\"-%-20s: %s\\n\"\\\n", testOptions[i].optionString, testOptions[i].usageString);
123 }
124 }
125
126 void
127 printDefinitions()
128 {
129 // Prints to SCTestOptions.m
130
131 const char *definitionTemplate = "const NSString * const";
132 for (int i = 0; i < testOptionsCount; i++) {
133 char buffer[256] = {0};
134 snprintf(buffer, sizeof(buffer), "%s %-50s= @\"%s_Str\";", definitionTemplate, testOptions[i].optionKey, testOptions[i].optionString);
135 printf("%s\n", buffer);
136 }
137 }
138
139 int
140 main(int argc, char * argv[])
141 {
142 char * type = "";
143
144 if (argc >= 2) {
145 type = argv[1];
146 }
147
148 if (strcmp(type, "header") == 0) {
149 // Preamble
150 printf("//\n");
151 printf("// This file is automatically generated. DO NOT EDIT!\n");
152 printf("// To add options, see genSCTestOptions.c\n");
153 printf("//\n");
154
155 // Import header files
156 printf("#import <Foundation/Foundation.h>\n");
157 printf("#import <getopt.h>");
158
159 printf("\n\n");
160
161 // Print the declarations
162 printDeclarations();
163
164 printf("\n\n");
165
166 // Print the option Entries
167 printOptionEntries();
168
169 // Print the usage
170 printUsage();
171 } else if (strcmp(type, "mfile") == 0) {
172 printf("//\n");
173 printf("// This file is automatically generated. DO NOT EDIT!\n");
174 printf("// To add options, see genSCTestOptions.c\n");
175 printf("//\n");
176
177 // Import header files
178 printf("#import \"SCTestOptions.h\"");
179
180 printf("\n\n");
181 printDefinitions();
182 }
183 return 0;
184 }