Skip to content

cslinker: do not abort on empty property in scan.ini #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/lib/writer-json-common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,26 @@ std::string sanitizeUTF8(const std::string &str)
return convert_string<char>(str.data(), str.data() + str.size());
}

/// return true if the given string represents a number
bool isNumber(const std::string &str)
{
static auto isDigit = [](unsigned char c){ return std::isdigit(c); };

if (str.empty())
// an empty string cannot be parsed as a number
return false;

return std::all_of(str.begin(), str.end(), isDigit);
}

// TODO: This should not necessary! TScanProps should be able to contain
// any type so that no conversions here are needed.
object jsonSerializeScanProps(const TScanProps &scanProps)
{
static auto isDigit = [](unsigned char c){ return std::isdigit(c); };

object scan;
for (const auto &prop : scanProps) {
const auto &val = prop.second;
if (std::all_of(val.begin(), val.end(), isDigit))
if (isNumber(val))
scan[prop.first] = boost::lexical_cast<int>(val);
else
scan[prop.first] = val;
Expand Down
9 changes: 9 additions & 0 deletions tests/cslinker/0003-ini-parser/runtest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e
set -x

# run cslinker
"${CSLINKER_BIN}" --inifile "${TEST_SRC_DIR}/scan.ini" <() > scan-results.json

# compare the results with the expected results
diff -up "${TEST_SRC_DIR}/scan-results.json" "${PWD}/scan-results.json"
17 changes: 17 additions & 0 deletions tests/cslinker/0003-ini-parser/scan-results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"scan": {
"enabled-plugins": "",
"exit-code": 0,
"host": "f37",
"known-false-positives": "/usr/share/csmock/known-false-positives.js",
"mock-config": "rhel-8-rhaos-x86_64",
"project-name": "openshift-clients-4.7.0-202103251046.p0.git.3957.c4da68b.el8",
"store-results-to": "/tmp/openshift-clients-4.7.0-202103251046.p0.git.3957.c4da68b.el8.tar.xz",
"time-created": "2023-06-19 10:27:13",
"time-finished": "2023-06-19 10:34:00",
"tool": "csmock",
"tool-args": "'/usr/bin/csmock' '-r' 'rhel-8-rhaos-x86_64' '-f' 'openshift-clients-4.7.0-202103251046.p0.git.3957.c4da68b.el8.src.rpm'",
"tool-version": "csmock-3.4.1.20230406.104621.gdb10285.internal-1.fc37"
},
"defects": []
}
13 changes: 13 additions & 0 deletions tests/cslinker/0003-ini-parser/scan.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[scan]
tool = csmock
tool-version = csmock-3.4.1.20230406.104621.gdb10285.internal-1.fc37
tool-args = '/usr/bin/csmock' '-r' 'rhel-8-rhaos-x86_64' '-f' 'openshift-clients-4.7.0-202103251046.p0.git.3957.c4da68b.el8.src.rpm'
host = f37
store-results-to = /tmp/openshift-clients-4.7.0-202103251046.p0.git.3957.c4da68b.el8.tar.xz
time-created = 2023-06-19 10:27:13
enabled-plugins =
mock-config = rhel-8-rhaos-x86_64
project-name = openshift-clients-4.7.0-202103251046.p0.git.3957.c4da68b.el8
known-false-positives = /usr/share/csmock/known-false-positives.js
time-finished = 2023-06-19 10:34:00
exit-code = 0
14 changes: 14 additions & 0 deletions tests/cslinker/0003-ini-parser/sync-expected-output.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -e
set -x

# check whether we are invoked from the correct directory
test -d ../../cslinker/0003-ini-parser/

# find cslinker BINARY
export CSLINKER_BIN=../../../csdiff_build/src/cslinker
test -x $CSLINKER_BIN
CSLINKER_BIN=$(realpath $CSLINKER_BIN)

# run the test in the _source_ directory to overwrite the expected output
TEST_SRC_DIR="$PWD" ./runtest.sh