test_checkdir.c 5.0 KB

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