test_checkdir.c 4.8 KB

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