Debian Codegraph @

src/common/test-webcertificate.c

raw
1 /*
2 * Copyright (C) 2020 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7 #include <fcntl.h>
8 #include <unistd.h>
9
10 #include "cockpitwebcertificate.h"
11
12 #include "testlib/cockpittest.h"
13
14 static void
15 do_locate_test (int dirfd, const char *certname, const char *expected_path, const char *expected_error)
16 {
17 char *error = NULL;
18 gchar *path;
19
20 if (certname)
21 {
22 int fd = openat (dirfd, certname, O_CREAT | O_WRONLY, 0666);
23 g_assert_cmpint (fd, >=, 0);
24 close (fd);
25 }
26
27 path = cockpit_certificate_locate (false, &error);
28
29 if (expected_path)
30 cockpit_assert_strmatch (path, expected_path);
31 else
32 g_assert_cmpstr (path, ==, NULL);
33
34 if (expected_error)
35 cockpit_assert_strmatch (error, expected_error);
36 else
37 g_assert_cmpstr (error, ==, NULL);
38
39 g_free (error);
40 g_free (path);
41 if (certname)
42 g_assert_cmpint (unlinkat (dirfd, certname, 0), ==, 0);
43 }
44
45 static void
46 test_locate (void)
47 {
48 g_autofree gchar *workdir = g_strdup ("/tmp/test-cockpit-webcertificate.XXXXXX");
49 g_autofree gchar *cert_dir = NULL;
50 int cert_dir_fd;
51
52 g_assert (g_mkdtemp (workdir) == workdir);
53 cockpit_test_setenv ("XDG_CONFIG_DIRS", workdir);
54
55 /* nonexisting dir, nothing found */
56 do_locate_test (-1, NULL, NULL, "No certificate found in dir: */ws-certs.d");
57
58 /* empty dir, nothing found */
59 cert_dir = g_build_filename (workdir, "cockpit", "ws-certs.d", NULL);
60 g_assert_cmpint (g_mkdir_with_parents (cert_dir, 0777), ==, 0);
61 do_locate_test (-1, NULL, NULL, "No certificate found in dir: */ws-certs.d");
62
63 /* one unrelated file */
64 cert_dir_fd = open (cert_dir, O_PATH);
65 g_assert_cmpint (cert_dir_fd, >=, 0);
66 do_locate_test (cert_dir_fd, "noise.zrt", NULL, "No certificate found in dir: */ws-certs.d");
67
68 /* one good file */
69 do_locate_test (cert_dir_fd, "01-first.cert", "*/cockpit/ws-certs.d/01-first.cert", NULL);
70
71 /* asciibetically last one wins */
72 do_locate_test (cert_dir_fd, "50-better.cert", "*/cockpit/ws-certs.d/50-better.cert", NULL);
73
74 /* *.crt works, too */
75 do_locate_test (cert_dir_fd, "60-best.crt", "*/cockpit/ws-certs.d/60-best.crt", NULL);
76
77 close (cert_dir_fd);
78 g_unsetenv ("XDG_CONFIG_DIRS");
79 rmdir (cert_dir);
80 rmdir (workdir);
81 }
82
83 static void
84 test_keypath (void)
85 {
86 char *path;
87
88 path = cockpit_certificate_key_path ("/etc/cockpit/ws-certs.d/50-good.cert");
89 g_assert_cmpstr (path, ==, "/etc/cockpit/ws-certs.d/50-good.key");
90 g_free (path);
91 path = cockpit_certificate_key_path ("a.cert");
92 g_assert_cmpstr (path, ==, "a.key");
93 g_free (path);
94 path = cockpit_certificate_key_path ("a.crt");
95 g_assert_cmpstr (path, ==, "a.key");
96 g_free (path);
97 }
98
99 int
100 main (int argc,
101 char *argv[])
102 {
103 cockpit_test_init (&argc, &argv);
104
105 g_test_add_func ("/webcertificate/locate", test_locate);
106 g_test_add_func ("/webcertificate/keypath", test_keypath);
107
108 return g_test_run ();
109 }