:bug: BUG found in the Security-Policy unit test & more testcases are needed
Describe the bug
When a single not-a-security-policy file is added as a testcase to the Security-Policy unit test (https://github.com/ossf/scorecard/blob/main/checks/security_policy_test.go), it reports a GitHub token env var not set error, then triggers a nil pointer deference error, and the test fails.
Reproduction steps Steps to reproduce the behavior:
- Make sure your GitHub token is correctly set (https://github.com/ossf/scorecard#authentication);
- Pull the latest main branch
git clone https://github.com/ossf/scorecard.gitand then go to filescorecard/checks/security_policy_test.go; - Directly run the Security-Policy test, and it should be okay;
- Add a testcase for failure testing (security policy not detected) such as:
{
name: "Fail Case: not a security policy file",
files: []string {
"notASecurityPolicyFile.go",
},
want: scut.TestReturn{
Score: 0,
NumberOfInfo: 0,
},
},
- Run the test again, it fails and reports
GitHub token env var is not setat first, then comes topanic: runtime error: invalid memory address or nil pointer dereference
Expected behavior
The expected behavior is it would give a score of 0 to this testcase since func isSecurityPolicyFilename (https://github.com/ossf/scorecard/blob/main/checks/raw/security_policy.go#L89) should return false because it's not a match in the security policy files list, then, it should still pass all the tests.
Additional context
- (Not sure) The BUG here might relate to the
mockRepoat https://github.com/ossf/scorecard/blob/main/checks/security_policy_test.go#L123-L132. I'll look into it later. - Since the
filesis a type of[]string, maybe we could put some test cases into onefilesrather than onefilenameperfiles- this might make the unit test cleaner. - When multiple filenames, including security-policy and non-security-policy ones, are in var
files, it passes the unit test. - Extra testcases for failure testing (score of zero) are needed for this part.
I can take this issue. I will add a regression case for a non-security-policy file, make the test fixture fail closed without requiring a GitHub token, and run the focused Security-Policy test suite before opening a PR.