test_checkdir.c 5.2 KB

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