1 /* Copyright (c) 2014-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
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)
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. */
27 test_checkdir_perms(void *testdata
)
30 or_options_t
*options
= get_options_mutable();
31 const char *subdir
= "test_checkdir";
33 cpd_check_t cpd_chkopts
;
34 cpd_check_t unix_create_opts
;
35 cpd_check_t unix_verify_optsmask
;
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
));
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
));
63 /* test: should get an error on existing dir with
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
));
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
));
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
));
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
));
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
));
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
));
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
));
142 #define CHECKDIR(name,flags) \
143 { #name, test_checkdir_##name, (flags), NULL, NULL }
145 struct testcase_t checkdir_tests
[] = {
146 CHECKDIR(perms
, TT_FORK
),