Skip to content

Commit 5bef089

Browse files
committed
Improve test and fix revealed bugs
Adds tests that test explicit typecasts errors, and tests that quoted strings are always treated as strings. When using alternatives in regular expression '^' and '$' are part of the only alternative, therefore groups have to be used to have these anchors in all alternatives.
1 parent a5dfd5f commit 5bef089

File tree

4 files changed

+169
-23
lines changed

4 files changed

+169
-23
lines changed

src/OVAL/probes/independent/yamlfilecontent_probe.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ static SEXP_t *yaml_scalar_event_to_sexp(yaml_event_t event)
9494
/* Regular expressions based on https://yaml.org/spec/1.2/spec.html#id2804923 */
9595

9696
if (question || !strcmp(tag, OSCAP_YAML_BOOL_TAG)) {
97-
if (match_regex("^true|True|TRUE$", value)) {
97+
if (match_regex("^(true|True|TRUE)$", value)) {
9898
return SEXP_number_newb(true);
99-
} else if (match_regex("^false|False|FALSE$", value)) {
99+
} else if (match_regex("^(false|False|FALSE)$", value)) {
100100
return SEXP_number_newb(false);
101101
} else if (!question) {
102102
return NULL;
@@ -127,7 +127,7 @@ static SEXP_t *yaml_scalar_event_to_sexp(yaml_event_t event)
127127
double_value = -INFINITY;
128128
}
129129
return SEXP_number_newf(double_value);
130-
} else if (match_regex("^\\.nan|\\.NaN|\\.NAN$", value)) {
130+
} else if (match_regex("^(\\.nan|\\.NaN|\\.NAN)$", value)) {
131131
double double_value = NAN;
132132
return SEXP_number_newf(double_value);
133133
} else if (!question) {
@@ -219,7 +219,8 @@ static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, str
219219
SEXP_t *sexp = yaml_scalar_event_to_sexp(event);
220220
if (sexp == NULL) {
221221
SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
222-
"Can't convert '%s' to SEXP", event.data.scalar.tag);
222+
"Can't convert '%s %s' to SEXP", event.data.scalar.tag,
223+
event.data.scalar.value);
223224
probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
224225
SEXP_free(msg);
225226
probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);

tests/probes/yamlfilecontent/test_probes_yamlfilecontent_types.sh

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,49 @@ function test_probes_yamlfilecontent_types {
2222

2323
[ -f $result ]
2424

25-
sd='/oval_results/results/system/oval_system_characteristics/system_data/'
25+
sd='/oval_results/results/system/oval_system_characteristics/system_data'
26+
2627
assert_exists 8 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="boolean"]'
28+
assert_exists 4 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="boolean" and text()="true"]'
29+
assert_exists 4 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="boolean" and text()="false"]'
30+
2731
assert_exists 5 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="int"]'
32+
# int_10: 42
33+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="int" and text()="42"]'
34+
# int_10_neg: -17
35+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="int" and text()="-17"]'
36+
# int_8: 0o33
37+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="int" and text()="27"]'
38+
# int_16: 0xFF
39+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="int" and text()="255"]'
40+
# int_cast: !!int "369"
41+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="int" and text()="369"]'
42+
2843
assert_exists 7 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="float"]'
44+
# float: 7.4
45+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="float" and text()="7.400000"]'
46+
# float_neg: -0.3
47+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="float" and text()="-0.300000"]'
48+
# float_exp: +12e03
49+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="float" and text()="12000.000000"]'
50+
# float_exp_neg: -43e-4
51+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="float" and text()="-0.004300"]'
52+
# float: .inf
53+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="float" and text()="inf"]'
54+
# float: .NAN
55+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="float" and text()="nan"]'
56+
# float_cast: !!float "978.65"
57+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype="float" and text()="978.650000"]'
58+
59+
# string_true
60+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype!="boolean" and text()="true"]'
61+
# string_number
62+
assert_exists 1 $sd'/ind-sys:yamlfilecontent_item/ind-sys:value_of[@datatype!="int" and text()="81"]'
63+
64+
# bool_error_cast, int_error_cast, float_error_cast
65+
co='/oval_results/results/system/oval_system_characteristics/collected_objects'
66+
assert_exists 3 $co'/object[@flag="error"]'
67+
assert_exists 3 $co'/object[@flag="error"]/message'
2968

3069
rm -f $result
3170
rm -f $YAML_FILE

tests/probes/yamlfilecontent/test_probes_yamlfilecontent_types.xml

