blob: 70543cce3b5eea37530f294ea9c89eff47e77284 [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
17#include <string.h>
18
David Benjamin2561dc32014-08-24 01:25:27 -040019#include <memory>
20
21#include <openssl/base64.h>
David Benjamin5a593af2014-08-11 19:51:50 -040022
23namespace {
24
25typedef bool TestConfig::*BoolMember;
26typedef std::string TestConfig::*StringMember;
27
28struct BoolFlag {
29 const char *flag;
30 BoolMember member;
31};
32
33struct StringFlag {
34 const char *flag;
35 StringMember member;
36};
37
38const BoolFlag kBoolFlags[] = {
39 { "-server", &TestConfig::is_server },
David Benjamin6fd297b2014-08-11 18:43:38 -040040 { "-dtls", &TestConfig::is_dtls },
David Benjamin5a593af2014-08-11 19:51:50 -040041 { "-resume", &TestConfig::resume },
42 { "-fallback-scsv", &TestConfig::fallback_scsv },
43 { "-require-any-client-certificate",
44 &TestConfig::require_any_client_certificate },
45 { "-false-start", &TestConfig::false_start },
46 { "-async", &TestConfig::async },
47 { "-write-different-record-sizes",
48 &TestConfig::write_different_record_sizes },
49 { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
50 { "-partial-write", &TestConfig::partial_write },
51 { "-no-tls12", &TestConfig::no_tls12 },
52 { "-no-tls11", &TestConfig::no_tls11 },
53 { "-no-tls1", &TestConfig::no_tls1 },
54 { "-no-ssl3", &TestConfig::no_ssl3 },
David Benjamin6fd297b2014-08-11 18:43:38 -040055 { "-cookie-exchange", &TestConfig::cookie_exchange },
David Benjamine58c4f52014-08-24 03:47:07 -040056 { "-shim-writes-first", &TestConfig::shim_writes_first },
David Benjamin5c24a1d2014-08-31 00:59:27 -040057 { "-tls-d5-bug", &TestConfig::tls_d5_bug },
David Benjamin01fe8202014-09-24 15:21:44 -040058 { "-expect-session-miss", &TestConfig::expect_session_miss },
David Benjamin5a593af2014-08-11 19:51:50 -040059};
60
61const size_t kNumBoolFlags = sizeof(kBoolFlags) / sizeof(kBoolFlags[0]);
62
David Benjamin5a593af2014-08-11 19:51:50 -040063const StringFlag kStringFlags[] = {
64 { "-key-file", &TestConfig::key_file },
65 { "-cert-file", &TestConfig::cert_file },
66 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -040067 { "-advertise-npn", &TestConfig::advertise_npn },
68 { "-expect-next-proto", &TestConfig::expected_next_proto },
69 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -040070 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamine78bfde2014-09-06 12:45:15 -040071 { "-host-name", &TestConfig::host_name },
David Benjaminae2888f2014-09-06 12:58:58 -040072 { "-advertise-alpn", &TestConfig::advertise_alpn },
73 { "-expect-alpn", &TestConfig::expected_alpn },
74 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
75 { "-select-alpn", &TestConfig::select_alpn },
David Benjamin5a593af2014-08-11 19:51:50 -040076};
77
78const size_t kNumStringFlags = sizeof(kStringFlags) / sizeof(kStringFlags[0]);
79
David Benjamin2561dc32014-08-24 01:25:27 -040080const StringFlag kBase64Flags[] = {
81 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -040082 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin2561dc32014-08-24 01:25:27 -040083};
84
85const size_t kNumBase64Flags = sizeof(kBase64Flags) / sizeof(kBase64Flags[0]);
86
David Benjamin5a593af2014-08-11 19:51:50 -040087} // namespace
88
89TestConfig::TestConfig()
90 : is_server(false),
David Benjamin6fd297b2014-08-11 18:43:38 -040091 is_dtls(false),
David Benjamin5a593af2014-08-11 19:51:50 -040092 resume(false),
93 fallback_scsv(false),
94 require_any_client_certificate(false),
95 false_start(false),
96 async(false),
97 write_different_record_sizes(false),
98 cbc_record_splitting(false),
99 partial_write(false),
100 no_tls12(false),
101 no_tls11(false),
102 no_tls1(false),
David Benjamin6fd297b2014-08-11 18:43:38 -0400103 no_ssl3(false),
David Benjamine58c4f52014-08-24 03:47:07 -0400104 cookie_exchange(false),
David Benjamin5c24a1d2014-08-31 00:59:27 -0400105 shim_writes_first(false),
David Benjamin01fe8202014-09-24 15:21:44 -0400106 tls_d5_bug(false),
107 expect_session_miss(false) {
David Benjamin5a593af2014-08-11 19:51:50 -0400108}
109
110bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
111 for (int i = 0; i < argc; i++) {
112 size_t j;
113 for (j = 0; j < kNumBoolFlags; j++) {
114 if (strcmp(argv[i], kBoolFlags[j].flag) == 0) {
115 break;
116 }
117 }
118 if (j < kNumBoolFlags) {
119 out_config->*(kBoolFlags[j].member) = true;
120 continue;
121 }
122
123 for (j = 0; j < kNumStringFlags; j++) {
124 if (strcmp(argv[i], kStringFlags[j].flag) == 0) {
125 break;
126 }
127 }
128 if (j < kNumStringFlags) {
129 i++;
130 if (i >= argc) {
131 fprintf(stderr, "Missing parameter\n");
132 return false;
133 }
134 out_config->*(kStringFlags[j].member) = argv[i];
135 continue;
136 }
137
David Benjamin2561dc32014-08-24 01:25:27 -0400138 for (j = 0; j < kNumBase64Flags; j++) {
139 if (strcmp(argv[i], kBase64Flags[j].flag) == 0) {
140 break;
141 }
142 }
143 if (j < kNumBase64Flags) {
144 i++;
145 if (i >= argc) {
146 fprintf(stderr, "Missing parameter\n");
147 return false;
148 }
149 size_t len;
150 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
151 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
152 }
153 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
154 if (!EVP_DecodeBase64(decoded.get(), &len, len,
155 reinterpret_cast<const uint8_t *>(argv[i]),
156 strlen(argv[i]))) {
157 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
158 }
159 out_config->*(kBase64Flags[j].member) = std::string(
160 reinterpret_cast<const char *>(decoded.get()), len);
161 continue;
162 }
163
David Benjamin5a593af2014-08-11 19:51:50 -0400164 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
165 return false;
166 }
167
168 return true;
169}