blob: 5c5d34672fc78c4d42bcb720e1308c3b426dce1d [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 Sloan69939df2017-01-09 10:53:07 -080084 { "-expect-early-data-info", &TestConfig::expect_early_data_info },
Kenny Rootb8494592015-09-25 02:29:14 +000085 { "-use-ticket-callback", &TestConfig::use_ticket_callback },
86 { "-renew-ticket", &TestConfig::renew_ticket },
Robert Sloan69939df2017-01-09 10:53:07 -080087 { "-enable-early-data", &TestConfig::enable_early_data },
Robert Sloan6d0d00e2017-03-27 07:13:07 -070088 { "-enable-resume-early-data", &TestConfig::enable_resume_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 },
Kenny Roote99801b2015-11-06 15:31:15 -080099 { "-expect-verify-result", &TestConfig::expect_verify_result },
100 { "-renegotiate-once", &TestConfig::renegotiate_once },
101 { "-renegotiate-freely", &TestConfig::renegotiate_freely },
Adam Langleyfad63272015-11-12 12:15:39 -0800102 { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
Adam Langley4139edb2016-01-13 15:00:54 -0800103 { "-p384-only", &TestConfig::p384_only },
104 { "-enable-all-curves", &TestConfig::enable_all_curves },
David Benjamin4969cc92016-04-22 15:02:23 -0400105 { "-use-old-client-cert-callback",
106 &TestConfig::use_old_client_cert_callback },
David Benjaminc895d6b2016-08-11 13:26:41 -0400107 { "-send-alert", &TestConfig::send_alert },
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400108 { "-peek-then-read", &TestConfig::peek_then_read },
109 { "-enable-grease", &TestConfig::enable_grease },
Steven Valdez909b19f2016-11-21 15:35:44 -0500110 { "-use-exporter-between-reads", &TestConfig::use_exporter_between_reads },
111 { "-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 },
Robert Sloan69939df2017-01-09 10:53:07 -0800119 { "-read-with-unfinished-write", &TestConfig::read_with_unfinished_write },
Steven Valdezb0b45c62017-01-17 16:23:54 -0500120 { "-expect-secure-renegotiation",
121 &TestConfig::expect_secure_renegotiation },
122 { "-expect-no-secure-renegotiation",
123 &TestConfig::expect_no_secure_renegotiation },
Robert Sloan4d1ac502017-02-06 08:36:14 -0800124 { "-expect-session-id", &TestConfig::expect_session_id },
125 { "-expect-no-session-id", &TestConfig::expect_no_session_id },
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700126 { "-expect-accept-early-data", &TestConfig::expect_accept_early_data },
127 { "-expect-reject-early-data", &TestConfig::expect_reject_early_data },
128 { "-expect-no-alpn", &TestConfig::expect_no_alpn },
129 { "-expect-no-resume-alpn", &TestConfig::expect_no_resume_alpn },
Robert Sloan6f79a502017-04-03 09:16:40 -0700130 { "-no-op-extra-handshake", &TestConfig::no_op_extra_handshake },
131 { "-handshake-twice", &TestConfig::handshake_twice },
Robert Sloan572a4e22017-04-17 10:52:19 -0700132 { "-allow-unknown-alpn-protos", &TestConfig::allow_unknown_alpn_protos },
133 { "-enable-ed25519", &TestConfig::enable_ed25519 },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800134};
135
136const Flag<std::string> kStringFlags[] = {
Kenny Rootb8494592015-09-25 02:29:14 +0000137 { "-digest-prefs", &TestConfig::digest_prefs },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800138 { "-key-file", &TestConfig::key_file },
139 { "-cert-file", &TestConfig::cert_file },
140 { "-expect-server-name", &TestConfig::expected_server_name },
141 { "-advertise-npn", &TestConfig::advertise_npn },
142 { "-expect-next-proto", &TestConfig::expected_next_proto },
143 { "-select-next-proto", &TestConfig::select_next_proto },
144 { "-send-channel-id", &TestConfig::send_channel_id },
145 { "-host-name", &TestConfig::host_name },
146 { "-advertise-alpn", &TestConfig::advertise_alpn },
147 { "-expect-alpn", &TestConfig::expected_alpn },
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700148 { "-expect-resume-alpn", &TestConfig::expected_resume_alpn },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800149 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
150 { "-select-alpn", &TestConfig::select_alpn },
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700151 { "-select-resume-alpn", &TestConfig::select_resume_alpn },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152 { "-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 },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800161};
162
163const Flag<std::string> kBase64Flags[] = {
164 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
165 { "-expect-channel-id", &TestConfig::expected_channel_id },
166 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
167 { "-expect-signed-cert-timestamps",
168 &TestConfig::expected_signed_cert_timestamps },
Kenny Rootb8494592015-09-25 02:29:14 +0000169 { "-ocsp-response", &TestConfig::ocsp_response },
170 { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
Steven Valdez909b19f2016-11-21 15:35:44 -0500171 { "-ticket-key", &TestConfig::ticket_key },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172};
173
174const Flag<int> kIntFlags[] = {
Adam Langleye9ada862015-05-11 17:20:37 -0700175 { "-port", &TestConfig::port },
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400176 { "-resume-count", &TestConfig::resume_count },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800177 { "-min-version", &TestConfig::min_version },
178 { "-max-version", &TestConfig::max_version },
179 { "-mtu", &TestConfig::mtu },
Adam Langleye9ada862015-05-11 17:20:37 -0700180 { "-export-keying-material", &TestConfig::export_keying_material },
Kenny Roote99801b2015-11-06 15:31:15 -0800181 { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
David Benjaminc895d6b2016-08-11 13:26:41 -0400182 { "-expect-peer-signature-algorithm",
183 &TestConfig::expect_peer_signature_algorithm },
184 { "-expect-curve-id", &TestConfig::expect_curve_id },
Steven Valdeze7531f02016-12-14 13:29:57 -0500185 { "-expect-resume-curve-id", &TestConfig::expect_resume_curve_id },
David Benjamind316cba2016-06-02 16:17:39 -0400186 { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400187 { "-max-cert-list", &TestConfig::max_cert_list },
Steven Valdez909b19f2016-11-21 15:35:44 -0500188 { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
189 { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
190 { "-resumption-delay", &TestConfig::resumption_delay },
Steven Valdezb0b45c62017-01-17 16:23:54 -0500191 { "-max-send-fragment", &TestConfig::max_send_fragment },
192 { "-read-size", &TestConfig::read_size },
Robert Sloan1c9db532017-03-13 08:03:59 -0700193 { "-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew },
Adam Langleyd9e397b2015-01-22 14:27:53 -0800194};
195
David Benjaminc895d6b2016-08-11 13:26:41 -0400196const Flag<std::vector<int>> kIntVectorFlags[] = {
197 { "-signing-prefs", &TestConfig::signing_prefs },
Robert Sloan572a4e22017-04-17 10:52:19 -0700198 { "-verify-prefs", &TestConfig::verify_prefs },
David Benjaminc895d6b2016-08-11 13:26:41 -0400199};
200
Adam Langleyd9e397b2015-01-22 14:27:53 -0800201} // namespace
202
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
204 for (int i = 0; i < argc; i++) {
205 bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
206 if (bool_field != NULL) {
207 *bool_field = true;
208 continue;
209 }
210
211 std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
212 if (string_field != NULL) {
213 i++;
214 if (i >= argc) {
215 fprintf(stderr, "Missing parameter\n");
216 return false;
217 }
218 string_field->assign(argv[i]);
219 continue;
220 }
221
222 std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
223 if (base64_field != NULL) {
224 i++;
225 if (i >= argc) {
226 fprintf(stderr, "Missing parameter\n");
227 return false;
228 }
229 size_t len;
230 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
231 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
David Benjamin4969cc92016-04-22 15:02:23 -0400232 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 }
234 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
235 if (!EVP_DecodeBase64(decoded.get(), &len, len,
236 reinterpret_cast<const uint8_t *>(argv[i]),
237 strlen(argv[i]))) {
238 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
David Benjamin4969cc92016-04-22 15:02:23 -0400239 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 }
241 base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
242 continue;
243 }
244
245 int *int_field = FindField(out_config, kIntFlags, argv[i]);
246 if (int_field) {
247 i++;
248 if (i >= argc) {
249 fprintf(stderr, "Missing parameter\n");
250 return false;
251 }
252 *int_field = atoi(argv[i]);
253 continue;
254 }
255
David Benjaminc895d6b2016-08-11 13:26:41 -0400256 std::vector<int> *int_vector_field =
257 FindField(out_config, kIntVectorFlags, argv[i]);
258 if (int_vector_field) {
259 i++;
260 if (i >= argc) {
261 fprintf(stderr, "Missing parameter\n");
262 return false;
263 }
264
265 // Each instance of the flag adds to the list.
266 int_vector_field->push_back(atoi(argv[i]));
267 continue;
268 }
269
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
271 return false;
272 }
273
274 return true;
275}