Debian Codegraph @

tests/API/CPE/name/test_api_cpe_uri.c

raw symbols SCIP
1 /*
2 * Copyright 2008 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * OpenScap CPE URI Module Test Suite Helper
6 *
7 * Authors:
8 * Ondrej Moris <[email protected]>
9 */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <cpe_name.h>
20
21 void print_usage(const char *, FILE *);
22
23 struct cpe_name *cpe_example_creation(int, const char *, const char *, const char *,
24 const char *, const char *, const char *,
25 const char *, const char *, const char *, const char *);
26
27 void cpe_print(struct cpe_name *);
28
29 int main(int argc, char **argv)
30 {
31 struct cpe_name *name = NULL, *candidate_cpe = NULL, **cpes = NULL;
32 FILE *f;
33 int ret_val = 1, i;
34 char *cpe_uri = NULL;
35 bool match_result_b = false;
36
37 if (argc == 2 && !strcmp(argv[1], "--help")) {
38 print_usage(argv[0], stdout);
39 ret_val = 0;
40 }
41 // Create a new CPE name from given information and store it to given file.
42 else if (argc == 13 && strcmp(argv[1], "--creation") == 0) {
43
44 // Create CPE from given information.
45 if ((name =
46 cpe_example_creation(atoi(argv[2]), argv[3], argv[4], argv[5],
47 argv[6], argv[7], argv[8],
48 argv[9], argv[10], argv[11], argv[12])) != NULL) {
49
50 // Extract URI of created CPE.
51 if ((cpe_uri = cpe_name_get_as_str(name)) != NULL) {
52
53 // Check if URI is valid.
54 if (cpe_name_check(cpe_uri)) {
55
56 // Write CPE URI to the screen.
57 if (cpe_name_write(name, stdout) > 0)
58 ret_val = 0;
59 else
60 fprintf(stderr,
61 "Unable to write CPE to output!\n");
62 } else
63 fprintf(stderr,
64 "CPE URI is not valid!\n");
65 free(cpe_uri);
66 } else
67 fprintf(stderr, "Unable to get CPE uri!\n");
68 cpe_name_free(name);
69 } else
70 fprintf(stderr, "Unable to create CPE name!\n");
71 }
72 // Parse given CPE URI.
73 else if (argc == 4 && strcmp(argv[1], "--parsing") == 0) {
74
75 // Check if URI is valid.
76 if (cpe_name_check(argv[2])) {
77
78 // Create CPE from URI string.
79 if ((name = cpe_name_new(argv[2])) != NULL) {
80
81 // Extract URI from created CPE.
82 if ((cpe_uri = cpe_name_get_as_str(name)) != NULL) {
83
84 // Compare it to the original URI.
85 if (!strcmp(cpe_uri, argv[2])) {
86
87 // Write CPE to file.
88 if ((f =
89 fopen(argv[3],
90 "w")) == NULL)
91 fprintf(stderr,
92 "Unable to open or create file %s!\n",
93 argv[3]);
94 else
95 cpe_name_write(name, f),
96 fclose(f);
97
98 // Print CPE to the screen.
99 cpe_print(name);
100 ret_val = 0;
101 }
102 cpe_name_free(name);
103 } else
104 fprintf(stderr, "Not enough memory!\n");
105 } else
106 fprintf(stderr, "Parse error!\n");
107 } else
108 fprintf(stderr, "CPE URI is not valid!\n");
109 }
110 // Match candidate CPE URI agains the others.
111 else if (argc >= 4 && strcmp(argv[1], "--matching") == 0) {
112
113 // Load candidate CPE.
114 if ((candidate_cpe = cpe_name_new(argv[2])) != NULL) {
115 if ((cpes =
116 (struct cpe_name **)malloc((argc - 3) *
117 sizeof(struct cpe_name
118 *))) != NULL) {
119 for (i = 0; i < argc - 3; i++) {
120
121 // Load CPE to check match.
122 cpes[i] = cpe_name_new(argv[i + 3]);
123
124 // Match check (against last CPE).
125 match_result_b = match_result_b
126 || cpe_name_match_one(candidate_cpe,
127 cpes[i]);
128 }
129
130 // Report result - all should be the same.
131 if (match_result_b)
132 printf("Match\n"), ret_val = 0;
133 if (!match_result_b)
134 printf("Mismatch\n"), ret_val = 0;
135
136 } else
137 fprintf(stderr, "Not enough memory!\n");
138
139 for (i = 0; i < argc - 3; i++)
140 cpe_name_free(cpes[i]);
141 free(cpes);
142 cpe_name_free(candidate_cpe);
143
144 } else
145 fprintf(stderr, "Parse error!\n");
146 }
147
148 else if (argc == 2 && strcmp(argv[1], "--smoke-test") == 0) {
149 if ((name = cpe_name_new(NULL)) != NULL) {
150 cpe_name_free(name);
151 ret_val = 0;
152 }
153 }
154 // Print usage only, invalid parameters.
155 else
156 print_usage(argv[0], stderr);
157
158 return ret_val;
159 }
160
161 // Print usage.
162 void print_usage(const char *program_name, FILE * out)
163 {
164 fprintf(out,
165 "Usage: \n\n"
166 " %s --help\n"
167 " %s --creation PART VENDOR PRODUCT VERSION UPDATE EDITION LANGUAGE\n"
168 " %s --parsing URI FILE\n"
169 " %s --matching CANDIDATE_URI URI_1 ... URI_n\n"
170 " %s --smoke-test\n",
171 program_name, program_name, program_name, program_name,
172 program_name);
173 }
174
175 // Create a new CPE according to given information.
176 struct cpe_name *cpe_example_creation(int part,
177 const char *vendor,
178 const char *product,
179 const char *version,
180 const char *update,
181 const char *edition,
182 const char *language,
183 const char *sw_edition,
184 const char *target_sw,
185 const char *target_hw,
186 const char *other)
187 {
188 struct cpe_name *cpe = NULL;
189
190 if ((cpe = cpe_name_new(NULL)) != NULL) {
191
192 cpe_name_set_format(cpe, CPE_FORMAT_URI);
193 // Set fields.
194 cpe_name_set_part(cpe, part);
195 cpe_name_set_vendor (cpe, vendor);
196 cpe_name_set_product(cpe, product);
197 cpe_name_set_version(cpe, version);
198 cpe_name_set_update(cpe, update);
199 cpe_name_set_edition(cpe, edition);
200 cpe_name_set_language(cpe, language);
201 cpe_name_set_sw_edition(cpe, sw_edition);
202 cpe_name_set_target_sw(cpe, target_sw);
203 cpe_name_set_target_hw(cpe, target_hw);
204 cpe_name_set_other(cpe, other);
205 }
206
207 return cpe;
208 }
209
210 // Parse CPE URI string and print all contained information.
211 void cpe_print(struct cpe_name *name)
212 {
213 // Print out all contained information.
214 printf(" %d\n", cpe_name_get_part(name));
215 printf(" %s\n", cpe_name_get_vendor(name) ? cpe_name_get_vendor(name) : "");
216 printf(" %s\n", cpe_name_get_product(name) ? cpe_name_get_product(name) : "");
217 printf(" %s\n", cpe_name_get_version(name) ? cpe_name_get_version(name) : "");
218 printf(" %s\n", cpe_name_get_update(name) ? cpe_name_get_update(name) : "");
219 printf(" %s\n", cpe_name_get_edition(name) ? cpe_name_get_edition(name) : "");
220 printf(" %s\n", cpe_name_get_language(name) ? cpe_name_get_language(name) : "");
221 printf(" %s\n", cpe_name_get_sw_edition(name) ? cpe_name_get_sw_edition(name) : "");
222 printf(" %s\n", cpe_name_get_target_sw(name) ? cpe_name_get_target_sw(name) : "");
223 printf(" %s\n", cpe_name_get_target_hw(name) ? cpe_name_get_target_hw(name) : "");
224 printf(" %s\n", cpe_name_get_other(name) ? cpe_name_get_other(name) : "");
225 }