blob: 270fbfb5d5674d833d3377dcc598bd6f35da879a [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 },
David Benjamin5a593af2014-08-11 19:51:50 -040060};
61
62const size_t kNumBoolFlags = sizeof(kBoolFlags) / sizeof(kBoolFlags[0]);
63
David Benjamin5a593af2014-08-11 19:51:50 -040064const StringFlag kStringFlags[] = {
65 { "-key-file", &TestConfig::key_file },
66 { "-cert-file", &TestConfig::cert_file },
67 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -040068 { "-advertise-npn", &TestConfig::advertise_npn },
69 { "-expect-next-proto", &TestConfig::expected_next_proto },
70 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -040071 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamine78bfde2014-09-06 12:45:15 -040072 { "-host-name", &TestConfig::host_name },
David Benjaminae2888f2014-09-06 12:58:58 -040073 { "-advertise-alpn", &TestConfig::advertise_alpn },
74 { "-expect-alpn", &TestConfig::expected_alpn },
75 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
76 { "-select-alpn", &TestConfig::select_alpn },
David Benjamin5a593af2014-08-11 19:51:50 -040077};
78
79const size_t kNumStringFlags = sizeof(kStringFlags) / sizeof(kStringFlags[0]);
80
David Benjamin2561dc32014-08-24 01:25:27 -040081const StringFlag kBase64Flags[] = {
82 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -040083 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin2561dc32014-08-24 01:25:27 -040084};
85
86const size_t kNumBase64Flags = sizeof(kBase64Flags) / sizeof(kBase64Flags[0]);
87
David Benjamin5a593af2014-08-11 19:51:50 -040088} // namespace
89
90TestConfig::TestConfig()
91 : is_server(false),
David Benjamin6fd297b2014-08-11 18:43:38 -040092 is_dtls(false),
David Benjamin5a593af2014-08-11 19:51:50 -040093 resume(false),
94 fallback_scsv(false),
95 require_any_client_certificate(false),
96 false_start(false),
97 async(false),
98 write_different_record_sizes(false),
99 cbc_record_splitting(false),
100 partial_write(false),
101 no_tls12(false),
102 no_tls11(false),
103 no_tls1(false),
David Benjamin6fd297b2014-08-11 18:43:38 -0400104 no_ssl3(false),
David Benjamine58c4f52014-08-24 03:47:07 -0400105 cookie_exchange(false),
David Benjamin5c24a1d2014-08-31 00:59:27 -0400106 shim_writes_first(false),
David Benjamin01fe8202014-09-24 15:21:44 -0400107 tls_d5_bug(false),
108 expect_session_miss(false) {
David Benjamin5a593af2014-08-11 19:51:50 -0400109}
110
111bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
112 for (int i = 0; i < argc; i++) {
113 size_t j;
114 for (j = 0; j < kNumBoolFlags; j++) {
115 if (strcmp(argv[i], kBoolFlags[j].flag) == 0) {
116 break;
117 }
118 }
119 if (j < kNumBoolFlags) {
120 out_config->*(kBoolFlags[j].member) = true;
121 continue;
122 }
123
124 for (j = 0; j < kNumStringFlags; j++) {
125 if (strcmp(argv[i], kStringFlags[j].flag) == 0) {
126 break;
127 }
128 }
129 if (j < kNumStringFlags) {
130 i++;
131 if (i >= argc) {
132 fprintf(stderr, "Missing parameter\n");
133 return false;
134 }
135 out_config->*(kStringFlags[j].member) = argv[i];
136 continue;
137 }
138
David Benjamin2561dc32014-08-24 01:25:27 -0400139 for (j = 0; j < kNumBase64Flags; j++) {
140 if (strcmp(argv[i], kBase64Flags[j].flag) == 0) {
141 break;
142 }
143 }
144 if (j < kNumBase64Flags) {
145 i++;
146 if (i >= argc) {
147 fprintf(stderr, "Missing parameter\n");
148 return false;
149 }
150 size_t len;
151 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
152 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
153 }
154 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
155 if (!EVP_DecodeBase64(decoded.get(), &len, len,
156 reinterpret_cast<const uint8_t *>(argv[i]),
157 strlen(argv[i]))) {
158 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
159 }
160 out_config->*(kBase64Flags[j].member) = std::string(
161 reinterpret_cast<const char *>(decoded.get()), len);
162 continue;
163 }
164
David Benjamin5a593af2014-08-11 19:51:50 -0400165 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
166 return false;
167 }
168
169 return true;
170}