[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Add tests for util_process



commit cf4f50f94332615abcf6dd345ee1e8fce69232ca
Author: Ola Bini <ola@xxxxxxxxxx>
Date:   Tue Sep 15 17:37:25 2015 +0200

    Add tests for util_process
---
 src/test/include.am          |    2 +
 src/test/log_test_helpers.c  |   95 ++++++++++++++++++++++++++++++++++++++++++
 src/test/log_test_helpers.h  |   27 ++++++++++++
 src/test/test.c              |    3 +-
 src/test/test_util_process.c |   67 +++++++++++++++++++++++++++++
 5 files changed, 193 insertions(+), 1 deletion(-)

diff --git a/src/test/include.am b/src/test/include.am
index f7c0204..0e584d4 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -51,6 +51,7 @@ src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
 # matters a lot there, and is quite hard to debug if you forget to do it.
 
 src_test_test_SOURCES = \
+	src/test/log_test_helpers.c \
 	src/test/test.c \
 	src/test/test_accounting.c \
 	src/test/test_addr.c \
@@ -96,6 +97,7 @@ src_test_test_SOURCES = \
 	src/test/test_status.c \
 	src/test/test_threads.c \
 	src/test/test_util.c \
+	src/test/test_util_process.c \
 	src/test/test_helpers.c \
         src/test/test_dns.c \
 	src/test/testing_common.c \
diff --git a/src/test/log_test_helpers.c b/src/test/log_test_helpers.c
new file mode 100644
index 0000000..42778d4
--- /dev/null
+++ b/src/test/log_test_helpers.c
@@ -0,0 +1,95 @@
+#define LOG_PRIVATE
+#include "torlog.h"
+#include "log_test_helpers.h"
+
+static smartlist_t *saved_logs = NULL;
+
+int
+setup_capture_of_logs(int new_level)
+{
+  int previous_log = log_global_min_severity_;
+  log_global_min_severity_ = new_level;
+  mock_clean_saved_logs();
+  MOCK(logv, mock_saving_logv);
+  return previous_log;
+}
+
+void
+teardown_capture_of_logs(int prev)
+{
+  UNMOCK(logv);
+  log_global_min_severity_ = prev;
+  mock_clean_saved_logs();
+}
+
+void
+mock_clean_saved_logs(void)
+{
+  if (!saved_logs)
+    return;
+  SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
+                    { tor_free(m->generated_msg); tor_free(m); });
+  smartlist_free(saved_logs);
+  saved_logs = NULL;
+}
+
+char *
+mock_saved_log_at(int ix)
+{
+  int saved_log_count = mock_saved_log_number();
+  if(ix < 0) {
+    ix = saved_log_count + ix;
+  }
+
+  if (saved_log_count <= ix)
+    return "";
+  return ((mock_saved_log_entry_t *)smartlist_get(saved_logs, ix))->generated_msg;
+}
+
+int
+mock_saved_severity_at(int ix)
+{
+  int saved_log_count = mock_saved_log_number();
+  if(ix < 0) {
+    ix = saved_log_count + ix;
+  }
+
+  if (saved_log_count <= ix)
+    return -1;
+  return ((mock_saved_log_entry_t *)smartlist_get(saved_logs, ix))->severity;
+}
+
+int
+mock_saved_log_number(void)
+{
+  if (!saved_logs)
+    return 0;
+  return smartlist_len(saved_logs);
+}
+
+const smartlist_t *
+mock_saved_logs(void)
+{
+  return saved_logs;
+}
+
+void
+mock_saving_logv(int severity, log_domain_mask_t domain, const char *funcname, const char *suffix, const char *format, va_list ap)
+{
+  char *buf = tor_malloc_zero(10240);
+  int n;
+  n = tor_vsnprintf(buf,10240,format,ap);
+  buf[n]='\n';
+  buf[n+1]='\0';
+
+  mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
+  e->severity = severity;
+  e->funcname = funcname;
+  e->suffix = suffix;
+  e->format = format;
+  e->generated_msg = buf;
+
+  if (!saved_logs)
+    saved_logs = smartlist_new();
+  smartlist_add(saved_logs, e);
+}
diff --git a/src/test/log_test_helpers.h b/src/test/log_test_helpers.h
new file mode 100644
index 0000000..789bfe4
--- /dev/null
+++ b/src/test/log_test_helpers.h
@@ -0,0 +1,27 @@
+/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "or.h"
+
+#ifndef TOR_LOG_TEST_HELPERS_H
+#define TOR_LOG_TEST_HELPERS_H
+
+typedef struct mock_saved_log_entry_t {
+  int severity;
+  const char *funcname;
+  const char *suffix;
+  const char *format;
+  char *generated_msg;
+  struct mock_saved_log_entry_t *next;
+} mock_saved_log_entry_t;
+
+void mock_saving_logv(int severity, log_domain_mask_t domain, const char *funcname, const char *suffix, const char *format, va_list ap);
+void mock_clean_saved_logs(void);
+const smartlist_t *mock_saved_logs(void);
+int setup_capture_of_logs(int new_level);
+void teardown_capture_of_logs(int prev);
+char *mock_saved_log_at(int ix);
+int mock_saved_severity_at(int ix);
+int mock_saved_log_number(void);
+
+#endif
diff --git a/src/test/test.c b/src/test/test.c
index e10e260..a431538 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1158,6 +1158,7 @@ extern struct testcase_t socks_tests[];
 extern struct testcase_t status_tests[];
 extern struct testcase_t thread_tests[];
 extern struct testcase_t util_tests[];
