1 /*
2  * This file is part of libmodulemd
3  * Copyright (C) 2018 Red Hat, Inc.
4  *
5  * Fedora-License-Identifier: MIT
6  * SPDX-2.0-License-Identifier: MIT
7  * SPDX-3.0-License-Identifier: MIT
8  *
9  * This program is free software.
10  * For more information on the license, see COPYING.
11  * For more information on free software, see <https://www.gnu.org/philosophy/free-sw.en.html>.
12  */
13 
14 #include <glib.h>
15 #include <glib/gstdio.h>
16 #include <locale.h>
17 #include <signal.h>
18 
19 #include "modulemd-buildopts.h"
20 #include "private/glib-extensions.h"
21 #include "private/modulemd-buildopts-private.h"
22 #include "private/modulemd-yaml.h"
23 #include "private/test-utils.h"
24 
25 typedef struct _BuildoptsFixture
26 {
27 } BuildoptsFixture;
28 
29 static void
buildopts_test_construct(void)30 buildopts_test_construct (void)
31 {
32   g_autoptr (ModulemdBuildopts) b = NULL;
33   g_auto (GStrv) whitelist = NULL;
34   g_auto (GStrv) arches = NULL;
35 
36   /* Test that the new() function works */
37   b = modulemd_buildopts_new ();
38   g_assert_nonnull (b);
39   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
40   g_assert_null (modulemd_buildopts_get_rpm_macros (b));
41   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
42   g_assert_nonnull (whitelist);
43   g_assert_cmpint (g_strv_length (whitelist), ==, 0);
44   arches = modulemd_buildopts_get_arches_as_strv (b);
45   g_assert_nonnull (arches);
46   g_assert_cmpint (g_strv_length (arches), ==, 0);
47   g_clear_object (&b);
48 
49   /* Test that object instantiation works */
50   b = g_object_new (MODULEMD_TYPE_BUILDOPTS, NULL);
51   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
52   g_clear_object (&b);
53 
54   /* Test instantiation works with rpm_macros */
55   b = g_object_new (MODULEMD_TYPE_BUILDOPTS, "rpm_macros", "A test", NULL);
56   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
57   g_assert_cmpstr (modulemd_buildopts_get_rpm_macros (b), ==, "A test");
58   g_clear_object (&b);
59 }
60 
61 
62 static void
buildopts_test_comparison(void)63 buildopts_test_comparison (void)
64 {
65   g_autoptr (ModulemdBuildopts) b_1 = NULL;
66   g_autoptr (ModulemdBuildopts) b_2 = NULL;
67 
68   /*Test 2 objects with no rpm_macros*/
69   b_1 = modulemd_buildopts_new ();
70   g_assert_nonnull (b_1);
71   g_assert_true (MODULEMD_IS_BUILDOPTS (b_1));
72 
73   b_2 = modulemd_buildopts_new ();
74   g_assert_nonnull (b_2);
75   g_assert_true (MODULEMD_IS_BUILDOPTS (b_2));
76 
77   g_assert_true (modulemd_buildopts_equals (b_1, b_2));
78   g_assert_cmpint (modulemd_buildopts_compare (b_1, b_2), ==, 0);
79   g_assert_cmpint (modulemd_buildopts_compare (b_2, b_1), ==, 0);
80   g_clear_object (&b_1);
81   g_clear_object (&b_2);
82 
83   /*Test 2 objects with matching rpm_macros*/
84   b_1 = modulemd_buildopts_new ();
85   modulemd_buildopts_set_rpm_macros (b_1, "a test");
86   g_assert_nonnull (b_1);
87   g_assert_true (MODULEMD_IS_BUILDOPTS (b_1));
88 
89   b_2 = modulemd_buildopts_new ();
90   modulemd_buildopts_set_rpm_macros (b_2, "a test");
91   g_assert_nonnull (b_2);
92   g_assert_true (MODULEMD_IS_BUILDOPTS (b_2));
93 
94   g_assert_true (modulemd_buildopts_equals (b_1, b_2));
95   g_assert_cmpint (modulemd_buildopts_compare (b_1, b_2), ==, 0);
96   g_assert_cmpint (modulemd_buildopts_compare (b_2, b_1), ==, 0);
97   g_clear_object (&b_1);
98   g_clear_object (&b_2);
99 
100   /*Test 2 objects with different rpm_macros*/
101   b_1 = modulemd_buildopts_new ();
102   modulemd_buildopts_set_rpm_macros (b_1, "a test");
103   g_assert_nonnull (b_1);
104   g_assert_true (MODULEMD_IS_BUILDOPTS (b_1));
105 
106   b_2 = modulemd_buildopts_new ();
107   modulemd_buildopts_set_rpm_macros (b_2, "b test");
108   g_assert_nonnull (b_2);
109   g_assert_true (MODULEMD_IS_BUILDOPTS (b_2));
110 
111   g_assert_false (modulemd_buildopts_equals (b_1, b_2));
112   g_assert_cmpint (modulemd_buildopts_compare (b_1, b_2), <, 0);
113   g_assert_cmpint (modulemd_buildopts_compare (b_2, b_1), >, 0);
114   g_clear_object (&b_1);
115   g_clear_object (&b_2);
116 
117   /*Test 2 objects with matching rpm_macros, rpm_whitelist, and arches*/
118   b_1 = modulemd_buildopts_new ();
119   modulemd_buildopts_set_rpm_macros (b_1, "a test");
120   modulemd_buildopts_add_rpm_to_whitelist (b_1, "testrpm");
121   modulemd_buildopts_add_arch (b_1, "x86_64");
122   g_assert_nonnull (b_1);
123   g_assert_true (MODULEMD_IS_BUILDOPTS (b_1));
124 
125   b_2 = modulemd_buildopts_new ();
126   modulemd_buildopts_set_rpm_macros (b_2, "a test");
127   modulemd_buildopts_add_rpm_to_whitelist (b_2, "testrpm");
128   modulemd_buildopts_add_arch (b_2, "x86_64");
129   g_assert_nonnull (b_2);
130   g_assert_true (MODULEMD_IS_BUILDOPTS (b_2));
131 
132   g_assert_true (modulemd_buildopts_equals (b_1, b_2));
133   g_assert_cmpint (modulemd_buildopts_compare (b_1, b_2), ==, 0);
134   g_assert_cmpint (modulemd_buildopts_compare (b_2, b_1), ==, 0);
135   g_clear_object (&b_1);
136   g_clear_object (&b_2);
137 
138   /*Test 2 objects with matching rpm_macros and different whitelist*/
139   b_1 = modulemd_buildopts_new ();
140   modulemd_buildopts_set_rpm_macros (b_1, "a test");
141   modulemd_buildopts_add_rpm_to_whitelist (b_1, "testrpm");
142   g_assert_nonnull (b_1);
143   g_assert_true (MODULEMD_IS_BUILDOPTS (b_1));
144 
145   b_2 = modulemd_buildopts_new ();
146   modulemd_buildopts_set_rpm_macros (b_2, "a test");
147   modulemd_buildopts_add_rpm_to_whitelist (b_2, "testing");
148   g_assert_nonnull (b_2);
149   g_assert_true (MODULEMD_IS_BUILDOPTS (b_2));
150 
151   g_assert_false (modulemd_buildopts_equals (b_1, b_2));
152   g_assert_cmpint (modulemd_buildopts_compare (b_1, b_2), >, 0);
153   g_assert_cmpint (modulemd_buildopts_compare (b_2, b_1), <, 0);
154   g_clear_object (&b_1);
155   g_clear_object (&b_2);
156 
157   /*Test 2 objects with matching rpm_macros and subsets of matching whitelist*/
158   b_1 = modulemd_buildopts_new ();
159   modulemd_buildopts_set_rpm_macros (b_1, "a test");
160   modulemd_buildopts_add_rpm_to_whitelist (b_1, "a");
161   modulemd_buildopts_add_rpm_to_whitelist (b_1, "b");
162   g_assert_nonnull (b_1);
163   g_assert_true (MODULEMD_IS_BUILDOPTS (b_1));
164 
165   b_2 = modulemd_buildopts_new ();
166   modulemd_buildopts_set_rpm_macros (b_2, "a test");
167   modulemd_buildopts_add_rpm_to_whitelist (b_2, "a");
168   modulemd_buildopts_add_rpm_to_whitelist (b_2, "b");
169   modulemd_buildopts_add_rpm_to_whitelist (b_2, "c");
170   g_assert_nonnull (b_2);
171   g_assert_true (MODULEMD_IS_BUILDOPTS (b_2));
172 
173   g_assert_false (modulemd_buildopts_equals (b_1, b_2));
174   g_assert_cmpint (modulemd_buildopts_compare (b_1, b_2), <, 0);
175   g_assert_cmpint (modulemd_buildopts_compare (b_2, b_1), >, 0);
176   g_clear_object (&b_1);
177   g_clear_object (&b_2);
178 
179   /*Test 2 objects with matching rpm_macros and rpm_whitelist,
180    * but different arches*/
181   b_1 = modulemd_buildopts_new ();
182   modulemd_buildopts_set_rpm_macros (b_1, "a test");
183   modulemd_buildopts_add_rpm_to_whitelist (b_1, "testrpm");
184   modulemd_buildopts_add_arch (b_1, "x86_64");
185   g_assert_nonnull (b_1);
186   g_assert_true (MODULEMD_IS_BUILDOPTS (b_1));
187 
188   b_2 = modulemd_buildopts_new ();
189   modulemd_buildopts_set_rpm_macros (b_2, "a test");
190   modulemd_buildopts_add_rpm_to_whitelist (b_2, "testrpm");
191   modulemd_buildopts_add_arch (b_2, "ppc64le");
192   g_assert_nonnull (b_2);
193   g_assert_true (MODULEMD_IS_BUILDOPTS (b_2));
194 
195   g_assert_false (modulemd_buildopts_equals (b_1, b_2));
196   g_assert_cmpint (modulemd_buildopts_compare (b_1, b_2), >, 0);
197   g_assert_cmpint (modulemd_buildopts_compare (b_2, b_1), <, 0);
198   g_clear_object (&b_1);
199   g_clear_object (&b_2);
200 
201   /*Test 2 objects with matching rpm_macros and rpm_whitelist,
202    * and subsets of matching arches*/
203   b_1 = modulemd_buildopts_new ();
204   modulemd_buildopts_set_rpm_macros (b_1, "a test");
205   modulemd_buildopts_add_rpm_to_whitelist (b_1, "testrpm");
206   modulemd_buildopts_add_arch (b_1, "x86_64");
207   modulemd_buildopts_add_arch (b_1, "ppc64le");
208   g_assert_nonnull (b_1);
209   g_assert_true (MODULEMD_IS_BUILDOPTS (b_1));
210 
211   b_2 = modulemd_buildopts_new ();
212   modulemd_buildopts_set_rpm_macros (b_2, "a test");
213   modulemd_buildopts_add_rpm_to_whitelist (b_2, "testrpm");
214   modulemd_buildopts_add_arch (b_2, "x86_64");
215   modulemd_buildopts_add_arch (b_2, "ppc64le");
216   modulemd_buildopts_add_arch (b_2, "s390x");
217   g_assert_nonnull (b_2);
218   g_assert_true (MODULEMD_IS_BUILDOPTS (b_2));
219 
220   g_assert_false (modulemd_buildopts_equals (b_1, b_2));
221   g_assert_cmpint (modulemd_buildopts_compare (b_1, b_2), >, 0);
222   g_assert_cmpint (modulemd_buildopts_compare (b_2, b_1), <, 0);
223   g_clear_object (&b_1);
224   g_clear_object (&b_2);
225 }
226 
227 
228 static void
buildopts_test_copy(void)229 buildopts_test_copy (void)
230 {
231   g_autoptr (ModulemdBuildopts) b = NULL;
232   g_autoptr (ModulemdBuildopts) b_copy = NULL;
233   g_auto (GStrv) whitelist = NULL;
234   g_auto (GStrv) arches = NULL;
235 
236   b = modulemd_buildopts_new ();
237   g_assert_nonnull (b);
238   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
239   g_assert_null (modulemd_buildopts_get_rpm_macros (b));
240   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
241   g_assert_nonnull (whitelist);
242   g_assert_cmpint (g_strv_length (whitelist), ==, 0);
243   g_clear_pointer (&whitelist, g_strfreev);
244   arches = modulemd_buildopts_get_arches_as_strv (b);
245   g_assert_nonnull (arches);
246   g_assert_cmpint (g_strv_length (arches), ==, 0);
247   g_clear_pointer (&arches, g_strfreev);
248 
249   b_copy = modulemd_buildopts_copy (b);
250   g_assert_nonnull (b_copy);
251   g_assert_true (MODULEMD_IS_BUILDOPTS (b_copy));
252   g_assert_null (modulemd_buildopts_get_rpm_macros (b_copy));
253   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b_copy);
254   g_assert_nonnull (whitelist);
255   g_assert_cmpint (g_strv_length (whitelist), ==, 0);
256   g_clear_pointer (&whitelist, g_strfreev);
257   arches = modulemd_buildopts_get_arches_as_strv (b_copy);
258   g_assert_nonnull (arches);
259   g_assert_cmpint (g_strv_length (arches), ==, 0);
260   g_clear_pointer (&arches, g_strfreev);
261   g_clear_object (&b);
262   g_clear_object (&b_copy);
263 
264   /* Test copying buildopts with rpm_macros */
265   b = modulemd_buildopts_new ();
266   modulemd_buildopts_set_rpm_macros (b, "a test");
267   g_assert_nonnull (b);
268   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
269   g_assert_cmpstr (modulemd_buildopts_get_rpm_macros (b), ==, "a test");
270   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
271   g_assert_nonnull (whitelist);
272   g_assert_cmpint (g_strv_length (whitelist), ==, 0);
273   g_clear_pointer (&whitelist, g_strfreev);
274   arches = modulemd_buildopts_get_arches_as_strv (b);
275   g_assert_nonnull (arches);
276   g_assert_cmpint (g_strv_length (arches), ==, 0);
277   g_clear_pointer (&arches, g_strfreev);
278 
279   b_copy = modulemd_buildopts_copy (b);
280   g_assert_nonnull (b_copy);
281   g_assert_true (MODULEMD_IS_BUILDOPTS (b_copy));
282   g_assert_cmpstr (modulemd_buildopts_get_rpm_macros (b_copy), ==, "a test");
283   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b_copy);
284   g_assert_nonnull (whitelist);
285   g_assert_cmpint (g_strv_length (whitelist), ==, 0);
286   g_clear_pointer (&whitelist, g_strfreev);
287   arches = modulemd_buildopts_get_arches_as_strv (b_copy);
288   g_assert_nonnull (arches);
289   g_assert_cmpint (g_strv_length (arches), ==, 0);
290   g_clear_pointer (&arches, g_strfreev);
291   g_clear_object (&b);
292   g_clear_object (&b_copy);
293 
294   /* Test copying buildopts with whitelist */
295   b = modulemd_buildopts_new ();
296   modulemd_buildopts_add_rpm_to_whitelist (b, "testrpm");
297   g_assert_nonnull (b);
298   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
299   g_assert_null (modulemd_buildopts_get_rpm_macros (b));
300   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
301   g_assert_nonnull (whitelist);
302   g_assert_cmpint (g_strv_length (whitelist), ==, 1);
303   g_assert_cmpstr (whitelist[0], ==, "testrpm");
304   g_clear_pointer (&whitelist, g_strfreev);
305   arches = modulemd_buildopts_get_arches_as_strv (b);
306   g_assert_nonnull (arches);
307   g_assert_cmpint (g_strv_length (arches), ==, 0);
308   g_clear_pointer (&arches, g_strfreev);
309 
310   b_copy = modulemd_buildopts_copy (b);
311   g_assert_nonnull (b_copy);
312   g_assert_true (MODULEMD_IS_BUILDOPTS (b_copy));
313   g_assert_null (modulemd_buildopts_get_rpm_macros (b_copy));
314   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b_copy);
315   g_assert_nonnull (whitelist);
316   g_assert_cmpint (g_strv_length (whitelist), ==, 1);
317   g_assert_cmpstr (whitelist[0], ==, "testrpm");
318   g_clear_pointer (&whitelist, g_strfreev);
319   arches = modulemd_buildopts_get_arches_as_strv (b_copy);
320   g_assert_nonnull (arches);
321   g_assert_cmpint (g_strv_length (arches), ==, 0);
322   g_clear_pointer (&arches, g_strfreev);
323   g_clear_object (&b);
324   g_clear_object (&b_copy);
325 
326   /* Test copying buildopts with arches */
327   b = modulemd_buildopts_new ();
328   modulemd_buildopts_add_arch (b, "x86_64");
329   g_assert_nonnull (b);
330   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
331   g_assert_null (modulemd_buildopts_get_rpm_macros (b));
332   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
333   g_assert_nonnull (whitelist);
334   g_assert_cmpint (g_strv_length (whitelist), ==, 0);
335   g_clear_pointer (&whitelist, g_strfreev);
336   arches = modulemd_buildopts_get_arches_as_strv (b);
337   g_assert_nonnull (arches);
338   g_assert_cmpint (g_strv_length (arches), ==, 1);
339   g_assert_cmpstr (arches[0], ==, "x86_64");
340   g_clear_pointer (&arches, g_strfreev);
341 
342   b_copy = modulemd_buildopts_copy (b);
343   g_assert_nonnull (b_copy);
344   g_assert_true (MODULEMD_IS_BUILDOPTS (b_copy));
345   g_assert_null (modulemd_buildopts_get_rpm_macros (b_copy));
346   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b_copy);
347   g_assert_nonnull (whitelist);
348   g_assert_cmpint (g_strv_length (whitelist), ==, 0);
349   g_clear_pointer (&whitelist, g_strfreev);
350   arches = modulemd_buildopts_get_arches_as_strv (b_copy);
351   g_assert_nonnull (arches);
352   g_assert_cmpint (g_strv_length (arches), ==, 1);
353   g_assert_cmpstr (arches[0], ==, "x86_64");
354   g_clear_pointer (&arches, g_strfreev);
355   g_clear_object (&b);
356   g_clear_object (&b_copy);
357 }
358 
359 
360 static void
buildopts_test_get_set_rpm_macros(void)361 buildopts_test_get_set_rpm_macros (void)
362 {
363   g_autoptr (ModulemdBuildopts) b = NULL;
364   g_autofree gchar *rpm_macros;
365 
366   b = modulemd_buildopts_new ();
367   g_assert_nonnull (b);
368   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
369 
370   g_assert_null (modulemd_buildopts_get_rpm_macros (b));
371   g_object_get (b, "rpm_macros", &rpm_macros, NULL);
372   g_assert_null (rpm_macros);
373   g_clear_pointer (&rpm_macros, g_free);
374 
375   /* Set rpm macros */
376   modulemd_buildopts_set_rpm_macros (b, "Some macro");
377   g_assert_cmpstr (modulemd_buildopts_get_rpm_macros (b), ==, "Some macro");
378   g_object_get (b, "rpm_macros", &rpm_macros, NULL);
379   g_assert_cmpstr (rpm_macros, ==, "Some macro");
380   g_clear_pointer (&rpm_macros, g_free);
381 
382   /* Clear rpm_macros */
383   modulemd_buildopts_set_rpm_macros (b, NULL);
384   g_object_get (b, "rpm_macros", &rpm_macros, NULL);
385   g_assert_null (rpm_macros);
386   g_clear_pointer (&rpm_macros, g_free);
387 }
388 
389 static void
buildopts_test_whitelist(void)390 buildopts_test_whitelist (void)
391 {
392   g_autoptr (ModulemdBuildopts) b = NULL;
393   g_auto (GStrv) whitelist = NULL;
394 
395   b = modulemd_buildopts_new ();
396   g_assert_nonnull (b);
397   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
398 
399   /* Assert we start with 0 rpms */
400   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
401   g_assert_cmpint (g_strv_length (whitelist), ==, 0);
402   g_clear_pointer (&whitelist, g_strfreev);
403 
404   /* Whitelist some rpms */
405   modulemd_buildopts_add_rpm_to_whitelist (b, "test2");
406   modulemd_buildopts_add_rpm_to_whitelist (b, "test3");
407   modulemd_buildopts_add_rpm_to_whitelist (b, "test1");
408   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
409   g_assert_cmpint (g_strv_length (whitelist), ==, 3);
410   // They should be sorted
411   g_assert_cmpstr (whitelist[0], ==, "test1");
412   g_assert_cmpstr (whitelist[1], ==, "test2");
413   g_assert_cmpstr (whitelist[2], ==, "test3");
414   g_clear_pointer (&whitelist, g_strfreev);
415 
416   /* Remove some rpms */
417   modulemd_buildopts_remove_rpm_from_whitelist (b, "test2");
418   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
419   g_assert_cmpint (g_strv_length (whitelist), ==, 2);
420   // They should be sorted
421   g_assert_cmpstr (whitelist[0], ==, "test1");
422   g_assert_cmpstr (whitelist[1], ==, "test3");
423   g_clear_pointer (&whitelist, g_strfreev);
424 }
425 
426 static void
buildopts_test_arches(void)427 buildopts_test_arches (void)
428 {
429   g_autoptr (ModulemdBuildopts) b = NULL;
430   g_auto (GStrv) arches = NULL;
431 
432   b = modulemd_buildopts_new ();
433   g_assert_nonnull (b);
434   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
435 
436   /* Assert we start with no arches */
437   arches = modulemd_buildopts_get_arches_as_strv (b);
438   g_assert_cmpint (g_strv_length (arches), ==, 0);
439   g_clear_pointer (&arches, g_strfreev);
440 
441   /* Add some arches */
442   modulemd_buildopts_add_arch (b, "s390x");
443   modulemd_buildopts_add_arch (b, "x86_64");
444   modulemd_buildopts_add_arch (b, "ppc64le");
445   arches = modulemd_buildopts_get_arches_as_strv (b);
446   g_assert_cmpint (g_strv_length (arches), ==, 3);
447   // They should be sorted
448   g_assert_cmpstr (arches[0], ==, "ppc64le");
449   g_assert_cmpstr (arches[1], ==, "s390x");
450   g_assert_cmpstr (arches[2], ==, "x86_64");
451   g_clear_pointer (&arches, g_strfreev);
452 
453   /* Remove an arch */
454   modulemd_buildopts_remove_arch (b, "s390x");
455   arches = modulemd_buildopts_get_arches_as_strv (b);
456   g_assert_cmpint (g_strv_length (arches), ==, 2);
457   // They should be sorted
458   g_assert_cmpstr (arches[0], ==, "ppc64le");
459   g_assert_cmpstr (arches[1], ==, "x86_64");
460   g_clear_pointer (&arches, g_strfreev);
461 }
462 
463 
464 static void
buildopts_test_parse_yaml(void)465 buildopts_test_parse_yaml (void)
466 {
467   g_autoptr (ModulemdBuildopts) b = NULL;
468   g_autoptr (GError) error = NULL;
469   MMD_INIT_YAML_PARSER (parser);
470   g_autofree gchar *yaml_path = NULL;
471   g_auto (GStrv) whitelist = NULL;
472   g_auto (GStrv) arches = NULL;
473   g_autoptr (FILE) yaml_stream = NULL;
474   yaml_path = g_strdup_printf ("%s/b.yaml", g_getenv ("TEST_DATA_PATH"));
475   g_assert_nonnull (yaml_path);
476 
477   yaml_stream = g_fopen (yaml_path, "rbe");
478   g_assert_nonnull (yaml_stream);
479 
480   yaml_parser_set_input_file (&parser, yaml_stream);
481 
482   parser_skip_document_start (&parser);
483 
484   b = modulemd_buildopts_parse_yaml (&parser, TRUE, &error);
485   g_assert_nonnull (b);
486   g_assert_true (MODULEMD_IS_BUILDOPTS (b));
487   g_assert_cmpstr (modulemd_buildopts_get_rpm_macros (b),
488                    ==,
489                    "%demomacro 1\n"
490                    "%demomacro2 %{demomacro}23\n");
491   whitelist = modulemd_buildopts_get_rpm_whitelist_as_strv (b);
492   g_assert_cmpint (g_strv_length (whitelist), ==, 4);
493   g_assert_cmpstr (whitelist[0], ==, "fooscl-1-bar");
494   g_assert_cmpstr (whitelist[1], ==, "fooscl-1-baz");
495   g_assert_cmpstr (whitelist[2], ==, "xxx");
496   g_assert_cmpstr (whitelist[3], ==, "xyz");
497   arches = modulemd_buildopts_get_arches_as_strv (b);
498   g_assert_cmpint (g_strv_length (arches), ==, 2);
499   g_assert_cmpstr (arches[0], ==, "ppc64le");
500   g_assert_cmpstr (arches[1], ==, "x86_64");
501 }
502 
503 
504 static void
buildopts_test_emit_yaml(void)505 buildopts_test_emit_yaml (void)
506 {
507   g_autoptr (ModulemdBuildopts) b = NULL;
508   g_autoptr (GError) error = NULL;
509   MMD_INIT_YAML_EMITTER (emitter);
510   MMD_INIT_YAML_STRING (&emitter, yaml_string);
511 
512   b = modulemd_buildopts_new ();
513   g_assert_true (mmd_emitter_start_stream (&emitter, &error));
514   g_assert_true (mmd_emitter_start_document (&emitter, &error));
515   g_assert_true (
516     mmd_emitter_start_mapping (&emitter, YAML_BLOCK_MAPPING_STYLE, &error));
517   g_assert_true (modulemd_buildopts_emit_yaml (b, &emitter, &error));
518   g_assert_true (mmd_emitter_end_mapping (&emitter, &error));
519   g_assert_true (mmd_emitter_end_document (&emitter, &error));
520   g_assert_true (mmd_emitter_end_stream (&emitter, &error));
521   g_assert_cmpstr (yaml_string->str,
522                    ==,
523                    "---\n"
524                    "rpms: {}\n"
525                    "...\n");
526 
527   g_clear_pointer (&yaml_string, modulemd_yaml_string_free);
528   yaml_emitter_delete (&emitter);
529   yaml_emitter_initialize (&emitter);
530   yaml_string = g_malloc0_n (1, sizeof (modulemd_yaml_string));
531   yaml_emitter_set_output (&emitter, write_yaml_string, (void *)yaml_string);
532   modulemd_buildopts_set_rpm_macros (b, "%testmacro 1\n%anothermacro 2");
533   modulemd_buildopts_add_rpm_to_whitelist (b, "test2");
534   modulemd_buildopts_add_rpm_to_whitelist (b, "test3");
535   modulemd_buildopts_add_rpm_to_whitelist (b, "test1");
536   modulemd_buildopts_add_arch (b, "s390x");
537   modulemd_buildopts_add_arch (b, "x86_64");
538   modulemd_buildopts_add_arch (b, "ppc64le");
539 
540   g_assert_true (mmd_emitter_start_stream (&emitter, &error));
541   g_assert_true (mmd_emitter_start_document (&emitter, &error));
542   g_assert_true (
543     mmd_emitter_start_mapping (&emitter, YAML_BLOCK_MAPPING_STYLE, &error));
544   g_assert_true (modulemd_buildopts_emit_yaml (b, &emitter, &error));
545   g_assert_true (mmd_emitter_end_mapping (&emitter, &error));
546   g_assert_true (mmd_emitter_end_document (&emitter, &error));
547   g_assert_true (mmd_emitter_end_stream (&emitter, &error));
548   g_assert_cmpstr (yaml_string->str,
549                    ==,
550                    "---\n"
551                    "rpms:\n"
552                    "  macros: >-\n"
553                    "    %testmacro 1\n"
554                    "\n"
555                    "    %anothermacro 2\n"
556                    "  whitelist:\n"
557                    "  - test1\n"
558                    "  - test2\n"
559                    "  - test3\n"
560                    "arches: [ppc64le, s390x, x86_64]\n"
561                    "...\n");
562 }
563 
564 
565 int
main(int argc,char * argv[])566 main (int argc, char *argv[])
567 {
568   setlocale (LC_ALL, "");
569 
570   g_test_init (&argc, &argv, NULL);
571   g_test_bug_base ("https://bugzilla.redhat.com/show_bug.cgi?id=");
572 
573   // Define the tests.
574 
575   g_test_add_func ("/modulemd/v2/buildopts/construct",
576                    buildopts_test_construct);
577 
578   g_test_add_func ("/modulemd/v2/buildopts/comparison",
579                    buildopts_test_comparison);
580 
581   g_test_add_func ("/modulemd/v2/buildopts/copy", buildopts_test_copy);
582 
583   g_test_add_func ("/modulemd/v2/buildopts/get_set_rpm_macros",
584                    buildopts_test_get_set_rpm_macros);
585 
586   g_test_add_func ("/modulemd/v2/buildopts/whitelist",
587                    buildopts_test_whitelist);
588 
589   g_test_add_func ("/modulemd/v2/buildopts/arches", buildopts_test_arches);
590 
591   g_test_add_func ("/modulemd/v2/buildopts/yaml/parse",
592                    buildopts_test_parse_yaml);
593 
594   g_test_add_func ("/modulemd/v2/buildopts/yaml/emit",
595                    buildopts_test_emit_yaml);
596 
597   return g_test_run ();
598 }
599