blob: f925504dc692a93c890531270ef597273654a6d6 [file] [log] [blame]
David Benjamin5a593af2014-08-11 19:51:50 -04001/* 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
Ben Laurieeba23842014-09-30 12:44:15 +010017#include <stdio.h>
David Benjamin1eb367c2014-12-12 18:17:51 -050018#include <stdlib.h>
David Benjamin5a593af2014-08-11 19:51:50 -040019#include <string.h>
20
David Benjamin2561dc32014-08-24 01:25:27 -040021#include <memory>
22
23#include <openssl/base64.h>
David Benjamin5a593af2014-08-11 19:51:50 -040024
25namespace {
26
David Benjamin1eb367c2014-12-12 18:17:51 -050027template <typename T>
28struct Flag {
David Benjamin5a593af2014-08-11 19:51:50 -040029 const char *flag;
David Benjamin1eb367c2014-12-12 18:17:51 -050030 T TestConfig::*member;
David Benjamin5a593af2014-08-11 19:51:50 -040031};
32
David Benjamin1eb367c2014-12-12 18:17:51 -050033// 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}
David Benjamin5a593af2014-08-11 19:51:50 -040045
David Benjamin1eb367c2014-12-12 18:17:51 -050046const Flag<bool> kBoolFlags[] = {
David Benjamin5a593af2014-08-11 19:51:50 -040047 { "-server", &TestConfig::is_server },
David Benjamin6fd297b2014-08-11 18:43:38 -040048 { "-dtls", &TestConfig::is_dtls },
David Benjamin5a593af2014-08-11 19:51:50 -040049 { "-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 },
Steven Valdez4f94b1c2016-05-24 12:31:07 -040058 { "-no-tls13", &TestConfig::no_tls13 },
David Benjamin5a593af2014-08-11 19:51:50 -040059 { "-no-tls12", &TestConfig::no_tls12 },
60 { "-no-tls11", &TestConfig::no_tls11 },
61 { "-no-tls1", &TestConfig::no_tls1 },
62 { "-no-ssl3", &TestConfig::no_ssl3 },
Steven Valdez143e8b32016-07-11 13:19:03 -040063 { "-enable-channel-id", &TestConfig::enable_channel_id },
David Benjamine58c4f52014-08-24 03:47:07 -040064 { "-shim-writes-first", &TestConfig::shim_writes_first },
David Benjamin01fe8202014-09-24 15:21:44 -040065 { "-expect-session-miss", &TestConfig::expect_session_miss },
David Benjamin594e7d22016-03-17 17:49:56 -040066 { "-decline-alpn", &TestConfig::decline_alpn },
Adam Langley75712922014-10-10 16:23:43 -070067 { "-expect-extended-master-secret",
68 &TestConfig::expect_extended_master_secret },
David Benjamin61f95272014-11-25 01:55:35 -050069 { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
70 { "-enable-signed-cert-timestamps",
71 &TestConfig::enable_signed_cert_timestamps },
David Benjamine0e7d0d2015-02-08 19:33:25 -050072 { "-implicit-handshake", &TestConfig::implicit_handshake },
David Benjamin6f5c0f42015-02-24 01:23:21 -050073 { "-use-early-callback", &TestConfig::use_early_callback },
74 { "-fail-early-callback", &TestConfig::fail_early_callback },
Adam Langley524e7172015-02-20 16:04:00 -080075 { "-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 },
David Benjaminb8d74f52016-11-14 22:02:50 +090078 { "-fail-cert-callback", &TestConfig::fail_cert_callback },
David Benjamin87e4acd2015-04-02 19:57:35 -040079 { "-handshake-never-done", &TestConfig::handshake_never_done },
David Benjaminc565ebb2015-04-03 04:06:36 -040080 { "-use-export-context", &TestConfig::use_export_context },
Adam Langleyaf0e32c2015-06-03 09:57:23 -070081 { "-tls-unique", &TestConfig::tls_unique },
David Benjaminba4594a2015-06-18 18:36:15 -040082 { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
83 { "-expect-no-session", &TestConfig::expect_no_session },
Steven Valdez08b65f42016-12-07 15:29:45 -050084 { "-expect-early-data-info", &TestConfig::expect_early_data_info },
David Benjamind98452d2015-06-16 14:16:23 -040085 { "-use-ticket-callback", &TestConfig::use_ticket_callback },
86 { "-renew-ticket", &TestConfig::renew_ticket },
Steven Valdez08b65f42016-12-07 15:29:45 -050087 { "-enable-early-data", &TestConfig::enable_early_data },
Adam Langley09505632015-07-30 18:10:13 -070088 { "-enable-client-custom-extension",
89 &TestConfig::enable_client_custom_extension },
90 { "-enable-server-custom-extension",
91 &TestConfig::enable_server_custom_extension },
92 { "-custom-extension-skip", &TestConfig::custom_extension_skip },
93 { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
David Benjamin30789da2015-08-29 22:56:45 -040094 { "-check-close-notify", &TestConfig::check_close_notify },
95 { "-shim-shuts-down", &TestConfig::shim_shuts_down },
Paul Lietar8f1c2682015-08-18 12:21:54 +010096 { "-verify-fail", &TestConfig::verify_fail },
97 { "-verify-peer", &TestConfig::verify_peer },
David Benjaminc3648fa2017-07-01 10:50:56 -040098 { "-verify-peer-if-no-obc", &TestConfig::verify_peer_if_no_obc },
David Benjamin1d5ef3b2015-10-12 19:54:18 -040099 { "-expect-verify-result", &TestConfig::expect_verify_result },
100 { "-renegotiate-once", &TestConfig::renegotiate_once },
101 { "-renegotiate-freely", &TestConfig::renegotiate_freely },
Adam Langley27a0d082015-11-03 13:34:10 -0800102 { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
David Benjamin99fdfb92015-11-02 12:11:35 -0500103 { "-p384-only", &TestConfig::p384_only },
David Benjamin8c2b3bf2015-12-18 20:55:44 -0500104 { "-enable-all-curves", &TestConfig::enable_all_curves },
David Benjaminacb6dcc2016-03-10 09:15:01 -0500105 { "-use-old-client-cert-callback",
106 &TestConfig::use_old_client_cert_callback },
David Benjamin1d4f4c02016-07-26 18:03:08 -0400107 { "-send-alert", &TestConfig::send_alert },
David Benjaminf3fbade2016-09-19 13:08:16 -0400108 { "-peek-then-read", &TestConfig::peek_then_read },
David Benjamin65ac9972016-09-02 21:35:25 -0400109 { "-enable-grease", &TestConfig::enable_grease },
David Benjamin7bb1d292016-11-01 19:45:06 -0400110 { "-use-exporter-between-reads", &TestConfig::use_exporter_between_reads },
David Benjaminbbaf3672016-11-17 10:53:09 +0900111 { "-retain-only-sha256-client-cert-initial",
112 &TestConfig::retain_only_sha256_client_cert_initial },
113 { "-retain-only-sha256-client-cert-resume",
114 &TestConfig::retain_only_sha256_client_cert_resume },
115 { "-expect-sha256-client-cert-initial",
116 &TestConfig::expect_sha256_client_cert_initial },
117 { "-expect-sha256-client-cert-resume",
118 &TestConfig::expect_sha256_client_cert_resume },
David Benjamina1eaba12017-01-01 23:19:22 -0500119 { "-read-with-unfinished-write", &TestConfig::read_with_unfinished_write },
David Benjamind2610042017-01-03 10:49:28 -0500120 { "-expect-secure-renegotiation",
121 &TestConfig::expect_secure_renegotiation },
122 { "-expect-no-secure-renegotiation",
123 &TestConfig::expect_no_secure_renegotiation },
David Benjaminb5c58db2017-01-28 01:39:29 -0500124 { "-expect-session-id", &TestConfig::expect_session_id },
125 { "-expect-no-session-id", &TestConfig::expect_no_session_id },
Steven Valdez2d850622017-01-11 11:34:52 -0500126 { "-expect-accept-early-data", &TestConfig::expect_accept_early_data },
127 { "-expect-reject-early-data", &TestConfig::expect_reject_early_data },
David Benjamin8c26d752017-03-26 15:13:51 -0500128 { "-no-op-extra-handshake", &TestConfig::no_op_extra_handshake },
129 { "-handshake-twice", &TestConfig::handshake_twice },
David Benjaminc8ff30c2017-04-04 13:52:36 -0400130 { "-allow-unknown-alpn-protos", &TestConfig::allow_unknown_alpn_protos },
David Benjamin69522112017-03-28 15:38:29 -0500131 { "-enable-ed25519", &TestConfig::enable_ed25519 },
David Benjamin5a593af2014-08-11 19:51:50 -0400132};
133
David Benjamin1eb367c2014-12-12 18:17:51 -0500134const Flag<std::string> kStringFlags[] = {
David Benjamin0fde2eb2017-06-30 19:11:22 -0400135 { "-write-settings", &TestConfig::write_settings },
Steven Valdez0d62f262015-09-04 12:41:04 -0400136 { "-digest-prefs", &TestConfig::digest_prefs },
David Benjamin5a593af2014-08-11 19:51:50 -0400137 { "-key-file", &TestConfig::key_file },
138 { "-cert-file", &TestConfig::cert_file },
139 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -0400140 { "-advertise-npn", &TestConfig::advertise_npn },
141 { "-expect-next-proto", &TestConfig::expected_next_proto },
142 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -0400143 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamine78bfde2014-09-06 12:45:15 -0400144 { "-host-name", &TestConfig::host_name },
David Benjaminae2888f2014-09-06 12:58:58 -0400145 { "-advertise-alpn", &TestConfig::advertise_alpn },
146 { "-expect-alpn", &TestConfig::expected_alpn },
Steven Valdeze831a812017-03-09 14:56:07 -0500147 { "-expect-late-alpn", &TestConfig::expected_late_alpn },
David Benjaminae2888f2014-09-06 12:58:58 -0400148 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
149 { "-select-alpn", &TestConfig::select_alpn },
David Benjamin48cae082014-10-27 01:06:24 -0400150 { "-psk", &TestConfig::psk },
151 { "-psk-identity", &TestConfig::psk_identity },
David Benjaminca6c8262014-11-15 19:06:08 -0500152 { "-srtp-profiles", &TestConfig::srtp_profiles },
David Benjamin67d1fb52015-03-16 15:16:23 -0400153 { "-cipher", &TestConfig::cipher },
David Benjaminc565ebb2015-04-03 04:06:36 -0400154 { "-export-label", &TestConfig::export_label },
155 { "-export-context", &TestConfig::export_context },
David Benjamin2c516452016-11-15 10:16:54 +0900156 { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
Adam Langley2ff79332017-02-28 13:45:39 -0800157 { "-use-client-ca-list", &TestConfig::use_client_ca_list },
158 { "-expect-client-ca-list", &TestConfig::expected_client_ca_list },
David Benjamin5a593af2014-08-11 19:51:50 -0400159};
160
David Benjamin1eb367c2014-12-12 18:17:51 -0500161const Flag<std::string> kBase64Flags[] = {
David Benjamin2561dc32014-08-24 01:25:27 -0400162 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -0400163 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin61f95272014-11-25 01:55:35 -0500164 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
165 { "-expect-signed-cert-timestamps",
166 &TestConfig::expected_signed_cert_timestamps },
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100167 { "-ocsp-response", &TestConfig::ocsp_response },
Paul Lietar4fac72e2015-09-09 13:44:55 +0100168 { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
David Benjamin4199b0d2016-11-01 13:58:25 -0400169 { "-ticket-key", &TestConfig::ticket_key },
David Benjamin2561dc32014-08-24 01:25:27 -0400170};
171
David Benjamin1eb367c2014-12-12 18:17:51 -0500172const Flag<int> kIntFlags[] = {
David Benjamin87c8a642015-02-21 01:54:29 -0500173 { "-port", &TestConfig::port },
David Benjamin46662482016-08-17 00:51:00 -0400174 { "-resume-count", &TestConfig::resume_count },
David Benjamin1eb367c2014-12-12 18:17:51 -0500175 { "-min-version", &TestConfig::min_version },
176 { "-max-version", &TestConfig::max_version },
David Benjamind9cbb532017-07-07 13:17:19 -0400177 { "-expect-version", &TestConfig::expect_version },
David Benjamin13be1de2015-01-11 16:29:36 -0500178 { "-mtu", &TestConfig::mtu },
David Benjaminc565ebb2015-04-03 04:06:36 -0400179 { "-export-keying-material", &TestConfig::export_keying_material },
David Benjamin324dce42015-10-12 19:49:00 -0400180 { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
Nick Harper60edffd2016-06-21 15:19:24 -0700181 { "-expect-peer-signature-algorithm",
182 &TestConfig::expect_peer_signature_algorithm },
David Benjamin9e68f192016-06-30 14:55:33 -0400183 { "-expect-curve-id", &TestConfig::expect_curve_id },
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700184 { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
David Benjamina252b342016-09-26 19:57:53 -0400185 { "-max-cert-list", &TestConfig::max_cert_list },
David Benjaminabbbee12016-10-31 19:20:42 -0400186 { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
187 { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
Steven Valdeza833c352016-11-01 13:39:36 -0400188 { "-resumption-delay", &TestConfig::resumption_delay },
David Benjamine3fbb362017-01-06 16:19:28 -0500189 { "-max-send-fragment", &TestConfig::max_send_fragment },
190 { "-read-size", &TestConfig::read_size },
David Benjamin35ac5b72017-03-03 15:05:56 -0500191 { "-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew },
Steven Valdez520e1222017-06-13 12:45:25 -0400192 { "-tls13-variant", &TestConfig::tls13_variant },
David Benjamin1eb367c2014-12-12 18:17:51 -0500193};
David Benjamin2561dc32014-08-24 01:25:27 -0400194
David Benjaminca3d5452016-07-14 12:51:01 -0400195const Flag<std::vector<int>> kIntVectorFlags[] = {
196 { "-signing-prefs", &TestConfig::signing_prefs },
David Benjamin71c21b42017-04-14 17:05:40 -0400197 { "-verify-prefs", &TestConfig::verify_prefs },
David Benjaminca3d5452016-07-14 12:51:01 -0400198};
199
Steven Valdeze831a812017-03-09 14:56:07 -0500200bool ParseFlag(char *flag, int argc, char **argv, int *i,
201 bool skip, TestConfig *out_config) {
202 bool *bool_field = FindField(out_config, kBoolFlags, flag);
203 if (bool_field != NULL) {
204 if (!skip) {
205 *bool_field = true;
206 }
207 return true;
208 }
209
210 std::string *string_field = FindField(out_config, kStringFlags, flag);
211 if (string_field != NULL) {
212 *i = *i + 1;
213 if (*i >= argc) {
214 fprintf(stderr, "Missing parameter\n");
215 return false;
216 }
217 if (!skip) {
218 string_field->assign(argv[*i]);
219 }
220 return true;
221 }
222
223 std::string *base64_field = FindField(out_config, kBase64Flags, flag);
224 if (base64_field != NULL) {
225 *i = *i + 1;
226 if (*i >= argc) {
227 fprintf(stderr, "Missing parameter\n");
228 return false;
229 }
230 size_t len;
231 if (!EVP_DecodedLength(&len, strlen(argv[*i]))) {
232 fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
233 return false;
234 }
235 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
236 if (!EVP_DecodeBase64(decoded.get(), &len, len,
237 reinterpret_cast<const uint8_t *>(argv[*i]),
238 strlen(argv[*i]))) {
239 fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
240 return false;
241 }
242 if (!skip) {
243 base64_field->assign(reinterpret_cast<const char *>(decoded.get()),
244 len);
245 }
246 return true;
247 }
248
249 int *int_field = FindField(out_config, kIntFlags, flag);
250 if (int_field) {
251 *i = *i + 1;
252 if (*i >= argc) {
253 fprintf(stderr, "Missing parameter\n");
254 return false;
255 }
256 if (!skip) {
257 *int_field = atoi(argv[*i]);
258 }
259 return true;
260 }
261
262 std::vector<int> *int_vector_field =
263 FindField(out_config, kIntVectorFlags, flag);
264 if (int_vector_field) {
265 *i = *i + 1;
266 if (*i >= argc) {
267 fprintf(stderr, "Missing parameter\n");
268 return false;
269 }
270
271 // Each instance of the flag adds to the list.
272 if (!skip) {
273 int_vector_field->push_back(atoi(argv[*i]));
274 }
275 return true;
276 }
277
278 fprintf(stderr, "Unknown argument: %s\n", flag);
279 return false;
280}
281
Steven Valdez873ebc92017-05-09 12:12:58 -0400282const char kInit[] = "-on-initial";
283const char kResume[] = "-on-resume";
Steven Valdeze831a812017-03-09 14:56:07 -0500284const char kRetry[] = "-on-retry";
Steven Valdez873ebc92017-05-09 12:12:58 -0400285
David Benjamin5a593af2014-08-11 19:51:50 -0400286} // namespace
287
Steven Valdeze831a812017-03-09 14:56:07 -0500288bool ParseConfig(int argc, char **argv,
289 TestConfig *out_initial,
290 TestConfig *out_resume,
291 TestConfig *out_retry) {
David Benjamin5a593af2014-08-11 19:51:50 -0400292 for (int i = 0; i < argc; i++) {
Steven Valdez873ebc92017-05-09 12:12:58 -0400293 bool skip = false;
294 char *flag = argv[i];
Steven Valdeze831a812017-03-09 14:56:07 -0500295 if (strncmp(flag, kInit, strlen(kInit)) == 0) {
296 if (!ParseFlag(flag + strlen(kInit), argc, argv, &i, skip, out_initial)) {
David Benjamin5a593af2014-08-11 19:51:50 -0400297 return false;
298 }
Steven Valdeze831a812017-03-09 14:56:07 -0500299 } else if (strncmp(flag, kResume, strlen(kResume)) == 0) {
300 if (!ParseFlag(flag + strlen(kResume), argc, argv, &i, skip,
301 out_resume)) {
302 return false;
Steven Valdez873ebc92017-05-09 12:12:58 -0400303 }
Steven Valdeze831a812017-03-09 14:56:07 -0500304 } else if (strncmp(flag, kRetry, strlen(kRetry)) == 0) {
305 if (!ParseFlag(flag + strlen(kRetry), argc, argv, &i, skip, out_retry)) {
306 return false;
307 }
308 } else {
309 int i_init = i;
310 int i_resume = i;
311 if (!ParseFlag(flag, argc, argv, &i_init, skip, out_initial) ||
312 !ParseFlag(flag, argc, argv, &i_resume, skip, out_resume) ||
313 !ParseFlag(flag, argc, argv, &i, skip, out_retry)) {
314 return false;
315 }
David Benjamin5a593af2014-08-11 19:51:50 -0400316 }
David Benjamin5a593af2014-08-11 19:51:50 -0400317 }
318
319 return true;
320}