github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/libsnap-confine-private/feature-test.c (about)

     1  /*
     2   * Copyright (C) 2018 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 "feature.h"
    19  #include "feature.c"
    20  
    21  #include <limits.h>
    22  
    23  #include <glib.h>
    24  
    25  #include "string-utils.h"
    26  #include "test-utils.h"
    27  
    28  static char *sc_testdir(void)
    29  {
    30  	char *d = g_dir_make_tmp(NULL, NULL);
    31  	g_assert_nonnull(d);
    32  	g_test_queue_free(d);
    33  	g_test_queue_destroy((GDestroyNotify) rm_rf_tmp, d);
    34  	return d;
    35  }
    36  
    37  // Set the feature flag directory to given value, useful for cleanup handlers.
    38  static void set_feature_flag_dir(const char *dir)
    39  {
    40  	feature_flag_dir = dir;
    41  }
    42  
    43  // Mock the location of the feature flag directory.
    44  static void sc_mock_feature_flag_dir(const char *d)
    45  {
    46  	g_test_queue_destroy((GDestroyNotify) set_feature_flag_dir,
    47  			     (void *)feature_flag_dir);
    48  	set_feature_flag_dir(d);
    49  }
    50  
    51  static void test_feature_enabled__missing_dir(void)
    52  {
    53  	const char *d = sc_testdir();
    54  	char subd[PATH_MAX];
    55  	sc_must_snprintf(subd, sizeof subd, "%s/absent", d);
    56  	sc_mock_feature_flag_dir(subd);
    57  	g_assert(!sc_feature_enabled(SC_FEATURE_PER_USER_MOUNT_NAMESPACE));
    58  }
    59  
    60  static void test_feature_enabled__missing_file(void)
    61  {
    62  	const char *d = sc_testdir();
    63  	sc_mock_feature_flag_dir(d);
    64  	g_assert(!sc_feature_enabled(SC_FEATURE_PER_USER_MOUNT_NAMESPACE));
    65  }
    66  
    67  static void test_feature_enabled__present_file(void)
    68  {
    69  	const char *d = sc_testdir();
    70  	sc_mock_feature_flag_dir(d);
    71  	char pname[PATH_MAX];
    72  	sc_must_snprintf(pname, sizeof pname, "%s/per-user-mount-namespace", d);
    73  	g_file_set_contents(pname, "", -1, NULL);
    74  
    75  	g_assert(sc_feature_enabled(SC_FEATURE_PER_USER_MOUNT_NAMESPACE));
    76  }
    77  
    78  static void test_feature_parallel_instances(void)
    79  {
    80  	const char *d = sc_testdir();
    81  	sc_mock_feature_flag_dir(d);
    82  
    83  	g_assert(!sc_feature_enabled(SC_FEATURE_PARALLEL_INSTANCES));
    84  
    85  	char pname[PATH_MAX];
    86  	sc_must_snprintf(pname, sizeof pname, "%s/parallel-instances", d);
    87  	g_file_set_contents(pname, "", -1, NULL);
    88  
    89  	g_assert(sc_feature_enabled(SC_FEATURE_PARALLEL_INSTANCES));
    90  }
    91  
    92  static void test_feature_hidden_snap_folder(void)
    93  {
    94  	const char *d = sc_testdir();
    95  	sc_mock_feature_flag_dir(d);
    96  
    97  	g_assert(!sc_feature_enabled(SC_FEATURE_HIDDEN_SNAP_FOLDER));
    98  
    99  	char pname[PATH_MAX];
   100  	sc_must_snprintf(pname, sizeof pname, "%s/hidden-snap-folder", d);
   101  	g_file_set_contents(pname, "", -1, NULL);
   102  
   103  	g_assert(sc_feature_enabled(SC_FEATURE_HIDDEN_SNAP_FOLDER));
   104  }
   105  
   106  static void __attribute__((constructor)) init(void)
   107  {
   108  	g_test_add_func("/feature/missing_dir",
   109  			test_feature_enabled__missing_dir);
   110  	g_test_add_func("/feature/missing_file",
   111  			test_feature_enabled__missing_file);
   112  	g_test_add_func("/feature/present_file",
   113  			test_feature_enabled__present_file);
   114  	g_test_add_func("/feature/parallel_instances",
   115  			test_feature_parallel_instances);
   116  	g_test_add_func("/feature/hidden_snap_folder",
   117  			test_feature_hidden_snap_folder);
   118  }