conf_include_test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2016-2018 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. #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. int setup_tests(void)
  110. {
  111. const char *conf_file;
  112. const char *arg2;
  113. if (!TEST_ptr(conf = NCONF_new(NULL)))
  114. return 0;
  115. conf_file = test_get_argument(0);
  116. if (!TEST_ptr(conf_file)
  117. || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) {
  118. TEST_note("Unable to open the file argument");
  119. return 0;
  120. }
  121. if ((arg2 = test_get_argument(1)) != NULL && *arg2 == 'f') {
  122. expect_failure = 1;
  123. }
  124. /*
  125. * For this test we need to chdir as we use relative
  126. * path names in the config files.
  127. */
  128. change_path(conf_file);
  129. ADD_TEST(test_load_config);
  130. return 1;
  131. }
  132. void cleanup_tests(void)
  133. {
  134. BIO_vfree(in);
  135. NCONF_free(conf);
  136. CONF_modules_unload(1);
  137. }