Skip to contentSkip to conversation

tests/unit/test-util-sockets: Use g_file_open_tmp() to create temp file

PATCH In review Philippe Mathieu-Daudé 2 participants 3 messages Dec 24, 2021 – Dec 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/unit/test-util-sockets: Use g_file_open_tmp() to create temp file

Similarly to commit e63ed64c6d1 ("tests/qtest/virtio-net-failover:
Use g_file_open_tmp() to create temporary file"), avoid calling g_test_rand_int() before g_test_init(): use g_file_open_tmp().

---
 tests/unit/test-util-sockets.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tests/unit/test-util-sockets.c +4 -2
index 72b92465298..896247e3ed3 100644
@@ -305,9 +305,11 @@ static void test_socket_unix_abstract(void)
     };
     int i;
 
+    i = g_file_open_tmp("unix-XXXXXX", &addr.u.q_unix.path, NULL);
+    g_assert_true(i >= 0);
+    close(i);
+
     addr.type = SOCKET_ADDRESS_TYPE_UNIX;
-    addr.u.q_unix.path = g_strdup_printf("unix-%d-%u",
-                                         getpid(), g_random_int());
     addr.u.q_unix.has_abstract = true;
     addr.u.q_unix.abstract = true;
     addr.u.q_unix.has_tight = false;
Signature · 1 line
2.33.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Philippe Mathieu-Daudé

Similarly to commit e63ed64c6d1 ("tests/qtest/virtio-net-failover:
Use g_file_open_tmp() to create temporary file"), avoid calling g_test_rand_int() before g_test_init(): use g_file_open_tmp().

---
  tests/unit/test-util-sockets.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
tests/unit/test-util-sockets.c +0 -0
index 72b92465298..896247e3ed3 100644

tests/unit/test-util-sockets.c static void test_socket_unix_abstract(void) · line 309

Quoted 5 lines
tests/unit/test-util-sockets.c +2 -0
@@ -305,9 +305,11 @@ static void test_socket_unix_abstract(void)
      };
      int i;
 
+    i = g_file_open_tmp("unix-XXXXXX", &addr.u.q_unix.path, NULL);
+    g_assert_true(i >= 0);

Just g_assert. Otherwise,

r~

Quoted 9 lines
tests/unit/test-util-sockets.c +2 -2
+    close(i);
+
      addr.type = SOCKET_ADDRESS_TYPE_UNIX;
-    addr.u.q_unix.path = g_strdup_printf("unix-%d-%u",
-                                         getpid(), g_random_int());
      addr.u.q_unix.has_abstract = true;
      addr.u.q_unix.abstract = true;
      addr.u.q_unix.has_tight = false;
#2 lore raw
Quoted 10 lines · Richard Henderson
Quoted 9 lines · Philippe Mathieu-Daudé

Similarly to commit e63ed64c6d1 ("tests/qtest/virtio-net-failover:
Use g_file_open_tmp() to create temporary file"), avoid calling g_test_rand_int() before g_test_init(): use g_file_open_tmp().

---
  tests/unit/test-util-sockets.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 12 lines

tests/unit/test-util-sockets.c static void test_socket_unix_abstract(void) · line 308

Quoted 10 lines
tests/unit/test-util-sockets.c +1 -0
index 72b92465298..896247e3ed3 100644
@@ -305,9 +305,11 @@ static void test_socket_unix_abstract(void)
       };
       int i;
   +    i = g_file_open_tmp("unix-XXXXXX", &addr.u.q_unix.path, NULL);
+    g_assert_true(i >= 0);

Just g_assert.

Per https://docs.gtk.org/glib/testing.html#testing-framework:

Note that g_assert() should not be used in unit tests, since it is a no-op when compiling with G_DISABLE_ASSERT. Use g_assert() in production code, and g_assert_true() in unit tests.

We don't really use G_DISABLE_ASSERT, but g_assert_true() seems appropriate here...

Quoted 2 lines

Otherwise,

Thanks.