github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/cmd/libsnap-confine-private/test-utils.c (about) 1 /* 2 * Copyright (C) 2016 Canonical Ltd 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 3 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * 16 */ 17 18 #include "test-utils.h" 19 #include "string-utils.h" 20 21 #include "error.h" 22 #include "utils.h" 23 24 #include <glib.h> 25 26 #if !GLIB_CHECK_VERSION(2, 69, 0) 27 // g_spawn_check_exit_status is considered deprecated since 2.69 28 #define g_spawn_check_wait_status(x, y) (g_spawn_check_exit_status (x, y)) 29 #endif 30 31 void rm_rf_tmp(const char *dir) 32 { 33 // Sanity check, don't remove anything that's not in the temporary 34 // directory. This is here to prevent unintended data loss. 35 if (!g_str_has_prefix(dir, "/tmp/")) 36 die("refusing to remove: %s", dir); 37 const gchar *working_directory = NULL; 38 gchar **argv = NULL; 39 gchar **envp = NULL; 40 GSpawnFlags flags = G_SPAWN_SEARCH_PATH; 41 GSpawnChildSetupFunc child_setup = NULL; 42 gpointer user_data = NULL; 43 gchar **standard_output = NULL; 44 gchar **standard_error = NULL; 45 gint exit_status = 0; 46 GError *error = NULL; 47 48 argv = calloc(5, sizeof *argv); 49 if (argv == NULL) 50 die("cannot allocate command argument array"); 51 argv[0] = g_strdup("rm"); 52 if (argv[0] == NULL) 53 die("cannot allocate memory"); 54 argv[1] = g_strdup("-rf"); 55 if (argv[1] == NULL) 56 die("cannot allocate memory"); 57 argv[2] = g_strdup("--"); 58 if (argv[2] == NULL) 59 die("cannot allocate memory"); 60 argv[3] = g_strdup(dir); 61 if (argv[3] == NULL) 62 die("cannot allocate memory"); 63 argv[4] = NULL; 64 g_assert_true(g_spawn_sync 65 (working_directory, argv, envp, flags, child_setup, 66 user_data, standard_output, standard_error, &exit_status, 67 &error)); 68 g_assert_true(g_spawn_check_wait_status(exit_status, NULL)); 69 if (error != NULL) { 70 g_test_message("cannot remove temporary directory: %s\n", 71 error->message); 72 g_error_free(error); 73 } 74 g_free(argv[0]); 75 g_free(argv[1]); 76 g_free(argv[2]); 77 g_free(argv[3]); 78 g_free(argv); 79 } 80 81 void 82 __attribute__((sentinel)) test_argc_argv(int *argcp, char ***argvp, ...) 83 { 84 int argc = 0; 85 char **argv = NULL; 86 va_list ap; 87 88 /* find out how many elements there are */ 89 va_start(ap, argvp); 90 while (NULL != va_arg(ap, const char *)) { 91 argc += 1; 92 } 93 va_end(ap); 94 95 /* argc + terminating NULL entry */ 96 argv = calloc(argc + 1, sizeof argv[0]); 97 g_assert_nonnull(argv); 98 99 va_start(ap, argvp); 100 for (int i = 0; i < argc; i++) { 101 const char *arg = va_arg(ap, const char *); 102 char *arg_copy = sc_strdup(arg); 103 g_test_queue_free(arg_copy); 104 argv[i] = arg_copy; 105 } 106 va_end(ap); 107 108 /* free argv last, so that entries do not leak */ 109 g_test_queue_free(argv); 110 111 *argcp = argc; 112 *argvp = argv; 113 }