David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1 | /* 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 Laurie | eba2384 | 2014-09-30 12:44:15 +0100 | [diff] [blame] | 17 | #include <stdio.h> |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 20 | #include <memory> |
| 21 | |
| 22 | #include <openssl/base64.h> |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
| 26 | typedef bool TestConfig::*BoolMember; |
| 27 | typedef std::string TestConfig::*StringMember; |
| 28 | |
| 29 | struct BoolFlag { |
| 30 | const char *flag; |
| 31 | BoolMember member; |
| 32 | }; |
| 33 | |
| 34 | struct StringFlag { |
| 35 | const char *flag; |
| 36 | StringMember member; |
| 37 | }; |
| 38 | |
| 39 | const BoolFlag kBoolFlags[] = { |
| 40 | { "-server", &TestConfig::is_server }, |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 41 | { "-dtls", &TestConfig::is_dtls }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 42 | { "-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 Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 56 | { "-cookie-exchange", &TestConfig::cookie_exchange }, |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 57 | { "-shim-writes-first", &TestConfig::shim_writes_first }, |
David Benjamin | 5c24a1d | 2014-08-31 00:59:27 -0400 | [diff] [blame] | 58 | { "-tls-d5-bug", &TestConfig::tls_d5_bug }, |
David Benjamin | 01fe820 | 2014-09-24 15:21:44 -0400 | [diff] [blame] | 59 | { "-expect-session-miss", &TestConfig::expect_session_miss }, |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 60 | { "-expect-extended-master-secret", |
| 61 | &TestConfig::expect_extended_master_secret }, |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 62 | { "-renegotiate", &TestConfig::renegotiate }, |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 63 | { "-allow-unsafe-legacy-renegotiation", |
| 64 | &TestConfig::allow_unsafe_legacy_renegotiation }, |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame^] | 65 | { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling }, |
| 66 | { "-enable-signed-cert-timestamps", |
| 67 | &TestConfig::enable_signed_cert_timestamps }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | const size_t kNumBoolFlags = sizeof(kBoolFlags) / sizeof(kBoolFlags[0]); |
| 71 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 72 | const StringFlag kStringFlags[] = { |
| 73 | { "-key-file", &TestConfig::key_file }, |
| 74 | { "-cert-file", &TestConfig::cert_file }, |
| 75 | { "-expect-server-name", &TestConfig::expected_server_name }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 76 | { "-advertise-npn", &TestConfig::advertise_npn }, |
| 77 | { "-expect-next-proto", &TestConfig::expected_next_proto }, |
| 78 | { "-select-next-proto", &TestConfig::select_next_proto }, |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 79 | { "-send-channel-id", &TestConfig::send_channel_id }, |
David Benjamin | e78bfde | 2014-09-06 12:45:15 -0400 | [diff] [blame] | 80 | { "-host-name", &TestConfig::host_name }, |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 81 | { "-advertise-alpn", &TestConfig::advertise_alpn }, |
| 82 | { "-expect-alpn", &TestConfig::expected_alpn }, |
| 83 | { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn }, |
| 84 | { "-select-alpn", &TestConfig::select_alpn }, |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 85 | { "-psk", &TestConfig::psk }, |
| 86 | { "-psk-identity", &TestConfig::psk_identity }, |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 87 | { "-srtp-profiles", &TestConfig::srtp_profiles }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | const size_t kNumStringFlags = sizeof(kStringFlags) / sizeof(kStringFlags[0]); |
| 91 | |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 92 | const StringFlag kBase64Flags[] = { |
| 93 | { "-expect-certificate-types", &TestConfig::expected_certificate_types }, |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 94 | { "-expect-channel-id", &TestConfig::expected_channel_id }, |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame^] | 95 | { "-expect-ocsp-response", &TestConfig::expected_ocsp_response }, |
| 96 | { "-expect-signed-cert-timestamps", |
| 97 | &TestConfig::expected_signed_cert_timestamps }, |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | const size_t kNumBase64Flags = sizeof(kBase64Flags) / sizeof(kBase64Flags[0]); |
| 101 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 102 | } // namespace |
| 103 | |
| 104 | TestConfig::TestConfig() |
| 105 | : is_server(false), |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 106 | is_dtls(false), |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 107 | resume(false), |
| 108 | fallback_scsv(false), |
| 109 | require_any_client_certificate(false), |
| 110 | false_start(false), |
| 111 | async(false), |
| 112 | write_different_record_sizes(false), |
| 113 | cbc_record_splitting(false), |
| 114 | partial_write(false), |
| 115 | no_tls12(false), |
| 116 | no_tls11(false), |
| 117 | no_tls1(false), |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 118 | no_ssl3(false), |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 119 | cookie_exchange(false), |
David Benjamin | 5c24a1d | 2014-08-31 00:59:27 -0400 | [diff] [blame] | 120 | shim_writes_first(false), |
David Benjamin | 01fe820 | 2014-09-24 15:21:44 -0400 | [diff] [blame] | 121 | tls_d5_bug(false), |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 122 | expect_session_miss(false), |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 123 | expect_extended_master_secret(false), |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 124 | renegotiate(false), |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame^] | 125 | allow_unsafe_legacy_renegotiation(false), |
| 126 | enable_ocsp_stapling(false), |
| 127 | enable_signed_cert_timestamps(false) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | bool ParseConfig(int argc, char **argv, TestConfig *out_config) { |
| 131 | for (int i = 0; i < argc; i++) { |
| 132 | size_t j; |
| 133 | for (j = 0; j < kNumBoolFlags; j++) { |
| 134 | if (strcmp(argv[i], kBoolFlags[j].flag) == 0) { |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | if (j < kNumBoolFlags) { |
| 139 | out_config->*(kBoolFlags[j].member) = true; |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | for (j = 0; j < kNumStringFlags; j++) { |
| 144 | if (strcmp(argv[i], kStringFlags[j].flag) == 0) { |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | if (j < kNumStringFlags) { |
| 149 | i++; |
| 150 | if (i >= argc) { |
| 151 | fprintf(stderr, "Missing parameter\n"); |
| 152 | return false; |
| 153 | } |
| 154 | out_config->*(kStringFlags[j].member) = argv[i]; |
| 155 | continue; |
| 156 | } |
| 157 | |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 158 | for (j = 0; j < kNumBase64Flags; j++) { |
| 159 | if (strcmp(argv[i], kBase64Flags[j].flag) == 0) { |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | if (j < kNumBase64Flags) { |
| 164 | i++; |
| 165 | if (i >= argc) { |
| 166 | fprintf(stderr, "Missing parameter\n"); |
| 167 | return false; |
| 168 | } |
| 169 | size_t len; |
| 170 | if (!EVP_DecodedLength(&len, strlen(argv[i]))) { |
| 171 | fprintf(stderr, "Invalid base64: %s\n", argv[i]); |
| 172 | } |
| 173 | std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]); |
| 174 | if (!EVP_DecodeBase64(decoded.get(), &len, len, |
| 175 | reinterpret_cast<const uint8_t *>(argv[i]), |
| 176 | strlen(argv[i]))) { |
| 177 | fprintf(stderr, "Invalid base64: %s\n", argv[i]); |
| 178 | } |
| 179 | out_config->*(kBase64Flags[j].member) = std::string( |
| 180 | reinterpret_cast<const char *>(decoded.get()), len); |
| 181 | continue; |
| 182 | } |
| 183 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 184 | fprintf(stderr, "Unknown argument: %s\n", argv[i]); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | return true; |
| 189 | } |