]> git.saurik.com Git - apple/configd.git/blob - sctest/genSCTestOptions.c
configd-1109.101.1.tar.gz
[apple/configd.git] / sctest / genSCTestOptions.c
1 /*
2 * Copyright (c) 2016, 2017, 2019, 2020 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, "Configures the Proxy server for certain domains. A comma-separated list of domains can be specified. Default is 'apple.com'"},
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 printf("\t\t{NULL, 0, NULL, 0},\n");
107 }
108
109 void
110 printUsage()
111 {
112 // Prints to SCTestOptions.h
113
114 const char *usageTemplate = "#define kSCTestOptionHelp";
115 printf("\n%s \\\n", usageTemplate);
116 const char *last = "";
117 for (int i = 0; i < testOptionsCount; i++) {
118 if (strcmp(last, testOptions[i].scope)) {
119 last = testOptions[i].scope;
120 printf("\t\t\"\\n============== %s options =============\\n\"\\\n", testOptions[i].scope);
121 }
122
123 printf("\t\t\"-%-20s: %s\\n\"\\\n", testOptions[i].optionString, testOptions[i].usageString);
124 }
125 }
126
127 void
128 printDefinitions()
129 {
130 // Prints to SCTestOptions.m
131
132 const char *definitionTemplate = "const NSString * const";
133 for (int i = 0; i < testOptionsCount; i++) {
134 char buffer[256] = {0};
135 snprintf(buffer, sizeof(buffer), "%s %-50s= @\"%s_Str\";", definitionTemplate, testOptions[i].optionKey, testOptions[i].optionString);
136 printf("%s\n", buffer);
137 }
138 }
139
140 int
141 main(int argc, char * argv[])
142 {
143 char * type = "";
144
145 if (argc >= 2) {
146 type = argv[1];
147 }
148
149 if (strcmp(type, "header") == 0) {
150 // Preamble
151 printf("//\n");
152 printf("// This file is automatically generated. DO NOT EDIT!\n");
153 printf("// To add options, see genSCTestOptions.c\n");
154 printf("//\n");
155
156 // Import header files
157 printf("#import <Foundation/Foundation.h>\n");
158 printf("#import <getopt.h>");
159
160 printf("\n\n");
161
162 // Print the declarations
163 printDeclarations();
164
165 printf("\n\n");
166
167 // Print the option Entries
168 printOptionEntries();
169
170 // Print the usage
171 printUsage();
172 } else if (strcmp(type, "mfile") == 0) {
173 printf("//\n");
174 printf("// This file is automatically generated. DO NOT EDIT!\n");
175 printf("// To add options, see genSCTestOptions.c\n");
176 printf("//\n");
177
178 // Import header files
179 printf("#import \"SCTestOptions.h\"");
180
181 printf("\n\n");
182 printDefinitions();
183 }
184 return 0;
185 }