Skip to content

Commit ca1de05

Browse files
authored
test: add test for null byte in filename (#1635)
* fix: add special case for 3.7 ValueError
1 parent 9e32cd7 commit ca1de05

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

test/test_cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import logging
88
import os
9+
import sys
910
import tempfile
1011
import unittest
1112
from test.utils import (
@@ -97,6 +98,33 @@ def test_invalid_file_or_directory(self):
9798
main(["cve-bin-tool", "non-existant"])
9899
assert e.value.args[0] == ERROR_CODES[FileNotFoundError]
99100

101+
def test_null_byte_in_filename(self):
102+
"""Test behaviour with an invalid file/directory that contains a \0"""
103+
104+
# Put a null byte into the filename of a real file used in other tests
105+
CSV_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "csv")
106+
null_byte_file = os.path.join(CSV_PATH, "test_triage\0.csv")
107+
108+
# for Python 3.8+ this should raise FileNotFound
109+
if sys.version_info.major == 3 and sys.version_info.minor > 7:
110+
with pytest.raises(SystemExit) as e:
111+
main(["cve-bin-tool", null_byte_file])
112+
assert e.value.args[0] == ERROR_CODES[FileNotFoundError]
113+
114+
null_byte_file = os.path.join(CSV_PATH, "test_triage.csv\0something")
115+
with pytest.raises(SystemExit) as e:
116+
main(["cve-bin-tool", null_byte_file])
117+
assert e.value.args[0] == ERROR_CODES[FileNotFoundError]
118+
119+
# for Python 3.7 it will raise a ValueError (embedded null byte)
120+
if sys.version_info.major == 3 and sys.version_info.minor == 7:
121+
with pytest.raises(ValueError) as e:
122+
main(["cve-bin-tool", null_byte_file])
123+
124+
null_byte_file = os.path.join(CSV_PATH, "test_triage.csv\0something")
125+
with pytest.raises(ValueError) as e:
126+
main(["cve-bin-tool", null_byte_file])
127+
100128
def test_invalid_parameter(self):
101129
"""Test that invalid parmeters exit with expected error code.
102130
ArgParse calls sys.exit(2) for all errors"""

0 commit comments

Comments
 (0)