blob: b4da5ce6956d5e4524d7ac8bff075e09cb2701df [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 { "-resume", &TestConfig::resume },
50 { "-fallback-scsv", &TestConfig::fallback_scsv },
51 { "-require-any-client-certificate",
52 &TestConfig::require_any_client_certificate },
53 { "-false-start", &TestConfig::false_start },
54 { "-async", &TestConfig::async },
55 { "-write-different-record-sizes",
56 &TestConfig::write_different_record_sizes },
57 { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
58 { "-partial-write", &TestConfig::partial_write },
59 { "-no-tls12", &TestConfig::no_tls12 },
60 { "-no-tls11", &TestConfig::no_tls11 },
61 { "-no-tls1", &TestConfig::no_tls1 },
62 { "-no-ssl3", &TestConfig::no_ssl3 },
David Benjamine58c4f52014-08-24 03:47:07 -040063 { "-shim-writes-first", &TestConfig::shim_writes_first },
David Benjamin5c24a1d2014-08-31 00:59:27 -040064 { "-tls-d5-bug", &TestConfig::tls_d5_bug },
David Benjamin01fe8202014-09-24 15:21:44 -040065 { "-expect-session-miss", &TestConfig::expect_session_miss },
Adam Langley75712922014-10-10 16:23:43 -070066 { "-expect-extended-master-secret",
67 &TestConfig::expect_extended_master_secret },
David Benjaminca6554b2014-11-08 12:31:52 -050068 { "-allow-unsafe-legacy-renegotiation",
69 &TestConfig::allow_unsafe_legacy_renegotiation },
David Benjamin61f95272014-11-25 01:55:35 -050070 { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
71 { "-enable-signed-cert-timestamps",
72 &TestConfig::enable_signed_cert_timestamps },
Feng Lu41aa3252014-11-21 22:47:56 -080073 { "-fastradio-padding", &TestConfig::fastradio_padding },
David Benjamine0e7d0d2015-02-08 19:33:25 -050074 { "-implicit-handshake", &TestConfig::implicit_handshake },
David Benjamin6f5c0f42015-02-24 01:23:21 -050075 { "-use-early-callback", &TestConfig::use_early_callback },
76 { "-fail-early-callback", &TestConfig::fail_early_callback },
Adam Langley524e7172015-02-20 16:04:00 -080077 { "-install-ddos-callback", &TestConfig::install_ddos_callback },
78 { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
79 { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
David Benjamin87e4acd2015-04-02 19:57:35 -040080 { "-handshake-never-done", &TestConfig::handshake_never_done },
David Benjaminc565ebb2015-04-03 04:06:36 -040081 { "-use-export-context", &TestConfig::use_export_context },
David Benjaminb16346b2015-04-08 19:16:58 -040082 { "-reject-peer-renegotiations", &TestConfig::reject_peer_renegotiations },
David Benjamincff0b902015-05-15 23:09:47 -040083 { "-no-legacy-server-connect", &TestConfig::no_legacy_server_connect },
Adam Langleyaf0e32c2015-06-03 09:57:23 -070084 { "-tls-unique", &TestConfig::tls_unique },
David Benjaminb4d65fd2015-05-29 17:11:21 -040085 { "-use-async-private-key", &TestConfig::use_async_private_key },
David Benjaminba4594a2015-06-18 18:36:15 -040086 { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
87 { "-expect-no-session", &TestConfig::expect_no_session },
David Benjamind98452d2015-06-16 14:16:23 -040088 { "-use-ticket-callback", &TestConfig::use_ticket_callback },
89 { "-renew-ticket", &TestConfig::renew_ticket },
David Benjamin5a593af2014-08-11 19:51:50 -040090};
91
David Benjamin1eb367c2014-12-12 18:17:51 -050092const Flag<std::string> kStringFlags[] = {
David Benjamin5a593af2014-08-11 19:51:50 -040093 { "-key-file", &TestConfig::key_file },
94 { "-cert-file", &TestConfig::cert_file },
95 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -040096 { "-advertise-npn", &TestConfig::advertise_npn },
97 { "-expect-next-proto", &TestConfig::expected_next_proto },
98 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -040099 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamine78bfde2014-09-06 12:45:15 -0400100 { "-host-name", &TestConfig::host_name },
David Benjaminae2888f2014-09-06 12:58:58 -0400101 { "-advertise-alpn", &TestConfig::advertise_alpn },
102 { "-expect-alpn", &TestConfig::expected_alpn },
103 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
104 { "-select-alpn", &TestConfig::select_alpn },
David Benjamin48cae082014-10-27 01:06:24 -0400105 { "-psk", &TestConfig::psk },
106 { "-psk-identity", &TestConfig::psk_identity },
David Benjaminca6c8262014-11-15 19:06:08 -0500107 { "-srtp-profiles", &TestConfig::srtp_profiles },
David Benjamin67d1fb52015-03-16 15:16:23 -0400108 { "-cipher", &TestConfig::cipher },
David Benjaminc565ebb2015-04-03 04:06:36 -0400109 { "-export-label", &TestConfig::export_label },
110 { "-export-context", &TestConfig::export_context },
David Benjamin5a593af2014-08-11 19:51:50 -0400111};
112
David Benjamin1eb367c2014-12-12 18:17:51 -0500113const Flag<std::string> kBase64Flags[] = {
David Benjamin2561dc32014-08-24 01:25:27 -0400114 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -0400115 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin61f95272014-11-25 01:55:35 -0500116 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
117 { "-expect-signed-cert-timestamps",
118 &TestConfig::expected_signed_cert_timestamps },
David Benjamin2561dc32014-08-24 01:25:27 -0400119};
120
David Benjamin1eb367c2014-12-12 18:17:51 -0500121const Flag<int> kIntFlags[] = {
David Benjamin87c8a642015-02-21 01:54:29 -0500122 { "-port", &TestConfig::port },
David Benjamin1eb367c2014-12-12 18:17:51 -0500123 { "-min-version", &TestConfig::min_version },
124 { "-max-version", &TestConfig::max_version },
David Benjamin13be1de2015-01-11 16:29:36 -0500125 { "-mtu", &TestConfig::mtu },
David Benjaminc565ebb2015-04-03 04:06:36 -0400126 { "-export-keying-material", &TestConfig::export_keying_material },
David Benjamin1eb367c2014-12-12 18:17:51 -0500127};
David Benjamin2561dc32014-08-24 01:25:27 -0400128
David Benjamin5a593af2014-08-11 19:51:50 -0400129} // namespace
130
David Benjamin5a593af2014-08-11 19:51:50 -0400131bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
132 for (int i = 0; i < argc; i++) {
David Benjamin1eb367c2014-12-12 18:17:51 -0500133 bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
134 if (bool_field != NULL) {
135 *bool_field = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400136 continue;
137 }
138
David Benjamin1eb367c2014-12-12 18:17:51 -0500139 std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
140 if (string_field != NULL) {
David Benjamin5a593af2014-08-11 19:51:50 -0400141 i++;
142 if (i >= argc) {
143 fprintf(stderr, "Missing parameter\n");
144 return false;
145 }
David Benjamin1eb367c2014-12-12 18:17:51 -0500146 string_field->assign(argv[i]);
David Benjamin5a593af2014-08-11 19:51:50 -0400147 continue;
148 }
149
David Benjamin1eb367c2014-12-12 18:17:51 -0500150 std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
151 if (base64_field != NULL) {
David Benjamin2561dc32014-08-24 01:25:27 -0400152 i++;
153 if (i >= argc) {
154 fprintf(stderr, "Missing parameter\n");
155 return false;
156 }
157 size_t len;
158 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
159 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
160 }
161 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
162 if (!EVP_DecodeBase64(decoded.get(), &len, len,
163 reinterpret_cast<const uint8_t *>(argv[i]),
164 strlen(argv[i]))) {
165 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
166 }
David Benjamin1eb367c2014-12-12 18:17:51 -0500167 base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
168 continue;
169 }
170
171 int *int_field = FindField(out_config, kIntFlags, argv[i]);
172 if (int_field) {
173 i++;
174 if (i >= argc) {
175 fprintf(stderr, "Missing parameter\n");
176 return false;
177 }
178 *int_field = atoi(argv[i]);
David Benjamin2561dc32014-08-24 01:25:27 -0400179 continue;
180 }
181
David Benjamin5a593af2014-08-11 19:51:50 -0400182 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
183 return false;
184 }
185
186 return true;
187}