Skip to content

Commit 493efa9

Browse files
authored
Merge pull request #4916 from hai1337/docs/improve-test-readme
docs: improve test README
2 parents 9d608c4 + 6d23d65 commit 493efa9

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

test/README.md

+19-20
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,37 @@ You can see all existing tests in [`test/`](https://github.com/intel/cve-bin-too
1313

1414
## Running all tests
1515

16-
To run the tests for `cve-bin-tool`
16+
To run all tests for `cve-bin-tool`:
1717

1818
```console
1919
pytest
2020
```
2121

22-
To run scanner and checker tests
22+
To run scanner and checker tests:
2323

2424
```console
2525
pytest test/test_scanner.py test/test_checkers.py
2626
```
2727

28-
By default, some longer-running tests are turned off. If you want to enable them, you can set the environment variable LONG_TESTS to 1. You can do this just for a single command line as follows:
28+
By default, some longer-running tests are turned off. If you want to enable them, you can set the environment variable `LONG_TESTS` to 1. You can do this just in single command as follows:
2929

3030
```console
31-
LONG_TESTS=1 pytest
31+
LONG_TESTS=1 pytest {$TEST_FILE}
3232
```
3333

34-
For scanner tests
34+
For scanner tests:
3535

3636
```console
3737
LONG_TESTS=1 pytest test/test_scanner.py test/test_checkers.py
3838
```
3939

40-
By default, tests which rely on external connectivity are turned off. If you want to enable them, you can set the environment variable EXTERNAL_SYSTEM to 1. You can do this just for a single command line as follows:
40+
By default, tests which rely on external connectivity are turned off. If you want to enable them, you can set the environment variable `EXTERNAL_SYSTEM` to 1. You can do this just in single command as follows:
4141

4242
```console
43-
EXTERNAL_SYSTEM=1 pytest
43+
EXTERNAL_SYSTEM=1 pytest {$TEST_FILE}
4444
```
4545

46-
For nvd tests
46+
For NVD tests
4747

4848
```console
4949
EXTERNAL_SYSTEM=1 pytest test/test_source_nvd.py
@@ -52,14 +52,13 @@ EXTERNAL_SYSTEM=1 pytest test/test_source_nvd.py
5252
## Running a single test
5353

5454
To run a single test, you can use the unittest framework. For example, here's how to run
55-
the test for sqlite:
55+
the test for `sqlite`:
5656

5757
```console
5858
python -m unittest test.test_scanner.TestScanner.test_sqlite_3_12_2
5959
```
6060

61-
To run a single test in test_scanner you can use pytest. For example, here's how to run
62-
the test for vendor_package_pairs:
61+
To run a single test in test_scanner you can use the pytest framework. For example, here's how to run the test for `vendor_package_pairs`:
6362

6463
```console
6564
pytest test/test_scanner.py::TestScanner::test_version_mapping
@@ -92,7 +91,7 @@ deactivate
9291

9392
- You can see the code for scanner tests in ['test/test_scanner.py'](https://github.com/intel/cve-bin-tool/blob/main/test/test_scanner.py)
9493
- You can see checker wise test data in ['test/test_data'](https://github.com/intel/cve-bin-tool/blob/main/test/test_data)
95-
- If you just want to add a new mapping test for a checker, add a dictionary of _product_, _version_ and _version_strings_ in the mapping_test_data list . Here, _version_strings_ are the list of strings that contain version signature or strings that commonly can be found in the module. For example: this is how the current mapping_test_data for gnutls look like. You should add the details of the new test case data at the end of `mapping_test_data` list:
94+
- If you just want to add a new mapping test for a checker, add a dictionary of _product_, _version_ and _version_strings_ in the mapping_test_data list. Here, _version_strings_ are the list of strings that contain version signature or strings that commonly can be found in the module. For example: this is how the current mapping_test_data for gnutls look like. You should add the details of the new test case data at the end of `mapping_test_data` list:
9695

9796
```python
9897
mapping_test_data = [
@@ -109,17 +108,17 @@ mapping_test_data = [
109108
]
110109
```
111110

112-
- Please note that sometimes the database we're using doesn't have perfect mapping between CVEs and product versions -- if you try to write a test that doesn't work because of that mapping but the description in the CVE says that version should be vulnerable, don't discard it! Instead, please make a note of it in a github issue can investigate and maybe report it upstream.
111+
> Note: sometimes the database we're using doesn't have perfect mapping between CVEs and product versions -- if you try to write a test that doesn't work because of that mapping but the description in the CVE says that version should be vulnerable, don't discard it! Instead, please make a note of it in a GitHub issue so it can be investigated and possibly reported upstream.
113112
114113
## Adding new tests: Signature tests against real files
115114

116115
To make the basic test suite run quickly, we create "faked" binary files to test the **CVE mappings**. However, we want to be able to test real files to test that the **signatures** work on real-world data.
117116

118117
You can see test data for package tests in _package_test_data_ variable of the test data file you are writing test for.
119118

120-
We have `test_version_in_package` function in `test_scanner` that takes a _url_, and _package name_, _module name_ and a _version_, and downloads the package, runs the scanner against it, and makes sure it is the package that you've specified. But we need more tests!
119+
We have a `test_version_in_package` function in `test_scanner` that takes in package details (_url_, _package name_, _module name_, _version_), downloads the package, runs the scanner against it, and confirms it is the package that you've specified. But we need more tests!
121120

122-
- To add a new test, find an appropriate publicly available file (linux distribution packages and public releases of the packages itself are ideal). You should add the details of the new test case in the `package_test_data` variable of the file for which you are writing test for. For example: this is how the current package_test_data for binutils look like. You should add the details of the new test case data at the end of `package_test_data` list:
121+
- To add a new test, find an appropriate publicly available file (linux distribution packages and public releases of the packages itself are ideal). You should add the details of the new test case in the `package_test_data` variable of the file for which you are writing test for. For example: this is how the current `package_test_data` for binutils look like. You should add the details of the new test case data at the end of `package_test_data` list:
123122

124123
```python
125124
package_test_data = [
@@ -140,15 +139,15 @@ package_test_data = [
140139
]
141140

142141
```
143-
The ```other_products``` attribute might match any binaries provided, so we can check that only the expected products are found in a given binary. (e.g. if an imaginary package called CryptographyExtensions included OpenSSL, we'd expect to detect both in CryptographyExtensions-1.2.rpm).
142+
The ```other_products``` attribute might match any binaries provided, so we can check that only the expected products are found in a given binary. (e.g. if an imaginary package called `CryptographyExtensions` included OpenSSL, we'd expect to detect both in `CryptographyExtensions-1.2.rpm`).
144143

145-
Ideally, we should have at least one such test for each checker, and it would be nice to have some different sources for each as well. For example, for packages available in common Linux distributions, we might want to have one from fedora, one from debian, and one direct from upstream to show that we detect all those versions.
144+
Ideally, we should have at least one such test for each checker, and it would be nice to have some different sources for each as well. For example, for packages available in common Linux distributions, we might want to have one from Fedora, one from Debian, and one direct from upstream to show that we detect all of those versions.
146145

147-
Note that we're getting the LONG_TESTS() from tests.util in the top of the files where it's being used. If you're adding a long test to a test file that previously didn't have any, you'll need to add that at the top of the file as well.
146+
> Note: we're getting the `LONG_TESTS()` from tests.util in the top of the files where it's being used. If you're adding a long test to a test file that previously didn't have any, you'll need to add that at the top of the file as well.
148147
149148
## Adding new tests: Checker filename mappings
150149

151-
To test the filename mappings, rather than making a bunch of empty files, we're calling the checkers directly in `test/test_checkers.py`. You can add a new test by specifying a the name of the checker you want to test, the file name, and the expected result that the scanner should say it "is".
150+
To test the filename mappings, rather than making a bunch of empty files, we're calling the checkers directly in `test/test_checkers.py`. You can add a new test by specifying the name of the checker you want to test, the file name, and the expected result that the scanner should say it "is".
152151

153152
```python
154153
@pytest.mark.parametrize(
@@ -160,7 +159,7 @@ To test the filename mappings, rather than making a bunch of empty files, we're
160159
)
161160
```
162161

163-
The function test_filename_is will then load the checker you have specified (and fail spectacularly if you specify a checker that does not exist), try to run get_versions() with an empty file content and the filename you specified, then check that it "is" something (as opposed to "contains") and that the modulename that get_version returns is in fact the expected_result you specified.
162+
The function `test_filename_is` will then load the checker you have specified (and fail spectacularly if you specify a checker that does not exist), try to run `get_version()` with an empty file content and the filename you specified, then check that it "is" something (as opposed to "contains") and that the modulename that `get_version` returns is in fact the `expected_result` you specified.
164163

165164
For ease of maintenance, please keep the parametrize list in alphabetical order when you add a new tests.
166165

0 commit comments

Comments
 (0)