Skip to content

Commit

Permalink
CPE regex
Browse files Browse the repository at this point in the history
- addresses parts of oasis-tcs#693
- add new local test cases
- adopt test script
  • Loading branch information
tschmidtb51 committed Mar 27, 2024
1 parent 61e78c8 commit 1cfd26f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/csaf_2.1_cpe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ jobs:
with:
node-version: '20'
- name: Perform CPE Dictionary Test
run: ./csaf_2.1/test/cpe/run_tests.sh
run: ./csaf_2.1/test/cpe/run_dictionary_tests.sh
- name: Perform CPE local examples Test
run: ./csaf_2.1/test/cpe/run_local_tests.sh
5 changes: 5 additions & 0 deletions csaf_2.1/test/cpe/data/invalid/cpe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PREFIXcpe:/o:redhat:rhel_aus:7.6::server
cpe:/o:redhat:rhel_aus:7.6::server::SUFFIX
PREFIXcpe:2.3:a:admin_management_xtended_project:admin_management_xtended:0.8:*:*:*:*:wordpress:*:*
cpe:2.3:a:admin_management_xtended_project:admin_management_xtended:0.8:*:*:*:*:wordpress:*:*"
cpe:2.3:a:admin_management_xtended_project:admin_management_xtended:0.8:*:*:*:*:wordpress:*:**
3 changes: 3 additions & 0 deletions csaf_2.1/test/cpe/data/valid/cpe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cpe:2.3:a:admin_management_xtended_project:admin_management_xtended:0.8:*:*:*:*:wordpress:*:*other*
cpe:2.3:a:admin_management_xtended_project:admin_management_xtended:0.8:*:*:*:*:wordpress:*:*other????
cpe:/o:redhat:rhel_aus:7.6::server
File renamed without changes.
36 changes: 36 additions & 0 deletions csaf_2.1/test/cpe/run_local_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

SCHEMA=csaf_2.1/json_schema/csaf_json_schema.json
VALIDATOR=csaf_2.1/test/cpe/test-regex.js
DATA_VALID=csaf_2.1/test/cpe/data/valid/cpe.txt
DATA_INVALID=csaf_2.1/test/cpe/data/invalid/cpe.txt

FAIL=0

# go to root of git repository
cd "$(dirname "$0")"/../../.. || exit


validate() {
printf "Testing file %s against cpe regex from %s ... \n" "$1" "$SCHEMA"
if node "$VALIDATOR" "$SCHEMA" "$1" "$2"; then
printf "SUCCESS\n"
else
printf "FAILED\n"
FAIL=1
fi

}

echo -n "Test conforming (not necessary existing) CPEs... "
DATA=$DATA_VALID
validate $DATA true
printf "done\n"

echo -n "Test non-conforming CPEs... "
DATA=$DATA_INVALID
validate $DATA false
printf "done\n"


exit $FAIL
7 changes: 4 additions & 3 deletions csaf_2.1/test/cpe/test-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ const r = new RegExp(pattern)
console.log('Current regex to test:', '\n', pattern)

const cpeStr = fs.readFileSync(args[1], 'utf8').split('\n')
const assertion = !((args[2] ?? true) === "false")

let failed = false

cpeStr.forEach(element => {
if (element.length > 0) {
const result = (r.exec(element) != null)
failed = failed | !result
if (!result) {
console.log(result, '\t', element)
failed = failed | (result !== assertion)
if (result !== assertion) {
console.log(result,'but expected', assertion, '\t', element)
}
}
});
Expand Down

0 comments on commit 1cfd26f

Please sign in to comment.