dirvote: Handling adding vote and signature if module is disabled
[tor.git] / src / test / test_checkdir.c
blobbf6a8376b3b0ae8f3f9ae3af62d7ae8e0179c953
1 /* Copyright (c) 2014-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
5 #include "or.h"
7 #ifdef _WIN32
8 #include <direct.h>
9 #else
10 #include <dirent.h>
11 #endif
13 #include "config.h"
14 #include "test.h"
15 #include "util.h"
17 #ifdef _WIN32
18 #define mkdir(a,b) mkdir(a)
19 #define tt_int_op_nowin(a,op,b) do { (void)(a); (void)(b); } while (0)
20 #define umask(mask) ((void)0)
21 #else
22 #define tt_int_op_nowin(a,op,b) tt_int_op((a),op,(b))
23 #endif /* defined(_WIN32) */
25 /** Run unit tests for private dir permission enforcement logic. */
26 static void
27 test_checkdir_perms(void *testdata)
29 (void)testdata;
30 or_options_t *options = get_options_mutable();
31 const char *subdir = "test_checkdir";
32 char *testdir = NULL;
33 cpd_check_t cpd_chkopts;
34 cpd_check_t unix_create_opts;
35 cpd_check_t unix_verify_optsmask;
36 struct stat st;
38 umask(022);
40 /* setup data directory before tests. */
41 tor_free(options->DataDirectory);
42 options->DataDirectory = tor_strdup(get_fname(subdir));
43 tt_int_op(mkdir(options->DataDirectory, 0750), OP_EQ, 0);
45 /* test: create new dir, no flags. */
46 testdir = get_datadir_fname("checkdir_new_none");
47 cpd_chkopts = CPD_CREATE;
48 unix_verify_optsmask = 0077;
49 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
50 tt_int_op(0, OP_EQ, stat(testdir, &st));
51 tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
52 tor_free(testdir);
54 /* test: create new dir, CPD_GROUP_OK option set. */
55 testdir = get_datadir_fname("checkdir_new_groupok");
56 cpd_chkopts = CPD_CREATE|CPD_GROUP_OK;
57 unix_verify_optsmask = 0077;
58 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
59 tt_int_op(0, OP_EQ, stat(testdir, &st));
60 tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
61 tor_free(testdir);
63 /* test: should get an error on existing dir with
64 wrong perms */
65 testdir = get_datadir_fname("checkdir_new_groupok_err");
66 tt_int_op(0, OP_EQ, mkdir(testdir, 027));
67 cpd_chkopts = CPD_CHECK_MODE_ONLY|CPD_CREATE|CPD_GROUP_OK;
68 tt_int_op_nowin(-1, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
69 tor_free(testdir);
71 /* test: create new dir, CPD_GROUP_READ option set. */
72 testdir = get_datadir_fname("checkdir_new_groupread");
73 cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
74 unix_verify_optsmask = 0027;
75 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
76 tt_int_op(0, OP_EQ, stat(testdir, &st));
77 tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
78 tor_free(testdir);
80 /* test: check existing dir created with defaults,
81 and verify with CPD_CREATE only. */
82 testdir = get_datadir_fname("checkdir_exists_none");
83 cpd_chkopts = CPD_CREATE;
84 unix_create_opts = 0700;
85 (void)unix_create_opts;
86 unix_verify_optsmask = 0077;
87 tt_int_op(0, OP_EQ, mkdir(testdir, unix_create_opts));
88 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
89 tt_int_op(0, OP_EQ, stat(testdir, &st));
90 tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
91 tor_free(testdir);
93 /* test: check existing dir created with defaults,
94 and verify with CPD_GROUP_OK option set. */
95 testdir = get_datadir_fname("checkdir_exists_groupok");
96 cpd_chkopts = CPD_CREATE;
97 unix_verify_optsmask = 0077;
98 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
99 cpd_chkopts = CPD_GROUP_OK;
100 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
101 tt_int_op(0, OP_EQ, stat(testdir, &st));
102 tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
103 tor_free(testdir);
105 /* test: check existing dir created with defaults,
106 and verify with CPD_GROUP_READ option set. */
107 testdir = get_datadir_fname("checkdir_exists_groupread");
108 cpd_chkopts = CPD_CREATE;
109 unix_verify_optsmask = 0027;
110 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
111 cpd_chkopts = CPD_GROUP_READ;
112 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
113 tt_int_op(0, OP_EQ, stat(testdir, &st));
114 tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
115 tor_free(testdir);
117 /* test: check existing dir created with CPD_GROUP_READ,
118 and verify with CPD_GROUP_OK option set. */
119 testdir = get_datadir_fname("checkdir_existsread_groupok");
120 cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
121 unix_verify_optsmask = 0027;
122 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
123 cpd_chkopts = CPD_GROUP_OK;
124 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
125 tt_int_op(0, OP_EQ, stat(testdir, &st));
126 tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
127 tor_free(testdir);
129 /* test: check existing dir created with CPD_GROUP_READ,
130 and verify with CPD_GROUP_READ option set. */
131 testdir = get_datadir_fname("checkdir_existsread_groupread");
132 cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
133 unix_verify_optsmask = 0027;
134 tt_int_op(0, OP_EQ, check_private_dir(testdir, cpd_chkopts, NULL));
135 tt_int_op(0, OP_EQ, stat(testdir, &st));
136 tt_int_op_nowin(0, OP_EQ, (st.st_mode & unix_verify_optsmask));
138 done:
139 tor_free(testdir);
142 #define CHECKDIR(name,flags) \
143 { #name, test_checkdir_##name, (flags), NULL, NULL }
145 struct testcase_t checkdir_tests[] = {
146 CHECKDIR(perms, TT_FORK),
147 END_OF_TESTCASES