github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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  void rm_rf_tmp(const char *dir)
    27  {
    28  	// Sanity check, don't remove anything that's not in the temporary
    29  	// directory. This is here to prevent unintended data loss.
    30  	if (!g_str_has_prefix(dir, "/tmp/"))
    31  		die("refusing to remove: %s", dir);
    32  	const gchar *working_directory = NULL;
    33  	gchar **argv = NULL;
    34  	gchar **envp = NULL;
    35  	GSpawnFlags flags = G_SPAWN_SEARCH_PATH;
    36  	GSpawnChildSetupFunc child_setup = NULL;
    37  	gpointer user_data = NULL;
    38  	gchar **standard_output = NULL;
    39  	gchar **standard_error = NULL;
    40  	gint exit_status = 0;
    41  	GError *error = NULL;
    42  
    43  	argv = calloc(5, sizeof *argv);
    44  	if (argv == NULL)
    45  		die("cannot allocate command argument array");
    46  	argv[0] = g_strdup("rm");
    47  	if (argv[0] == NULL)
    48  		die("cannot allocate memory");
    49  	argv[1] = g_strdup("-rf");
    50  	if (argv[1] == NULL)
    51  		die("cannot allocate memory");
    52  	argv[2] = g_strdup("--");
    53  	if (argv[2] == NULL)
    54  		die("cannot allocate memory");
    55  	argv[3] = g_strdup(dir);
    56  	if (argv[3] == NULL)
    57  		die("cannot allocate memory");
    58  	argv[4] = NULL;
    59  	g_assert_true(g_spawn_sync
    60  		      (working_directory, argv, envp, flags, child_setup,
    61  		       user_data, standard_output, standard_error, &exit_status,
    62  		       &error));
    63  	g_assert_true(g_spawn_check_exit_status(exit_status, NULL));
    64  	if (error != NULL) {
    65  		g_test_message("cannot remove temporary directory: %s\n",
    66  			       error->message);
    67  		g_error_free(error);
    68  	}
    69  	g_free(argv[0]);
    70  	g_free(argv[1]);
    71  	g_free(argv[2]);
    72  	g_free(argv[3]);
    73  	g_free(argv);
    74  }
    75  
    76  void
    77      __attribute__((sentinel)) test_argc_argv(int *argcp, char ***argvp, ...)
    78  {
    79  	int argc = 0;
    80  	char **argv = NULL;
    81  	va_list ap;
    82  
    83  	/* find out how many elements there are */
    84  	va_start(ap, argvp);
    85  	while (NULL != va_arg(ap, const char *)) {
    86  		argc += 1;
    87  	}
    88  	va_end(ap);
    89  
    90  	/* argc + terminating NULL entry */
    91  	argv = calloc(argc + 1, sizeof argv[0]);
    92  	g_assert_nonnull(argv);
    93  
    94  	va_start(ap, argvp);
    95  	for (int i = 0; i < argc; i++) {
    96  		const char *arg = va_arg(ap, const char *);
    97  		char *arg_copy = sc_strdup(arg);
    98  		g_test_queue_free(arg_copy);
    99  		argv[i] = arg_copy;
   100  	}
   101  	va_end(ap);
   102  
   103  	/* free argv last, so that entries do not leak */
   104  	g_test_queue_free(argv);
   105  
   106  	*argcp = argc;
   107  	*argvp = argv;
   108  }