Debian Codegraph @

tests/API/CPE/dict/test_api_cpe_dict.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # Copyright 2009 Red Hat Inc., Durham, North Carolina.
4 # All Rights Reserved.
5 #
6 # OpenScap CPE Dict Module Test Suite.
7 #
8 # Authors:
9 # Ondrej Moris <[email protected]>
10
11 . $builddir/tests/test_common.sh
12
13 # Test cases.
14
15 function test_api_cpe_dict_smoke {
16 ./test_api_cpe_dict --smoke-test
17 }
18
19 function test_api_cpe_dict_remove_cpe {
20 require "grep" || return 255
21 ./test_api_cpe_dict --remove $srcdir/dict.xml "UTF-8" "cpe:/a:addsoft" | \
22 grep -s "addsoft"
23 return $([ $? -eq 1 ])
24 }
25
26 function test_api_cpe_dict_import_damaged_xml {
27 ./test_api_cpe_dict --list-cpe-names $srcdir/dict-damaged.xml "UTF-8"
28 return $([ $? -eq 2 ])
29 }
30
31 function test_api_cpe_dict_match_non_existing_cpe {
32 ./test_api_cpe_dict --match $srcdir/dict.xml \
33 "UTF-8" "cpe:/a:3com:3c16115-usNOT_IN_THE_DICTIONARY"
34 return $([ $? -eq 1 ])
35 }
36
37 function test_api_cpe_dict_match_existing_cpe {
38 require "grep" || return 255
39 CPE_URIS=(`grep "cpe:" $srcdir/dict.xml | \
40 sed 's/^.*cpe:/cpe:/g' | sed 's/".*$//g' | tr '\n' ' '`)
41 for URI in ${CPE_URIS[@]}; do
42 ./test_api_cpe_dict --match $srcdir/dict.xml "UTF-8" "$URI"
43 [ ! $? -eq 0 ] && return 1
44 done
45 return 0
46 }
47
48 function test_api_cpe_dict_export_xml {
49 ./test_api_cpe_dict --export $srcdir/dict.xml "UTF-8" \
50 dict.xml.out "UTF-8" && \
51 $XMLDIFF $srcdir/dict.xml dict.xml.out
52 }
53
54 function test_api_cpe_dict_import_cp1250_xml {
55 ./test_api_cpe_dict --list-cpe-names $srcdir/dict-cp1250-dos.xml "CP-1250"
56 }
57
58 function test_api_cpe_dict_import_utf8_xml {
59 ./test_api_cpe_dict --list-cpe-names $srcdir/dict.xml "UTF8"
60 }
61
62 function test_api_cpe_dict_import_official_v22(){
63 set -e -o pipefail
64 local name="official-cpe-dictionary_v2.2.xml"
65 local dict="$srcdir/$name"
66 local out=$(mktemp -t $name.out.XXXXXX)
67
68 $OSCAP info $dict 2> $out
69 [ ! -s $out ]
70
71 $OSCAP cpe validate $dict 2>&1 > $out
72 [ ! -s $out ]
73
74 ./test_api_cpe_dict --list $dict "UTF-8" > $out
75 grep 'National Vulnerability Database (NVD)' $out
76 [ "`cat $out | wc -l | xargs`" == "1" ]
77
78 ./test_api_cpe_dict --list-cpe-names $dict "UTF-8" > $out
79 grep 'cpe:/a:acronis:backup_%26_recovery_agent:10.0.11639' $out
80 [ "`cat $out | wc -l | xargs`" == "8" ]
81
82 rm -f dict.xml.out
83 ./test_api_cpe_dict --export $dict "UTF-8" dict.xml.out "UTF-8" 2>&1 > $out
84 [ ! -s $out ]
85 $XMLDIFF $dict dict.xml.out
86 rm $out
87 }
88
89 function test_api_cpe_dict_import_official_v23(){
90 set -e -o pipefail
91 local name="official-cpe-dictionary_v2.3.xml"
92 local dict="$srcdir/$name"
93 local out=$(mktemp -t $name.out.XXXXXX)
94
95 $OSCAP info $dict 2> $out
96 [ ! -s $out ]
97
98 $OSCAP cpe validate $dict 2>&1 > $out
99 [ ! -s $out ]
100
101 ./test_api_cpe_dict --list $dict "UTF-8" > $out
102 grep 'National Vulnerability Database (NVD)' $out
103 [ "`cat $out | wc -l | xargs`" == "1" ]
104
105 ./test_api_cpe_dict --list-cpe-names $dict "UTF-8" > $out
106 grep 'cpe:/h:hp:advancestack_10base-t_switching_hub_j3200a:a.03.07' $out
107 [ "`cat $out | wc -l | xargs`" == "7" ]
108
109 rm -f dict.xml.out
110 ./test_api_cpe_dict --export $dict "UTF-8" dict.xml.out "UTF-8" 2>&1 > $out
111 [ ! -s $out ]
112 $XMLDIFF $dict dict.xml.out
113 rm $out
114 }
115
116 # Testing.
117
118 test_init
119
120 if [ -z ${CUSTOM_OSCAP+x} ] ; then
121 test_run "test_api_cpe_dict_smoke" test_api_cpe_dict_smoke
122 test_run "test_api_cpe_dict_remove_cpe" test_api_cpe_dict_remove_cpe
123 test_run "test_api_cpe_dict_import_damaged_xml" \
124 test_api_cpe_dict_import_damaged_xml
125 test_run "test_api_cpe_dict_match_non_existing_cpe" \
126 test_api_cpe_dict_match_non_existing_cpe
127 test_run "test_api_cpe_dict_match_existing_cpe" \
128 test_api_cpe_dict_match_existing_cpe
129 test_run "test_api_cpe_dict_export_xml" test_api_cpe_dict_export_xml
130 #test_run "test_api_cpe_dict_import_cp1250_xml" \
131 # test_api_cpe_dict_import_cp1250_xml
132 test_run "test_api_cpe_dict_import_utf8_xml" test_api_cpe_dict_import_utf8_xml
133 test_run "test_api_cpe_dict_import_official_v22" test_api_cpe_dict_import_official_v22
134 test_run "test_api_cpe_dict_import_official_v23" test_api_cpe_dict_import_official_v23
135 fi
136
137 test_exit