Debian Codegraph @

src/ws/test-branding.c

raw
1 /*
2 * Copyright (C) 2025 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include <errno.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #include <glib.h>
13 #include <glib/gstdio.h>
14
15 #include "cockpitbranding.h"
16 #include "testlib/cockpittest.h"
17
18
19 static void
20 setup_branding_dir (const gchar *data_dir)
21 {
22 /* Create the cockpit directory structure in temp dir */
23 g_autofree gchar *cockpit_dir = g_build_filename (data_dir, "cockpit", NULL);
24 g_autofree gchar *static_dir = g_build_filename (cockpit_dir, "static", NULL);
25
26 g_mkdir_with_parents (static_dir, 0755);
27
28 /* Create a dummy file in static to make it a valid document root */
29 g_autofree gchar *dummy_file = g_build_filename (static_dir, "test.txt", NULL);
30 g_file_set_contents (dummy_file, "test", -1, NULL);
31
32 /* Mirror the branding directory structure from source tree
33 * calculate_static_roots() doesn't accept symlink and we also want to modify the structure */
34 g_autofree gchar *branding_dir = g_build_filename (cockpit_dir, "branding", NULL);
35 g_mkdir_with_parents (branding_dir, 0755);
36
37 g_autofree gchar *source_branding = g_build_filename (SRCDIR, "src", "branding", NULL);
38 GDir *source_dir = g_dir_open (source_branding, 0, NULL);
39 g_assert_nonnull (source_dir);
40
41 const gchar *brand_name;
42 while ((brand_name = g_dir_read_name (source_dir)) != NULL)
43 {
44 g_autofree gchar *brand_subdir = g_build_filename (branding_dir, brand_name, NULL);
45 g_mkdir_with_parents (brand_subdir, 0755);
46
47 /* Create a branding.css file to make it a valid branding directory */
48 g_autofree gchar *brand_css = g_build_filename (brand_subdir, "branding.css", NULL);
49 g_file_set_contents (brand_css, "/* test branding */", -1, NULL);
50 }
51
52 g_dir_close (source_dir);
53 }
54
55 static void
56 assert_roots_contains (GStrv roots, const gchar *data_dir, const gchar *suffix_path)
57 {
58 g_autofree gchar *expected_path = g_build_filename (data_dir, suffix_path, NULL);
59 for (int i = 0; roots[i]; i++)
60 {
61 if (g_strcmp0 (roots[i], expected_path) == 0)
62 return;
63 }
64 g_autofree gchar *msg = g_strdup_printf ("Expected to find path '%s' in roots array", expected_path);
65 g_test_fail ();
66 g_test_message ("%s", msg);
67 }
68
69 /**
70 * Tests
71 */
72
73 static void
74 test_roots_local (gconstpointer _unused)
75 {
76 g_auto(GStrv) roots_local = cockpit_branding_calculate_static_roots ("fedora", NULL, NULL, TRUE);
77 /* Can't assert much here, as this tests the actual system branding;
78 * just that it doesn't crash and delivers some list */
79 g_assert_cmpint (g_strv_length (roots_local), >=, 0);
80 }
81
82
83 static void
84 test_roots_basic (gconstpointer data)
85 {
86 const gchar *data_dir = (const gchar *)data;
87
88 g_auto(GStrv) roots_initial = cockpit_branding_calculate_static_roots (NULL, NULL, NULL, FALSE);
89 guint roots_length = g_strv_length (roots_initial);
90 setup_branding_dir (data_dir);
91
92 /* No IDs at all */
93 g_auto(GStrv) roots_none = cockpit_branding_calculate_static_roots (NULL, NULL, NULL, FALSE);
94 g_assert_cmpint (g_strv_length (roots_none), ==, roots_length + 2);
95 assert_roots_contains (roots_none, data_dir, "cockpit/branding/default");
96 assert_roots_contains (roots_none, data_dir, "cockpit/static");
97
98 /* ID */
99 g_auto(GStrv) roots_id = cockpit_branding_calculate_static_roots ("rhel", NULL, NULL, FALSE);
100 g_assert_cmpint (g_strv_length (roots_id), ==, roots_length + 3);
101 assert_roots_contains (roots_id, data_dir, "cockpit/branding/rhel");
102 assert_roots_contains (roots_id, data_dir, "cockpit/branding/default");
103 assert_roots_contains (roots_id, data_dir, "cockpit/static");
104
105 /* ID + VARIANT; We don't actually have rhel-server nor any other variant branding, so it
106 * should not appear; see test_roots_variant() below */
107 g_auto(GStrv) roots_variant = cockpit_branding_calculate_static_roots ("rhel", "server", NULL, FALSE);
108 g_assert_cmpint (g_strv_length (roots_variant), ==, roots_length + 3);
109 assert_roots_contains (roots_variant, data_dir, "cockpit/branding/rhel");
110 assert_roots_contains (roots_variant, data_dir, "cockpit/branding/default");
111 assert_roots_contains (roots_variant, data_dir, "cockpit/static");
112
113 /* ID_LIKE */
114 g_auto(GStrv) roots_like = cockpit_branding_calculate_static_roots ("centos", NULL, "rhel fedora", FALSE);
115 g_assert_cmpint (g_strv_length (roots_like), ==, roots_length + 5);
116 assert_roots_contains (roots_like, data_dir, "cockpit/branding/centos");
117 assert_roots_contains (roots_like, data_dir, "cockpit/branding/rhel");
118 assert_roots_contains (roots_like, data_dir, "cockpit/branding/fedora");
119 assert_roots_contains (roots_like, data_dir, "cockpit/branding/default");
120 assert_roots_contains (roots_like, data_dir, "cockpit/static");
121 }
122
123 static void
124 test_roots_variant (gconstpointer data)
125 {
126
127 g_auto(GStrv) roots_initial = cockpit_branding_calculate_static_roots (NULL, NULL, NULL, FALSE);
128 guint roots_length = g_strv_length (roots_initial);
129
130 const gchar *data_dir = (const gchar *)data;
131
132 /* Create a test variant branding directory (rhel-server) */
133 g_autofree gchar *variant_dir = g_build_filename (data_dir, "cockpit", "branding", "rhel-server", NULL);
134 g_mkdir_with_parents (variant_dir, 0755);
135 g_autofree gchar *variant_css = g_build_filename (variant_dir, "branding.css", NULL);
136 g_file_set_contents (variant_css, "/* test variant branding */", -1, NULL);
137
138 /* That is found */
139 g_auto(GStrv) roots_variant = cockpit_branding_calculate_static_roots ("rhel", "server", NULL, FALSE);
140 g_assert_cmpint (g_strv_length (roots_variant), ==, roots_length + 2);
141 assert_roots_contains (roots_variant, data_dir, "cockpit/branding/rhel-server");
142
143 /* Non-existing variant */
144 g_auto(GStrv) roots_missing = cockpit_branding_calculate_static_roots ("rhel", "workstation", NULL, FALSE);
145 g_assert_cmpint (g_strv_length (roots_missing), ==, roots_length + 1);
146 assert_roots_contains (roots_variant, data_dir, "cockpit/branding/rhel");
147 assert_roots_contains (roots_variant, data_dir, "cockpit/branding/default");
148 assert_roots_contains (roots_variant, data_dir, "cockpit/static");
149 }
150
151 static void
152 test_roots_config (gconstpointer data)
153 {
154 g_auto(GStrv) roots_initial = cockpit_branding_calculate_static_roots (NULL, NULL, NULL, FALSE);
155 guint roots_length = g_strv_length (roots_initial);
156
157 const gchar *tmp_dir = (const gchar *)data;
158 g_autofree gchar *data_dir = g_build_filename (tmp_dir, "data", NULL);
159 g_autofree gchar *config_dir = g_build_filename (tmp_dir, "config", NULL);
160
161 /* Create config branding directory */
162 g_autofree gchar *config_branding_dir = g_build_filename (config_dir, "cockpit", "branding", NULL);
163 g_assert_cmpint (g_mkdir_with_parents (config_branding_dir, 0755), ==, 0);
164
165 g_auto(GStrv) roots = cockpit_branding_calculate_static_roots ("fedora", NULL, NULL, FALSE);
166
167 g_assert_cmpint (g_strv_length (roots), ==, roots_length + 2);
168 assert_roots_contains (roots, config_dir, "cockpit/branding");
169 assert_roots_contains (roots, data_dir, "cockpit/branding/fedora");
170 assert_roots_contains (roots, data_dir, "cockpit/branding/default");
171 assert_roots_contains (roots, data_dir, "cockpit/static");
172 }
173
174 int
175 main (int argc, char *argv[])
176 {
177 int result;
178
179 cockpit_test_init (&argc, &argv);
180
181 /* Create a global temp directory shared by all tests, as
182 * g_get_system_data_dirs() reads XDG_DATA_DIRS just once and caches the result.
183 * So the tests below are sorted by incrementally populating that dir. */
184 g_autofree gchar* global_temp_dir = g_dir_make_tmp ("cockpit-branding-test-XXXXXX", NULL);
185 g_assert_nonnull (global_temp_dir);
186
187 g_autofree gchar *data_dir = g_build_filename (global_temp_dir, "data", NULL);
188 g_mkdir(data_dir, 0755);
189 g_setenv ("XDG_DATA_DIRS", data_dir, TRUE);
190
191 g_autofree gchar *config_dir = g_build_filename (global_temp_dir, "config", NULL);
192 g_mkdir(config_dir, 0755);
193 g_setenv ("XDG_CONFIG_DIRS", config_dir, TRUE);
194
195 g_test_add_data_func ("/branding/roots/local", data_dir,
196 test_roots_local);
197
198 g_test_add_data_func ("/branding/roots/basic", data_dir,
199 test_roots_basic);
200
201 g_test_add_data_func ("/branding/roots/variant", data_dir,
202 test_roots_variant);
203
204 g_test_add_data_func ("/branding/roots/config", global_temp_dir,
205 test_roots_config);
206
207 result = g_test_run ();
208
209 /* Clean up global temp directory */
210 const gchar *argv_rm[] = { "rm", "-rf", global_temp_dir, NULL };
211 g_spawn_sync (NULL, (gchar **)argv_rm, NULL, G_SPAWN_SEARCH_PATH,
212 NULL, NULL, NULL, NULL, NULL, NULL);
213
214 return result;
215 }