blob: c032d9673f44e0bbf91fbe27f35c71e8d9473084 [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 Benjamin6fd297b2014-08-11 18:43:38 -040063 { "-cookie-exchange", &TestConfig::cookie_exchange },
David Benjamine58c4f52014-08-24 03:47:07 -040064 { "-shim-writes-first", &TestConfig::shim_writes_first },
David Benjamin5c24a1d2014-08-31 00:59:27 -040065 { "-tls-d5-bug", &TestConfig::tls_d5_bug },
David Benjamin01fe8202014-09-24 15:21:44 -040066 { "-expect-session-miss", &TestConfig::expect_session_miss },
Adam Langley75712922014-10-10 16:23:43 -070067 { "-expect-extended-master-secret",
68 &TestConfig::expect_extended_master_secret },
Adam Langley2ae77d22014-10-28 17:29:33 -070069 { "-renegotiate", &TestConfig::renegotiate },
David Benjaminca6554b2014-11-08 12:31:52 -050070 { "-allow-unsafe-legacy-renegotiation",
71 &TestConfig::allow_unsafe_legacy_renegotiation },
David Benjamin61f95272014-11-25 01:55:35 -050072 { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
73 { "-enable-signed-cert-timestamps",
74 &TestConfig::enable_signed_cert_timestamps },
Feng Lu41aa3252014-11-21 22:47:56 -080075 { "-fastradio-padding", &TestConfig::fastradio_padding },
David Benjamin5a593af2014-08-11 19:51:50 -040076};
77
David Benjamin1eb367c2014-12-12 18:17:51 -050078const Flag<std::string> kStringFlags[] = {
David Benjamin5a593af2014-08-11 19:51:50 -040079 { "-key-file", &TestConfig::key_file },
80 { "-cert-file", &TestConfig::cert_file },
81 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -040082 { "-advertise-npn", &TestConfig::advertise_npn },
83 { "-expect-next-proto", &TestConfig::expected_next_proto },
84 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -040085 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamine78bfde2014-09-06 12:45:15 -040086 { "-host-name", &TestConfig::host_name },
David Benjaminae2888f2014-09-06 12:58:58 -040087 { "-advertise-alpn", &TestConfig::advertise_alpn },
88 { "-expect-alpn", &TestConfig::expected_alpn },
89 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
90 { "-select-alpn", &TestConfig::select_alpn },
David Benjamin48cae082014-10-27 01:06:24 -040091 { "-psk", &TestConfig::psk },
92 { "-psk-identity", &TestConfig::psk_identity },
David Benjaminca6c8262014-11-15 19:06:08 -050093 { "-srtp-profiles", &TestConfig::srtp_profiles },
David Benjamin5a593af2014-08-11 19:51:50 -040094};
95
David Benjamin1eb367c2014-12-12 18:17:51 -050096const Flag<std::string> kBase64Flags[] = {
David Benjamin2561dc32014-08-24 01:25:27 -040097 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -040098 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin61f95272014-11-25 01:55:35 -050099 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
100 { "-expect-signed-cert-timestamps",
101 &TestConfig::expected_signed_cert_timestamps },
David Benjamin2561dc32014-08-24 01:25:27 -0400102};
103
David Benjamin1eb367c2014-12-12 18:17:51 -0500104const Flag<int> kIntFlags[] = {
105 { "-min-version", &TestConfig::min_version },
106 { "-max-version", &TestConfig::max_version },
David Benjamin13be1de2015-01-11 16:29:36 -0500107 { "-mtu", &TestConfig::mtu },
David Benjamin1eb367c2014-12-12 18:17:51 -0500108};
David Benjamin2561dc32014-08-24 01:25:27 -0400109
David Benjamin5a593af2014-08-11 19:51:50 -0400110} // namespace
111
112TestConfig::TestConfig()
113 : is_server(false),
David Benjamin6fd297b2014-08-11 18:43:38 -0400114 is_dtls(false),
David Benjamin5a593af2014-08-11 19:51:50 -0400115 resume(false),
116 fallback_scsv(false),
117 require_any_client_certificate(false),
118 false_start(false),
119 async(false),
120 write_different_record_sizes(false),
121 cbc_record_splitting(false),
122 partial_write(false),
123 no_tls12(false),
124 no_tls11(false),
125 no_tls1(false),
David Benjamin6fd297b2014-08-11 18:43:38 -0400126 no_ssl3(false),
David Benjamine58c4f52014-08-24 03:47:07 -0400127 cookie_exchange(false),
David Benjamin5c24a1d2014-08-31 00:59:27 -0400128 shim_writes_first(false),
David Benjamin01fe8202014-09-24 15:21:44 -0400129 tls_d5_bug(false),
Adam Langley75712922014-10-10 16:23:43 -0700130 expect_session_miss(false),
Adam Langley2ae77d22014-10-28 17:29:33 -0700131 expect_extended_master_secret(false),
David Benjaminca6554b2014-11-08 12:31:52 -0500132 renegotiate(false),
David Benjamin61f95272014-11-25 01:55:35 -0500133 allow_unsafe_legacy_renegotiation(false),
134 enable_ocsp_stapling(false),
Feng Lu41aa3252014-11-21 22:47:56 -0800135 enable_signed_cert_timestamps(false),
David Benjamin1eb367c2014-12-12 18:17:51 -0500136 fastradio_padding(false),
137 min_version(0),
David Benjamin13be1de2015-01-11 16:29:36 -0500138 max_version(0),
139 mtu(0) {
David Benjamin5a593af2014-08-11 19:51:50 -0400140}
141
142bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
143 for (int i = 0; i < argc; i++) {
David Benjamin1eb367c2014-12-12 18:17:51 -0500144 bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
145 if (bool_field != NULL) {
146 *bool_field = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400147 continue;
148 }
149
David Benjamin1eb367c2014-12-12 18:17:51 -0500150 std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
151 if (string_field != NULL) {
David Benjamin5a593af2014-08-11 19:51:50 -0400152 i++;
153 if (i >= argc) {
154 fprintf(stderr, "Missing parameter\n");
155 return false;
156 }
David Benjamin1eb367c2014-12-12 18:17:51 -0500157 string_field->assign(argv[i]);
David Benjamin5a593af2014-08-11 19:51:50 -0400158 continue;
159 }
160
David Benjamin1eb367c2014-12-12 18:17:51 -0500161 std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
162 if (base64_field != NULL) {
David Benjamin2561dc32014-08-24 01:25:27 -0400163 i++;
164 if (i >= argc) {
165 fprintf(stderr, "Missing parameter\n");
166 return false;
167 }
168 size_t len;
169 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
170 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
171 }
172 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
173 if (!EVP_DecodeBase64(decoded.get(), &len, len,
174 reinterpret_cast<const uint8_t *>(argv[i]),
175 strlen(argv[i]))) {
176 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
177 }
David Benjamin1eb367c2014-12-12 18:17:51 -0500178 base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
179 continue;
180 }
181
182 int *int_field = FindField(out_config, kIntFlags, argv[i]);
183 if (int_field) {
184 i++;
185 if (i >= argc) {
186 fprintf(stderr, "Missing parameter\n");
187 return false;
188 }
189 *int_field = atoi(argv[i]);
David Benjamin2561dc32014-08-24 01:25:27 -0400190 continue;
191 }
192
David Benjamin5a593af2014-08-11 19:51:50 -0400193 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
194 return false;
195 }
196
197 return true;
198}