blob: 59874ef5c5779eb1bf47f026d02dc8831e83e34f [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 Benjamin5a593af2014-08-11 19:51:50 -040018#include <string.h>
19
David Benjamin2561dc32014-08-24 01:25:27 -040020#include <memory>
21
22#include <openssl/base64.h>
David Benjamin5a593af2014-08-11 19:51:50 -040023
24namespace {
25
26typedef bool TestConfig::*BoolMember;
27typedef std::string TestConfig::*StringMember;
28
29struct BoolFlag {
30 const char *flag;
31 BoolMember member;
32};
33
34struct StringFlag {
35 const char *flag;
36 StringMember member;
37};
38
39const BoolFlag kBoolFlags[] = {
40 { "-server", &TestConfig::is_server },
David Benjamin6fd297b2014-08-11 18:43:38 -040041 { "-dtls", &TestConfig::is_dtls },
David Benjamin5a593af2014-08-11 19:51:50 -040042 { "-resume", &TestConfig::resume },
43 { "-fallback-scsv", &TestConfig::fallback_scsv },
44 { "-require-any-client-certificate",
45 &TestConfig::require_any_client_certificate },
46 { "-false-start", &TestConfig::false_start },
47 { "-async", &TestConfig::async },
48 { "-write-different-record-sizes",
49 &TestConfig::write_different_record_sizes },
50 { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
51 { "-partial-write", &TestConfig::partial_write },
52 { "-no-tls12", &TestConfig::no_tls12 },
53 { "-no-tls11", &TestConfig::no_tls11 },
54 { "-no-tls1", &TestConfig::no_tls1 },
55 { "-no-ssl3", &TestConfig::no_ssl3 },
David Benjamin6fd297b2014-08-11 18:43:38 -040056 { "-cookie-exchange", &TestConfig::cookie_exchange },
David Benjamine58c4f52014-08-24 03:47:07 -040057 { "-shim-writes-first", &TestConfig::shim_writes_first },
David Benjamin5c24a1d2014-08-31 00:59:27 -040058 { "-tls-d5-bug", &TestConfig::tls_d5_bug },
David Benjamin01fe8202014-09-24 15:21:44 -040059 { "-expect-session-miss", &TestConfig::expect_session_miss },
Adam Langley75712922014-10-10 16:23:43 -070060 { "-expect-extended-master-secret",
61 &TestConfig::expect_extended_master_secret },
Adam Langley2ae77d22014-10-28 17:29:33 -070062 { "-renegotiate", &TestConfig::renegotiate },
David Benjaminca6554b2014-11-08 12:31:52 -050063 { "-allow-unsafe-legacy-renegotiation",
64 &TestConfig::allow_unsafe_legacy_renegotiation },
David Benjamin61f95272014-11-25 01:55:35 -050065 { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
66 { "-enable-signed-cert-timestamps",
67 &TestConfig::enable_signed_cert_timestamps },
Feng Lu41aa3252014-11-21 22:47:56 -080068 { "-fastradio-padding", &TestConfig::fastradio_padding },
David Benjamin5a593af2014-08-11 19:51:50 -040069};
70
71const size_t kNumBoolFlags = sizeof(kBoolFlags) / sizeof(kBoolFlags[0]);
72
David Benjamin5a593af2014-08-11 19:51:50 -040073const StringFlag kStringFlags[] = {
74 { "-key-file", &TestConfig::key_file },
75 { "-cert-file", &TestConfig::cert_file },
76 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -040077 { "-advertise-npn", &TestConfig::advertise_npn },
78 { "-expect-next-proto", &TestConfig::expected_next_proto },
79 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -040080 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamine78bfde2014-09-06 12:45:15 -040081 { "-host-name", &TestConfig::host_name },
David Benjaminae2888f2014-09-06 12:58:58 -040082 { "-advertise-alpn", &TestConfig::advertise_alpn },
83 { "-expect-alpn", &TestConfig::expected_alpn },
84 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
85 { "-select-alpn", &TestConfig::select_alpn },
David Benjamin48cae082014-10-27 01:06:24 -040086 { "-psk", &TestConfig::psk },
87 { "-psk-identity", &TestConfig::psk_identity },
David Benjaminca6c8262014-11-15 19:06:08 -050088 { "-srtp-profiles", &TestConfig::srtp_profiles },
David Benjamin5a593af2014-08-11 19:51:50 -040089};
90
91const size_t kNumStringFlags = sizeof(kStringFlags) / sizeof(kStringFlags[0]);
92
David Benjamin2561dc32014-08-24 01:25:27 -040093const StringFlag kBase64Flags[] = {
94 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -040095 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin61f95272014-11-25 01:55:35 -050096 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
97 { "-expect-signed-cert-timestamps",
98 &TestConfig::expected_signed_cert_timestamps },
David Benjamin2561dc32014-08-24 01:25:27 -040099};
100
101const size_t kNumBase64Flags = sizeof(kBase64Flags) / sizeof(kBase64Flags[0]);
102
David Benjamin5a593af2014-08-11 19:51:50 -0400103} // namespace
104
105TestConfig::TestConfig()
106 : is_server(false),
David Benjamin6fd297b2014-08-11 18:43:38 -0400107 is_dtls(false),
David Benjamin5a593af2014-08-11 19:51:50 -0400108 resume(false),
109 fallback_scsv(false),
110 require_any_client_certificate(false),
111 false_start(false),
112 async(false),
113 write_different_record_sizes(false),
114 cbc_record_splitting(false),
115 partial_write(false),
116 no_tls12(false),
117 no_tls11(false),
118 no_tls1(false),
David Benjamin6fd297b2014-08-11 18:43:38 -0400119 no_ssl3(false),
David Benjamine58c4f52014-08-24 03:47:07 -0400120 cookie_exchange(false),
David Benjamin5c24a1d2014-08-31 00:59:27 -0400121 shim_writes_first(false),
David Benjamin01fe8202014-09-24 15:21:44 -0400122 tls_d5_bug(false),
Adam Langley75712922014-10-10 16:23:43 -0700123 expect_session_miss(false),
Adam Langley2ae77d22014-10-28 17:29:33 -0700124 expect_extended_master_secret(false),
David Benjaminca6554b2014-11-08 12:31:52 -0500125 renegotiate(false),
David Benjamin61f95272014-11-25 01:55:35 -0500126 allow_unsafe_legacy_renegotiation(false),
127 enable_ocsp_stapling(false),
Feng Lu41aa3252014-11-21 22:47:56 -0800128 enable_signed_cert_timestamps(false),
129 fastradio_padding(false) {
David Benjamin5a593af2014-08-11 19:51:50 -0400130}
131
132bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
133 for (int i = 0; i < argc; i++) {
134 size_t j;
135 for (j = 0; j < kNumBoolFlags; j++) {
136 if (strcmp(argv[i], kBoolFlags[j].flag) == 0) {
137 break;
138 }
139 }
140 if (j < kNumBoolFlags) {
141 out_config->*(kBoolFlags[j].member) = true;
142 continue;
143 }
144
145 for (j = 0; j < kNumStringFlags; j++) {
146 if (strcmp(argv[i], kStringFlags[j].flag) == 0) {
147 break;
148 }
149 }
150 if (j < kNumStringFlags) {
151 i++;
152 if (i >= argc) {
153 fprintf(stderr, "Missing parameter\n");
154 return false;
155 }
156 out_config->*(kStringFlags[j].member) = argv[i];
157 continue;
158 }
159
David Benjamin2561dc32014-08-24 01:25:27 -0400160 for (j = 0; j < kNumBase64Flags; j++) {
161 if (strcmp(argv[i], kBase64Flags[j].flag) == 0) {
162 break;
163 }
164 }
165 if (j < kNumBase64Flags) {
166 i++;
167 if (i >= argc) {
168 fprintf(stderr, "Missing parameter\n");
169 return false;
170 }
171 size_t len;
172 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
173 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
174 }
175 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
176 if (!EVP_DecodeBase64(decoded.get(), &len, len,
177 reinterpret_cast<const uint8_t *>(argv[i]),
178 strlen(argv[i]))) {
179 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
180 }
181 out_config->*(kBase64Flags[j].member) = std::string(
182 reinterpret_cast<const char *>(decoded.get()), len);
183 continue;
184 }
185
David Benjamin5a593af2014-08-11 19:51:50 -0400186 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
187 return false;
188 }
189
190 return true;
191}