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 | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 18 | #include <stdlib.h> |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 21 | #include <memory> |
| 22 | |
| 23 | #include <openssl/base64.h> |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 27 | template <typename T> |
| 28 | struct Flag { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 29 | const char *flag; |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 30 | T TestConfig::*member; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 31 | }; |
| 32 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 33 | // FindField looks for the flag in |flags| that matches |flag|. If one is found, |
| 34 | // it returns a pointer to the corresponding field in |config|. Otherwise, it |
| 35 | // returns NULL. |
| 36 | template<typename T, size_t N> |
| 37 | T *FindField(TestConfig *config, const Flag<T> (&flags)[N], const char *flag) { |
| 38 | for (size_t i = 0; i < N; i++) { |
| 39 | if (strcmp(flag, flags[i].flag) == 0) { |
| 40 | return &(config->*(flags[i].member)); |
| 41 | } |
| 42 | } |
| 43 | return NULL; |
| 44 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 45 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 46 | const Flag<bool> kBoolFlags[] = { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 47 | { "-server", &TestConfig::is_server }, |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 48 | { "-dtls", &TestConfig::is_dtls }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 49 | { "-resume", &TestConfig::resume }, |
| 50 | { "-fallback-scsv", &TestConfig::fallback_scsv }, |
| 51 | { "-require-any-client-certificate", |
| 52 | &TestConfig::require_any_client_certificate }, |
| 53 | { "-false-start", &TestConfig::false_start }, |
| 54 | { "-async", &TestConfig::async }, |
| 55 | { "-write-different-record-sizes", |
| 56 | &TestConfig::write_different_record_sizes }, |
| 57 | { "-cbc-record-splitting", &TestConfig::cbc_record_splitting }, |
| 58 | { "-partial-write", &TestConfig::partial_write }, |
| 59 | { "-no-tls12", &TestConfig::no_tls12 }, |
| 60 | { "-no-tls11", &TestConfig::no_tls11 }, |
| 61 | { "-no-tls1", &TestConfig::no_tls1 }, |
| 62 | { "-no-ssl3", &TestConfig::no_ssl3 }, |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 63 | { "-shim-writes-first", &TestConfig::shim_writes_first }, |
David Benjamin | 5c24a1d | 2014-08-31 00:59:27 -0400 | [diff] [blame] | 64 | { "-tls-d5-bug", &TestConfig::tls_d5_bug }, |
David Benjamin | 01fe820 | 2014-09-24 15:21:44 -0400 | [diff] [blame] | 65 | { "-expect-session-miss", &TestConfig::expect_session_miss }, |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 66 | { "-expect-extended-master-secret", |
| 67 | &TestConfig::expect_extended_master_secret }, |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 68 | { "-renegotiate", &TestConfig::renegotiate }, |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 69 | { "-allow-unsafe-legacy-renegotiation", |
| 70 | &TestConfig::allow_unsafe_legacy_renegotiation }, |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 71 | { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling }, |
| 72 | { "-enable-signed-cert-timestamps", |
| 73 | &TestConfig::enable_signed_cert_timestamps }, |
Feng Lu | 41aa325 | 2014-11-21 22:47:56 -0800 | [diff] [blame] | 74 | { "-fastradio-padding", &TestConfig::fastradio_padding }, |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 75 | { "-implicit-handshake", &TestConfig::implicit_handshake }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 78 | const Flag<std::string> kStringFlags[] = { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 79 | { "-key-file", &TestConfig::key_file }, |
| 80 | { "-cert-file", &TestConfig::cert_file }, |
| 81 | { "-expect-server-name", &TestConfig::expected_server_name }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 82 | { "-advertise-npn", &TestConfig::advertise_npn }, |
| 83 | { "-expect-next-proto", &TestConfig::expected_next_proto }, |
| 84 | { "-select-next-proto", &TestConfig::select_next_proto }, |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 85 | { "-send-channel-id", &TestConfig::send_channel_id }, |
David Benjamin | e78bfde | 2014-09-06 12:45:15 -0400 | [diff] [blame] | 86 | { "-host-name", &TestConfig::host_name }, |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 87 | { "-advertise-alpn", &TestConfig::advertise_alpn }, |
| 88 | { "-expect-alpn", &TestConfig::expected_alpn }, |
| 89 | { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn }, |
| 90 | { "-select-alpn", &TestConfig::select_alpn }, |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 91 | { "-psk", &TestConfig::psk }, |
| 92 | { "-psk-identity", &TestConfig::psk_identity }, |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 93 | { "-srtp-profiles", &TestConfig::srtp_profiles }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 94 | }; |
| 95 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 96 | const Flag<std::string> kBase64Flags[] = { |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 97 | { "-expect-certificate-types", &TestConfig::expected_certificate_types }, |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 98 | { "-expect-channel-id", &TestConfig::expected_channel_id }, |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 99 | { "-expect-ocsp-response", &TestConfig::expected_ocsp_response }, |
| 100 | { "-expect-signed-cert-timestamps", |
| 101 | &TestConfig::expected_signed_cert_timestamps }, |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 102 | }; |
| 103 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 104 | const Flag<int> kIntFlags[] = { |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame^] | 105 | { "-port", &TestConfig::port }, |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 106 | { "-min-version", &TestConfig::min_version }, |
| 107 | { "-max-version", &TestConfig::max_version }, |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 108 | { "-mtu", &TestConfig::mtu }, |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 109 | }; |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 110 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 111 | } // namespace |
| 112 | |
| 113 | TestConfig::TestConfig() |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame^] | 114 | : port(0), |
| 115 | is_server(false), |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 116 | is_dtls(false), |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 117 | resume(false), |
| 118 | fallback_scsv(false), |
| 119 | require_any_client_certificate(false), |
| 120 | false_start(false), |
| 121 | async(false), |
| 122 | write_different_record_sizes(false), |
| 123 | cbc_record_splitting(false), |
| 124 | partial_write(false), |
| 125 | no_tls12(false), |
| 126 | no_tls11(false), |
| 127 | no_tls1(false), |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 128 | no_ssl3(false), |
David Benjamin | 5c24a1d | 2014-08-31 00:59:27 -0400 | [diff] [blame] | 129 | shim_writes_first(false), |
David Benjamin | 01fe820 | 2014-09-24 15:21:44 -0400 | [diff] [blame] | 130 | tls_d5_bug(false), |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 131 | expect_session_miss(false), |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 132 | expect_extended_master_secret(false), |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 133 | renegotiate(false), |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 134 | allow_unsafe_legacy_renegotiation(false), |
| 135 | enable_ocsp_stapling(false), |
Feng Lu | 41aa325 | 2014-11-21 22:47:56 -0800 | [diff] [blame] | 136 | enable_signed_cert_timestamps(false), |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 137 | fastradio_padding(false), |
| 138 | min_version(0), |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 139 | max_version(0), |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 140 | mtu(0), |
| 141 | implicit_handshake(false) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool ParseConfig(int argc, char **argv, TestConfig *out_config) { |
| 145 | for (int i = 0; i < argc; i++) { |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 146 | bool *bool_field = FindField(out_config, kBoolFlags, argv[i]); |
| 147 | if (bool_field != NULL) { |
| 148 | *bool_field = true; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 149 | continue; |
| 150 | } |
| 151 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 152 | std::string *string_field = FindField(out_config, kStringFlags, argv[i]); |
| 153 | if (string_field != NULL) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 154 | i++; |
| 155 | if (i >= argc) { |
| 156 | fprintf(stderr, "Missing parameter\n"); |
| 157 | return false; |
| 158 | } |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 159 | string_field->assign(argv[i]); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 160 | continue; |
| 161 | } |
| 162 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 163 | std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]); |
| 164 | if (base64_field != NULL) { |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 165 | i++; |
| 166 | if (i >= argc) { |
| 167 | fprintf(stderr, "Missing parameter\n"); |
| 168 | return false; |
| 169 | } |
| 170 | size_t len; |
| 171 | if (!EVP_DecodedLength(&len, strlen(argv[i]))) { |
| 172 | fprintf(stderr, "Invalid base64: %s\n", argv[i]); |
| 173 | } |
| 174 | std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]); |
| 175 | if (!EVP_DecodeBase64(decoded.get(), &len, len, |
| 176 | reinterpret_cast<const uint8_t *>(argv[i]), |
| 177 | strlen(argv[i]))) { |
| 178 | fprintf(stderr, "Invalid base64: %s\n", argv[i]); |
| 179 | } |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 180 | base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len); |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | int *int_field = FindField(out_config, kIntFlags, argv[i]); |
| 185 | if (int_field) { |
| 186 | i++; |
| 187 | if (i >= argc) { |
| 188 | fprintf(stderr, "Missing parameter\n"); |
| 189 | return false; |
| 190 | } |
| 191 | *int_field = atoi(argv[i]); |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 192 | continue; |
| 193 | } |
| 194 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 195 | fprintf(stderr, "Unknown argument: %s\n", argv[i]); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | return true; |
| 200 | } |