blob: 470f034e86034383149b12f1b5fd396c4b859ddb [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 Benjamin87e4acd2015-04-02 19:57:35 -040078 { "-handshake-never-done", &TestConfig::handshake_never_done },
David Benjaminc565ebb2015-04-03 04:06:36 -040079 { "-use-export-context", &TestConfig::use_export_context },
Adam Langleyaf0e32c2015-06-03 09:57:23 -070080 { "-tls-unique", &TestConfig::tls_unique },
David Benjaminba4594a2015-06-18 18:36:15 -040081 { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
82 { "-expect-no-session", &TestConfig::expect_no_session },
David Benjamind98452d2015-06-16 14:16:23 -040083 { "-use-ticket-callback", &TestConfig::use_ticket_callback },
84 { "-renew-ticket", &TestConfig::renew_ticket },
Adam Langley09505632015-07-30 18:10:13 -070085 { "-enable-client-custom-extension",
86 &TestConfig::enable_client_custom_extension },
87 { "-enable-server-custom-extension",
88 &TestConfig::enable_server_custom_extension },
89 { "-custom-extension-skip", &TestConfig::custom_extension_skip },
90 { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
David Benjamin30789da2015-08-29 22:56:45 -040091 { "-check-close-notify", &TestConfig::check_close_notify },
92 { "-shim-shuts-down", &TestConfig::shim_shuts_down },
Paul Lietar8f1c2682015-08-18 12:21:54 +010093 { "-verify-fail", &TestConfig::verify_fail },
94 { "-verify-peer", &TestConfig::verify_peer },
David Benjamin1d5ef3b2015-10-12 19:54:18 -040095 { "-expect-verify-result", &TestConfig::expect_verify_result },
96 { "-renegotiate-once", &TestConfig::renegotiate_once },
97 { "-renegotiate-freely", &TestConfig::renegotiate_freely },
Adam Langley27a0d082015-11-03 13:34:10 -080098 { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
David Benjamin091c4b92015-10-26 13:33:21 -040099 { "-disable-npn", &TestConfig::disable_npn },
David Benjamin99fdfb92015-11-02 12:11:35 -0500100 { "-p384-only", &TestConfig::p384_only },
David Benjamin8c2b3bf2015-12-18 20:55:44 -0500101 { "-enable-all-curves", &TestConfig::enable_all_curves },
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800102 { "-use-sparse-dh-prime", &TestConfig::use_sparse_dh_prime },
David Benjaminacb6dcc2016-03-10 09:15:01 -0500103 { "-use-old-client-cert-callback",
104 &TestConfig::use_old_client_cert_callback },
David Benjamin2f8935d2016-07-13 19:47:39 -0400105 { "-use-null-client-ca-list", &TestConfig::use_null_client_ca_list },
David Benjamin1d4f4c02016-07-26 18:03:08 -0400106 { "-send-alert", &TestConfig::send_alert },
David Benjaminf3fbade2016-09-19 13:08:16 -0400107 { "-peek-then-read", &TestConfig::peek_then_read },
David Benjamin65ac9972016-09-02 21:35:25 -0400108 { "-enable-grease", &TestConfig::enable_grease },
David Benjamin7bb1d292016-11-01 19:45:06 -0400109 { "-use-exporter-between-reads", &TestConfig::use_exporter_between_reads },
David Benjamin5a593af2014-08-11 19:51:50 -0400110};
111
David Benjamin1eb367c2014-12-12 18:17:51 -0500112const Flag<std::string> kStringFlags[] = {
Steven Valdez0d62f262015-09-04 12:41:04 -0400113 { "-digest-prefs", &TestConfig::digest_prefs },
David Benjamin5a593af2014-08-11 19:51:50 -0400114 { "-key-file", &TestConfig::key_file },
115 { "-cert-file", &TestConfig::cert_file },
116 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -0400117 { "-advertise-npn", &TestConfig::advertise_npn },
118 { "-expect-next-proto", &TestConfig::expected_next_proto },
119 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -0400120 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamine78bfde2014-09-06 12:45:15 -0400121 { "-host-name", &TestConfig::host_name },
David Benjaminae2888f2014-09-06 12:58:58 -0400122 { "-advertise-alpn", &TestConfig::advertise_alpn },
123 { "-expect-alpn", &TestConfig::expected_alpn },
124 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
125 { "-select-alpn", &TestConfig::select_alpn },
David Benjamin48cae082014-10-27 01:06:24 -0400126 { "-psk", &TestConfig::psk },
127 { "-psk-identity", &TestConfig::psk_identity },
David Benjaminca6c8262014-11-15 19:06:08 -0500128 { "-srtp-profiles", &TestConfig::srtp_profiles },
David Benjamin67d1fb52015-03-16 15:16:23 -0400129 { "-cipher", &TestConfig::cipher },
Adam Langleycef75832015-09-03 14:51:12 -0700130 { "-cipher-tls10", &TestConfig::cipher_tls10 },
131 { "-cipher-tls11", &TestConfig::cipher_tls11 },
David Benjaminc565ebb2015-04-03 04:06:36 -0400132 { "-export-label", &TestConfig::export_label },
133 { "-export-context", &TestConfig::export_context },
David Benjamin2c516452016-11-15 10:16:54 +0900134 { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
David Benjamin5a593af2014-08-11 19:51:50 -0400135};
136
David Benjamin1eb367c2014-12-12 18:17:51 -0500137const Flag<std::string> kBase64Flags[] = {
David Benjamin2561dc32014-08-24 01:25:27 -0400138 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -0400139 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin61f95272014-11-25 01:55:35 -0500140 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
141 { "-expect-signed-cert-timestamps",
142 &TestConfig::expected_signed_cert_timestamps },
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100143 { "-ocsp-response", &TestConfig::ocsp_response },
Paul Lietar4fac72e2015-09-09 13:44:55 +0100144 { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
David Benjamin4199b0d2016-11-01 13:58:25 -0400145 { "-ticket-key", &TestConfig::ticket_key },
David Benjamin2561dc32014-08-24 01:25:27 -0400146};
147
David Benjamin1eb367c2014-12-12 18:17:51 -0500148const Flag<int> kIntFlags[] = {
David Benjamin87c8a642015-02-21 01:54:29 -0500149 { "-port", &TestConfig::port },
David Benjamin46662482016-08-17 00:51:00 -0400150 { "-resume-count", &TestConfig::resume_count },
David Benjamin1eb367c2014-12-12 18:17:51 -0500151 { "-min-version", &TestConfig::min_version },
152 { "-max-version", &TestConfig::max_version },
David Benjamin13be1de2015-01-11 16:29:36 -0500153 { "-mtu", &TestConfig::mtu },
David Benjaminc565ebb2015-04-03 04:06:36 -0400154 { "-export-keying-material", &TestConfig::export_keying_material },
David Benjamin324dce42015-10-12 19:49:00 -0400155 { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
Nick Harper60edffd2016-06-21 15:19:24 -0700156 { "-expect-peer-signature-algorithm",
157 &TestConfig::expect_peer_signature_algorithm },
David Benjamin9e68f192016-06-30 14:55:33 -0400158 { "-expect-curve-id", &TestConfig::expect_curve_id },
159 { "-expect-dhe-group-size", &TestConfig::expect_dhe_group_size },
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700160 { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
David Benjamina252b342016-09-26 19:57:53 -0400161 { "-max-cert-list", &TestConfig::max_cert_list },
David Benjaminabbbee12016-10-31 19:20:42 -0400162 { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
163 { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
Steven Valdeza833c352016-11-01 13:39:36 -0400164 { "-resumption-delay", &TestConfig::resumption_delay },
David Benjamin1eb367c2014-12-12 18:17:51 -0500165};
David Benjamin2561dc32014-08-24 01:25:27 -0400166
David Benjaminca3d5452016-07-14 12:51:01 -0400167const Flag<std::vector<int>> kIntVectorFlags[] = {
168 { "-signing-prefs", &TestConfig::signing_prefs },
169};
170
David Benjamin5a593af2014-08-11 19:51:50 -0400171} // namespace
172
David Benjamin5a593af2014-08-11 19:51:50 -0400173bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
174 for (int i = 0; i < argc; i++) {
David Benjamin1eb367c2014-12-12 18:17:51 -0500175 bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
176 if (bool_field != NULL) {
177 *bool_field = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400178 continue;
179 }
180
David Benjamin1eb367c2014-12-12 18:17:51 -0500181 std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
182 if (string_field != NULL) {
David Benjamin5a593af2014-08-11 19:51:50 -0400183 i++;
184 if (i >= argc) {
185 fprintf(stderr, "Missing parameter\n");
186 return false;
187 }
David Benjamin1eb367c2014-12-12 18:17:51 -0500188 string_field->assign(argv[i]);
David Benjamin5a593af2014-08-11 19:51:50 -0400189 continue;
190 }
191
David Benjamin1eb367c2014-12-12 18:17:51 -0500192 std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
193 if (base64_field != NULL) {
David Benjamin2561dc32014-08-24 01:25:27 -0400194 i++;
195 if (i >= argc) {
196 fprintf(stderr, "Missing parameter\n");
197 return false;
198 }
199 size_t len;
200 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
201 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
David Benjamin154c2f22016-03-05 11:57:44 -0500202 return false;
David Benjamin2561dc32014-08-24 01:25:27 -0400203 }
204 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
205 if (!EVP_DecodeBase64(decoded.get(), &len, len,
206 reinterpret_cast<const uint8_t *>(argv[i]),
207 strlen(argv[i]))) {
208 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
David Benjamin154c2f22016-03-05 11:57:44 -0500209 return false;
David Benjamin2561dc32014-08-24 01:25:27 -0400210 }
David Benjamin1eb367c2014-12-12 18:17:51 -0500211 base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
212 continue;
213 }
214
215 int *int_field = FindField(out_config, kIntFlags, argv[i]);
216 if (int_field) {
217 i++;
218 if (i >= argc) {
219 fprintf(stderr, "Missing parameter\n");
220 return false;
221 }
222 *int_field = atoi(argv[i]);
David Benjamin2561dc32014-08-24 01:25:27 -0400223 continue;
224 }
225
David Benjaminca3d5452016-07-14 12:51:01 -0400226 std::vector<int> *int_vector_field =
227 FindField(out_config, kIntVectorFlags, argv[i]);
228 if (int_vector_field) {
229 i++;
230 if (i >= argc) {
231 fprintf(stderr, "Missing parameter\n");
232 return false;
233 }
234
235 // Each instance of the flag adds to the list.
236 int_vector_field->push_back(atoi(argv[i]));
237 continue;
238 }
239
David Benjamin5a593af2014-08-11 19:51:50 -0400240 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
241 return false;
242 }
243
244 return true;
245}