blob: 97162270386322aeb0899a4e8e184fa487554748 [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
19
20namespace {
21
22typedef bool TestConfig::*BoolMember;
23typedef std::string TestConfig::*StringMember;
24
25struct BoolFlag {
26 const char *flag;
27 BoolMember member;
28};
29
30struct StringFlag {
31 const char *flag;
32 StringMember member;
33};
34
35const BoolFlag kBoolFlags[] = {
36 { "-server", &TestConfig::is_server },
David Benjamin6fd297b2014-08-11 18:43:38 -040037 { "-dtls", &TestConfig::is_dtls },
David Benjamin5a593af2014-08-11 19:51:50 -040038 { "-resume", &TestConfig::resume },
39 { "-fallback-scsv", &TestConfig::fallback_scsv },
40 { "-require-any-client-certificate",
41 &TestConfig::require_any_client_certificate },
42 { "-false-start", &TestConfig::false_start },
43 { "-async", &TestConfig::async },
44 { "-write-different-record-sizes",
45 &TestConfig::write_different_record_sizes },
46 { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
47 { "-partial-write", &TestConfig::partial_write },
48 { "-no-tls12", &TestConfig::no_tls12 },
49 { "-no-tls11", &TestConfig::no_tls11 },
50 { "-no-tls1", &TestConfig::no_tls1 },
51 { "-no-ssl3", &TestConfig::no_ssl3 },
David Benjamin6fd297b2014-08-11 18:43:38 -040052 { "-cookie-exchange", &TestConfig::cookie_exchange },
David Benjamin5a593af2014-08-11 19:51:50 -040053};
54
55const size_t kNumBoolFlags = sizeof(kBoolFlags) / sizeof(kBoolFlags[0]);
56
57// TODO(davidben): Some of these should be in a new kBase64Flags to allow NUL
58// bytes.
59const StringFlag kStringFlags[] = {
60 { "-key-file", &TestConfig::key_file },
61 { "-cert-file", &TestConfig::cert_file },
62 { "-expect-server-name", &TestConfig::expected_server_name },
63 // Conveniently, 00 is not a certificate type.
64 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
65 { "-advertise-npn", &TestConfig::advertise_npn },
66 { "-expect-next-proto", &TestConfig::expected_next_proto },
67 { "-select-next-proto", &TestConfig::select_next_proto },
68};
69
70const size_t kNumStringFlags = sizeof(kStringFlags) / sizeof(kStringFlags[0]);
71
72} // namespace
73
74TestConfig::TestConfig()
75 : is_server(false),
David Benjamin6fd297b2014-08-11 18:43:38 -040076 is_dtls(false),
David Benjamin5a593af2014-08-11 19:51:50 -040077 resume(false),
78 fallback_scsv(false),
79 require_any_client_certificate(false),
80 false_start(false),
81 async(false),
82 write_different_record_sizes(false),
83 cbc_record_splitting(false),
84 partial_write(false),
85 no_tls12(false),
86 no_tls11(false),
87 no_tls1(false),
David Benjamin6fd297b2014-08-11 18:43:38 -040088 no_ssl3(false),
89 cookie_exchange(false) {
David Benjamin5a593af2014-08-11 19:51:50 -040090}
91
92bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
93 for (int i = 0; i < argc; i++) {
94 size_t j;
95 for (j = 0; j < kNumBoolFlags; j++) {
96 if (strcmp(argv[i], kBoolFlags[j].flag) == 0) {
97 break;
98 }
99 }
100 if (j < kNumBoolFlags) {
101 out_config->*(kBoolFlags[j].member) = true;
102 continue;
103 }
104
105 for (j = 0; j < kNumStringFlags; j++) {
106 if (strcmp(argv[i], kStringFlags[j].flag) == 0) {
107 break;
108 }
109 }
110 if (j < kNumStringFlags) {
111 i++;
112 if (i >= argc) {
113 fprintf(stderr, "Missing parameter\n");
114 return false;
115 }
116 out_config->*(kStringFlags[j].member) = argv[i];
117 continue;
118 }
119
120 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
121 return false;
122 }
123
124 return true;
125}