conf_include_test.c 5.9 KB

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