Re: [PATCH 07/51] tests: Avoid using hardcoded /tmp in test cases

2022-08-25 Thread Thomas Huth

On 24/08/2022 11.39, Bin Meng wrote:

From: Bin Meng 

Use g_get_tmp_dir() to get the directory to use for temporary files.

Signed-off-by: Bin Meng 
---

  tests/qtest/fuzz/generic_fuzz_configs.h |  6 --
  tests/qtest/ahci-test.c | 15 +++
  tests/qtest/aspeed_smc-test.c   |  4 +++-
  tests/qtest/boot-serial-test.c  |  8 ++--
  tests/qtest/cxl-test.c  |  9 ++---
  tests/qtest/fdc-test.c  |  4 +++-
  tests/qtest/fuzz/virtio_blk_fuzz.c  |  2 +-
  tests/qtest/hd-geo-test.c   |  8 
  tests/qtest/ide-test.c  |  8 ++--
  tests/qtest/libqtest.c  | 10 +++---
  tests/qtest/migration-test.c|  4 +++-
  tests/qtest/pflash-cfi02-test.c |  7 +--
  tests/qtest/qmp-test.c  |  4 +++-
  tests/qtest/vhost-user-blk-test.c   |  3 ++-
  tests/qtest/vhost-user-test.c   |  3 ++-
  tests/qtest/virtio-blk-test.c   |  2 +-
  tests/qtest/virtio-scsi-test.c  |  3 ++-
  tests/unit/test-image-locking.c |  6 --
  tests/unit/test-qga.c   |  2 +-
  tests/vhost-user-bridge.c   |  3 ++-
  20 files changed, 76 insertions(+), 35 deletions(-)

