test_checkdir.c 4.7 KB

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