Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

191 строка
7.0 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. { "-resume", &TestConfig::resume },
  42. { "-fallback-scsv", &TestConfig::fallback_scsv },
  43. { "-require-any-client-certificate",
  44. &TestConfig::require_any_client_certificate },
  45. { "-false-start", &TestConfig::false_start },
  46. { "-async", &TestConfig::async },
  47. { "-write-different-record-sizes",
  48. &TestConfig::write_different_record_sizes },
  49. { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
  50. { "-partial-write", &TestConfig::partial_write },
  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. { "-shim-writes-first", &TestConfig::shim_writes_first },
  56. { "-tls-d5-bug", &TestConfig::tls_d5_bug },
  57. { "-expect-session-miss", &TestConfig::expect_session_miss },
  58. { "-expect-extended-master-secret",
  59. &TestConfig::expect_extended_master_secret },
  60. { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
  61. { "-enable-signed-cert-timestamps",
  62. &TestConfig::enable_signed_cert_timestamps },
  63. { "-implicit-handshake", &TestConfig::implicit_handshake },
  64. { "-use-early-callback", &TestConfig::use_early_callback },
  65. { "-fail-early-callback", &TestConfig::fail_early_callback },
  66. { "-install-ddos-callback", &TestConfig::install_ddos_callback },
  67. { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
  68. { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
  69. { "-handshake-never-done", &TestConfig::handshake_never_done },
  70. { "-use-export-context", &TestConfig::use_export_context },
  71. { "-reject-peer-renegotiations", &TestConfig::reject_peer_renegotiations },
  72. { "-no-legacy-server-connect", &TestConfig::no_legacy_server_connect },
  73. { "-tls-unique", &TestConfig::tls_unique },
  74. { "-use-async-private-key", &TestConfig::use_async_private_key },
  75. { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
  76. { "-expect-no-session", &TestConfig::expect_no_session },
  77. { "-use-ticket-callback", &TestConfig::use_ticket_callback },
  78. { "-renew-ticket", &TestConfig::renew_ticket },
  79. { "-enable-client-custom-extension",
  80. &TestConfig::enable_client_custom_extension },
  81. { "-enable-server-custom-extension",
  82. &TestConfig::enable_server_custom_extension },
  83. { "-custom-extension-skip", &TestConfig::custom_extension_skip },
  84. { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
  85. };
  86. const Flag<std::string> kStringFlags[] = {
  87. { "-key-file", &TestConfig::key_file },
  88. { "-cert-file", &TestConfig::cert_file },
  89. { "-expect-server-name", &TestConfig::expected_server_name },
  90. { "-advertise-npn", &TestConfig::advertise_npn },
  91. { "-expect-next-proto", &TestConfig::expected_next_proto },
  92. { "-select-next-proto", &TestConfig::select_next_proto },
  93. { "-send-channel-id", &TestConfig::send_channel_id },
  94. { "-host-name", &TestConfig::host_name },
  95. { "-advertise-alpn", &TestConfig::advertise_alpn },
  96. { "-expect-alpn", &TestConfig::expected_alpn },
  97. { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
  98. { "-select-alpn", &TestConfig::select_alpn },
  99. { "-psk", &TestConfig::psk },
  100. { "-psk-identity", &TestConfig::psk_identity },
  101. { "-srtp-profiles", &TestConfig::srtp_profiles },
  102. { "-cipher", &TestConfig::cipher },
  103. { "-export-label", &TestConfig::export_label },
  104. { "-export-context", &TestConfig::export_context },
  105. };
  106. const Flag<std::string> kBase64Flags[] = {
  107. { "-expect-certificate-types", &TestConfig::expected_certificate_types },
  108. { "-expect-channel-id", &TestConfig::expected_channel_id },
  109. { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
  110. { "-expect-signed-cert-timestamps",
  111. &TestConfig::expected_signed_cert_timestamps },
  112. };
  113. const Flag<int> kIntFlags[] = {
  114. { "-port", &TestConfig::port },
  115. { "-min-version", &TestConfig::min_version },
  116. { "-max-version", &TestConfig::max_version },
  117. { "-mtu", &TestConfig::mtu },
  118. { "-export-keying-material", &TestConfig::export_keying_material },
  119. };
  120. } // namespace
  121. bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
  122. for (int i = 0; i < argc; i++) {
  123. bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
  124. if (bool_field != NULL) {
  125. *bool_field = true;
  126. continue;
  127. }
  128. std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
  129. if (string_field != NULL) {
  130. i++;
  131. if (i >= argc) {
  132. fprintf(stderr, "Missing parameter\n");
  133. return false;
  134. }
  135. string_field->assign(argv[i]);
  136. continue;
  137. }
  138. std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
  139. if (base64_field != NULL) {
  140. i++;
  141. if (i >= argc) {
  142. fprintf(stderr, "Missing parameter\n");
  143. return false;
  144. }
  145. size_t len;
  146. if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
  147. fprintf(stderr, "Invalid base64: %s\n", argv[i]);
  148. }
  149. std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
  150. if (!EVP_DecodeBase64(decoded.get(), &len, len,
  151. reinterpret_cast<const uint8_t *>(argv[i]),
  152. strlen(argv[i]))) {
  153. fprintf(stderr, "Invalid base64: %s\n", argv[i]);
  154. }
  155. base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
  156. continue;
  157. }
  158. int *int_field = FindField(out_config, kIntFlags, argv[i]);
  159. if (int_field) {
  160. i++;
  161. if (i >= argc) {
  162. fprintf(stderr, "Missing parameter\n");
  163. return false;
  164. }
  165. *int_field = atoi(argv[i]);
  166. continue;
  167. }
  168. fprintf(stderr, "Unknown argument: %s\n", argv[i]);
  169. return false;
  170. }
  171. return true;
  172. }