test_checkdir.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (c) 2014-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include "or.h"
  5. #ifdef _WIN32
  6. #include <direct.h>
  7. #else
  8. #include <dirent.h>
  9. #endif
  10. #include "config.h"
  11. #include "test.h"
  12. #include "util.h"
  13. #ifdef _WIN32
  14. #define mkdir(a,b) mkdir(a)
  15. #define tt_int_op_nowin(a,op,b) do { (void)(a); (void)(b); } while (0)
  16. #define umask(mask) ((void)0)
  17. #else
  18. #define tt_int_op_nowin(a,op,b) tt_int_op((a),op,(b))
  19. #endif /* defined(_WIN32) */
  20. /** Run unit tests for private dir permission enforcement logic. */
  21. static void
  22. test_checkdir_perms(void *testdata)
  23. {
  24. (void)testdata;
  25. or_options_t *options = get_options_mutable();
  26. const char *subdir = "test_checkdir";
  27. char *testdir = NULL;
  28. cpd_check_t cpd_chkopts;
  29. cpd_check_t unix_create_opts;
  30. cpd_check_t unix_verify_optsmask;
  31. struct stat st;
  32. umask(022);
  33. /* setup data directory before tests. */
  34. tor_free(options->DataDirectory);
  35. options->DataDirectory = tor_strdup(get_fname(subdir));
  36. tt_int_op(mkdir(options->DataDirectory, 0750), OP_EQ, 0);
  37. /* test: create new dir, no flags. */
  38. testdir = get_datadir_fname("checkdir_new_none");
  39. cpd_chkopts = CPD_CREATE;
  40. unix_verify_optsmask = 0077;
  41. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  42. tt_int_op(0, OP_EQ, stat(testdir, &st));
  43. tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
  44. tor_free(testdir);
  45. /* test: create new dir, CPD_GROUP_OK option set. */
  46. testdir = get_datadir_fname("checkdir_new_groupok");
  47. cpd_chkopts = CPD_CREATE|CPD_GROUP_OK;
  48. unix_verify_optsmask = 0077;
  49. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  50. tt_int_op(0, OP_EQ, stat(testdir, &st));
  51. tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
  52. tor_free(testdir);
  53. /* test: should get an error on existing dir with
  54. wrong perms */
  55. testdir = get_datadir_fname("checkdir_new_groupok_err");
  56. tt_int_op(0, OP_EQ, mkdir(testdir, 027));
  57. cpd_chkopts = CPD_CHECK_MODE_ONLY|CPD_CREATE|CPD_GROUP_OK;
  58. tt_int_op_nowin(-1, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  59. tor_free(testdir);
  60. /* test: create new dir, CPD_GROUP_READ option set. */
  61. testdir = get_datadir_fname("checkdir_new_groupread");
  62. cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
  63. unix_verify_optsmask = 0027;
  64. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  65. tt_int_op(0, OP_EQ, stat(testdir, &st));
  66. tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
  67. tor_free(testdir);
  68. /* test: check existing dir created with defaults,
  69. and verify with CPD_CREATE only. */
  70. testdir = get_datadir_fname("checkdir_exists_none");
  71. cpd_chkopts = CPD_CREATE;
  72. unix_create_opts = 0700;
  73. (void)unix_create_opts;
  74. unix_verify_optsmask = 0077;
  75. tt_int_op(0, OP_EQ, mkdir(testdir, unix_create_opts));
  76. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  77. tt_int_op(0, OP_EQ, stat(testdir, &st));
  78. tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
  79. tor_free(testdir);
  80. /* test: check existing dir created with defaults,
  81. and verify with CPD_GROUP_OK option set. */
  82. testdir = get_datadir_fname("checkdir_exists_groupok");
  83. cpd_chkopts = CPD_CREATE;
  84. unix_verify_optsmask = 0077;
  85. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  86. cpd_chkopts = CPD_GROUP_OK;
  87. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  88. tt_int_op(0, OP_EQ, stat(testdir, &st));
  89. tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
  90. tor_free(testdir);
  91. /* test: check existing dir created with defaults,
  92. and verify with CPD_GROUP_READ option set. */
  93. testdir = get_datadir_fname("checkdir_exists_groupread");
  94. cpd_chkopts = CPD_CREATE;
  95. unix_verify_optsmask = 0027;
  96. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  97. cpd_chkopts = CPD_GROUP_READ;
  98. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  99. tt_int_op(0, OP_EQ, stat(testdir, &st));
  100. tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
  101. tor_free(testdir);
  102. /* test: check existing dir created with CPD_GROUP_READ,
  103. and verify with CPD_GROUP_OK option set. */
  104. testdir = get_datadir_fname("checkdir_existsread_groupok");
  105. cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
  106. unix_verify_optsmask = 0027;
  107. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  108. cpd_chkopts = CPD_GROUP_OK;
  109. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  110. tt_int_op(0, OP_EQ, stat(testdir, &st));
  111. tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
  112. tor_free(testdir);
  113. /* test: check existing dir created with CPD_GROUP_READ,
  114. and verify with CPD_GROUP_READ option set. */
  115. testdir = get_datadir_fname("checkdir_existsread_groupread");
  116. cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
  117. unix_verify_optsmask = 0027;
  118. tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
  119. tt_int_op(0, OP_EQ, stat(testdir, &st));
  120. tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
  121. done:
  122. tor_free(testdir);
  123. }
  124. #define CHECKDIR(name,flags) \
  125. { #name, test_checkdir_##name, (flags), NULL, NULL }
  126. struct testcase_t checkdir_tests[] = {
  127. CHECKDIR(perms, TT_FORK),
  128. END_OF_TESTCASES
  129. };