Lines changed: 119 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,58 @@
212212
</criteria>
213213
</definition>
214214

215+
<definition class="compliance" version="1" id="oval:0:def:21">
216+
<metadata>
217+
<title></title>
218+
<description></description>
219+
</metadata>
220+
<criteria operator="AND">
221+
<criterion comment="comment" test_ref="oval:0:tst:21"/>
222+
</criteria>
223+
</definition>
224+
225+
<definition class="compliance" version="1" id="oval:0:def:22">
226+
<metadata>
227+
<title></title>
228+
<description></description>
229+
</metadata>
230+
<criteria operator="AND">
231+
<criterion comment="comment" test_ref="oval:0:tst:22"/>
232+
</criteria>
233+
</definition>
234+
235+
<definition class="compliance" version="1" id="oval:0:def:23">
236+
<metadata>
237+
<title></title>
238+
<description></description>
239+
</metadata>
240+
<criteria operator="AND">
241+
<criterion comment="comment" test_ref="oval:0:tst:23"/>
242+
</criteria>
243+
</definition>
244+
245+
<definition class="compliance" version="1" id="oval:0:def:24">
246+
<metadata>
247+
<title></title>
248+
<description></description>
249+
</metadata>
250+
<criteria operator="AND">
251+
<criterion comment="comment" test_ref="oval:0:tst:24"/>
252+
</criteria>
253+
</definition>
254+
255+
<definition class="compliance" version="1" id="oval:0:def:25">
256+
<metadata>
257+
<title></title>
258+
<description></description>
259+
</metadata>
260+
<criteria operator="AND">
261+
<criterion comment="comment" test_ref="oval:0:tst:25"/>
262+
</criteria>
263+
</definition>
264+
215265
</definitions>
266+
216267
<tests>
217268

218269
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:1" check="all" comment="true">
@@ -223,76 +274,96 @@
223274
<ind-def:object object_ref="oval:0:obj:2"/>
224275
</ind-def:yamlfilecontent_test>
225276

226-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:3" check="all" comment="error">
277+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:3" check="all" comment="true">
227278
<ind-def:object object_ref="oval:0:obj:3"/>
228279
</ind-def:yamlfilecontent_test>
229280

230-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:4" check="all" comment="false">
281+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:4" check="all" comment="true">
231282
<ind-def:object object_ref="oval:0:obj:4"/>
232283
</ind-def:yamlfilecontent_test>
233-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:5" check="all" comment="false">
284+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:5" check="all" comment="true">
234285
<ind-def:object object_ref="oval:0:obj:5"/>
235286
</ind-def:yamlfilecontent_test>
236-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:6" check="all" comment="false">
287+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:6" check="all" comment="true">
237288
<ind-def:object object_ref="oval:0:obj:6"/>
238289
</ind-def:yamlfilecontent_test>
239290

240-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:7" check="all" comment="false">
291+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:7" check="all" comment="true">
241292
<ind-def:object object_ref="oval:0:obj:7"/>
242293
</ind-def:yamlfilecontent_test>
243294

244-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:8" check="all" comment="false">
295+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:8" check="all" comment="true">
245296
<ind-def:object object_ref="oval:0:obj:8"/>
246297
</ind-def:yamlfilecontent_test>
247298

248-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:9" check="all" comment="false">
299+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:9" check="all" comment="true">
249300
<ind-def:object object_ref="oval:0:obj:9"/>
250301
</ind-def:yamlfilecontent_test>
251302

252-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:10" check="all" comment="false">
303+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:10" check="all" comment="true">
253304
<ind-def:object object_ref="oval:0:obj:10"/>
254305
</ind-def:yamlfilecontent_test>
255306

256-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:11" check="all" comment="false">
307+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:11" check="all" comment="true">
257308
<ind-def:object object_ref="oval:0:obj:11"/>
258309
</ind-def:yamlfilecontent_test>
259310

260-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:12" check="all" comment="false">
311+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:12" check="all" comment="true">
261312
<ind-def:object object_ref="oval:0:obj:12"/>
262313
</ind-def:yamlfilecontent_test>
263314

264-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:13" check="all" comment="false">
315+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:13" check="all" comment="true">
265316
<ind-def:object object_ref="oval:0:obj:13"/>
266317
</ind-def:yamlfilecontent_test>
267318

268-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:14" check="all" comment="false">
319+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:14" check="all" comment="true">
269320
<ind-def:object object_ref="oval:0:obj:14"/>
270321
</ind-def:yamlfilecontent_test>
271322

