summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2016-06-30 11:18:00 -0400
committerNick Mathewson <nickm@torproject.org>2016-06-30 11:18:00 -0400
commitc6846d7bf0d8a382bea17304ea29a51c3a895f90 (patch)
tree9a19d14b30659e2e7b1cf47be683bc9da2abc677 /src/common
parenta31f55b16ba26c5c3720640bf27157fd7ab45c40 (diff)
parent13a16e001164581b687dae2d3377f77eedb701ff (diff)
downloadtor-c6846d7bf0d8a382bea17304ea29a51c3a895f90.tar.gz
tor-c6846d7bf0d8a382bea17304ea29a51c3a895f90.zip
Merge remote-tracking branch 'andrea/bug18322_v3_squashed'
Diffstat (limited to 'src/common')
-rw-r--r--src/common/testsupport.h10
-rw-r--r--src/common/util.c28
-rw-r--r--src/common/util.h17
3 files changed, 40 insertions, 15 deletions
diff --git a/src/common/testsupport.h b/src/common/testsupport.h
index 3bb11a7e41..9ad2ba77e0 100644
--- a/src/common/testsupport.h
+++ b/src/common/testsupport.h
@@ -6,8 +6,10 @@
#ifdef TOR_UNIT_TESTS
#define STATIC
+#define EXTERN(type, name) extern type name;
#else
#define STATIC static
+#define EXTERN(type, name)
#endif
/** Quick and dirty macros to implement test mocking.
@@ -60,6 +62,12 @@
#define MOCK_IMPL(rv, funcname, arglist) \
rv(*funcname) arglist = funcname ##__real; \
rv funcname ##__real arglist
+#define MOCK_DECL_ATTR(rv, funcname, arglist, attr) \
+ rv funcname ##__real arglist attr; \
+ extern rv(*funcname) arglist
+#define MOCK_IMPL(rv, funcname, arglist) \
+ rv(*funcname) arglist = funcname ##__real; \
+ rv funcname ##__real arglist
#define MOCK(func, replacement) \
do { \
(func) = (replacement); \
@@ -71,6 +79,8 @@
#else
#define MOCK_DECL(rv, funcname, arglist) \
rv funcname arglist
+#define MOCK_DECL_ATTR(rv, funcname, arglist, attr) \
+ rv funcname arglist attr
#define MOCK_IMPL(rv, funcname, arglist) \
rv funcname arglist
#endif
diff --git a/src/common/util.c b/src/common/util.c
index fce99ee3f4..8a4fa1d710 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -2103,6 +2103,16 @@ clean_name_for_stat(char *name)
#endif
}
+/** Wrapper for unlink() to make it mockable for the test suite; returns 0
+ * if unlinking the file succeeded, -1 and sets errno if unlinking fails.
+ */
+
+MOCK_IMPL(int,
+tor_unlink,(const char *pathname))
+{
+ return unlink(pathname);
+}
+
/** Return:
* FN_ERROR if filename can't be read, is NULL, or is zero-length,
* FN_NOENT if it doesn't exist,
@@ -2166,9 +2176,9 @@ file_status(const char *fname)
* When effective_user is not NULL, check permissions against the given user
* and its primary group.
*/
-int
-check_private_dir(const char *dirname, cpd_check_t check,
- const char *effective_user)
+MOCK_IMPL(int,
+check_private_dir,(const char *dirname, cpd_check_t check,
+ const char *effective_user))
{
int r;
struct stat st;
@@ -2396,8 +2406,8 @@ check_private_dir(const char *dirname, cpd_check_t check,
* function, and all other functions in util.c that create files, create them
* with mode 0600.
*/
-int
-write_str_to_file(const char *fname, const char *str, int bin)
+MOCK_IMPL(int,
+write_str_to_file,(const char *fname, const char *str, int bin))
{
#ifdef _WIN32
if (!bin && strchr(str, '\r')) {
@@ -2756,8 +2766,8 @@ read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out)
* the call to stat and the call to read_all: the resulting string will
* be truncated.
*/
-char *
-read_file_to_str(const char *filename, int flags, struct stat *stat_out)
+MOCK_IMPL(char *,
+read_file_to_str, (const char *filename, int flags, struct stat *stat_out))
{
int fd; /* router file */
struct stat statbuf;
@@ -3492,8 +3502,8 @@ smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
/** Return a new list containing the filenames in the directory <b>dirname</b>.
* Return NULL on error or if <b>dirname</b> is not a directory.
*/
-smartlist_t *
-tor_listdir(const char *dirname)
+MOCK_IMPL(smartlist_t *,
+tor_listdir, (const char *dirname))
{
smartlist_t *result;
#ifdef _WIN32
diff --git a/src/common/util.h b/src/common/util.h
index 7cb33dc680..44f510cef7 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -309,6 +309,8 @@ const char *stream_status_to_string(enum stream_status stream_status);
enum stream_status get_string_from_pipe(FILE *stream, char *buf, size_t count);
+MOCK_DECL(int,tor_unlink,(const char *pathname));
+
/** Return values from file_status(); see that function's documentation
* for details. */
typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY } file_status_t;
@@ -324,8 +326,9 @@ typedef unsigned int cpd_check_t;
#define CPD_GROUP_READ (1u << 3)
#define CPD_CHECK_MODE_ONLY (1u << 4)
#define CPD_RELAX_DIRMODE_CHECK (1u << 5)
-int check_private_dir(const char *dirname, cpd_check_t check,
- const char *effective_user);
+MOCK_DECL(int, check_private_dir,
+ (const char *dirname, cpd_check_t check,
+ const char *effective_user));
#define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
#define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
@@ -338,7 +341,8 @@ FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
FILE *fdopen_file(open_file_t *file_data);
int finish_writing_to_file(open_file_t *file_data);
int abort_writing_to_file(open_file_t *file_data);
-int write_str_to_file(const char *fname, const char *str, int bin);
+MOCK_DECL(int,
+write_str_to_file,(const char *fname, const char *str, int bin));
MOCK_DECL(int,
write_bytes_to_file,(const char *fname, const char *str, size_t len,
int bin));
@@ -363,8 +367,9 @@ int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
#ifndef _WIN32
struct stat;
#endif
-char *read_file_to_str(const char *filename, int flags, struct stat *stat_out)
- ATTR_MALLOC;
+MOCK_DECL_ATTR(char *, read_file_to_str,
+ (const char *filename, int flags, struct stat *stat_out),
+ ATTR_MALLOC);
char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
size_t *sz_out)
ATTR_MALLOC;
@@ -372,7 +377,7 @@ const char *parse_config_line_from_str_verbose(const char *line,
char **key_out, char **value_out,
const char **err_out);
char *expand_filename(const char *filename);
-struct smartlist_t *tor_listdir(const char *dirname);
+MOCK_DECL(struct smartlist_t *, tor_listdir, (const char *dirname));
int path_is_relative(const char *filename);
/* Process helpers */