summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-11-05 14:11:47 -0500
committerNick Mathewson <nickm@torproject.org>2014-11-05 14:11:47 -0500
commit4df419a4b19c3b4033b964ec73e82aa988034c81 (patch)
treec9067b2662db7966201101b7e5afe41d99fdb432 /src/test
parent3d8cb107323fa5d9cc375087e69a9940b947d0e3 (diff)
parent3d0d49be230a8720ebdadf668b993f8ba2c5b2ca (diff)
downloadtor-4df419a4b19c3b4033b964ec73e82aa988034c81.tar.gz
tor-4df419a4b19c3b4033b964ec73e82aa988034c81.zip
Merge remote-tracking branch 'meejah/ticket-11291-extra-utests'
Conflicts: src/or/config.c
Diffstat (limited to 'src/test')
-rw-r--r--src/test/Makefile.nmake4
-rw-r--r--src/test/include.am1
-rw-r--r--src/test/test.c2
-rw-r--r--src/test/test_checkdir.c132
4 files changed, 137 insertions, 2 deletions
diff --git a/src/test/Makefile.nmake b/src/test/Makefile.nmake
index 822431f3b8..f6ee7f3f53 100644
--- a/src/test/Makefile.nmake
+++ b/src/test/Makefile.nmake
@@ -12,8 +12,8 @@ LIBS = ..\..\..\build-alpha\lib\libevent.lib \
crypt32.lib gdi32.lib user32.lib
TEST_OBJECTS = test.obj test_addr.obj test_containers.obj \
- test_controller_events.ogj test_crypto.obj test_data.obj test_dir.obj \
- test_microdesc.obj test_pt.obj test_util.obj test_config.obj \
+ test_controller_events.obj test_crypto.obj test_data.obj test_dir.obj \
+ test_checkdir.obj test_microdesc.obj test_pt.obj test_util.obj test_config.obj \
test_cell_formats.obj test_replay.obj test_introduce.obj tinytest.obj \
test_hs.obj
diff --git a/src/test/include.am b/src/test/include.am
index d0f3224dc5..9abf3094eb 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -28,6 +28,7 @@ src_test_test_SOURCES = \
src/test/test_cell_queue.c \
src/test/test_data.c \
src/test/test_dir.c \
+ src/test/test_checkdir.c \
src/test/test_entrynodes.c \
src/test/test_extorport.c \
src/test/test_introduce.c \
diff --git a/src/test/test.c b/src/test/test.c
index 0511eb4054..203b7489df 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1279,6 +1279,7 @@ extern struct testcase_t crypto_tests[];
extern struct testcase_t container_tests[];
extern struct testcase_t util_tests[];
extern struct testcase_t dir_tests[];
+extern struct testcase_t checkdir_tests[];
extern struct testcase_t microdesc_tests[];
extern struct testcase_t pt_tests[];
extern struct testcase_t config_tests[];
@@ -1316,6 +1317,7 @@ static struct testgroup_t testgroups[] = {
{ "cellfmt/", cell_format_tests },
{ "cellqueue/", cell_queue_tests },
{ "dir/", dir_tests },
+ { "checkdir/", checkdir_tests },
{ "dir/md/", microdesc_tests },
{ "pt/", pt_tests },
{ "config/", config_tests },
diff --git a/src/test/test_checkdir.c b/src/test/test_checkdir.c
new file mode 100644
index 0000000000..7bf735e061
--- /dev/null
+++ b/src/test/test_checkdir.c
@@ -0,0 +1,132 @@
+/* Copyright (c) 2014, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#include "or.h"
+#include <dirent.h>
+#include "config.h"
+#include "test.h"
+#include "util.h"
+
+/** Run unit tests for private dir permission enforcement logic. */
+static void
+test_checkdir_perms(void *testdata)
+{
+ or_options_t *options = get_options_mutable();
+ const char *subdir = "test_checkdir";
+ char *testdir;
+ cpd_check_t cpd_chkopts;
+ cpd_check_t unix_create_opts;
+ cpd_check_t unix_verify_optsmask;
+ struct stat st;
+
+ /* setup data directory before tests. */
+ tor_free(options->DataDirectory);
+ options->DataDirectory = tor_strdup(get_fname(subdir));
+ tt_int_op(mkdir(options->DataDirectory, 0750), ==, 0);
+
+ /* test: create new dir, no flags. */
+ testdir = get_datadir_fname("checkdir_new_none");
+ cpd_chkopts = CPD_CREATE;
+ unix_verify_optsmask = 0077;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tt_int_op(0, ==, stat(testdir, &st));
+ tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
+ tor_free(testdir);
+
+ /* test: create new dir, CPD_GROUP_OK option set. */
+ testdir = get_datadir_fname("checkdir_new_groupok");
+ cpd_chkopts = CPD_CREATE|CPD_GROUP_OK;
+ unix_verify_optsmask = 0077;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tt_int_op(0, ==, stat(testdir, &st));
+ tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
+ tor_free(testdir);
+
+ /* test: should get an error on existing dir with
+ wrong perms */
+ testdir = get_datadir_fname("checkdir_new_groupok_err");
+ tt_int_op(0, ==, mkdir(testdir, 027));
+ cpd_chkopts = CPD_CHECK_MODE_ONLY|CPD_CREATE|CPD_GROUP_OK;
+ tt_int_op(-1, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tor_free(testdir);
+
+ /* test: create new dir, CPD_GROUP_READ option set. */
+ testdir = get_datadir_fname("checkdir_new_groupread");
+ cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
+ unix_verify_optsmask = 0027;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tt_int_op(0, ==, stat(testdir, &st));
+ tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
+ tor_free(testdir);
+
+ /* test: check existing dir created with defaults,
+ and verify with CPD_CREATE only. */
+ testdir = get_datadir_fname("checkdir_exists_none");
+ cpd_chkopts = CPD_CREATE;
+ unix_create_opts = 0700;
+ unix_verify_optsmask = 0077;
+ tt_int_op(0, ==, mkdir(testdir, unix_create_opts));
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tt_int_op(0, ==, stat(testdir, &st));
+ tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
+ tor_free(testdir);
+
+ /* test: check existing dir created with defaults,
+ and verify with CPD_GROUP_OK option set. */
+ testdir = get_datadir_fname("checkdir_exists_groupok");
+ cpd_chkopts = CPD_CREATE;
+ unix_verify_optsmask = 0077;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ cpd_chkopts = CPD_GROUP_OK;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tt_int_op(0, ==, stat(testdir, &st));
+ tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
+ tor_free(testdir);
+
+ /* test: check existing dir created with defaults,
+ and verify with CPD_GROUP_READ option set. */
+ testdir = get_datadir_fname("checkdir_exists_groupread");
+ cpd_chkopts = CPD_CREATE;
+ unix_verify_optsmask = 0027;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ cpd_chkopts = CPD_GROUP_READ;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tt_int_op(0, ==, stat(testdir, &st));
+ tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
+ tor_free(testdir);
+
+ /* test: check existing dir created with CPD_GROUP_READ,
+ and verify with CPD_GROUP_OK option set. */
+ testdir = get_datadir_fname("checkdir_existsread_groupok");
+ cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
+ unix_verify_optsmask = 0027;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ cpd_chkopts = CPD_GROUP_OK;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tt_int_op(0, ==, stat(testdir, &st));
+ tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
+ tor_free(testdir);
+
+ /* test: check existing dir created with CPD_GROUP_READ,
+ and verify with CPD_GROUP_READ option set. */
+ testdir = get_datadir_fname("checkdir_existsread_groupread");
+ cpd_chkopts = CPD_CREATE|CPD_GROUP_READ;
+ unix_verify_optsmask = 0027;
+ tt_int_op(0, ==, check_private_dir(testdir, cpd_chkopts, NULL));
+ tt_int_op(0, ==, stat(testdir, &st));
+ tt_int_op(0, ==, (st.st_mode & unix_verify_optsmask));
+ tor_free(testdir);
+
+ done:
+ ;
+}
+
+#define CHECKDIR(name,flags) \
+ { #name, test_checkdir_##name, (flags), NULL, NULL }
+
+struct testcase_t checkdir_tests[] = {
+ CHECKDIR(perms, 0),
+ END_OF_TESTCASES
+};
+