blob: 5d0119d5fb52b0bad00c6be82c087382cb21f704 [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 Benjamin5a593af2014-08-11 19:51:50 -040065};
66
67const size_t kNumBoolFlags = sizeof(kBoolFlags) / sizeof(kBoolFlags[0]);
68
David Benjamin5a593af2014-08-11 19:51:50 -040069const StringFlag kStringFlags[] = {
70 { "-key-file", &TestConfig::key_file },
71 { "-cert-file", &TestConfig::cert_file },
72 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -040073 { "-advertise-npn", &TestConfig::advertise_npn },
74 { "-expect-next-proto", &TestConfig::expected_next_proto },
75 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -040076 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamine78bfde2014-09-06 12:45:15 -040077 { "-host-name", &TestConfig::host_name },
David Benjaminae2888f2014-09-06 12:58:58 -040078 { "-advertise-alpn", &TestConfig::advertise_alpn },
79 { "-expect-alpn", &TestConfig::expected_alpn },
80 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
81 { "-select-alpn", &TestConfig::select_alpn },
David Benjamin48cae082014-10-27 01:06:24 -040082 { "-psk", &TestConfig::psk },
83 { "-psk-identity", &TestConfig::psk_identity },
David Benjaminca6c8262014-11-15 19:06:08 -050084 { "-srtp-profiles", &TestConfig::srtp_profiles },
David Benjamin5a593af2014-08-11 19:51:50 -040085};
86
87const size_t kNumStringFlags = sizeof(kStringFlags) / sizeof(kStringFlags[0]);
88
David Benjamin2561dc32014-08-24 01:25:27 -040089const StringFlag kBase64Flags[] = {
90 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -040091 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin2561dc32014-08-24 01:25:27 -040092};
93
94const size_t kNumBase64Flags = sizeof(kBase64Flags) / sizeof(kBase64Flags[0]);
95
David Benjamin5a593af2014-08-11 19:51:50 -040096} // namespace
97
98TestConfig::TestConfig()
99 : is_server(false),
David Benjamin6fd297b2014-08-11 18:43:38 -0400100 is_dtls(false),
David Benjamin5a593af2014-08-11 19:51:50 -0400101 resume(false),
102 fallback_scsv(false),
103 require_any_client_certificate(false),
104 false_start(false),
105 async(false),
106 write_different_record_sizes(false),
107 cbc_record_splitting(false),
108 partial_write(false),
109 no_tls12(false),
110 no_tls11(false),
111 no_tls1(false),
David Benjamin6fd297b2014-08-11 18:43:38 -0400112 no_ssl3(false),
David Benjamine58c4f52014-08-24 03:47:07 -0400113 cookie_exchange(false),
David Benjamin5c24a1d2014-08-31 00:59:27 -0400114 shim_writes_first(false),
David Benjamin01fe8202014-09-24 15:21:44 -0400115 tls_d5_bug(false),
Adam Langley75712922014-10-10 16:23:43 -0700116 expect_session_miss(false),
Adam Langley2ae77d22014-10-28 17:29:33 -0700117 expect_extended_master_secret(false),
David Benjaminca6554b2014-11-08 12:31:52 -0500118 renegotiate(false),
119 allow_unsafe_legacy_renegotiation(false) {
David Benjamin5a593af2014-08-11 19:51:50 -0400120}
121
122bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
123 for (int i = 0; i < argc; i++) {
124 size_t j;
125 for (j = 0; j < kNumBoolFlags; j++) {
126 if (strcmp(argv[i], kBoolFlags[j].flag) == 0) {
127 break;
128 }
129 }
130 if (j < kNumBoolFlags) {
131 out_config->*(kBoolFlags[j].member) = true;
132 continue;
133 }
134
135 for (j = 0; j < kNumStringFlags; j++) {
136 if (strcmp(argv[i], kStringFlags[j].flag) == 0) {
137 break;
138 }
139 }
140 if (j < kNumStringFlags) {
141 i++;
142 if (i >= argc) {
143 fprintf(stderr, "Missing parameter\n");
144 return false;
145 }
146 out_config->*(kStringFlags[j].member) = argv[i];
147 continue;
148 }
149
David Benjamin2561dc32014-08-24 01:25:27 -0400150 for (j = 0; j < kNumBase64Flags; j++) {
151 if (strcmp(argv[i], kBase64Flags[j].flag) == 0) {
152 break;
153 }
154 }
155 if (j < kNumBase64Flags) {
156 i++;
157 if (i >= argc) {
158 fprintf(stderr, "Missing parameter\n");
159 return false;
160 }
161 size_t len;
162 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
163 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
164 }
165 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
166 if (!EVP_DecodeBase64(decoded.get(), &len, len,
167 reinterpret_cast<const uint8_t *>(argv[i]),
168 strlen(argv[i]))) {
169 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
170 }
171 out_config->*(kBase64Flags[j].member) = std::string(
172 reinterpret_cast<const char *>(decoded.get()), len);
173 continue;
174 }
175
David Benjamin5a593af2014-08-11 19:51:50 -0400176 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
177 return false;
178 }
179
180 return true;
181}