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 | 01fe820 | 2014-09-24 15:21:44 -0400 | [diff] [blame] | 64 | { "-expect-session-miss", &TestConfig::expect_session_miss }, |
David Benjamin | 594e7d2 | 2016-03-17 17:49:56 -0400 | [diff] [blame] | 65 | { "-decline-alpn", &TestConfig::decline_alpn }, |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 66 | { "-expect-extended-master-secret", |
| 67 | &TestConfig::expect_extended_master_secret }, |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 68 | { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling }, |
| 69 | { "-enable-signed-cert-timestamps", |
| 70 | &TestConfig::enable_signed_cert_timestamps }, |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 71 | { "-implicit-handshake", &TestConfig::implicit_handshake }, |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 72 | { "-use-early-callback", &TestConfig::use_early_callback }, |
| 73 | { "-fail-early-callback", &TestConfig::fail_early_callback }, |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 74 | { "-install-ddos-callback", &TestConfig::install_ddos_callback }, |
| 75 | { "-fail-ddos-callback", &TestConfig::fail_ddos_callback }, |
| 76 | { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback }, |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 77 | { "-handshake-never-done", &TestConfig::handshake_never_done }, |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 78 | { "-use-export-context", &TestConfig::use_export_context }, |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 79 | { "-tls-unique", &TestConfig::tls_unique }, |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 80 | { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal }, |
| 81 | { "-expect-no-session", &TestConfig::expect_no_session }, |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 82 | { "-use-ticket-callback", &TestConfig::use_ticket_callback }, |
| 83 | { "-renew-ticket", &TestConfig::renew_ticket }, |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 84 | { "-enable-client-custom-extension", |
| 85 | &TestConfig::enable_client_custom_extension }, |
| 86 | { "-enable-server-custom-extension", |
| 87 | &TestConfig::enable_server_custom_extension }, |
| 88 | { "-custom-extension-skip", &TestConfig::custom_extension_skip }, |
| 89 | { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add }, |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 90 | { "-check-close-notify", &TestConfig::check_close_notify }, |
| 91 | { "-shim-shuts-down", &TestConfig::shim_shuts_down }, |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 92 | { "-verify-fail", &TestConfig::verify_fail }, |
| 93 | { "-verify-peer", &TestConfig::verify_peer }, |
David Benjamin | 1d5ef3b | 2015-10-12 19:54:18 -0400 | [diff] [blame] | 94 | { "-expect-verify-result", &TestConfig::expect_verify_result }, |
| 95 | { "-renegotiate-once", &TestConfig::renegotiate_once }, |
| 96 | { "-renegotiate-freely", &TestConfig::renegotiate_freely }, |
Adam Langley | 27a0d08 | 2015-11-03 13:34:10 -0800 | [diff] [blame] | 97 | { "-renegotiate-ignore", &TestConfig::renegotiate_ignore }, |
David Benjamin | 091c4b9 | 2015-10-26 13:33:21 -0400 | [diff] [blame] | 98 | { "-disable-npn", &TestConfig::disable_npn }, |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 99 | { "-p384-only", &TestConfig::p384_only }, |
David Benjamin | 8c2b3bf | 2015-12-18 20:55:44 -0500 | [diff] [blame] | 100 | { "-enable-all-curves", &TestConfig::enable_all_curves }, |
Adam Langley | c4f25ce | 2015-11-26 16:39:08 -0800 | [diff] [blame] | 101 | { "-use-sparse-dh-prime", &TestConfig::use_sparse_dh_prime }, |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 102 | { "-use-old-client-cert-callback", |
| 103 | &TestConfig::use_old_client_cert_callback }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 104 | }; |
| 105 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 106 | const Flag<std::string> kStringFlags[] = { |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 107 | { "-digest-prefs", &TestConfig::digest_prefs }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 108 | { "-key-file", &TestConfig::key_file }, |
| 109 | { "-cert-file", &TestConfig::cert_file }, |
| 110 | { "-expect-server-name", &TestConfig::expected_server_name }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 111 | { "-advertise-npn", &TestConfig::advertise_npn }, |
| 112 | { "-expect-next-proto", &TestConfig::expected_next_proto }, |
| 113 | { "-select-next-proto", &TestConfig::select_next_proto }, |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 114 | { "-send-channel-id", &TestConfig::send_channel_id }, |
David Benjamin | e78bfde | 2014-09-06 12:45:15 -0400 | [diff] [blame] | 115 | { "-host-name", &TestConfig::host_name }, |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 116 | { "-advertise-alpn", &TestConfig::advertise_alpn }, |
| 117 | { "-expect-alpn", &TestConfig::expected_alpn }, |
| 118 | { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn }, |
| 119 | { "-select-alpn", &TestConfig::select_alpn }, |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 120 | { "-psk", &TestConfig::psk }, |
| 121 | { "-psk-identity", &TestConfig::psk_identity }, |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 122 | { "-srtp-profiles", &TestConfig::srtp_profiles }, |
David Benjamin | 67d1fb5 | 2015-03-16 15:16:23 -0400 | [diff] [blame] | 123 | { "-cipher", &TestConfig::cipher }, |
Adam Langley | cef7583 | 2015-09-03 14:51:12 -0700 | [diff] [blame] | 124 | { "-cipher-tls10", &TestConfig::cipher_tls10 }, |
| 125 | { "-cipher-tls11", &TestConfig::cipher_tls11 }, |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 126 | { "-export-label", &TestConfig::export_label }, |
| 127 | { "-export-context", &TestConfig::export_context }, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 128 | }; |
| 129 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 130 | const Flag<std::string> kBase64Flags[] = { |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 131 | { "-expect-certificate-types", &TestConfig::expected_certificate_types }, |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 132 | { "-expect-channel-id", &TestConfig::expected_channel_id }, |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 133 | { "-expect-ocsp-response", &TestConfig::expected_ocsp_response }, |
| 134 | { "-expect-signed-cert-timestamps", |
| 135 | &TestConfig::expected_signed_cert_timestamps }, |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 136 | { "-ocsp-response", &TestConfig::ocsp_response }, |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 137 | { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps }, |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 138 | }; |
| 139 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 140 | const Flag<int> kIntFlags[] = { |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 141 | { "-port", &TestConfig::port }, |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 142 | { "-min-version", &TestConfig::min_version }, |
| 143 | { "-max-version", &TestConfig::max_version }, |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 144 | { "-mtu", &TestConfig::mtu }, |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 145 | { "-export-keying-material", &TestConfig::export_keying_material }, |
David Benjamin | 324dce4 | 2015-10-12 19:49:00 -0400 | [diff] [blame] | 146 | { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations }, |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 147 | { "-expect-server-key-exchange-hash", |
| 148 | &TestConfig::expect_server_key_exchange_hash }, |
David Benjamin | 4cc36ad | 2015-12-19 14:23:26 -0500 | [diff] [blame] | 149 | { "-expect-key-exchange-info", |
| 150 | &TestConfig::expect_key_exchange_info }, |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame^] | 151 | { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms }, |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 152 | }; |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 153 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 154 | } // namespace |
| 155 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 156 | bool ParseConfig(int argc, char **argv, TestConfig *out_config) { |
| 157 | for (int i = 0; i < argc; i++) { |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 158 | bool *bool_field = FindField(out_config, kBoolFlags, argv[i]); |
| 159 | if (bool_field != NULL) { |
| 160 | *bool_field = true; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 161 | continue; |
| 162 | } |
| 163 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 164 | std::string *string_field = FindField(out_config, kStringFlags, argv[i]); |
| 165 | if (string_field != NULL) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 166 | i++; |
| 167 | if (i >= argc) { |
| 168 | fprintf(stderr, "Missing parameter\n"); |
| 169 | return false; |
| 170 | } |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 171 | string_field->assign(argv[i]); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 172 | continue; |
| 173 | } |
| 174 | |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 175 | std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]); |
| 176 | if (base64_field != NULL) { |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 177 | i++; |
| 178 | if (i >= argc) { |
| 179 | fprintf(stderr, "Missing parameter\n"); |
| 180 | return false; |
| 181 | } |
| 182 | size_t len; |
| 183 | if (!EVP_DecodedLength(&len, strlen(argv[i]))) { |
| 184 | fprintf(stderr, "Invalid base64: %s\n", argv[i]); |
David Benjamin | 154c2f2 | 2016-03-05 11:57:44 -0500 | [diff] [blame] | 185 | return false; |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 186 | } |
| 187 | std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]); |
| 188 | if (!EVP_DecodeBase64(decoded.get(), &len, len, |
| 189 | reinterpret_cast<const uint8_t *>(argv[i]), |
| 190 | strlen(argv[i]))) { |
| 191 | fprintf(stderr, "Invalid base64: %s\n", argv[i]); |
David Benjamin | 154c2f2 | 2016-03-05 11:57:44 -0500 | [diff] [blame] | 192 | return false; |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 193 | } |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 194 | base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | int *int_field = FindField(out_config, kIntFlags, argv[i]); |
| 199 | if (int_field) { |
| 200 | i++; |
| 201 | if (i >= argc) { |
| 202 | fprintf(stderr, "Missing parameter\n"); |
| 203 | return false; |
| 204 | } |
| 205 | *int_field = atoi(argv[i]); |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame] | 206 | continue; |
| 207 | } |
| 208 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 209 | fprintf(stderr, "Unknown argument: %s\n", argv[i]); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | return true; |
| 214 | } |