test_checkdir.c 4.8 KB

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