Debian Codegraph @

tests/wp/json-utils.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2023 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "../common/test-log.h"
10
11 static void
12 test_match_rules_update_properties (void)
13 {
14 static const gchar * const rules_json_string =
15 "["
16 " {"
17 " matches = ["
18 " {"
19 " device.name = \"~alsa_card.*\""
20 " }"
21 " ]"
22 " actions = {"
23 " update-props = {"
24 " api.alsa.use-acp = true"
25 " api.acp.auto-port = false"
26 " }"
27 " }"
28 " }"
29 " {"
30 " matches = ["
31 " {"
32 " node.name = \"alsa_output.0.my-alsa-device\""
33 " }"
34 " ]"
35 " actions = {"
36 " update-props = {"
37 " audio.rate = 96000"
38 " node.description = \"My ALSA Node\""
39 " media.class = null"
40 " }"
41 " }"
42 " }"
43 "]";
44
45 g_autoptr (WpSpaJson) rules = wp_spa_json_new_wrap_stringn (rules_json_string,
46 strlen (rules_json_string));
47 g_assert_nonnull (rules);
48
49 /* Unmatched */
50 {
51 g_autoptr (WpProperties) match_props = NULL;
52
53 match_props = wp_properties_new (
54 "device.name", "unmatched-device-name",
55 NULL);
56
57 g_assert_cmpint (wp_json_utils_match_rules_update_properties (rules, match_props), ==, 0);
58 }
59
60 /* Match regex with props filled */
61 {
62 g_autoptr (WpProperties) match_props = NULL;
63 const gchar *str = NULL;
64
65 match_props = wp_properties_new (
66 "device.name", "alsa_card_0.my-alsa-device",
67 NULL);
68 g_assert_nonnull (match_props);
69
70 str = wp_properties_get (match_props, "device.name");
71 g_assert_cmpstr (str, ==, "alsa_card_0.my-alsa-device");
72 str = wp_properties_get (match_props, "api.alsa.use-acp");
73 g_assert_null (str);
74 str = wp_properties_get (match_props, "api.acp.auto-port");
75 g_assert_null (str);
76
77 g_assert_cmpint (wp_json_utils_match_rules_update_properties (rules, match_props), ==, 2);
78
79 str = wp_properties_get (match_props, "device.name");
80 g_assert_cmpstr (str, ==, "alsa_card_0.my-alsa-device");
81 str = wp_properties_get (match_props, "api.alsa.use-acp");
82 g_assert_cmpstr (str, ==, "true");
83 str = wp_properties_get (match_props, "api.acp.auto-port");
84 g_assert_cmpstr (str, ==, "false");
85 }
86
87 /* Match equal with props filled */
88 {
89 g_autoptr (WpProperties) match_props = NULL;
90 const gchar *str = NULL;
91
92 match_props = wp_properties_new (
93 "node.name", "alsa_output.0.my-alsa-device",
94 NULL);
95 g_assert_nonnull (match_props);
96
97 str = wp_properties_get (match_props, "node.name");
98 g_assert_cmpstr (str, ==, "alsa_output.0.my-alsa-device");
99 str = wp_properties_get (match_props, "audio.rate");
100 g_assert_null (str);
101 str = wp_properties_get (match_props, "node.description");
102 g_assert_null (str);
103 str = wp_properties_get (match_props, "media.class");
104 g_assert_null (str);
105
106 g_assert_cmpint (wp_json_utils_match_rules_update_properties (rules, match_props), ==, 2);
107
108 str = wp_properties_get (match_props, "node.name");
109 g_assert_cmpstr (str, ==, "alsa_output.0.my-alsa-device");
110 str = wp_properties_get (match_props, "audio.rate");
111 g_assert_cmpstr (str, ==, "96000");
112 str = wp_properties_get (match_props, "node.description");
113 g_assert_cmpstr (str, ==, "My ALSA Node");
114 str = wp_properties_get (match_props, "media.class");
115 g_assert_null (str);
116 }
117
118 /* Match equal with 1 prop updated */
119 {
120 g_autoptr (WpProperties) match_props = NULL;
121 const gchar *str = NULL;
122
123 match_props = wp_properties_new (
124 "node.name", "alsa_output.0.my-alsa-device",
125 "audio.rate", "96000",
126 "node.description", "Test",
127 NULL);
128 g_assert_nonnull (match_props);
129
130 str = wp_properties_get (match_props, "node.name");
131 g_assert_cmpstr (str, ==, "alsa_output.0.my-alsa-device");
132 str = wp_properties_get (match_props, "audio.rate");
133 g_assert_cmpstr (str, ==, "96000");
134 str = wp_properties_get (match_props, "node.description");
135 g_assert_cmpstr (str, ==, "Test");
136 str = wp_properties_get (match_props, "media.class");
137 g_assert_null (str);
138
139 g_assert_cmpint (wp_json_utils_match_rules_update_properties (rules, match_props), ==, 1);
140
141 str = wp_properties_get (match_props, "node.name");
142 g_assert_cmpstr (str, ==, "alsa_output.0.my-alsa-device");
143 str = wp_properties_get (match_props, "audio.rate");
144 g_assert_cmpstr (str, ==, "96000");
145 str = wp_properties_get (match_props, "node.description");
146 g_assert_cmpstr (str, ==, "My ALSA Node");
147 str = wp_properties_get (match_props, "media.class");
148 g_assert_null (str);
149 }
150
151 /* Match equal with prop deleted */
152 {
153 g_autoptr (WpProperties) match_props = NULL;
154 const gchar *str = NULL;
155
156 match_props = wp_properties_new (
157 "node.name", "alsa_output.0.my-alsa-device",
158 "media.class", "Audio/Sink",
159 "audio.rate", "48000",
160 "node.description", "Test",
161 NULL);
162 g_assert_nonnull (match_props);
163
164 str = wp_properties_get (match_props, "node.name");
165 g_assert_cmpstr (str, ==, "alsa_output.0.my-alsa-device");
166 str = wp_properties_get (match_props, "audio.rate");
167 g_assert_cmpstr (str, ==, "48000");
168 str = wp_properties_get (match_props, "node.description");
169 g_assert_cmpstr (str, ==, "Test");
170 str = wp_properties_get (match_props, "media.class");
171 g_assert_cmpstr (str, ==, "Audio/Sink");
172
173 g_assert_cmpint (wp_json_utils_match_rules_update_properties (rules, match_props), ==, 3);
174
175 str = wp_properties_get (match_props, "node.name");
176 g_assert_cmpstr (str, ==, "alsa_output.0.my-alsa-device");
177 str = wp_properties_get (match_props, "audio.rate");
178 g_assert_cmpstr (str, ==, "96000");
179 str = wp_properties_get (match_props, "node.description");
180 g_assert_cmpstr (str, ==, "My ALSA Node");
181 str = wp_properties_get (match_props, "media.class");
182 g_assert_null (str);
183 }
184 }
185
186 static gboolean
187 match_rules_cb (gpointer data, const gchar * action, WpSpaJson * value,
188 GError ** error)
189 {
190 WpProperties *match_props = data;
191
192 if (g_str_equal (action, "update-props")) {
193 wp_properties_update_from_json (match_props, value);
194 }
195 else if (g_str_equal (action, "set-answer")) {
196 g_autofree gchar *str = wp_spa_json_to_string (value);
197 wp_properties_set (match_props, "answer.universe", str);
198 }
199 else if (g_str_equal (action, "generate-error")) {
200 g_autofree gchar *str = wp_spa_json_parse_string (value);
201 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
202 "error: %s", str);
203 return FALSE;
204 }
205 else if (g_str_equal (action, "set-description")) {
206 g_autofree gchar *str = wp_spa_json_parse_string (value);
207 wp_properties_set (match_props, "device.description", str);
208 }
209
210 return TRUE;
211 }
212
213 static void
214 test_match_rules (void)
215 {
216 static const gchar * const rules_json_string =
217 "["
218 " {"
219 " matches = ["
220 " {"
221 " device.name = \"~alsa_card.*\""
222 " }"
223 " ]"
224 " actions = {"
225 " update-props = {"
226 " device.name = alsa_card.1"
227 " api.acp.auto-port = false"
228 " }"
229 " set-answer = 42"
230 " }"
231 " }"
232 " {"
233 " matches = ["
234 " {"
235 " test.error = true"
236 " }"
237 " ]"
238 " actions = {"
239 " generate-error = \"test.error is true\""
240 " }"
241 " }"
242 " {"
243 " matches = ["
244 " {"
245 " device.name = \"alsa_card.1\""
246 " }"
247 " ]"
248 " actions = {"
249 " set-description = \"My ALSA Device\""
250 " }"
251 " }"
252 "]";
253
254 g_autoptr (WpSpaJson) rules = wp_spa_json_new_wrap_stringn (rules_json_string,
255 strlen (rules_json_string));
256 g_assert_nonnull (rules);
257
258 /* no error */
259 {
260 g_autoptr (GError) error = NULL;
261 g_autoptr (WpProperties) match_props = NULL;
262 const gchar *str = NULL;
263
264 match_props = wp_properties_new (
265 "device.name", "alsa_card.0",
266 "test.error", "false",
267 NULL);
268
269 str = wp_properties_get (match_props, "device.name");
270 g_assert_cmpstr (str, ==, "alsa_card.0");
271 str = wp_properties_get (match_props, "api.acp.auto-port");
272 g_assert_null (str);
273 str = wp_properties_get (match_props, "answer.universe");
274 g_assert_null (str);
275 str = wp_properties_get (match_props, "test.error");
276 g_assert_cmpstr (str, ==, "false");
277 str = wp_properties_get (match_props, "device.description");
278 g_assert_null (str);
279
280 g_assert_true (wp_json_utils_match_rules (rules, match_props, match_rules_cb,
281 match_props, &error));
282 g_assert_no_error (error);
283
284 str = wp_properties_get (match_props, "device.name");
285 g_assert_cmpstr (str, ==, "alsa_card.1");
286 str = wp_properties_get (match_props, "api.acp.auto-port");
287 g_assert_cmpstr (str, ==, "false");
288 str = wp_properties_get (match_props, "answer.universe");
289 g_assert_cmpstr (str, ==, "42");
290 str = wp_properties_get (match_props, "test.error");
291 g_assert_cmpstr (str, ==, "false");
292 str = wp_properties_get (match_props, "device.description");
293 g_assert_cmpstr (str, ==, "My ALSA Device");
294 }
295
296 /* with error */
297 {
298 g_autoptr (GError) error = NULL;
299 g_autoptr (WpProperties) match_props = NULL;
300 const gchar *str = NULL;
301
302 match_props = wp_properties_new (
303 "device.name", "alsa_card.256",
304 "test.error", "true",
305 NULL);
306
307 str = wp_properties_get (match_props, "device.name");
308 g_assert_cmpstr (str, ==, "alsa_card.256");
309 str = wp_properties_get (match_props, "api.acp.auto-port");
310 g_assert_null (str);
311 str = wp_properties_get (match_props, "answer.universe");
312 g_assert_null (str);
313 str = wp_properties_get (match_props, "test.error");
314 g_assert_cmpstr (str, ==, "true");
315 str = wp_properties_get (match_props, "device.description");
316 g_assert_null (str);
317
318 g_assert_false (wp_json_utils_match_rules (rules, match_props, match_rules_cb,
319 match_props, &error));
320 g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED);
321 g_assert_cmpstr (error->message, ==, "error: test.error is true");
322
323 str = wp_properties_get (match_props, "device.name");
324 g_assert_cmpstr (str, ==, "alsa_card.1");
325 str = wp_properties_get (match_props, "api.acp.auto-port");
326 g_assert_cmpstr (str, ==, "false");
327 str = wp_properties_get (match_props, "answer.universe");
328 g_assert_cmpstr (str, ==, "42");
329 str = wp_properties_get (match_props, "test.error");
330 g_assert_cmpstr (str, ==, "true");
331 str = wp_properties_get (match_props, "device.description");
332 g_assert_null (str);
333 }
334 }
335
336 gint
337 main (gint argc, gchar *argv[])
338 {
339 g_test_init (&argc, &argv, NULL);
340 g_log_set_writer_func (wp_log_writer_default, NULL, NULL);
341
342 g_test_add_func ("/wp/json-utils/match_rules_update_props",
343 test_match_rules_update_properties);
344 g_test_add_func ("/wp/json-utils/match_rules", test_match_rules);
345
346 return g_test_run ();
347 }