blob: 1125aefdd9bf5b22e642349c0ad9666bfea87d57 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include "test_config.h"
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include <memory>
22
23#include <openssl/base64.h>
24
25namespace {
26
27template <typename T>
28struct Flag {
29 const char *flag;
30 T TestConfig::*member;
31};
32
33// FindField looks for the flag in |flags| that matches |flag|. If one is found,
34// it returns a pointer to the corresponding field in |config|. Otherwise, it
35// returns NULL.
36template<typename T, size_t N>
37T *FindField(TestConfig *config, const Flag<T> (&flags)[N], const char *flag) {
38 for (size_t i = 0; i < N; i++) {
39 if (strcmp(flag, flags[i].flag) == 0) {
40 return &(config->*(flags[i].member));
41 }
42 }
43 return NULL;
44}
45
46const Flag<bool> kBoolFlags[] = {
47 { "-server", &TestConfig::is_server },
48 { "-dtls", &TestConfig::is_dtls },
Adam Langleyd9e397b2015-01-22 14:27:53 -080049 { "-fallback-scsv", &TestConfig::fallback_scsv },
50 { "-require-any-client-certificate",
51 &TestConfig::require_any_client_certificate },
52 { "-false-start", &TestConfig::false_start },
53 { "-async", &TestConfig::async },
54 { "-write-different-record-sizes",
55 &TestConfig::write_different_record_sizes },
56 { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
57 { "-partial-write", &TestConfig::partial_write },
David Benjamind316cba2016-06-02 16:17:39 -040058 { "-no-tls13", &TestConfig::no_tls13 },
Adam Langleyd9e397b2015-01-22 14:27:53 -080059 { "-no-tls12", &TestConfig::no_tls12 },
60 { "-no-tls11", &TestConfig::no_tls11 },
61 { "-no-tls1", &TestConfig::no_tls1 },
62 { "-no-ssl3", &TestConfig::no_ssl3 },
David Benjaminc895d6b2016-08-11 13:26:41 -040063 { "-enable-channel-id", &TestConfig::enable_channel_id },
Adam Langleyd9e397b2015-01-22 14:27:53 -080064 { "-shim-writes-first", &TestConfig::shim_writes_first },
Adam Langleyd9e397b2015-01-22 14:27:53 -080065 { "-expect-session-miss", &TestConfig::expect_session_miss },
David Benjamin4969cc92016-04-22 15:02:23 -040066 { "-decline-alpn", &TestConfig::decline_alpn },
Adam Langleyd9e397b2015-01-22 14:27:53 -080067 { "-expect-extended-master-secret",
68 &TestConfig::expect_extended_master_secret },
Adam Langleyd9e397b2015-01-22 14:27:53 -080069 { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
70 { "-enable-signed-cert-timestamps",
71 &TestConfig::enable_signed_cert_timestamps },
Adam Langleye9ada862015-05-11 17:20:37 -070072 { "-implicit-handshake", &TestConfig::implicit_handshake },
73 { "-use-early-callback", &TestConfig::use_early_callback },
74 { "-fail-early-callback", &TestConfig::fail_early_callback },
75 { "-install-ddos-callback", &TestConfig::install_ddos_callback },
76 { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
77 { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
Steven Valdez909b19f2016-11-21 15:35:44 -050078 { "-fail-cert-callback", &TestConfig::fail_cert_callback },
Adam Langleye9ada862015-05-11 17:20:37 -070079 { "-handshake-never-done", &TestConfig::handshake_never_done },
80 { "-use-export-context", &TestConfig::use_export_context },
Adam Langleyf4e42722015-06-04 17:45:09 -070081 { "-tls-unique", &TestConfig::tls_unique },
Kenny Rootb8494592015-09-25 02:29:14 +000082 { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
83 { "-expect-no-session", &TestConfig::expect_no_session },
Robert Sloanb1b54b82017-11-06 13:50:02 -080084 { "-expect-ticket-supports-early-data",
85 &TestConfig::expect_ticket_supports_early_data },
Kenny Rootb8494592015-09-25 02:29:14 +000086 { "-use-ticket-callback", &TestConfig::use_ticket_callback },
87 { "-renew-ticket", &TestConfig::renew_ticket },
Robert Sloan69939df2017-01-09 10:53:07 -080088 { "-enable-early-data", &TestConfig::enable_early_data },
Kenny Rootb8494592015-09-25 02:29:14 +000089 { "-enable-client-custom-extension",
90 &TestConfig::enable_client_custom_extension },
91 { "-enable-server-custom-extension",
92 &TestConfig::enable_server_custom_extension },
93 { "-custom-extension-skip", &TestConfig::custom_extension_skip },
94 { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
95 { "-check-close-notify", &TestConfig::check_close_notify },
96 { "-shim-shuts-down", &TestConfig::shim_shuts_down },
Kenny Rootb8494592015-09-25 02:29:14 +000097 { "-verify-fail", &TestConfig::verify_fail },
98 { "-verify-peer", &TestConfig::verify_peer },
Robert Sloanf6200e72017-07-10 08:09:18 -070099 { "-verify-peer-if-no-obc", &TestConfig::verify_peer_if_no_obc },
Kenny Roote99801b2015-11-06 15:31:15 -0800100 { "-expect-verify-result", &TestConfig::expect_verify_result },
101 { "-renegotiate-once", &TestConfig::renegotiate_once },
102 { "-renegotiate-freely", &TestConfig::renegotiate_freely },
Adam Langleyfad63272015-11-12 12:15:39 -0800103 { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
Adam Langley4139edb2016-01-13 15:00:54 -0800104 { "-p384-only", &TestConfig::p384_only },
105 { "-enable-all-curves", &TestConfig::enable_all_curves },
David Benjamin4969cc92016-04-22 15:02:23 -0400106 { "-use-old-client-cert-callback",
107 &TestConfig::use_old_client_cert_callback },
David Benjaminc895d6b2016-08-11 13:26:41 -0400108 { "-send-alert", &TestConfig::send_alert },
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400109 { "-peek-then-read", &TestConfig::peek_then_read },
110 { "-enable-grease", &TestConfig::enable_grease },
Steven Valdez909b19f2016-11-21 15:35:44 -0500111 { "-use-exporter-between-reads", &TestConfig::use_exporter_between_reads },
Robert Sloan8f860b12017-08-28 07:37:06 -0700112 { "-retain-only-sha256-client-cert",
113 &TestConfig::retain_only_sha256_client_cert },
114 { "-expect-sha256-client-cert",
115 &TestConfig::expect_sha256_client_cert },
Robert Sloan69939df2017-01-09 10:53:07 -0800116 { "-read-with-unfinished-write", &TestConfig::read_with_unfinished_write },
Steven Valdezb0b45c62017-01-17 16:23:54 -0500117 { "-expect-secure-renegotiation",
118 &TestConfig::expect_secure_renegotiation },
119 { "-expect-no-secure-renegotiation",
120 &TestConfig::expect_no_secure_renegotiation },
Robert Sloan4d1ac502017-02-06 08:36:14 -0800121 { "-expect-session-id", &TestConfig::expect_session_id },
122 { "-expect-no-session-id", &TestConfig::expect_no_session_id },
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700123 { "-expect-accept-early-data", &TestConfig::expect_accept_early_data },
124 { "-expect-reject-early-data", &TestConfig::expect_reject_early_data },
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700125 { "-expect-no-offer-early-data", &TestConfig::expect_no_offer_early_data },
Robert Sloan6f79a502017-04-03 09:16:40 -0700126 { "-no-op-extra-handshake", &TestConfig::no_op_extra_handshake },
127 { "-handshake-twice", &TestConfig::handshake_twice },
Robert Sloan572a4e22017-04-17 10:52:19 -0700128 { "-allow-unknown-alpn-protos", &TestConfig::allow_unknown_alpn_protos },
129 { "-enable-ed25519", &TestConfig::enable_ed25519 },
Robert Sloanb6d070c2017-07-24 08:40:01 -0700130 { "-use-custom-verify-callback", &TestConfig::use_custom_verify_callback },
Robert Sloancd79cde2017-12-11 09:06:12 -0800131 { "-allow-false-start-without-alpn",
132 &TestConfig::allow_false_start_without_alpn },
Robert Sloan0da43952018-01-03 15:13:14 -0800133 { "-expect-draft-downgrade", &TestConfig::expect_draft_downgrade },
Robert Sloan8542c082018-02-05 09:07:34 -0800134 { "-handoff", &TestConfig::handoff },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800135};
136
137const Flag<std::string> kStringFlags[] = {
Robert Sloanf6200e72017-07-10 08:09:18 -0700138 { "-write-settings", &TestConfig::write_settings },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800139 { "-key-file", &TestConfig::key_file },
140 { "-cert-file", &TestConfig::cert_file },
141 { "-expect-server-name", &TestConfig::expected_server_name },
142 { "-advertise-npn", &TestConfig::advertise_npn },
143 { "-expect-next-proto", &TestConfig::expected_next_proto },
144 { "-select-next-proto", &TestConfig::select_next_proto },
145 { "-send-channel-id", &TestConfig::send_channel_id },
146 { "-host-name", &TestConfig::host_name },
147 { "-advertise-alpn", &TestConfig::advertise_alpn },
148 { "-expect-alpn", &TestConfig::expected_alpn },
Robert Sloane56da3e2017-06-26 08:26:42 -0700149 { "-expect-late-alpn", &TestConfig::expected_late_alpn },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
151 { "-select-alpn", &TestConfig::select_alpn },
152 { "-psk", &TestConfig::psk },
153 { "-psk-identity", &TestConfig::psk_identity },
154 { "-srtp-profiles", &TestConfig::srtp_profiles },
Adam Langleye9ada862015-05-11 17:20:37 -0700155 { "-cipher", &TestConfig::cipher },
156 { "-export-label", &TestConfig::export_label },
157 { "-export-context", &TestConfig::export_context },
Steven Valdez909b19f2016-11-21 15:35:44 -0500158 { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
Robert Sloan7d422bc2017-03-06 10:04:29 -0800159 { "-use-client-ca-list", &TestConfig::use_client_ca_list },
160 { "-expect-client-ca-list", &TestConfig::expected_client_ca_list },
Robert Sloanfe7cd212017-08-07 09:03:39 -0700161 { "-expect-msg-callback", &TestConfig::expect_msg_callback },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162};
163
164const Flag<std::string> kBase64Flags[] = {
165 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
166 { "-expect-channel-id", &TestConfig::expected_channel_id },
Robert Sloan978112c2018-01-22 12:53:01 -0800167 { "-token-binding-params", &TestConfig::send_token_binding_params },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800168 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
169 { "-expect-signed-cert-timestamps",
170 &TestConfig::expected_signed_cert_timestamps },
Kenny Rootb8494592015-09-25 02:29:14 +0000171 { "-ocsp-response", &TestConfig::ocsp_response },
172 { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
Steven Valdez909b19f2016-11-21 15:35:44 -0500173 { "-ticket-key", &TestConfig::ticket_key },
Robert Sloan8542c082018-02-05 09:07:34 -0800174 { "-quic-transport-params", &TestConfig::quic_transport_params },
175 { "-expected-quic-transport-params",
176 &TestConfig::expected_quic_transport_params },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800177};
178
179const Flag<int> kIntFlags[] = {
Adam Langleye9ada862015-05-11 17:20:37 -0700180 { "-port", &TestConfig::port },
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400181 { "-resume-count", &TestConfig::resume_count },
Robert Sloan978112c2018-01-22 12:53:01 -0800182 { "-expected-token-binding-param",
183 &TestConfig::expected_token_binding_param },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800184 { "-min-version", &TestConfig::min_version },
185 { "-max-version", &TestConfig::max_version },
Robert Sloanf6200e72017-07-10 08:09:18 -0700186 { "-expect-version", &TestConfig::expect_version },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800187 { "-mtu", &TestConfig::mtu },
Robert Sloan55818102017-12-18 11:26:17 -0800188 { "-export-early-keying-material",
189 &TestConfig::export_early_keying_material },
Adam Langleye9ada862015-05-11 17:20:37 -0700190 { "-export-keying-material", &TestConfig::export_keying_material },
Kenny Roote99801b2015-11-06 15:31:15 -0800191 { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
David Benjaminc895d6b2016-08-11 13:26:41 -0400192 { "-expect-peer-signature-algorithm",
193 &TestConfig::expect_peer_signature_algorithm },
194 { "-expect-curve-id", &TestConfig::expect_curve_id },
David Benjamind316cba2016-06-02 16:17:39 -0400195 { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400196 { "-max-cert-list", &TestConfig::max_cert_list },
Steven Valdez909b19f2016-11-21 15:35:44 -0500197 { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
198 { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
199 { "-resumption-delay", &TestConfig::resumption_delay },
Steven Valdezb0b45c62017-01-17 16:23:54 -0500200 { "-max-send-fragment", &TestConfig::max_send_fragment },
201 { "-read-size", &TestConfig::read_size },
Robert Sloan1c9db532017-03-13 08:03:59 -0700202 { "-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew },
Robert Sloana12bf462017-07-17 07:08:26 -0700203 { "-tls13-variant", &TestConfig::tls13_variant },
Robert Sloan0db7f542018-01-16 15:48:33 -0800204 { "-dummy-pq-padding-len", &TestConfig::dummy_pq_padding_len },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800205};
206
David Benjaminc895d6b2016-08-11 13:26:41 -0400207const Flag<std::vector<int>> kIntVectorFlags[] = {
208 { "-signing-prefs", &TestConfig::signing_prefs },
Robert Sloan572a4e22017-04-17 10:52:19 -0700209 { "-verify-prefs", &TestConfig::verify_prefs },
David Benjaminc895d6b2016-08-11 13:26:41 -0400210};
211
Robert Sloane56da3e2017-06-26 08:26:42 -0700212bool ParseFlag(char *flag, int argc, char **argv, int *i,
213 bool skip, TestConfig *out_config) {
214 bool *bool_field = FindField(out_config, kBoolFlags, flag);
215 if (bool_field != NULL) {
216 if (!skip) {
217 *bool_field = true;
218 }
219 return true;
220 }
221
222 std::string *string_field = FindField(out_config, kStringFlags, flag);
223 if (string_field != NULL) {
224 *i = *i + 1;
225 if (*i >= argc) {
226 fprintf(stderr, "Missing parameter\n");
227 return false;
228 }
229 if (!skip) {
230 string_field->assign(argv[*i]);
231 }
232 return true;
233 }
234
235 std::string *base64_field = FindField(out_config, kBase64Flags, flag);
236 if (base64_field != NULL) {
237 *i = *i + 1;
238 if (*i >= argc) {
239 fprintf(stderr, "Missing parameter\n");
240 return false;
241 }
242 size_t len;
243 if (!EVP_DecodedLength(&len, strlen(argv[*i]))) {
244 fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
245 return false;
246 }
247 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
248 if (!EVP_DecodeBase64(decoded.get(), &len, len,
249 reinterpret_cast<const uint8_t *>(argv[*i]),
250 strlen(argv[*i]))) {
251 fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
252 return false;
253 }
254 if (!skip) {
255 base64_field->assign(reinterpret_cast<const char *>(decoded.get()),
256 len);
257 }
258 return true;
259 }
260
261 int *int_field = FindField(out_config, kIntFlags, flag);
262 if (int_field) {
263 *i = *i + 1;
264 if (*i >= argc) {
265 fprintf(stderr, "Missing parameter\n");
266 return false;
267 }
268 if (!skip) {
269 *int_field = atoi(argv[*i]);
270 }
271 return true;
272 }
273
274 std::vector<int> *int_vector_field =
275 FindField(out_config, kIntVectorFlags, flag);
276 if (int_vector_field) {
277 *i = *i + 1;
278 if (*i >= argc) {
279 fprintf(stderr, "Missing parameter\n");
280 return false;
281 }
282
283 // Each instance of the flag adds to the list.
284 if (!skip) {
285 int_vector_field->push_back(atoi(argv[*i]));
286 }
287 return true;
288 }
289
290 fprintf(stderr, "Unknown argument: %s\n", flag);
291 return false;
292}
293
Robert Sloan8ff03552017-06-14 12:40:58 -0700294const char kInit[] = "-on-initial";
295const char kResume[] = "-on-resume";
Robert Sloane56da3e2017-06-26 08:26:42 -0700296const char kRetry[] = "-on-retry";
Robert Sloan8ff03552017-06-14 12:40:58 -0700297
Adam Langleyd9e397b2015-01-22 14:27:53 -0800298} // namespace
299
Robert Sloane56da3e2017-06-26 08:26:42 -0700300bool ParseConfig(int argc, char **argv,
301 TestConfig *out_initial,
302 TestConfig *out_resume,
303 TestConfig *out_retry) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800304 for (int i = 0; i < argc; i++) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700305 bool skip = false;
306 char *flag = argv[i];
Robert Sloane56da3e2017-06-26 08:26:42 -0700307 if (strncmp(flag, kInit, strlen(kInit)) == 0) {
308 if (!ParseFlag(flag + strlen(kInit), argc, argv, &i, skip, out_initial)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800309 return false;
310 }
Robert Sloane56da3e2017-06-26 08:26:42 -0700311 } else if (strncmp(flag, kResume, strlen(kResume)) == 0) {
312 if (!ParseFlag(flag + strlen(kResume), argc, argv, &i, skip,
313 out_resume)) {
314 return false;
Robert Sloan8ff03552017-06-14 12:40:58 -0700315 }
Robert Sloane56da3e2017-06-26 08:26:42 -0700316 } else if (strncmp(flag, kRetry, strlen(kRetry)) == 0) {
317 if (!ParseFlag(flag + strlen(kRetry), argc, argv, &i, skip, out_retry)) {
318 return false;
319 }
320 } else {
321 int i_init = i;
322 int i_resume = i;
323 if (!ParseFlag(flag, argc, argv, &i_init, skip, out_initial) ||
324 !ParseFlag(flag, argc, argv, &i_resume, skip, out_resume) ||
325 !ParseFlag(flag, argc, argv, &i, skip, out_retry)) {
326 return false;
327 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800328 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329 }
330
331 return true;
332}