+extern struct testcase_t util_process_tests[];
 extern struct testcase_t dns_tests[];
 
 struct testgroup_t testgroups[] = {
@@ -1204,8 +1205,8 @@ struct testgroup_t testgroups[] = {
   { "status/" , status_tests },
   { "util/", util_tests },
   { "util/logging/", logging_tests },
+  { "util/process/", util_process_tests },
   { "util/thread/", thread_tests },
   { "dns/", dns_tests },
   END_OF_GROUPS
 };
-
diff --git a/src/test/test_util_process.c b/src/test/test_util_process.c
new file mode 100644
index 0000000..5d6c072
--- /dev/null
+++ b/src/test/test_util_process.c
@@ -0,0 +1,67 @@
+/* Copyright (c) 2010-2015, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define UTIL_PROCESS_PRIVATE
+#include "orconfig.h"
+#include "or.h"
+
+#include "test.h"
+
+#include "util_process.h"
+
+#include "log_test_helpers.h"
+
+#define NS_MODULE util_process
+
+static
+void temp_callback(int r, void *s)
+{
+  (void)r;
+  (void)s;
+}
+
+static void
+test_util_process_set_waitpid_callback(void *ignored)
+{
+  (void)ignored;
+  waitpid_callback_t *res;
+  int previous_log = setup_capture_of_logs(LOG_WARN);
+  pid_t pid = (pid_t)42;
+
+  res = set_waitpid_callback(pid, temp_callback, NULL);
+  tt_assert(res);
+
+  res = set_waitpid_callback(pid, temp_callback, NULL);
+  tt_assert(res);
+  tt_str_op(mock_saved_log_at(0), OP_EQ, "Replaced a waitpid monitor on pid 42. That should be impossible.\n");
+
+ done:
+  teardown_capture_of_logs(previous_log);
+}
+
+
+static void
+test_util_process_clear_waitpid_callback(void *ignored)
+{
+  (void)ignored;
+  waitpid_callback_t *res;
+  int previous_log = setup_capture_of_logs(LOG_WARN);
+  pid_t pid = (pid_t)43;
+
+  clear_waitpid_callback(NULL);
+
+  res = set_waitpid_callback(pid, temp_callback, NULL);
+  clear_waitpid_callback(res);
+  clear_waitpid_callback(res);
+
+  // done:
+  teardown_capture_of_logs(previous_log);
+}
+
+
+
+struct testcase_t util_process_tests[] = {
+  { "set_waitpid_callback", test_util_process_set_waitpid_callback, 0, NULL, NULL },
+  { "clear_waitpid_callback", test_util_process_clear_waitpid_callback, 0, NULL, NULL },
+  END_OF_TESTCASES
+};



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits