ssl_test_ctx_test.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Ideally, CONF should offer standard parsing methods and cover them
  11. * in tests. But since we have no CONF tests, we use a custom test for now.
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "e_os.h"
  16. #include "ssl_test_ctx.h"
  17. #include "testutil.h"
  18. #include <openssl/e_os2.h>
  19. #include <openssl/err.h>
  20. #include <openssl/conf.h>
  21. #include <openssl/ssl.h>
  22. static CONF *conf = NULL;
  23. typedef struct ssl_test_ctx_test_fixture {
  24. const char *test_case_name;
  25. const char *test_section;
  26. /* Expected parsed configuration. */
  27. SSL_TEST_CTX *expected_ctx;
  28. } SSL_TEST_CTX_TEST_FIXTURE;
  29. static int clientconf_eq(SSL_TEST_CLIENT_CONF *conf1,
  30. SSL_TEST_CLIENT_CONF *conf2)
  31. {
  32. if (!TEST_int_eq(conf1->verify_callback, conf2->verify_callback)
  33. || !TEST_int_eq(conf1->servername, conf2->servername)
  34. || !TEST_str_eq(conf1->npn_protocols, conf2->npn_protocols)
  35. || !TEST_str_eq(conf1->alpn_protocols, conf2->alpn_protocols)
  36. || !TEST_int_eq(conf1->ct_validation, conf2->ct_validation))
  37. return 0;
  38. return 1;
  39. }
  40. static int serverconf_eq(SSL_TEST_SERVER_CONF *serv,
  41. SSL_TEST_SERVER_CONF *serv2)
  42. {
  43. if (!TEST_int_eq(serv->servername_callback, serv2->servername_callback)
  44. || !TEST_str_eq(serv->npn_protocols, serv2->npn_protocols)
  45. || !TEST_str_eq(serv->alpn_protocols, serv2->alpn_protocols)
  46. || !TEST_int_eq(serv->broken_session_ticket,
  47. serv2->broken_session_ticket)
  48. || !TEST_int_eq(serv->cert_status, serv2->cert_status))
  49. return 0;
  50. return 1;
  51. }
  52. static int extraconf_eq(SSL_TEST_EXTRA_CONF *extra,
  53. SSL_TEST_EXTRA_CONF *extra2)
  54. {
  55. if (!TEST_true(clientconf_eq(&extra->client, &extra2->client))
  56. || !TEST_true(serverconf_eq(&extra->server, &extra2->server))
  57. || !TEST_true(serverconf_eq(&extra->server2, &extra2->server2)))
  58. return 0;
  59. return 1;
  60. }
  61. static int testctx_eq(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
  62. {
  63. if (!TEST_int_eq(ctx->method, ctx2->method)
  64. || !TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode)
  65. || !TEST_int_eq(ctx->app_data_size, ctx2->app_data_size)
  66. || !TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size)
  67. || !extraconf_eq(&ctx->extra, &ctx2->extra)
  68. || !extraconf_eq(&ctx->resume_extra, &ctx2->resume_extra)
  69. || !TEST_int_eq(ctx->expected_result, ctx2->expected_result)
  70. || !TEST_int_eq(ctx->expected_client_alert,
  71. ctx2->expected_client_alert)
  72. || !TEST_int_eq(ctx->expected_server_alert,
  73. ctx2->expected_server_alert)
  74. || !TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol)
  75. || !TEST_int_eq(ctx->expected_servername, ctx2->expected_servername)
  76. || !TEST_int_eq(ctx->session_ticket_expected,
  77. ctx2->session_ticket_expected)
  78. || !TEST_int_eq(ctx->compression_expected,
  79. ctx2->compression_expected)
  80. || !TEST_str_eq(ctx->expected_npn_protocol,
  81. ctx2->expected_npn_protocol)
  82. || !TEST_str_eq(ctx->expected_alpn_protocol,
  83. ctx2->expected_alpn_protocol)
  84. || !TEST_int_eq(ctx->resumption_expected,
  85. ctx2->resumption_expected))
  86. return 0;
  87. return 1;
  88. }
  89. static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
  90. {
  91. SSL_TEST_CTX_TEST_FIXTURE fixture;
  92. fixture.test_case_name = test_case_name;
  93. TEST_ptr(fixture.expected_ctx = SSL_TEST_CTX_new());
  94. return fixture;
  95. }
  96. static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
  97. {
  98. int success = 0;
  99. SSL_TEST_CTX *ctx;
  100. if (!TEST_ptr(ctx = SSL_TEST_CTX_create(conf, fixture.test_section))
  101. || !testctx_eq(ctx, fixture.expected_ctx))
  102. goto err;
  103. success = 1;
  104. err:
  105. SSL_TEST_CTX_free(ctx);
  106. return success;
  107. }
  108. static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
  109. {
  110. SSL_TEST_CTX_free(fixture.expected_ctx);
  111. }
  112. #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
  113. SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
  114. #define EXECUTE_SSL_TEST_CTX_TEST() \
  115. EXECUTE_TEST(execute_test, tear_down)
  116. static int test_empty_configuration()
  117. {
  118. SETUP_SSL_TEST_CTX_TEST_FIXTURE();
  119. fixture.test_section = "ssltest_default";
  120. fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
  121. EXECUTE_SSL_TEST_CTX_TEST();
  122. }
  123. static int test_good_configuration()
  124. {
  125. SETUP_SSL_TEST_CTX_TEST_FIXTURE();
  126. fixture.test_section = "ssltest_good";
  127. fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
  128. fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
  129. fixture.expected_ctx->app_data_size = 1024;
  130. fixture.expected_ctx->max_fragment_size = 2048;
  131. fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
  132. fixture.expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
  133. fixture.expected_ctx->expected_server_alert = 0; /* No alert. */
  134. fixture.expected_ctx->expected_protocol = TLS1_1_VERSION;
  135. fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
  136. fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
  137. fixture.expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
  138. fixture.expected_ctx->resumption_expected = 1;
  139. fixture.expected_ctx->extra.client.verify_callback =
  140. SSL_TEST_VERIFY_REJECT_ALL;
  141. fixture.expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
  142. fixture.expected_ctx->extra.client.npn_protocols =
  143. OPENSSL_strdup("foo,bar");
  144. TEST_check(fixture.expected_ctx->extra.client.npn_protocols != NULL);
  145. fixture.expected_ctx->extra.server.servername_callback =
  146. SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
  147. fixture.expected_ctx->extra.server.broken_session_ticket = 1;
  148. fixture.expected_ctx->resume_extra.server2.alpn_protocols =
  149. OPENSSL_strdup("baz");
  150. TEST_check(
  151. fixture.expected_ctx->resume_extra.server2.alpn_protocols != NULL);
  152. fixture.expected_ctx->resume_extra.client.ct_validation =
  153. SSL_TEST_CT_VALIDATION_STRICT;
  154. EXECUTE_SSL_TEST_CTX_TEST();
  155. }
  156. static const char *bad_configurations[] = {
  157. "ssltest_unknown_option",
  158. "ssltest_wrong_section",
  159. "ssltest_unknown_expected_result",
  160. "ssltest_unknown_alert",
  161. "ssltest_unknown_protocol",
  162. "ssltest_unknown_verify_callback",
  163. "ssltest_unknown_servername",
  164. "ssltest_unknown_servername_callback",
  165. "ssltest_unknown_session_ticket_expected",
  166. "ssltest_unknown_compression_expected",
  167. "ssltest_unknown_method",
  168. "ssltest_unknown_handshake_mode",
  169. "ssltest_unknown_resumption_expected",
  170. "ssltest_unknown_ct_validation",
  171. };
  172. static int test_bad_configuration(int idx)
  173. {
  174. SSL_TEST_CTX *ctx;
  175. if (!TEST_ptr_null(ctx = SSL_TEST_CTX_create(conf,
  176. bad_configurations[idx]))) {
  177. SSL_TEST_CTX_free(ctx);
  178. return 0;
  179. }
  180. return 1;
  181. }
  182. int test_main(int argc, char **argv)
  183. {
  184. int result = 0;
  185. if (argc != 2) {
  186. TEST_info("Missing file argument");
  187. goto end;
  188. }
  189. if (!TEST_ptr(conf = NCONF_new(NULL))
  190. /* argv[1] should point to test/ssl_test_ctx_test.conf */
  191. || !TEST_int_gt(NCONF_load(conf, argv[1], NULL), 0))
  192. goto end;
  193. ADD_TEST(test_empty_configuration);
  194. ADD_TEST(test_good_configuration);
  195. ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
  196. result = run_tests(argv[0]);
  197. end:
  198. NCONF_free(conf);
  199. return result;
  200. }