github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap-confine/cookie-support-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 "cookie-support.h"
    19  #include "cookie-support.c"
    20  
    21  #include "../libsnap-confine-private/cleanup-funcs.h"
    22  #include "../libsnap-confine-private/test-utils.h"
    23  
    24  #include <glib.h>
    25  #include <stdlib.h>
    26  #include <stdio.h>
    27  #include <string.h>
    28  
    29  // Set alternate cookie directory
    30  static void set_cookie_dir(const char *dir)
    31  {
    32  	sc_cookie_dir = dir;
    33  }
    34  
    35  static void set_fake_cookie_dir(void)
    36  {
    37  	char *ctx_dir = NULL;
    38  	ctx_dir = g_dir_make_tmp(NULL, NULL);
    39  	g_assert_nonnull(ctx_dir);
    40  	g_test_queue_free(ctx_dir);
    41  
    42  	g_test_queue_destroy((GDestroyNotify) rm_rf_tmp, ctx_dir);
    43  	g_test_queue_destroy((GDestroyNotify) set_cookie_dir, SC_COOKIE_DIR);
    44  
    45  	set_cookie_dir(ctx_dir);
    46  }
    47  
    48  static void create_dumy_cookie_file(const char *snap_name,
    49  				    const char *dummy_cookie)
    50  {
    51  	char path[PATH_MAX] = { 0 };
    52  	FILE *f;
    53  	int n;
    54  
    55  	snprintf(path, sizeof(path), "%s/snap.%s", sc_cookie_dir, snap_name);
    56  
    57  	f = fopen(path, "w");
    58  	g_assert_nonnull(f);
    59  
    60  	n = fwrite(dummy_cookie, 1, strlen(dummy_cookie), f);
    61  	g_assert_cmpint(n, ==, strlen(dummy_cookie));
    62  
    63  	fclose(f);
    64  }
    65  
    66  static void test_cookie_get_from_snapd__successful(void)
    67  {
    68  	struct sc_error *err SC_CLEANUP(sc_cleanup_error) = NULL;
    69  	char *cookie SC_CLEANUP(sc_cleanup_string) = NULL;
    70  
    71  	char *dummy = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijmnopqrst";
    72  
    73  	set_fake_cookie_dir();
    74  	create_dumy_cookie_file("test-snap", dummy);
    75  
    76  	cookie = sc_cookie_get_from_snapd("test-snap", &err);
    77  	g_assert_null(err);
    78  	g_assert_nonnull(cookie);
    79  	g_assert_cmpint(strlen(cookie), ==, 44);
    80  	g_assert_cmpstr(cookie, ==, dummy);
    81  }
    82  
    83  static void test_cookie_get_from_snapd__nofile(void)
    84  {
    85  	struct sc_error *err SC_CLEANUP(sc_cleanup_error) = NULL;
    86  	char *cookie SC_CLEANUP(sc_cleanup_string) = NULL;
    87  
    88  	set_fake_cookie_dir();
    89  
    90  	cookie = sc_cookie_get_from_snapd("test-snap2", &err);
    91  	g_assert_nonnull(err);
    92  	g_assert_cmpstr(sc_error_domain(err), ==, SC_ERRNO_DOMAIN);
    93  	g_assert_nonnull(strstr(sc_error_msg(err), "cannot open cookie file"));
    94  	g_assert_null(cookie);
    95  }
    96  
    97  static void __attribute__((constructor)) init(void)
    98  {
    99  	g_test_add_func("/snap-cookie/cookie_get_from_snapd/successful",
   100  			test_cookie_get_from_snapd__successful);
   101  	g_test_add_func("/snap-cookie/cookie_get_from_snapd/no_cookie_file",
   102  			test_cookie_get_from_snapd__nofile);
   103  }