272-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:15" check="all" comment="false">
323+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:15" check="all" comment="true">
273324
<ind-def:object object_ref="oval:0:obj:15"/>
274325
</ind-def:yamlfilecontent_test>
275326

276-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:16" check="all" comment="false">
327+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:16" check="all" comment="true">
277328
<ind-def:object object_ref="oval:0:obj:16"/>
278329
</ind-def:yamlfilecontent_test>
279330

280-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:17" check="all" comment="false">
331+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:17" check="all" comment="true">
281332
<ind-def:object object_ref="oval:0:obj:17"/>
282333
</ind-def:yamlfilecontent_test>
283334

284-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:18" check="all" comment="false">
335+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:18" check="all" comment="true">
285336
<ind-def:object object_ref="oval:0:obj:18"/>
286337
</ind-def:yamlfilecontent_test>
287338

288-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:19" check="all" comment="false">
339+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:19" check="all" comment="true">
289340
<ind-def:object object_ref="oval:0:obj:19"/>
290341
</ind-def:yamlfilecontent_test>
291342

292-
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:20" check="all" comment="false">
343+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:20" check="all" comment="true">
293344
<ind-def:object object_ref="oval:0:obj:20"/>
294345
</ind-def:yamlfilecontent_test>
295346

347+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:21" check="all" comment="true">
348+
<ind-def:object object_ref="oval:0:obj:21"/>
349+
</ind-def:yamlfilecontent_test>
350+
351+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:22" check="all" comment="true">
352+
<ind-def:object object_ref="oval:0:obj:22"/>
353+
</ind-def:yamlfilecontent_test>
354+
355+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:23" check="all" comment="error">
356+
<ind-def:object object_ref="oval:0:obj:23"/>
357+
</ind-def:yamlfilecontent_test>
358+
359+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:24" check="all" comment="error">
360+
<ind-def:object object_ref="oval:0:obj:24"/>
361+
</ind-def:yamlfilecontent_test>
362+
363+
<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:25" check="all" comment="error">
364+
<ind-def:object object_ref="oval:0:obj:25"/>
365+
</ind-def:yamlfilecontent_test>
366+
296367
</tests>
297368

298369
<objects>
@@ -416,6 +487,36 @@
416487
<ind-def:filename>types.yaml</ind-def:filename>
417488
<ind-def:yamlpath>.float_cast</ind-def:yamlpath>
418489
</ind-def:yamlfilecontent_object>
490+
491+
<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:21">
492+
<ind-def:path>/tmp</ind-def:path>
493+
<ind-def:filename>types.yaml</ind-def:filename>
494+
<ind-def:yamlpath>.string_true</ind-def:yamlpath>
495+
</ind-def:yamlfilecontent_object>
496+
497+
<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:22">
498+
<ind-def:path>/tmp</ind-def:path>
499+
<ind-def:filename>types.yaml</ind-def:filename>
500+
<ind-def:yamlpath>.string_number</ind-def:yamlpath>
501+
</ind-def:yamlfilecontent_object>
502+
503+
<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:23">
504+
<ind-def:path>/tmp</ind-def:path>
505+
<ind-def:filename>types.yaml</ind-def:filename>
506+
<ind-def:yamlpath>.bool_error_cast</ind-def:yamlpath>
507+
</ind-def:yamlfilecontent_object>
508+
509+
<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:24">
510+
<ind-def:path>/tmp</ind-def:path>
511+
<ind-def:filename>types.yaml</ind-def:filename>
512+
<ind-def:yamlpath>.int_error_cast</ind-def:yamlpath>
513+
</ind-def:yamlfilecontent_object>
514+
515+
<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:25">
516+
<ind-def:path>/tmp</ind-def:path>
517+
<ind-def:filename>types.yaml</ind-def:filename>
518+
<ind-def:yamlpath>.float_error_cast</ind-def:yamlpath>
519+
</ind-def:yamlfilecontent_object>
419520
</objects>
420521

421522
</oval_definitions>

tests/probes/yamlfilecontent/types.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ bool_true_cast: !!bool "true"
1818
bool_false_cast: !!bool "false"
1919
int_cast: !!int "369"
2020
float_cast: !!float "978.65"
21+
string_true: "true"
22+
string_number: "81"
23+
bool_error_cast: !!bool "falsee"
24+
int_error_cast: !!int "50%"
25+
float_error_cast: !!float "58.41$"

0 commit comments

Comments
 (0)