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