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

197 строки
6.5 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. { "-cookie-exchange", &TestConfig::cookie_exchange },
  56. { "-shim-writes-first", &TestConfig::shim_writes_first },
  57. { "-tls-d5-bug", &TestConfig::tls_d5_bug },
  58. { "-expect-session-miss", &TestConfig::expect_session_miss },
  59. { "-expect-extended-master-secret",
  60. &TestConfig::expect_extended_master_secret },
  61. { "-renegotiate", &TestConfig::renegotiate },
  62. { "-allow-unsafe-legacy-renegotiation",
  63. &TestConfig::allow_unsafe_legacy_renegotiation },
  64. { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
  65. { "-enable-signed-cert-timestamps",
  66. &TestConfig::enable_signed_cert_timestamps },
  67. { "-fastradio-padding", &TestConfig::fastradio_padding },
  68. };
  69. const Flag<std::string> kStringFlags[] = {
  70. { "-key-file", &TestConfig::key_file },
  71. { "-cert-file", &TestConfig::cert_file },
  72. { "-expect-server-name", &TestConfig::expected_server_name },
  73. { "-advertise-npn", &TestConfig::advertise_npn },
  74. { "-expect-next-proto", &TestConfig::expected_next_proto },
  75. { "-select-next-proto", &TestConfig::select_next_proto },
  76. { "-send-channel-id", &TestConfig::send_channel_id },
  77. { "-host-name", &TestConfig::host_name },
  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 },
  82. { "-psk", &TestConfig::psk },
  83. { "-psk-identity", &TestConfig::psk_identity },
  84. { "-srtp-profiles", &TestConfig::srtp_profiles },
  85. };
  86. const Flag<std::string> kBase64Flags[] = {
  87. { "-expect-certificate-types", &TestConfig::expected_certificate_types },
  88. { "-expect-channel-id", &TestConfig::expected_channel_id },
  89. { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
  90. { "-expect-signed-cert-timestamps",
  91. &TestConfig::expected_signed_cert_timestamps },
  92. };
  93. const Flag<int> kIntFlags[] = {
  94. { "-min-version", &TestConfig::min_version },
  95. { "-max-version", &TestConfig::max_version },
  96. };
  97. } // namespace
  98. TestConfig::TestConfig()
  99. : is_server(false),
  100. is_dtls(false),
  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),
  112. no_ssl3(false),
  113. cookie_exchange(false),
  114. shim_writes_first(false),
  115. tls_d5_bug(false),
  116. expect_session_miss(false),
  117. expect_extended_master_secret(false),
  118. renegotiate(false),
  119. allow_unsafe_legacy_renegotiation(false),
  120. enable_ocsp_stapling(false),
  121. enable_signed_cert_timestamps(false),
  122. fastradio_padding(false),
  123. min_version(0),
  124. max_version(0) {
  125. }
  126. bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
  127. for (int i = 0; i < argc; i++) {
  128. bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
  129. if (bool_field != NULL) {
  130. *bool_field = true;
  131. continue;
  132. }
  133. std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
  134. if (string_field != NULL) {
  135. i++;
  136. if (i >= argc) {
  137. fprintf(stderr, "Missing parameter\n");
  138. return false;
  139. }
  140. string_field->assign(argv[i]);
  141. continue;
  142. }
  143. std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
  144. if (base64_field != NULL) {
  145. i++;
  146. if (i >= argc) {
  147. fprintf(stderr, "Missing parameter\n");
  148. return false;
  149. }
  150. size_t len;
  151. if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
  152. fprintf(stderr, "Invalid base64: %s\n", argv[i]);
  153. }
  154. std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
  155. if (!EVP_DecodeBase64(decoded.get(), &len, len,
  156. reinterpret_cast<const uint8_t *>(argv[i]),
  157. strlen(argv[i]))) {
  158. fprintf(stderr, "Invalid base64: %s\n", argv[i]);
  159. }
  160. base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
  161. continue;
  162. }
  163. int *int_field = FindField(out_config, kIntFlags, argv[i]);
  164. if (int_field) {
  165. i++;
  166. if (i >= argc) {
  167. fprintf(stderr, "Missing parameter\n");
  168. return false;
  169. }
  170. *int_field = atoi(argv[i]);
  171. continue;
  172. }
  173. fprintf(stderr, "Unknown argument: %s\n", argv[i]);
  174. return false;
  175. }
  176. return true;
  177. }