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