[PATCH v1] platform: Add function to determine temp directory

Gert Doering <[email protected]>
Newsgroups gmane.network.openvpn.devel
Message-ID <[email protected]>
From: Frank Lichtenheld <[email protected]>

Move the code out of options.c and make it shareable.
Replace various stripped down versions of the code from
test_*

Change-Id: I40f72dc038a38e5ad1d3dd04cb10cb7d5cad5d19
Signed-off-by: Frank Lichtenheld <[email protected]>
Acked-by: Razvan Cojocaru <[email protected]>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1780
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1780
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Razvan Cojocaru <[email protected]>

        
diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index 87218d4..c57f398 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -881,25 +881,8 @@
     o->auth_token_generate = false;
 
     /* Set default --tmp-dir */
-#ifdef _WIN32
-    /* On Windows, find temp dir via environment variables */
-    o->tmp_dir = win_get_tempdir();
+    o->tmp_dir = platform_get_tmp_dir();
 
-    if (!o->tmp_dir)
-    {
-        /* Error out if we can't find a valid temporary directory, which should
-         * be very unlikely. */
-        msg(M_USAGE, "Could not find a suitable temporary directory."
-                     " (GetTempPath() failed).  Consider using --tmp-dir");
-    }
-#else  /* ifdef _WIN32 */
-    /* Non-windows platforms use $TMPDIR, and if not set, default to '/tmp' */
-    o->tmp_dir = getenv("TMPDIR");
-    if (!o->tmp_dir)
-    {
-        o->tmp_dir = "/tmp";
-    }
-#endif /* _WIN32 */
     o->allow_recursive_routing = false;
 
 #ifndef ENABLE_DCO
diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c
index 85b6408..6c766c2 100644
--- a/src/openvpn/platform.c
+++ b/src/openvpn/platform.c
@@ -584,6 +584,34 @@
     return NULL;
 }
 
+const char *
+platform_get_tmp_dir(void)
+{
+    const char *ret;
+#ifdef _WIN32
+    /* On Windows, find temp dir via environment variables */
+    ret = win_get_tempdir();
+
+    if (!ret)
+    {
+        /* Error out if we can't find a valid temporary directory, which should
+         * be very unlikely. */
+        msg(M_USAGE, "Could not find a suitable temporary directory."
+                     " (GetTempPath() failed).  Consider using --tmp-dir");
+    }
+#else
+    /* Non-windows platforms use $TMPDIR, and if not set, default to '/tmp' */
+    ret = getenv("TMPDIR");
+    if (!ret)
+    {
+        ret = "/tmp";
+    }
+#endif
+
+    return ret;
+}
+
+
 /*
  * Put a directory and filename together.
  */
diff --git a/src/openvpn/platform.h b/src/openvpn/platform.h
index 8e7ab7e..784427bb 100644
--- a/src/openvpn/platform.h
+++ b/src/openvpn/platform.h
@@ -128,6 +128,13 @@
 const char *platform_create_temp_file(const char *directory, const char *prefix,
                                       struct gc_arena *gc);
 
+/**
+ * Get a directory for temporary files
+ *
+ * @return path to a directory
+ */
+const char *platform_get_tmp_dir(void);
+
 /** Put a directory and filename together. */
 const char *platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc);
 
diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn/test_ssl.c
index 0e9cecf..065c7e3f 100644
--- a/tests/unit_tests/openvpn/test_ssl.c
+++ b/tests/unit_tests/openvpn/test_ssl.c
@@ -115,19 +115,6 @@
     "-----END PRIVATE KEY-----\n";
 
 
-static const char *
-get_tmp_dir(void)
-{
-    const char *ret;
-#ifdef _WIN32
-    ret = win_get_tempdir();
-#else
-    ret = "/tmp";
-#endif
-    assert_non_null(ret);
-    return ret;
-}
-
 static struct
 {
     struct gc_arena gc;
@@ -140,8 +127,8 @@
 {
     (void)state;
     global_state.gc = gc_new();
-    global_state.certfile = platform_create_temp_file(get_tmp_dir(), "cert", &global_state.gc);
-    global_state.keyfile = platform_create_temp_file(get_tmp_dir(), "key", &global_state.gc);
+    global_state.certfile = platform_create_temp_file(platform_get_tmp_dir(), "cert", &global_state.gc);
+    global_state.keyfile = platform_create_temp_file(platform_get_tmp_dir(), "key", &global_state.gc);
 
     int certfd = open(global_state.certfile, O_RDWR);
     int keyfd = open(global_state.keyfile, O_RDWR);
@@ -188,7 +175,7 @@
     cert = ctx.crt_chain;
 #endif
 
-    const char *tmpfile = platform_create_temp_file(get_tmp_dir(), "ut_pem", &gc);
+    const char *tmpfile = platform_create_temp_file(platform_get_tmp_dir(), "ut_pem", &gc);
     backend_x509_write_pem(cert, tmpfile);
 
     struct buffer exported_pem = buffer_read_from_file(tmpfile, &gc);
diff --git a/tests/unit_tests/openvpn/test_tls_crypt.c b/tests/unit_tests/openvpn/test_tls_crypt.c
index 1d8ac25..a2987c6 100644
--- a/tests/unit_tests/openvpn/test_tls_crypt.c
+++ b/tests/unit_tests/openvpn/test_tls_crypt.c
@@ -720,7 +720,7 @@
         tls_options.tls_crypt_v2_verify_script = "/bin/true";
     }
 
-    tls_options.tmp_dir = "/tmp";
+    tls_options.tmp_dir = platform_get_tmp_dir();
 
     /* Since we override rand_bytes the tmpfile name is non-random as well.
      * Build the expected name via the same code path as
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.