conf_include_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #include <stdlib.h>
  10. #include <string.h>
  11. #include <openssl/conf.h>
  12. #include <openssl/err.h>
  13. #include "testutil.h"
  14. #ifdef _WIN32
  15. # include <direct.h>
  16. # define DIRSEP "/\\"
  17. # ifndef __BORLANDC__
  18. # define chdir _chdir
  19. # endif
  20. # define DIRSEP_PRESERVE 0
  21. #elif !defined(OPENSSL_NO_POSIX_IO)
  22. # include <unistd.h>
  23. # ifndef OPENSSL_SYS_VMS
  24. # define DIRSEP "/"
  25. # define DIRSEP_PRESERVE 0
  26. # else
  27. # define DIRSEP "/]:"
  28. # define DIRSEP_PRESERVE 1
  29. # endif
  30. #else
  31. /* the test does not work without chdir() */
  32. # define chdir(x) (-1);
  33. # define DIRSEP "/"
  34. # define DIRSEP_PRESERVE 0
  35. #endif
  36. /* changes path to that of the filename */
  37. static int change_path(const char *file)
  38. {
  39. char *s = OPENSSL_strdup(file);
  40. char *p = s;
  41. char *last = NULL;
  42. int ret;
  43. if (s == NULL)
  44. return -1;
  45. while ((p = strpbrk(p, DIRSEP)) != NULL) {
  46. last = p++;
  47. }
  48. if (last == NULL)
  49. return 0;
  50. last[DIRSEP_PRESERVE] = 0;
  51. TEST_note("changing path to %s", s);
  52. ret = chdir(s);
  53. OPENSSL_free(s);
  54. return ret;
  55. }
  56. /*
  57. * This test program checks the operation of the .include directive.
  58. */
  59. static CONF *conf;
  60. static BIO *in;
  61. static int expect_failure = 0;
  62. static int test_load_config(void)
  63. {
  64. long errline;
  65. long val;
  66. char *str;
  67. long err;
  68. if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0)
  69. || !TEST_int_eq(err = ERR_peek_error(), 0)) {
  70. if (expect_failure)
  71. return 1;
  72. TEST_note("Failure loading the configuration at line %ld", errline);
  73. return 0;
  74. }
  75. if (expect_failure) {
  76. TEST_note("Failure expected but did not happen");
  77. return 0;
  78. }
  79. if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) {
  80. TEST_note("Failed in CONF_modules_load");
  81. return 0;
  82. }
  83. /* verify whether CA_default/default_days is set */
  84. val = 0;
  85. if (!TEST_int_eq(NCONF_get_number(conf, "CA_default", "default_days", &val), 1)
  86. || !TEST_int_eq(val, 365)) {
  87. TEST_note("default_days incorrect");
  88. return 0;
  89. }
  90. /* verify whether req/default_bits is set */
  91. val = 0;
  92. if (!TEST_int_eq(NCONF_get_number(conf, "req", "default_bits", &val), 1)
  93. || !TEST_int_eq(val, 2048)) {
  94. TEST_note("default_bits incorrect");
  95. return 0;
  96. }
  97. /* verify whether countryName_default is set correctly */
  98. str = NCONF_get_string(conf, "req_distinguished_name", "countryName_default");
  99. if (!TEST_ptr(str) || !TEST_str_eq(str, "AU")) {
  100. TEST_note("countryName_default incorrect");
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. static int test_check_null_numbers(void)
  106. {
  107. #if defined(_BSD_SOURCE) \
  108. || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
  109. || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
  110. long val = 0;
  111. /* Verify that a NULL config with a present environment variable returns
  112. * success and the value.
  113. */
  114. if (!TEST_int_eq(setenv("FNORD", "123", 1), 0)
  115. || !TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
  116. || !TEST_long_eq(val, 123)) {
  117. TEST_note("environment variable with NULL conf failed");
  118. return 0;
  119. }
  120. /*
  121. * Verify that a NULL config with a missing environment variable returns
  122. * a failure code.
  123. */
  124. if (!TEST_int_eq(unsetenv("FNORD"), 0)
  125. || !TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) {
  126. TEST_note("missing environment variable with NULL conf failed");
  127. return 0;
  128. }
  129. #endif
  130. return 1;
  131. }
  132. static int test_check_overflow(void)
  133. {
  134. #if defined(_BSD_SOURCE) \
  135. || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
  136. || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
  137. long val = 0;
  138. char max[(sizeof(long) * 8) / 3 + 3];
  139. char *p;
  140. p = max + sprintf(max, "0%ld", LONG_MAX) - 1;
  141. setenv("FNORD", max, 1);
  142. if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
  143. || !TEST_long_eq(val, LONG_MAX))
  144. return 0;
  145. while (++*p > '9')
  146. *p-- = '0';
  147. setenv("FNORD", max, 1);
  148. if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
  149. return 0;
  150. #endif
  151. return 1;
  152. }
  153. typedef enum OPTION_choice {
  154. OPT_ERR = -1,
  155. OPT_EOF = 0,
  156. OPT_FAIL,
  157. OPT_TEST_ENUM
  158. } OPTION_CHOICE;
  159. const OPTIONS *test_get_options(void)
  160. {
  161. static const OPTIONS test_options[] = {
  162. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
  163. { "f", OPT_FAIL, '-', "A failure is expected" },
  164. { NULL }
  165. };
  166. return test_options;
  167. }
  168. int setup_tests(void)
  169. {
  170. const char *conf_file;
  171. OPTION_CHOICE o;
  172. if (!TEST_ptr(conf = NCONF_new(NULL)))
  173. return 0;
  174. while ((o = opt_next()) != OPT_EOF) {
  175. switch (o) {
  176. case OPT_FAIL:
  177. expect_failure = 1;
  178. break;
  179. case OPT_TEST_CASES:
  180. break;
  181. default:
  182. return 0;
  183. }
  184. }
  185. conf_file = test_get_argument(0);
  186. if (!TEST_ptr(conf_file)
  187. || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) {
  188. TEST_note("Unable to open the file argument");
  189. return 0;
  190. }
  191. /*
  192. * For this test we need to chdir as we use relative
  193. * path names in the config files.
  194. */
  195. change_path(conf_file);
  196. ADD_TEST(test_load_config);
  197. ADD_TEST(test_check_null_numbers);
  198. ADD_TEST(test_check_overflow);
  199. return 1;
  200. }
  201. void cleanup_tests(void)
  202. {
  203. BIO_vfree(in);
  204. NCONF_free(conf);
  205. CONF_modules_unload(1);
  206. }