您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

335 行
13 KiB

  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. #include "test_config.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <memory>
  19. #include <openssl/base64.h>
  20. namespace {
  21. template <typename T>
  22. struct Flag {
  23. const char *flag;
  24. T TestConfig::*member;
  25. };
  26. // FindField looks for the flag in |flags| that matches |flag|. If one is found,
  27. // it returns a pointer to the corresponding field in |config|. Otherwise, it
  28. // returns NULL.
  29. template<typename T, size_t N>
  30. T *FindField(TestConfig *config, const Flag<T> (&flags)[N], const char *flag) {
  31. for (size_t i = 0; i < N; i++) {
  32. if (strcmp(flag, flags[i].flag) == 0) {
  33. return &(config->*(flags[i].member));
  34. }
  35. }
  36. return NULL;
  37. }
  38. const Flag<bool> kBoolFlags[] = {
  39. { "-server", &TestConfig::is_server },
  40. { "-dtls", &TestConfig::is_dtls },
  41. { "-fallback-scsv", &TestConfig::fallback_scsv },
  42. { "-require-any-client-certificate",
  43. &TestConfig::require_any_client_certificate },
  44. { "-false-start", &TestConfig::false_start },
  45. { "-async", &TestConfig::async },
  46. { "-write-different-record-sizes",
  47. &TestConfig::write_different_record_sizes },
  48. { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
  49. { "-partial-write", &TestConfig::partial_write },
  50. { "-no-tls13", &TestConfig::no_tls13 },
  51. { "-no-tls12", &TestConfig::no_tls12 },
  52. { "-no-tls11", &TestConfig::no_tls11 },
  53. { "-no-tls1", &TestConfig::no_tls1 },
  54. { "-no-ssl3", &TestConfig::no_ssl3 },
  55. { "-enable-channel-id", &TestConfig::enable_channel_id },
  56. { "-shim-writes-first", &TestConfig::shim_writes_first },
  57. { "-expect-session-miss", &TestConfig::expect_session_miss },
  58. { "-decline-alpn", &TestConfig::decline_alpn },
  59. { "-expect-extended-master-secret",
  60. &TestConfig::expect_extended_master_secret },
  61. { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
  62. { "-enable-signed-cert-timestamps",
  63. &TestConfig::enable_signed_cert_timestamps },
  64. { "-implicit-handshake", &TestConfig::implicit_handshake },
  65. { "-use-early-callback", &TestConfig::use_early_callback },
  66. { "-fail-early-callback", &TestConfig::fail_early_callback },
  67. { "-install-ddos-callback", &TestConfig::install_ddos_callback },
  68. { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
  69. { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
  70. { "-fail-cert-callback", &TestConfig::fail_cert_callback },
  71. { "-handshake-never-done", &TestConfig::handshake_never_done },
  72. { "-use-export-context", &TestConfig::use_export_context },
  73. { "-tls-unique", &TestConfig::tls_unique },
  74. { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
  75. { "-expect-no-session", &TestConfig::expect_no_session },
  76. { "-expect-ticket-supports-early-data",
  77. &TestConfig::expect_ticket_supports_early_data },
  78. { "-use-ticket-callback", &TestConfig::use_ticket_callback },
  79. { "-renew-ticket", &TestConfig::renew_ticket },
  80. { "-enable-early-data", &TestConfig::enable_early_data },
  81. { "-enable-client-custom-extension",
  82. &TestConfig::enable_client_custom_extension },
  83. { "-enable-server-custom-extension",
  84. &TestConfig::enable_server_custom_extension },
  85. { "-custom-extension-skip", &TestConfig::custom_extension_skip },
  86. { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
  87. { "-check-close-notify", &TestConfig::check_close_notify },
  88. { "-shim-shuts-down", &TestConfig::shim_shuts_down },
  89. { "-verify-fail", &TestConfig::verify_fail },
  90. { "-verify-peer", &TestConfig::verify_peer },
  91. { "-verify-peer-if-no-obc", &TestConfig::verify_peer_if_no_obc },
  92. { "-expect-verify-result", &TestConfig::expect_verify_result },
  93. { "-renegotiate-once", &TestConfig::renegotiate_once },
  94. { "-renegotiate-freely", &TestConfig::renegotiate_freely },
  95. { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
  96. { "-p384-only", &TestConfig::p384_only },
  97. { "-enable-all-curves", &TestConfig::enable_all_curves },
  98. { "-use-old-client-cert-callback",
  99. &TestConfig::use_old_client_cert_callback },
  100. { "-send-alert", &TestConfig::send_alert },
  101. { "-peek-then-read", &TestConfig::peek_then_read },
  102. { "-enable-grease", &TestConfig::enable_grease },
  103. { "-use-exporter-between-reads", &TestConfig::use_exporter_between_reads },
  104. { "-retain-only-sha256-client-cert",
  105. &TestConfig::retain_only_sha256_client_cert },
  106. { "-expect-sha256-client-cert",
  107. &TestConfig::expect_sha256_client_cert },
  108. { "-read-with-unfinished-write", &TestConfig::read_with_unfinished_write },
  109. { "-expect-secure-renegotiation",
  110. &TestConfig::expect_secure_renegotiation },
  111. { "-expect-no-secure-renegotiation",
  112. &TestConfig::expect_no_secure_renegotiation },
  113. { "-expect-session-id", &TestConfig::expect_session_id },
  114. { "-expect-no-session-id", &TestConfig::expect_no_session_id },
  115. { "-expect-accept-early-data", &TestConfig::expect_accept_early_data },
  116. { "-expect-reject-early-data", &TestConfig::expect_reject_early_data },
  117. { "-expect-no-offer-early-data", &TestConfig::expect_no_offer_early_data },
  118. { "-no-op-extra-handshake", &TestConfig::no_op_extra_handshake },
  119. { "-handshake-twice", &TestConfig::handshake_twice },
  120. { "-allow-unknown-alpn-protos", &TestConfig::allow_unknown_alpn_protos },
  121. { "-enable-ed25519", &TestConfig::enable_ed25519 },
  122. { "-use-custom-verify-callback", &TestConfig::use_custom_verify_callback },
  123. { "-allow-false-start-without-alpn",
  124. &TestConfig::allow_false_start_without_alpn },
  125. { "-expect-draft-downgrade", &TestConfig::expect_draft_downgrade },
  126. { "-handoff", &TestConfig::handoff },
  127. { "-expect-dummy-pq-padding", &TestConfig::expect_dummy_pq_padding },
  128. { "-no-rsa-pss-rsae-certs", &TestConfig::no_rsa_pss_rsae_certs },
  129. };
  130. const Flag<std::string> kStringFlags[] = {
  131. { "-write-settings", &TestConfig::write_settings },
  132. { "-key-file", &TestConfig::key_file },
  133. { "-cert-file", &TestConfig::cert_file },
  134. { "-expect-server-name", &TestConfig::expected_server_name },
  135. { "-advertise-npn", &TestConfig::advertise_npn },
  136. { "-expect-next-proto", &TestConfig::expected_next_proto },
  137. { "-select-next-proto", &TestConfig::select_next_proto },
  138. { "-send-channel-id", &TestConfig::send_channel_id },
  139. { "-host-name", &TestConfig::host_name },
  140. { "-advertise-alpn", &TestConfig::advertise_alpn },
  141. { "-expect-alpn", &TestConfig::expected_alpn },
  142. { "-expect-late-alpn", &TestConfig::expected_late_alpn },
  143. { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
  144. { "-select-alpn", &TestConfig::select_alpn },
  145. { "-psk", &TestConfig::psk },
  146. { "-psk-identity", &TestConfig::psk_identity },
  147. { "-srtp-profiles", &TestConfig::srtp_profiles },
  148. { "-cipher", &TestConfig::cipher },
  149. { "-export-label", &TestConfig::export_label },
  150. { "-export-context", &TestConfig::export_context },
  151. { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
  152. { "-use-client-ca-list", &TestConfig::use_client_ca_list },
  153. { "-expect-client-ca-list", &TestConfig::expected_client_ca_list },
  154. { "-expect-msg-callback", &TestConfig::expect_msg_callback },
  155. };
  156. const Flag<std::string> kBase64Flags[] = {
  157. { "-expect-certificate-types", &TestConfig::expected_certificate_types },
  158. { "-expect-channel-id", &TestConfig::expected_channel_id },
  159. { "-token-binding-params", &TestConfig::send_token_binding_params },
  160. { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
  161. { "-expect-signed-cert-timestamps",
  162. &TestConfig::expected_signed_cert_timestamps },
  163. { "-ocsp-response", &TestConfig::ocsp_response },
  164. { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
  165. { "-ticket-key", &TestConfig::ticket_key },
  166. { "-quic-transport-params", &TestConfig::quic_transport_params },
  167. { "-expected-quic-transport-params",
  168. &TestConfig::expected_quic_transport_params },
  169. };
  170. const Flag<int> kIntFlags[] = {
  171. { "-port", &TestConfig::port },
  172. { "-resume-count", &TestConfig::resume_count },
  173. { "-expected-token-binding-param",
  174. &TestConfig::expected_token_binding_param },
  175. { "-min-version", &TestConfig::min_version },
  176. { "-max-version", &TestConfig::max_version },
  177. { "-expect-version", &TestConfig::expect_version },
  178. { "-mtu", &TestConfig::mtu },
  179. { "-export-early-keying-material",
  180. &TestConfig::export_early_keying_material },
  181. { "-export-keying-material", &TestConfig::export_keying_material },
  182. { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
  183. { "-expect-peer-signature-algorithm",
  184. &TestConfig::expect_peer_signature_algorithm },
  185. { "-expect-curve-id", &TestConfig::expect_curve_id },
  186. { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
  187. { "-max-cert-list", &TestConfig::max_cert_list },
  188. { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
  189. { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
  190. { "-resumption-delay", &TestConfig::resumption_delay },
  191. { "-max-send-fragment", &TestConfig::max_send_fragment },
  192. { "-read-size", &TestConfig::read_size },
  193. { "-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew },
  194. { "-tls13-variant", &TestConfig::tls13_variant },
  195. { "-dummy-pq-padding-len", &TestConfig::dummy_pq_padding_len },
  196. };
  197. const Flag<std::vector<int>> kIntVectorFlags[] = {
  198. { "-signing-prefs", &TestConfig::signing_prefs },
  199. { "-verify-prefs", &TestConfig::verify_prefs },
  200. };
  201. bool ParseFlag(char *flag, int argc, char **argv, int *i,
  202. bool skip, TestConfig *out_config) {
  203. bool *bool_field = FindField(out_config, kBoolFlags, flag);
  204. if (bool_field != NULL) {
  205. if (!skip) {
  206. *bool_field = true;
  207. }
  208. return true;
  209. }
  210. std::string *string_field = FindField(out_config, kStringFlags, flag);
  211. if (string_field != NULL) {
  212. *i = *i + 1;
  213. if (*i >= argc) {
  214. fprintf(stderr, "Missing parameter\n");
  215. return false;
  216. }
  217. if (!skip) {
  218. string_field->assign(argv[*i]);
  219. }
  220. return true;
  221. }
  222. std::string *base64_field = FindField(out_config, kBase64Flags, flag);
  223. if (base64_field != NULL) {
  224. *i = *i + 1;
  225. if (*i >= argc) {
  226. fprintf(stderr, "Missing parameter\n");
  227. return false;
  228. }
  229. size_t len;
  230. if (!EVP_DecodedLength(&len, strlen(argv[*i]))) {
  231. fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
  232. return false;
  233. }
  234. std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
  235. if (!EVP_DecodeBase64(decoded.get(), &len, len,
  236. reinterpret_cast<const uint8_t *>(argv[*i]),
  237. strlen(argv[*i]))) {
  238. fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
  239. return false;
  240. }
  241. if (!skip) {
  242. base64_field->assign(reinterpret_cast<const char *>(decoded.get()),
  243. len);
  244. }
  245. return true;
  246. }
  247. int *int_field = FindField(out_config, kIntFlags, flag);
  248. if (int_field) {
  249. *i = *i + 1;
  250. if (*i >= argc) {
  251. fprintf(stderr, "Missing parameter\n");
  252. return false;
  253. }
  254. if (!skip) {
  255. *int_field = atoi(argv[*i]);
  256. }
  257. return true;
  258. }
  259. std::vector<int> *int_vector_field =
  260. FindField(out_config, kIntVectorFlags, flag);
  261. if (int_vector_field) {
  262. *i = *i + 1;
  263. if (*i >= argc) {
  264. fprintf(stderr, "Missing parameter\n");
  265. return false;
  266. }
  267. // Each instance of the flag adds to the list.
  268. if (!skip) {
  269. int_vector_field->push_back(atoi(argv[*i]));
  270. }
  271. return true;
  272. }
  273. fprintf(stderr, "Unknown argument: %s\n", flag);
  274. return false;
  275. }
  276. const char kInit[] = "-on-initial";
  277. const char kResume[] = "-on-resume";
  278. const char kRetry[] = "-on-retry";
  279. } // namespace
  280. bool ParseConfig(int argc, char **argv,
  281. TestConfig *out_initial,
  282. TestConfig *out_resume,
  283. TestConfig *out_retry) {
  284. for (int i = 0; i < argc; i++) {
  285. bool skip = false;
  286. char *flag = argv[i];
  287. if (strncmp(flag, kInit, strlen(kInit)) == 0) {
  288. if (!ParseFlag(flag + strlen(kInit), argc, argv, &i, skip, out_initial)) {
  289. return false;
  290. }
  291. } else if (strncmp(flag, kResume, strlen(kResume)) == 0) {
  292. if (!ParseFlag(flag + strlen(kResume), argc, argv, &i, skip,
  293. out_resume)) {
  294. return false;
  295. }
  296. } else if (strncmp(flag, kRetry, strlen(kRetry)) == 0) {
  297. if (!ParseFlag(flag + strlen(kRetry), argc, argv, &i, skip, out_retry)) {
  298. return false;
  299. }
  300. } else {
  301. int i_init = i;
  302. int i_resume = i;
  303. if (!ParseFlag(flag, argc, argv, &i_init, skip, out_initial) ||
  304. !ParseFlag(flag, argc, argv, &i_resume, skip, out_resume) ||
  305. !ParseFlag(flag, argc, argv, &i, skip, out_retry)) {
  306. return false;
  307. }
  308. }
  309. }
  310. return true;
  311. }