conf_include_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 CA_default/default_days is set */
  82. val = 0;
  83. if (!TEST_int_eq(NCONF_get_number(conf, "CA_default", "default_days", &val), 1)
  84. || !TEST_int_eq(val, 365)) {
  85. TEST_note("default_days incorrect");
  86. return 0;
  87. }
  88. /* verify whether req/default_bits is set */
  89. val = 0;
  90. if (!TEST_int_eq(NCONF_get_number(conf, "req", "default_bits", &val), 1)
  91. || !TEST_int_eq(val, 2048)) {
  92. TEST_note("default_bits incorrect");
  93. return 0;
  94. }
  95. /* verify whether countryName_default is set correctly */
  96. str = NCONF_get_string(conf, "req_distinguished_name", "countryName_default");
  97. if (!TEST_ptr(str) || !TEST_str_eq(str, "AU")) {
  98. TEST_note("countryName_default incorrect");
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. static int test_check_null_numbers(void)
  104. {
  105. #if defined(_BSD_SOURCE) \
  106. || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
  107. || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
  108. long val = 0;
  109. /* Verify that a NULL config with a present environment variable returns
  110. * success and the value.
  111. */
  112. if (!TEST_int_eq(setenv("FNORD", "123", 1), 0)
  113. || !TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
  114. || !TEST_long_eq(val, 123)) {
  115. TEST_note("environment variable with NULL conf failed");
  116. return 0;
  117. }
  118. /*
  119. * Verify that a NULL config with a missing environment variable returns
  120. * a failure code.
  121. */
  122. if (!TEST_int_eq(unsetenv("FNORD"), 0)
  123. || !TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) {
  124. TEST_note("missing environment variable with NULL conf failed");
  125. return 0;
  126. }
  127. #endif
  128. return 1;
  129. }
  130. static int test_check_overflow(void)
  131. {
  132. #if defined(_BSD_SOURCE) \
  133. || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
  134. || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
  135. long val = 0;
  136. char max[(sizeof(long) * 8) / 3 + 3];
  137. char *p;
  138. p = max + sprintf(max, "0%ld", LONG_MAX) - 1;
  139. setenv("FNORD", max, 1);
  140. if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
  141. || !TEST_long_eq(val, LONG_MAX))
  142. return 0;
  143. while (++*p > '9')
  144. *p-- = '0';
  145. setenv("FNORD", max, 1);
  146. if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
  147. return 0;
  148. #endif
  149. return 1;
  150. }
  151. typedef enum OPTION_choice {
  152. OPT_ERR = -1,
  153. OPT_EOF = 0,
  154. OPT_FAIL,
  155. OPT_TEST_ENUM
  156. } OPTION_CHOICE;
  157. const OPTIONS *test_get_options(void)
  158. {
  159. static const OPTIONS test_options[] = {
  160. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
  161. { "f", OPT_FAIL, '-', "A failure is expected" },
  162. { NULL }
  163. };
  164. return test_options;
  165. }
  166. int setup_tests(void)
  167. {
  168. const char *conf_file;
  169. OPTION_CHOICE o;
  170. if (!TEST_ptr(conf = NCONF_new(NULL)))
  171. return 0;
  172. while ((o = opt_next()) != OPT_EOF) {
  173. switch (o) {
  174. case OPT_FAIL:
  175. expect_failure = 1;
  176. break;
  177. case OPT_TEST_CASES:
  178. break;
  179. default:
  180. return 0;
  181. }
  182. }
  183. conf_file = test_get_argument(0);
  184. if (!TEST_ptr(conf_file)
  185. || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) {
  186. TEST_note("Unable to open the file argument");
  187. return 0;
  188. }
  189. /*
  190. * For this test we need to chdir as we use relative
  191. * path names in the config files.
  192. */
  193. change_path(conf_file);
  194. ADD_TEST(test_load_config);
  195. ADD_TEST(test_check_null_numbers);
  196. ADD_TEST(test_check_overflow);
  197. return 1;
  198. }
  199. void cleanup_tests(void)
  200. {
  201. BIO_vfree(in);
  202. NCONF_free(conf);
  203. CONF_modules_unload(1);
  204. }