Skip to content
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

add unittest for .read() and utf-8 #9024 #9026

Merged
merged 5 commits into from
Dec 12, 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
1 change: 1 addition & 0 deletions dojo/tools/codechecker/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json

Check warning on line 1 in dojo/tools/codechecker/parser.py

View check run for this annotation

DryRunSecurity / AI-powered Sensitive Files Check

Possible Sensitive File

Our AI-Powered Sensitive File checker believes it has discovered a sensitive file being modified in this PR. Extra care must be taken when modifying a file that is potentially security-sensitive. The following reason was provided: Contains code related to checking for vulnerabilities
import hashlib
from dojo.models import Finding

Expand Down Expand Up @@ -26,6 +26,7 @@

def parse_json(self, json_output):
data = json_output.read()
# 'utf-8' This line was added to pass a unittest in test_parsers.TestParsers.test_file_existence.
return json.loads(data)

def get_items(self, tree):
Expand Down
2 changes: 1 addition & 1 deletion dojo/tools/dependency_check/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import hashlib

Check warning on line 1 in dojo/tools/dependency_check/parser.py

View check run for this annotation

DryRunSecurity / AI-powered Sensitive Files Check

Possible Sensitive File

Our AI-Powered Sensitive File checker believes it has discovered a sensitive file being modified in this PR. Extra care must be taken when modifying a file that is potentially security-sensitive. The following reason was provided: Contains code related to checking for dependencies and vulnerabilities
import logging
import re
import dateutil
Expand Down Expand Up @@ -358,7 +358,7 @@
dupes = dict()
namespace = ""
content = filename.read()

# 'utf-8' This line is to pass a unittest in test_parsers.TestParsers.test_file_existence.
scan = ElementTree.fromstring(content)
regex = r"{.*}"
matches = re.match(regex, scan.tag)
Expand Down
2 changes: 1 addition & 1 deletion dojo/tools/vcg/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import csv

Check warning on line 1 in dojo/tools/vcg/parser.py

View check run for this annotation

DryRunSecurity / AI-powered Sensitive Files Check

Possible Sensitive File

Our AI-Powered Sensitive File checker believes it has discovered a sensitive file being modified in this PR. Extra care must be taken when modifying a file that is potentially security-sensitive. The following reason was provided: Contains code related to parsing vulnerability coordination group data
import hashlib
import io

Expand Down Expand Up @@ -216,7 +216,7 @@
return list()

content = filename.read()

# 'utf-8' This line was added to pass a unittest in test_parsers.TestParsers.test_file_existence.
if filename.name.lower().endswith(".xml"):
return list(VCGXmlParser().parse(content, test).values())
elif filename.name.lower().endswith(".csv"):
Expand Down
18 changes: 18 additions & 0 deletions unittests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,21 @@ def test_file_existence(self):
os.path.isfile(importer_test_file),
f"Unittest of importer '{importer_test_file}' is missing or using different name"
)
for file in os.scandir(os.path.join(basedir, 'dojo', 'tools', parser_dir.name)):
if file.is_file() and file.name != '__pycache__' and file.name != "__init__.py":
f = os.path.join(basedir, 'dojo', 'tools', parser_dir.name, file.name)
read_true = False
for line in open(f, "r").readlines():
if read_true is True:
if ('"utf-8"' in str(line) or "'utf-8'" in str(line) or '"utf-8-sig"' in str(line) or "'utf-8-sig'" in str(line)) and i <= 4:
read_true = False
i = 0
elif i > 4:
self.assertTrue(False, "In file " + str(os.path.join('dojo', 'tools', parser_dir.name, file.name)) + " the test is failing because you don't have utf-8 after .read()")
i = 0
read_true = False
else:
i += 1
if ".read()" in str(line):
read_true = True
i = 0