xref: /freebsd/contrib/pkgconf/tests/api/test-oom-spdxtool.c (revision 592efe252472a3385acf36b1f49ecf710a7f3d9c)
1*592efe25SPierre Pronchery /*
2*592efe25SPierre Pronchery  * test-oom.c
3*592efe25SPierre Pronchery  * Allocation-failure (OOM) tests for spdxtool, using the oom.h injection
4*592efe25SPierre Pronchery  * harness.  For each target, a failure is injected at every successive
5*592efe25SPierre Pronchery  * allocation; the target must report the error gracefully (NULL) and, under
6*592efe25SPierre Pronchery  * ASAN, leak nothing on the error path.
7*592efe25SPierre Pronchery  *
8*592efe25SPierre Pronchery  * SPDX-License-Identifier: pkgconf
9*592efe25SPierre Pronchery  *
10*592efe25SPierre Pronchery  * Copyright (c) 2026 pkgconf authors (see AUTHORS).
11*592efe25SPierre Pronchery  *
12*592efe25SPierre Pronchery  * Permission to use, copy, modify, and/or distribute this software for any
13*592efe25SPierre Pronchery  * purpose with or without fee is hereby granted, provided that the above
14*592efe25SPierre Pronchery  * copyright notice and this permission notice appear in all copies.
15*592efe25SPierre Pronchery  *
16*592efe25SPierre Pronchery  * This software is provided 'as is' and without any warranty, express or
17*592efe25SPierre Pronchery  * implied.  In no event shall the authors be liable for any damages arising
18*592efe25SPierre Pronchery  * from the use of this software.
19*592efe25SPierre Pronchery  */
20*592efe25SPierre Pronchery 
21*592efe25SPierre Pronchery #include <libpkgconf/stdinc.h>
22*592efe25SPierre Pronchery #include <libpkgconf/libpkgconf.h>
23*592efe25SPierre Pronchery #include "oom.h"
24*592efe25SPierre Pronchery #include "core.h"
25*592efe25SPierre Pronchery #include "software.h"
26*592efe25SPierre Pronchery #include "simplelicensing.h"
27*592efe25SPierre Pronchery #include "serialize.h"
28*592efe25SPierre Pronchery #include "util.h"
29*592efe25SPierre Pronchery 
30*592efe25SPierre Pronchery static pkgconf_client_t *g_client;
31*592efe25SPierre Pronchery 
32*592efe25SPierre Pronchery static void
oom_setup(void)33*592efe25SPierre Pronchery oom_setup(void)
34*592efe25SPierre Pronchery {
35*592efe25SPierre Pronchery 	g_client = test_client_new();
36*592efe25SPierre Pronchery 	// Mirror cli/spdxtool/main.c: the constructors read these globals
37*592efe25SPierre Pronchery 	spdxtool_util_set_uri_root(g_client, "https://example.com/test");
38*592efe25SPierre Pronchery 	spdxtool_util_set_uri_separator_colon(g_client, false);
39*592efe25SPierre Pronchery 	spdxtool_util_set_spdx_version(g_client, "3.0.1");
40*592efe25SPierre Pronchery 	spdxtool_util_set_spdx_license(g_client, "CC0-1.0");
41*592efe25SPierre Pronchery }
42*592efe25SPierre Pronchery 
43*592efe25SPierre Pronchery /*
44*592efe25SPierre Pronchery  * ==============================================
45*592efe25SPierre Pronchery  * core / software / simplelicensing constructors
46*592efe25SPierre Pronchery  * ==============================================
47*592efe25SPierre Pronchery  */
48*592efe25SPierre Pronchery 
49*592efe25SPierre Pronchery static void
test_oom_agent_new(void)50*592efe25SPierre Pronchery test_oom_agent_new(void)
51*592efe25SPierre Pronchery {
52*592efe25SPierre Pronchery 	spdxtool_core_agent_t *a;
53*592efe25SPierre Pronchery 	OOM_TEST_PTR(a, spdxtool_core_agent_new(g_client, "_:c1", "Agent Name"), spdxtool_core_agent_free(a));
54*592efe25SPierre Pronchery }
55*592efe25SPierre Pronchery 
56*592efe25SPierre Pronchery static void
test_oom_creation_info_new(void)57*592efe25SPierre Pronchery test_oom_creation_info_new(void)
58*592efe25SPierre Pronchery {
59*592efe25SPierre Pronchery 	spdxtool_core_creation_info_t *c;
60*592efe25SPierre Pronchery 	// NULL time exercises the get_current_iso8601_time() strdup path too
61*592efe25SPierre Pronchery 	OOM_TEST_PTR(c, spdxtool_core_creation_info_new(g_client, "agentid", "_:c1", NULL),
62*592efe25SPierre Pronchery 		spdxtool_core_creation_info_free(c));
63*592efe25SPierre Pronchery }
64*592efe25SPierre Pronchery 
65*592efe25SPierre Pronchery static void
test_oom_spdx_document_new(void)66*592efe25SPierre Pronchery test_oom_spdx_document_new(void)
67*592efe25SPierre Pronchery {
68*592efe25SPierre Pronchery 	spdxtool_core_spdx_document_t *d;
69*592efe25SPierre Pronchery 	OOM_TEST_PTR(d, spdxtool_core_spdx_document_new(g_client, "docid", "_:c1", "agentid"),
70*592efe25SPierre Pronchery 		spdxtool_core_spdx_document_free(d));
71*592efe25SPierre Pronchery }
72*592efe25SPierre Pronchery 
73*592efe25SPierre Pronchery static void
test_oom_sbom_new(void)74*592efe25SPierre Pronchery test_oom_sbom_new(void)
75*592efe25SPierre Pronchery {
76*592efe25SPierre Pronchery 	spdxtool_software_sbom_t *s;
77*592efe25SPierre Pronchery 	OOM_TEST_PTR(s, spdxtool_software_sbom_new(g_client, "sbomid", "_:c1", "build"), spdxtool_software_sbom_free(s));
78*592efe25SPierre Pronchery }
79*592efe25SPierre Pronchery 
80*592efe25SPierre Pronchery static void
test_oom_license_expression_new(void)81*592efe25SPierre Pronchery test_oom_license_expression_new(void)
82*592efe25SPierre Pronchery {
83*592efe25SPierre Pronchery 	spdxtool_simplelicensing_license_expression_t *e;
84*592efe25SPierre Pronchery 	OOM_TEST_PTR(e, spdxtool_simplelicensing_licenseExpression_new(g_client, "MIT"),
85*592efe25SPierre Pronchery 		spdxtool_simplelicensing_licenseExpression_free(e));
86*592efe25SPierre Pronchery }
87*592efe25SPierre Pronchery 
88*592efe25SPierre Pronchery /*
89*592efe25SPierre Pronchery  * ======================
90*592efe25SPierre Pronchery  * serializer value model
91*592efe25SPierre Pronchery  * ======================
92*592efe25SPierre Pronchery  */
93*592efe25SPierre Pronchery 
94*592efe25SPierre Pronchery static void
test_oom_serialize_constructors(void)95*592efe25SPierre Pronchery test_oom_serialize_constructors(void)
96*592efe25SPierre Pronchery {
97*592efe25SPierre Pronchery 	spdxtool_serialize_object_list_t *o;
98*592efe25SPierre Pronchery 	OOM_TEST_PTR(o, spdxtool_serialize_object_list_new(), spdxtool_serialize_object_list_free(o));
99*592efe25SPierre Pronchery 
100*592efe25SPierre Pronchery 	spdxtool_serialize_array_t *a;
101*592efe25SPierre Pronchery 	OOM_TEST_PTR(a, spdxtool_serialize_array_new(), spdxtool_serialize_array_free(a));
102*592efe25SPierre Pronchery 
103*592efe25SPierre Pronchery 	spdxtool_serialize_value_t *v;
104*592efe25SPierre Pronchery 	OOM_TEST_PTR(v, spdxtool_serialize_value_string("hello"), spdxtool_serialize_value_free(v));
105*592efe25SPierre Pronchery 	OOM_TEST_PTR(v, spdxtool_serialize_value_int(7), spdxtool_serialize_value_free(v));
106*592efe25SPierre Pronchery 	OOM_TEST_PTR(v, spdxtool_serialize_value_bool(true), spdxtool_serialize_value_free(v));
107*592efe25SPierre Pronchery 	OOM_TEST_PTR(v, spdxtool_serialize_value_null(), spdxtool_serialize_value_free(v));
108*592efe25SPierre Pronchery }
109*592efe25SPierre Pronchery 
110*592efe25SPierre Pronchery /*
111*592efe25SPierre Pronchery  * ============================================================
112*592efe25SPierre Pronchery  * *_to_object serializers that depend only on their own struct
113*592efe25SPierre Pronchery  * ============================================================
114*592efe25SPierre Pronchery  */
115*592efe25SPierre Pronchery 
116*592efe25SPierre Pronchery static void
test_oom_to_object(void)117*592efe25SPierre Pronchery test_oom_to_object(void)
118*592efe25SPierre Pronchery {
119*592efe25SPierre Pronchery 	spdxtool_serialize_value_t *v;
120*592efe25SPierre Pronchery 
121*592efe25SPierre Pronchery 	spdxtool_core_agent_t *agent = spdxtool_core_agent_new(g_client, "_:c1", "Agent");
122*592efe25SPierre Pronchery 	TEST_ASSERT_NONNULL(agent);
123*592efe25SPierre Pronchery 	OOM_TEST_PTR(v, spdxtool_core_agent_to_object(g_client, agent),
124*592efe25SPierre Pronchery 		spdxtool_serialize_value_free(v));
125*592efe25SPierre Pronchery 	spdxtool_core_agent_free(agent);
126*592efe25SPierre Pronchery 
127*592efe25SPierre Pronchery 	spdxtool_core_creation_info_t *ci =
128*592efe25SPierre Pronchery 		spdxtool_core_creation_info_new(g_client, "agentid", "_:c1", "2020-01-01T00:00:00Z");
129*592efe25SPierre Pronchery 	TEST_ASSERT_NONNULL(ci);
130*592efe25SPierre Pronchery 	OOM_TEST_PTR(v, spdxtool_core_creation_info_to_object(g_client, ci),
131*592efe25SPierre Pronchery 		spdxtool_serialize_value_free(v));
132*592efe25SPierre Pronchery 	spdxtool_core_creation_info_free(ci);
133*592efe25SPierre Pronchery 
134*592efe25SPierre Pronchery 	spdxtool_simplelicensing_license_expression_t *le =
135*592efe25SPierre Pronchery 		spdxtool_simplelicensing_licenseExpression_new(g_client, "MIT");
136*592efe25SPierre Pronchery 	TEST_ASSERT_NONNULL(le);
137*592efe25SPierre Pronchery 	OOM_TEST_PTR(v, spdxtool_simplelicensing_licenseExpression_to_object(g_client, "_:c1", le),
138*592efe25SPierre Pronchery 		spdxtool_serialize_value_free(v));
139*592efe25SPierre Pronchery 	spdxtool_simplelicensing_licenseExpression_free(le);
140*592efe25SPierre Pronchery }
141*592efe25SPierre Pronchery 
142*592efe25SPierre Pronchery int
main(int argc,const char ** argv)143*592efe25SPierre Pronchery main(int argc, const char **argv)
144*592efe25SPierre Pronchery {
145*592efe25SPierre Pronchery 	(void) argc;
146*592efe25SPierre Pronchery 	const char *basename = pkgconf_path_find_basename(argv[0]);
147*592efe25SPierre Pronchery 
148*592efe25SPierre Pronchery 	oom_setup();
149*592efe25SPierre Pronchery 
150*592efe25SPierre Pronchery 	TEST_RUN(basename, test_oom_agent_new);
151*592efe25SPierre Pronchery 	TEST_RUN(basename, test_oom_creation_info_new);
152*592efe25SPierre Pronchery 	TEST_RUN(basename, test_oom_spdx_document_new);
153*592efe25SPierre Pronchery 	TEST_RUN(basename, test_oom_sbom_new);
154*592efe25SPierre Pronchery 	TEST_RUN(basename, test_oom_license_expression_new);
155*592efe25SPierre Pronchery 	TEST_RUN(basename, test_oom_serialize_constructors);
156*592efe25SPierre Pronchery 	TEST_RUN(basename, test_oom_to_object);
157*592efe25SPierre Pronchery 
158*592efe25SPierre Pronchery 	pkgconf_client_free(g_client);
159*592efe25SPierre Pronchery 	return EXIT_SUCCESS;
160*592efe25SPierre Pronchery }
161