test_checkdir.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, STAT_RWXU), ==, 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 = STAT_RWXO|STAT_RWXG; /* 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 = STAT_RWXO|STAT_RWXG; /* 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: create new dir, CPD_GROUP_READ option set. */
  41. testdir = get_datadir_fname("checkdir_new_groupread");
  42. cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
  43. unix_verify_optsmask = STAT_RWXO|STAT_WGRP; /* 0027 */
  44. tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
  45. tt_int_op(0, ==, stat(testdir, &st));
  46. tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
  47. tor_free(testdir);
  48. /** test: check existing dir created with defaults,
  49. and verify with CPD_CREATE only. */
  50. testdir = get_datadir_fname("checkdir_exists_none");
  51. cpd_chkopts = CPD_CREATE;
  52. unix_create_opts = STAT_RWXU; /* 0700 */
  53. unix_verify_optsmask = STAT_RWXO|STAT_RWXG; /* 0077 */
  54. tt_int_op(0, ==, mkdir(testdir, unix_create_opts));
  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_GROUP_OK option set. */
  61. testdir = get_datadir_fname("checkdir_exists_groupok");
  62. cpd_chkopts = CPD_CREATE;
  63. unix_verify_optsmask = STAT_RWXO|STAT_RWXG; /* 0077 */
  64. tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
  65. cpd_chkopts = CPD_GROUP_OK;
  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_READ option set. */
  72. testdir = get_datadir_fname("checkdir_exists_groupread");
  73. cpd_chkopts = CPD_CREATE;
  74. unix_verify_optsmask = STAT_RWXO|STAT_WGRP; /* 0027 */
  75. tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
  76. cpd_chkopts = CPD_GROUP_READ;
  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 CPD_GROUP_READ,
  82. and verify with CPD_GROUP_OK option set. */
  83. testdir = get_datadir_fname("checkdir_existsread_groupok");
  84. cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
  85. unix_verify_optsmask = STAT_RWXO|STAT_WGRP; /* 0027 */
  86. tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
  87. cpd_chkopts = CPD_GROUP_OK;
  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_READ option set. */
  94. testdir = get_datadir_fname("checkdir_existsread_groupread");
  95. cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
  96. unix_verify_optsmask = STAT_RWXO|STAT_WGRP; /* 0027 */
  97. tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
  98. tt_int_op(0, ==, stat(testdir, &st));
  99. tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
  100. tor_free(testdir);
  101. done:
  102. ;
  103. }
  104. #define CHECKDIR(name,flags) \
  105. { #name, test_checkdir_##name, (flags), NULL, NULL }
  106. struct testcase_t checkdir_tests[] = {
  107. CHECKDIR(perms, 0),
  108. END_OF_TESTCASES
  109. };