forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookie-support-test.c
103 lines (83 loc) · 2.81 KB
/
cookie-support-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (C) 2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "cookie-support.h"
#include "cookie-support.c"
#include "../libsnap-confine-private/cleanup-funcs.h"
#include "../libsnap-confine-private/test-utils.h"
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Set alternate cookie directory
static void set_cookie_dir(const char *dir)
{
sc_cookie_dir = dir;
}
static void set_fake_cookie_dir(void)
{
char *ctx_dir = NULL;
ctx_dir = g_dir_make_tmp(NULL, NULL);
g_assert_nonnull(ctx_dir);
g_test_queue_free(ctx_dir);
g_test_queue_destroy((GDestroyNotify) rm_rf_tmp, ctx_dir);
g_test_queue_destroy((GDestroyNotify) set_cookie_dir, SC_COOKIE_DIR);
set_cookie_dir(ctx_dir);
}
static void create_dumy_cookie_file(const char *snap_name,
const char *dummy_cookie)
{
char path[PATH_MAX] = { 0 };
FILE *f;
int n;
snprintf(path, sizeof(path), "%s/snap.%s", sc_cookie_dir, snap_name);
f = fopen(path, "w");
g_assert_nonnull(f);
n = fwrite(dummy_cookie, 1, strlen(dummy_cookie), f);
g_assert_cmpint(n, ==, strlen(dummy_cookie));
fclose(f);
}
static void test_cookie_get_from_snapd__successful(void)
{
struct sc_error *err SC_CLEANUP(sc_cleanup_error) = NULL;
char *cookie SC_CLEANUP(sc_cleanup_string) = NULL;
char *dummy = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijmnopqrst";
set_fake_cookie_dir();
create_dumy_cookie_file("test-snap", dummy);
cookie = sc_cookie_get_from_snapd("test-snap", &err);
g_assert_null(err);
g_assert_nonnull(cookie);
g_assert_cmpint(strlen(cookie), ==, 44);
g_assert_cmpstr(cookie, ==, dummy);
}
static void test_cookie_get_from_snapd__nofile(void)
{
struct sc_error *err SC_CLEANUP(sc_cleanup_error) = NULL;
char *cookie SC_CLEANUP(sc_cleanup_string) = NULL;
set_fake_cookie_dir();
cookie = sc_cookie_get_from_snapd("test-snap2", &err);
g_assert_nonnull(err);
g_assert_cmpstr(sc_error_domain(err), ==, SC_ERRNO_DOMAIN);
g_assert_nonnull(strstr(sc_error_msg(err), "cannot open cookie file"));
g_assert_null(cookie);
}
static void __attribute__((constructor)) init(void)
{
g_test_add_func("/snap-cookie/cookie_get_from_snapd/successful",
test_cookie_get_from_snapd__successful);
g_test_add_func("/snap-cookie/cookie_get_from_snapd/no_cookie_file",
test_cookie_get_from_snapd__nofile);
}