/ tests / lib / pkg_cpe.c
pkg_cpe.c
 1  /*-
 2   * SPDX-License-Identifier: BSD-2-Clause
 3   *
 4   *​ Copyright (c) 2025 The FreeBSD Foundation
 5   *​
 6   *​ Portions of this software were developed by
 7   * Tuukka Pasanen <tuukka.pasanen@ilmi.fi> under sponsorship from
 8   * the FreeBSD Foundation
 9   */
10  
11  #include <atf-c.h>
12  #include <stdint.h>
13  #include <stdlib.h>
14  
15  #include <private/pkg_cpe.h>
16  
17  ATF_TC_WITHOUT_HEAD(cpeparse);
18  
19  ATF_TC_BODY(cpeparse, tc)
20  {
21  	struct pkg_audit_cpe *cpe_struct = pkg_cpe_parse("");
22  
23  	ATF_REQUIRE(cpe_struct == NULL);
24  
25  	cpe_struct = pkg_cpe_parse("cpe:");
26  	ATF_REQUIRE(cpe_struct == NULL);
27  
28  	cpe_struct = pkg_cpe_parse("cpe:2.3:");
29  	ATF_REQUIRE(cpe_struct == NULL);
30  
31  	cpe_struct = pkg_cpe_parse("cpe:2.2:a:test");
32  	ATF_REQUIRE(cpe_struct == NULL);
33  
34  	cpe_struct = pkg_cpe_parse("cpu:2.3:a:test");
35  	ATF_REQUIRE(cpe_struct == NULL);
36  
37  
38  	cpe_struct = pkg_cpe_parse("cpe:2.3:a:test");
39  	ATF_REQUIRE(cpe_struct != NULL);
40  
41  	ATF_CHECK_EQ(cpe_struct->version_major, 2);
42  	ATF_CHECK_EQ(cpe_struct->version_minor, 3);
43  	ATF_CHECK_EQ(cpe_struct->part, CPE_APPLICATIONS);
44  	ATF_CHECK_STREQ(cpe_struct->vendor, "test");
45  	ATF_REQUIRE(cpe_struct->product == NULL);
46  	ATF_REQUIRE(cpe_struct->version == NULL);
47  	ATF_REQUIRE(cpe_struct->update == NULL);
48  	ATF_REQUIRE(cpe_struct->edition == NULL);
49  	ATF_REQUIRE(cpe_struct->language == NULL);
50  	ATF_REQUIRE(cpe_struct->sw_edition == NULL);
51  	ATF_REQUIRE(cpe_struct->target_sw == NULL);
52  	ATF_REQUIRE(cpe_struct->target_hw == NULL);
53  	ATF_REQUIRE(cpe_struct->other == NULL);
54  
55  	char *test_str = pkg_cpe_create(cpe_struct);
56  	ATF_CHECK_STREQ(test_str, "cpe:2.3:a:test:::::::::");
57  	free(test_str);
58  	pkg_cpe_free(cpe_struct);
59  
60  	cpe_struct = pkg_cpe_parse("cpe:2.3:a:test:test_product:1.0:sp1:1:en-us:14.3:FreeBSD:x86_64:other_things");
61  	ATF_CHECK_EQ(cpe_struct->version_major, 2);
62  	ATF_CHECK_EQ(cpe_struct->version_minor, 3);
63  	ATF_CHECK_EQ(cpe_struct->part, CPE_APPLICATIONS);
64  	ATF_CHECK_STREQ(cpe_struct->vendor, "test");
65  	ATF_CHECK_STREQ(cpe_struct->product, "test_product");
66  	ATF_CHECK_STREQ(cpe_struct->version, "1.0");
67  	ATF_CHECK_STREQ(cpe_struct->update, "sp1");
68  	ATF_CHECK_STREQ(cpe_struct->edition, "1");
69  	ATF_CHECK_STREQ(cpe_struct->language, "en-us");
70  	ATF_CHECK_STREQ(cpe_struct->sw_edition, "14.3");
71  	ATF_CHECK_STREQ(cpe_struct->target_sw, "FreeBSD");
72  	ATF_CHECK_STREQ(cpe_struct->target_hw, "x86_64");
73  	ATF_CHECK_STREQ(cpe_struct->other, "other_things");
74  
75  	test_str = pkg_cpe_create(cpe_struct);
76  	ATF_CHECK_STREQ(test_str, "cpe:2.3:a:test:test_product:1.0:sp1:1:en-us:14.3:FreeBSD:x86_64:other_things");
77  	free(test_str);
78  
79  
80  	pkg_cpe_free(cpe_struct);
81  }
82  
83  ATF_TP_ADD_TCS(tp)
84  {
85  	ATF_TP_ADD_TC(tp, cpeparse);
86  
87  	return (atf_no_error());
88  }