conf_include_test.c 5.7 KB

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