blob: cf0d0af58786c4d31bf4ff2714b671710d47a878 [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 Benjamin5a593af2014-08-11 19:51:50 -040056};
57
58const size_t kNumBoolFlags = sizeof(kBoolFlags) / sizeof(kBoolFlags[0]);
59
David Benjamin5a593af2014-08-11 19:51:50 -040060const StringFlag kStringFlags[] = {
61 { "-key-file", &TestConfig::key_file },
62 { "-cert-file", &TestConfig::cert_file },
63 { "-expect-server-name", &TestConfig::expected_server_name },
David Benjamin5a593af2014-08-11 19:51:50 -040064 { "-advertise-npn", &TestConfig::advertise_npn },
65 { "-expect-next-proto", &TestConfig::expected_next_proto },
66 { "-select-next-proto", &TestConfig::select_next_proto },
David Benjamina08e49d2014-08-24 01:46:07 -040067 { "-send-channel-id", &TestConfig::send_channel_id },
David Benjamin5a593af2014-08-11 19:51:50 -040068};
69
70const size_t kNumStringFlags = sizeof(kStringFlags) / sizeof(kStringFlags[0]);
71
David Benjamin2561dc32014-08-24 01:25:27 -040072const StringFlag kBase64Flags[] = {
73 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
David Benjamina08e49d2014-08-24 01:46:07 -040074 { "-expect-channel-id", &TestConfig::expected_channel_id },
David Benjamin2561dc32014-08-24 01:25:27 -040075};
76
77const size_t kNumBase64Flags = sizeof(kBase64Flags) / sizeof(kBase64Flags[0]);
78
David Benjamin5a593af2014-08-11 19:51:50 -040079} // namespace
80
81TestConfig::TestConfig()
82 : is_server(false),
David Benjamin6fd297b2014-08-11 18:43:38 -040083 is_dtls(false),
David Benjamin5a593af2014-08-11 19:51:50 -040084 resume(false),
85 fallback_scsv(false),
86 require_any_client_certificate(false),
87 false_start(false),
88 async(false),
89 write_different_record_sizes(false),
90 cbc_record_splitting(false),
91 partial_write(false),
92 no_tls12(false),
93 no_tls11(false),
94 no_tls1(false),
David Benjamin6fd297b2014-08-11 18:43:38 -040095 no_ssl3(false),
96 cookie_exchange(false) {
David Benjamin5a593af2014-08-11 19:51:50 -040097}
98
99bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
100 for (int i = 0; i < argc; i++) {
101 size_t j;
102 for (j = 0; j < kNumBoolFlags; j++) {
103 if (strcmp(argv[i], kBoolFlags[j].flag) == 0) {
104 break;
105 }
106 }
107 if (j < kNumBoolFlags) {
108 out_config->*(kBoolFlags[j].member) = true;
109 continue;
110 }
111
112 for (j = 0; j < kNumStringFlags; j++) {
113 if (strcmp(argv[i], kStringFlags[j].flag) == 0) {
114 break;
115 }
116 }
117 if (j < kNumStringFlags) {
118 i++;
119 if (i >= argc) {
120 fprintf(stderr, "Missing parameter\n");
121 return false;
122 }
123 out_config->*(kStringFlags[j].member) = argv[i];
124 continue;
125 }
126
David Benjamin2561dc32014-08-24 01:25:27 -0400127 for (j = 0; j < kNumBase64Flags; j++) {
128 if (strcmp(argv[i], kBase64Flags[j].flag) == 0) {
129 break;
130 }
131 }
132 if (j < kNumBase64Flags) {
133 i++;
134 if (i >= argc) {
135 fprintf(stderr, "Missing parameter\n");
136 return false;
137 }
138 size_t len;
139 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
140 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
141 }
142 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
143 if (!EVP_DecodeBase64(decoded.get(), &len, len,
144 reinterpret_cast<const uint8_t *>(argv[i]),
145 strlen(argv[i]))) {
146 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
147 }
148 out_config->*(kBase64Flags[j].member) = std::string(
149 reinterpret_cast<const char *>(decoded.get()), len);
150 continue;
151 }
152
David Benjamin5a593af2014-08-11 19:51:50 -0400153 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
154 return false;
155 }
156
157 return true;
158}