diff --git a/tests/qtest/fuzz/generic_fuzz_configs.h 
b/tests/qtest/fuzz/generic_fuzz_configs.h
index 0775e6702b..d0f9961187 100644
--- a/tests/qtest/fuzz/generic_fuzz_configs.h
+++ b/tests/qtest/fuzz/generic_fuzz_configs.h
@@ -20,13 +20,15 @@ typedef struct generic_fuzz_config {
  } generic_fuzz_config;
  
  static inline gchar *generic_fuzzer_virtio_9p_args(void){

-char tmpdir[] = "/tmp/qemu-fuzz.XX";
+char *tmpdir = g_strdup_printf("%s/qemu-fuzz.XX", g_get_tmp_dir());
  g_assert_nonnull(g_mkdtemp(tmpdir));


Could you switch this to g_dir_make_tmp(), please ? (see 
https://docs.gtk.org/glib/type_func.Dir.make_tmp.html )


(and as David said, please use g_autofree to avoid the g_free() later)


-return g_strdup_printf("-machine q35 -nodefaults "
+gchar *args = g_strdup_printf("-machine q35 -nodefaults "
  "-device virtio-9p,fsdev=hshare,mount_tag=hshare "
  "-fsdev local,id=hshare,path=%s,security_model=mapped-xattr,"
  "writeout=immediate,fmode=0600,dmode=0700", tmpdir);
+g_free(tmpdir);
+return args;
  }
  
  const generic_fuzz_config predefined_configs[] = {

diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index f1e510b0ac..f26cd6f86f 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -44,9 +44,9 @@
  #define TEST_IMAGE_SIZE_MB_SMALL 64
  
  /*** Globals ***/

-static char tmp_path[] = "/tmp/qtest.XX";
-static char debug_path[] = "/tmp/qtest-blkdebug.XX";
-static char mig_socket[] = "/tmp/qtest-migration.XX";
+static char *tmp_path;
+static char *debug_path;
+static char *mig_socket;
  static bool ahci_pedantic;
  static const char *imgfmt;
  static unsigned test_image_size_mb;
@@ -1437,7 +1437,7 @@ static void test_ncq_simple(void)
  
  static int prepare_iso(size_t size, unsigned char **buf, char **name)

  {
-char cdrom_path[] = "/tmp/qtest.iso.XX";
+char *cdrom_path = g_strdup_printf("%s/qtest.iso.XX", g_get_tmp_dir());
  unsigned char *patt;
  ssize_t ret;
  int fd = mkstemp(cdrom_path);


Would it be better to use g_file_open_tmp() here?
(see https://docs.gtk.org/glib/func.file_open_tmp.html)


@@ -1872,6 +1873,7 @@ int main(int argc, char **argv)
  }
  
  /* Create a temporary image */

+tmp_path = g_strdup_printf("%s/qtest.XX", g_get_tmp_dir());
  fd = mkstemp(tmp_path);
  g_assert(fd >= 0);


g_file_open_tmp() ?

Also for the other temporary files in this test?


diff --git a/tests/qtest/aspeed_smc-test.c b/tests/qtest/aspeed_smc-test.c
index 05ce941566..cab769459c 100644
--- a/tests/qtest/aspeed_smc-test.c
+++ b/tests/qtest/aspeed_smc-test.c
@@ -608,7 +608,7 @@ static void test_write_block_protect_bottom_bit(void)
  flash_reset();
  }
  
-static char tmp_path[] = "/tmp/qtest.m25p80.XX";

+static char *tmp_path;
  
  int main(int argc, char **argv)

  {
@@ -617,6 +617,7 @@ int main(int argc, char **argv)
  
  g_test_init(, , NULL);
  
+tmp_path = g_strdup_printf("%s/qtest.m25p80.XX", g_get_tmp_dir());

  fd = mkstemp(tmp_path);


g_file_open_tmp() ?


diff --git a/tests/qtest/boot-serial-test.c b/tests/qtest/boot-serial-test.c
index 2f99d71cab..404adcfa20 100644
--- a/tests/qtest/boot-serial-test.c
+++ b/tests/qtest/boot-serial-test.c
@@ -224,8 +224,10 @@ static bool check_guest_output(QTestState *qts, const 
testdef_t *test, int fd)
  static void test_machine(const void *data)
  {
  const testdef_t *test = data;
-char serialtmp[] = "/tmp/qtest-boot-serial-sXX";
-char codetmp[] = "/tmp/qtest-boot-serial-cXX";
+char *serialtmp = g_strdup_printf("%s/qtest-boot-serial-sXX",
+  g_get_tmp_dir());
+char 

Re: [PATCH 07/51] tests: Avoid using hardcoded /tmp in test cases

2022-08-24 Thread Dr. David Alan Gilbert
* Bin Meng (bmeng...@gmail.com) wrote:
> From: Bin Meng 
> 
> Use g_get_tmp_dir() to get the directory to use for temporary files.
> 
> Signed-off-by: Bin Meng 
> ---
> 
>  tests/qtest/fuzz/generic_fuzz_configs.h |  6 --
>  tests/qtest/ahci-test.c | 15 +++
>  tests/qtest/aspeed_smc-test.c   |  4 +++-
>  tests/qtest/boot-serial-test.c  |  8 ++--
>  tests/qtest/cxl-test.c  |  9 ++---
>  tests/qtest/fdc-test.c  |  4 +++-
>  tests/qtest/fuzz/virtio_blk_fuzz.c  |  2 +-
>  tests/qtest/hd-geo-test.c   |  8 
>  tests/qtest/ide-test.c  |  8 ++--
>  tests/qtest/libqtest.c  | 10 +++---
>  tests/qtest/migration-test.c|  4 +++-
>  tests/qtest/pflash-cfi02-test.c |  7 +--
>  tests/qtest/qmp-test.c  |  4 +++-
>  tests/qtest/vhost-user-blk-test.c   |  3 ++-
>  tests/qtest/vhost-user-test.c   |  3 ++-
>  tests/qtest/virtio-blk-test.c   |  2 +-
>  tests/qtest/virtio-scsi-test.c  |  3 ++-
>  tests/unit/test-image-locking.c |  6 --
>  tests/unit/test-qga.c   |  2 +-
>  tests/vhost-user-bridge.c   |  3 ++-
>  20 files changed, 76 insertions(+), 35 deletions(-)
> 
> diff --git a/tests/qtest/fuzz/generic_fuzz_configs.h 
> b/tests/qtest/fuzz/generic_fuzz_configs.h
> index 0775e6702b..d0f9961187 100644
> --- a/tests/qtest/fuzz/generic_fuzz_configs.h
> +++ b/tests/qtest/fuzz/generic_fuzz_configs.h
> @@ -20,13 +20,15 @@ typedef struct generic_fuzz_config {
>  } generic_fuzz_config;
>  
>  static inline gchar *generic_fuzzer_virtio_9p_args(void){
> -char tmpdir[] = "/tmp/qemu-fuzz.XX";
> +char *tmpdir = g_strdup_printf("%s/qemu-fuzz.XX", g_get_tmp_dir());

You might find it easier to use g_autofree in a lot of these, and then
you don't need to bother with the g_free at the end.

Dave

>  g_assert_nonnull(g_mkdtemp(tmpdir));
>  
> -return g_strdup_printf("-machine q35 -nodefaults "
> +gchar *args = g_strdup_printf("-machine q35 -nodefaults "
>  "-device virtio-9p,fsdev=hshare,mount_tag=hshare "
>  "-fsdev local,id=hshare,path=%s,security_model=mapped-xattr,"
>  "writeout=immediate,fmode=0600,dmode=0700", tmpdir);
> +g_free(tmpdir);
> +return args;
>  }
>  
>  const generic_fuzz_config predefined_configs[] = {
> diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
> index f1e510b0ac..f26cd6f86f 100644
> --- a/tests/qtest/ahci-test.c
> +++ b/tests/qtest/ahci-test.c
> @@ -44,9 +44,9 @@
>  #define TEST_IMAGE_SIZE_MB_SMALL 64
>  
>  /*** Globals ***/
> -static char tmp_path[] = "/tmp/qtest.XX";
> -static char debug_path[] = "/tmp/qtest-blkdebug.XX";
> -static char mig_socket[] = "/tmp/qtest-migration.XX";
> +static char *tmp_path;
> +static char *debug_path;
> +static char *mig_socket;
>  static bool ahci_pedantic;
>  static const char *imgfmt;
>  static unsigned test_image_size_mb;
> @@ -1437,7 +1437,7 @@ static void test_ncq_simple(void)
>  
>  static int prepare_iso(size_t size, unsigned char **buf, char **name)
>  {
> -char cdrom_path[] = "/tmp/qtest.iso.XX";
> +char *cdrom_path = g_strdup_printf("%s/qtest.iso.XX", 
> g_get_tmp_dir());
>  unsigned char *patt;
>  ssize_t ret;
>  int fd = mkstemp(cdrom_path);
> @@ -1454,6 +1454,7 @@ static int prepare_iso(size_t size, unsigned char 
> **buf, char **name)
>  
>  *name = g_strdup(cdrom_path);
>  *buf = patt;
> +g_free(cdrom_path);
>  return fd;
>  }
>  
> @@ -1872,6 +1873,7 @@ int main(int argc, char **argv)
>  }
>  
>  /* Create a temporary image */
> +tmp_path = g_strdup_printf("%s/qtest.XX", g_get_tmp_dir());
>  fd = mkstemp(tmp_path);
>  g_assert(fd >= 0);
>  if (have_qemu_img()) {
> @@ -1889,11 +1891,13 @@ int main(int argc, char **argv)
>  close(fd);
>  
>  /* Create temporary blkdebug instructions */
> +debug_path = g_strdup_printf("%s/qtest-blkdebug.XX", 
> g_get_tmp_dir());
>  fd = mkstemp(debug_path);
>  g_assert(fd >= 0);
>  close(fd);
>  
>  /* Reserve a hollow file to use as a socket for migration tests */
> +mig_socket = g_strdup_printf("%s/qtest-migration.XX", 
> g_get_tmp_dir());
>  fd = mkstemp(mig_socket);
>  g_assert(fd >= 0);
>  close(fd);
> @@ -1947,8 +1951,11 @@ int main(int argc, char **argv)
>  
>  /* Cleanup */
>  unlink(tmp_path);
> +g_free(tmp_path);
>  unlink(debug_path);
> +g_free(debug_path);
>  unlink(mig_socket);
> +g_free(mig_socket);
>  
>  return ret;
>  }
> diff --git a/tests/qtest/aspeed_smc-test.c b/tests/qtest/aspeed_smc-test.c
> index 05ce941566..cab769459c 100644
> --- a/tests/qtest/aspeed_smc-test.c
> +++ b/tests/qtest/aspeed_smc-test.c
> @@ -608,7 +608,7 @@ static void test_write_block_protect_bottom_bit(void)
>  flash_reset();
>  }
>  
> -static char tmp_path[] = 

[PATCH 07/51] tests: Avoid using hardcoded /tmp in test cases

2022-08-24 Thread Bin Meng
From: Bin Meng 

Use g_get_tmp_dir() to get the directory to use for temporary files.

Signed-off-by: Bin Meng 
---

 tests/qtest/fuzz/generic_fuzz_configs.h |  6 --
 tests/qtest/ahci-test.c | 15 +++
 tests/qtest/aspeed_smc-test.c   |  4 +++-
 tests/qtest/boot-serial-test.c  |  8 ++--
 tests/qtest/cxl-test.c  |  9 ++---
 tests/qtest/fdc-test.c  |  4 +++-
 tests/qtest/fuzz/virtio_blk_fuzz.c  |  2 +-
 tests/qtest/hd-geo-test.c   |  8 
 tests/qtest/ide-test.c  |  8 ++--
 tests/qtest/libqtest.c  | 10 +++---
 tests/qtest/migration-test.c|  4 +++-
 tests/qtest/pflash-cfi02-test.c |  7 +--
 tests/qtest/qmp-test.c  |  4 +++-
 tests/qtest/vhost-user-blk-test.c   |  3 ++-
 tests/qtest/vhost-user-test.c   |  3 ++-
 tests/qtest/virtio-blk-test.c   |  2 +-
 tests/qtest/virtio-scsi-test.c  |  3 ++-
 tests/unit/test-image-locking.c |  6 --
 tests/unit/test-qga.c   |  2 +-
 tests/vhost-user-bridge.c   |  3 ++-
 20 files changed, 76 insertions(+), 35 deletions(-)

diff --git a/tests/qtest/fuzz/generic_fuzz_configs.h 
b/tests/qtest/fuzz/generic_fuzz_configs.h
index 0775e6702b..d0f9961187 100644
--- a/tests/qtest/fuzz/generic_fuzz_configs.h
+++ b/tests/qtest/fuzz/generic_fuzz_configs.h
@@ -20,13 +20,15 @@ typedef struct generic_fuzz_config {
 } generic_fuzz_config;
 
 static inline gchar *generic_fuzzer_virtio_9p_args(void){
-char tmpdir[] = "/tmp/qemu-fuzz.XX";
+char *tmpdir = g_strdup_printf("%s/qemu-fuzz.XX", g_get_tmp_dir());
 g_assert_nonnull(g_mkdtemp(tmpdir));
 
-return g_strdup_printf("-machine q35 -nodefaults "
+gchar *args = g_strdup_printf("-machine q35 -nodefaults "
 "-device virtio-9p,fsdev=hshare,mount_tag=hshare "
 "-fsdev local,id=hshare,path=%s,security_model=mapped-xattr,"
 "writeout=immediate,fmode=0600,dmode=0700", tmpdir);
+g_free(tmpdir);
+return args;
 }
 
 const generic_fuzz_config predefined_configs[] = {
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index f1e510b0ac..f26cd6f86f 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -44,9 +44,9 @@
 #define TEST_IMAGE_SIZE_MB_SMALL 64
 
 /*** Globals ***/
-static char tmp_path[] = "/tmp/qtest.XX";
-static char debug_path[] = "/tmp/qtest-blkdebug.XX";
-static char mig_socket[] = "/tmp/qtest-migration.XX";
+static char *tmp_path;
+static char *debug_path;
+static char *mig_socket;
 static bool ahci_pedantic;
 static const char *imgfmt;
 static unsigned test_image_size_mb;
@@ -1437,7 +1437,7 @@ static void test_ncq_simple(void)
 
 static int prepare_iso(size_t size, unsigned char **buf, char **name)
 {
-char cdrom_path[] = "/tmp/qtest.iso.XX";
+char *cdrom_path = g_strdup_printf("%s/qtest.iso.XX", g_get_tmp_dir());
 unsigned char *patt;
 ssize_t ret;
 int fd = mkstemp(cdrom_path);
@@ -1454,6 +1454,7 @@ static int prepare_iso(size_t size, unsigned char **buf, 
char **name)
 
 *name = g_strdup(cdrom_path);
 *buf = patt;
+g_free(cdrom_path);
 return fd;
 }
 
@@ -1872,6 +1873,7 @@ int main(int argc, char **argv)
 }
 
 /* Create a temporary image */
+tmp_path = g_strdup_printf("%s/qtest.XX", g_get_tmp_dir());
 fd = mkstemp(tmp_path);
 g_assert(fd >= 0);
 if (have_qemu_img()) {
@@ -1889,11 +1891,13 @@ int main(int argc, char **argv)
 close(fd);
 
 /* Create temporary blkdebug instructions */
+debug_path = g_strdup_printf("%s/qtest-blkdebug.XX", g_get_tmp_dir());
 fd = mkstemp(debug_path);
 g_assert(fd >= 0);
 close(fd);
 
 /* Reserve a hollow file to use as a socket for migration tests */
+mig_socket = g_strdup_printf("%s/qtest-migration.XX", g_get_tmp_dir());
 fd = mkstemp(mig_socket);
 g_assert(fd >= 0);
 close(fd);
@@ -1947,8 +1951,11 @@ int main(int argc, char **argv)
 
 /* Cleanup */
 unlink(tmp_path);
+g_free(tmp_path);
 unlink(debug_path);
+g_free(debug_path);
 unlink(mig_socket);
+g_free(mig_socket);
 
 return ret;
 }
diff --git a/tests/qtest/aspeed_smc-test.c b/tests/qtest/aspeed_smc-test.c
index 05ce941566..cab769459c 100644
--- a/tests/qtest/aspeed_smc-test.c
+++ b/tests/qtest/aspeed_smc-test.c
@@ -608,7 +608,7 @@ static void test_write_block_protect_bottom_bit(void)
 flash_reset();
 }
 
-static char tmp_path[] = "/tmp/qtest.m25p80.XX";
+static char *tmp_path;
 
 int main(int argc, char **argv)
 {
@@ -617,6 +617,7 @@ int main(int argc, char **argv)
 
 g_test_init(, , NULL);
 
+tmp_path = g_strdup_printf("%s/qtest.m25p80.XX", g_get_tmp_dir());
 fd = mkstemp(tmp_path);
 g_assert(fd >= 0);
 ret = ftruncate(fd, FLASH_SIZE);
@@ -646,5 +647,6 @@ int main(int argc, char **argv)
 
 qtest_quit(global_qtest);