瀏覽代碼

Add tests for process environment functionality of process_t.

This patch adds tests for the process_environment_t interaction in
process_t.

See: https://bugs.torproject.org/28179
Alexander Færøy 6 年之前
父節點
當前提交
289ed0849d
共有 1 個文件被更改,包括 52 次插入0 次删除
  1. 52 0
      src/test/test_process.c

+ 52 - 0
src/test/test_process.c

@@ -9,6 +9,7 @@
 #include "orconfig.h"
 #include "core/or/or.h"
 #include "test/test.h"
+#include "lib/process/env.h"
 
 #define PROCESS_PRIVATE
 #include "lib/process/process.h"
@@ -172,6 +173,56 @@ test_default_values(void *arg)
   process_free_all();
 }
 
+static void
+test_environment(void *arg)
+{
+  (void)arg;
+  process_init();
+
+  process_t *process = process_new("");
+  process_environment_t *env = NULL;
+
+  process_set_environment(process, "E", "F");
+  process_set_environment(process, "C", "D");
+  process_set_environment(process, "A", "B");
+
+  env = process_get_environment(process);
+  tt_mem_op(env->windows_environment_block, OP_EQ,
+            "A=B\0C=D\0E=F\0", 12);
+  tt_str_op(env->unixoid_environment_block[0], OP_EQ,
+            "A=B");
+  tt_str_op(env->unixoid_environment_block[1], OP_EQ,
+            "C=D");
+  tt_str_op(env->unixoid_environment_block[2], OP_EQ,
+            "E=F");
+  tt_ptr_op(env->unixoid_environment_block[3], OP_EQ,
+            NULL);
+  process_environment_free(env);
+
+  /* Reset our environment. */
+  smartlist_t *new_env = smartlist_new();
+  smartlist_add(new_env, (char *)"FOO=bar");
+  smartlist_add(new_env, (char *)"HELLO=world");
+
+  process_reset_environment(process, new_env);
+  smartlist_free(new_env);
+
+  env = process_get_environment(process);
+  tt_mem_op(env->windows_environment_block, OP_EQ,
+            "FOO=bar\0HELLO=world\0", 20);
+  tt_str_op(env->unixoid_environment_block[0], OP_EQ,
+            "FOO=bar");
+  tt_str_op(env->unixoid_environment_block[1], OP_EQ,
+            "HELLO=world");
+  tt_ptr_op(env->unixoid_environment_block[2], OP_EQ,
+            NULL);
+
+ done:
+  process_environment_free(env);
+  process_free(process);
+  process_free_all();
+}
+
 static void
 test_stringified_types(void *arg)
 {
@@ -589,6 +640,7 @@ test_win32(void *arg)
 
 struct testcase_t process_tests[] = {
   { "default_values", test_default_values, TT_FORK, NULL, NULL },
+  { "environment", test_environment, TT_FORK, NULL, NULL },
   { "stringified_types", test_stringified_types, TT_FORK, NULL, NULL },
   { "line_protocol_simple", test_line_protocol_simple, TT_FORK, NULL, NULL },
   { "line_protocol_multi", test_line_protocol_multi, TT_FORK, NULL, NULL },