github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/cmd/libsnap-confine-private/test-utils-test.c (about)

     1  /*
     2   * Copyright (C) 2017 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  
    20  #include <fcntl.h>
    21  #include <unistd.h>
    22  
    23  #include <glib.h>
    24  
    25  // Check that rm_rf_tmp doesn't remove things outside of /tmp
    26  static void test_rm_rf_tmp(void)
    27  {
    28  	if (access("/nonexistent", F_OK) == 0) {
    29  		g_test_message
    30  		    ("/nonexistent exists but this test doesn't want it to");
    31  		g_test_fail();
    32  		return;
    33  	}
    34  	if (g_test_subprocess()) {
    35  		rm_rf_tmp("/nonexistent");
    36  		return;
    37  	}
    38  	g_test_trap_subprocess(NULL, 0, 0);
    39  	g_test_trap_assert_failed();
    40  }
    41  
    42  static void test_test_argc_argv(void)
    43  {
    44  	// Check that test_argc_argv() correctly stores data
    45  	int argc = 0;
    46  	char **argv = NULL;
    47  
    48  	test_argc_argv(&argc, &argv, NULL);
    49  	g_assert_cmpint(argc, ==, 0);
    50  	g_assert_nonnull(argv);
    51  	g_assert_null(argv[0]);
    52  
    53  	argc = 0;
    54  	argv = NULL;
    55  
    56  	test_argc_argv(&argc, &argv, "zero", "one", "two", NULL);
    57  	g_assert_cmpint(argc, ==, 3);
    58  	g_assert_nonnull(argv);
    59  	g_assert_cmpstr(argv[0], ==, "zero");
    60  	g_assert_cmpstr(argv[1], ==, "one");
    61  	g_assert_cmpstr(argv[2], ==, "two");
    62  	g_assert_null(argv[3]);
    63  }
    64  
    65  static void __attribute__((constructor)) init(void)
    66  {
    67  	g_test_add_func("/test-utils/rm_rf_tmp", test_rm_rf_tmp);
    68  	g_test_add_func("/test-utils/test_argc_argv", test_test_argc_argv);
    69  }