test_checkdir.c 5.2 KB

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