Skip to content

Commit

Permalink
Add additional tests for git scanning, and markdown format (#569)
Browse files Browse the repository at this point in the history
Added additional tests and improves test coverage >80%.

- Tests for git scanning
- Tests for markdown format
- Tests for rust building
  • Loading branch information
another-rex committed Oct 6, 2023
1 parent c509779 commit 4d86a90
Show file tree
Hide file tree
Showing 22 changed files with 165 additions and 7 deletions.
31 changes: 24 additions & 7 deletions cmd/osv-scanner/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func TestRun(t *testing.T) {
`,
wantStderr: "",
},
// one specific supported sbom with vulns
// folder of supported sbom with vulns
{
name: "",
name: "folder of supported sbom with vulns",
args: []string{"", "--config=./fixtures/osv-scanner-empty-config.toml", "./fixtures/sbom-insecure/"},
wantExitCode: 1,
wantStdout: `
Expand All @@ -206,6 +206,21 @@ func TestRun(t *testing.T) {
`,
wantStderr: "",
},
// one specific supported sbom with vulns
{
name: "one specific supported sbom with vulns",
args: []string{"", "--config=./fixtures/osv-scanner-empty-config.toml", "--sbom", "./fixtures/sbom-insecure/alpine.cdx.xml"},
wantExitCode: 1,
wantStdout: `
Scanned <rootdir>/fixtures/sbom-insecure/alpine.cdx.xml as CycloneDX SBOM and found 15 packages
+--------------------------------+------+-----------+---------+-----------+---------------------------------------+
| OSV URL | CVSS | ECOSYSTEM | PACKAGE | VERSION | SOURCE |
+--------------------------------+------+-----------+---------+-----------+---------------------------------------+
| https://osv.dev/CVE-2022-37434 | 9.8 | Alpine | zlib | 1.2.10-r2 | fixtures/sbom-insecure/alpine.cdx.xml |
+--------------------------------+------+-----------+---------+-----------+---------------------------------------+
`,
wantStderr: "",
},
// one specific unsupported lockfile
{
name: "",
Expand Down Expand Up @@ -464,12 +479,14 @@ func TestRun(t *testing.T) {
// output format: markdown table
{
name: "",
args: []string{"", "--format", "markdown", "./fixtures/locks-many/composer.lock"},
wantExitCode: 0,
args: []string{"", "--format", "markdown", "--config", "./fixtures/osv-scanner-empty-config.toml", "./fixtures/locks-many/package-lock.json"},
wantExitCode: 1,
wantStdout: `
Scanning dir ./fixtures/locks-many/composer.lock
Scanned <rootdir>/fixtures/locks-many/composer.lock file and found 1 package
No vulnerabilities found
Scanning dir ./fixtures/locks-many/package-lock.json
Scanned <rootdir>/fixtures/locks-many/package-lock.json file and found 1 package
| OSV URL | CVSS | Ecosystem | Package | Version | Source |
| --- | --- | --- | --- | --- | --- |
| https://osv.dev/GHSA-whgm-jr23-g3j9 | 7.5 | npm | ansi-html | 0.0.1 | fixtures/locks-many/package-lock.json |
`,
wantStderr: "",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
7 changes: 7 additions & 0 deletions internal/sourceanalysis/fixtures-rust/rust-project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/sourceanalysis/fixtures-rust/rust-project/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "test-project"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# smallvec = "=0.6.13"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
println!("Hello, world!");
test_func();
}

fn test_func() {
println!("test func")
}
46 changes: 46 additions & 0 deletions internal/sourceanalysis/rust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"bytes"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/google/osv-scanner/internal/testutility"
"github.com/google/osv-scanner/pkg/models"
"github.com/google/osv-scanner/pkg/reporter"
)

func Test_extractRlibArchive(t *testing.T) {
Expand Down Expand Up @@ -62,3 +65,46 @@ func Test_functionsFromDWARF(t *testing.T) {
})
}
}

func Test_rustBuildSource(t *testing.T) {
t.Parallel()

workingDir, err := os.Getwd()
if err != nil {
t.Error(err)
}

type args struct {
r reporter.Reporter
source models.SourceInfo
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{
args: args{
r: &reporter.VoidReporter{},
source: models.SourceInfo{
Path: "fixtures-rust/rust-project/Cargo.lock",
Type: "lockfile",
},
},
want: []string{
workingDir + "/fixtures-rust/rust-project/target/release/test-project",
},
},
}
for _, tt := range tests {
got, err := rustBuildSource(tt.args.r, tt.args.source)
if (err != nil) != tt.wantErr {
t.Errorf("rustBuildSource() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("rustBuildSource() = %v, want %v", got, tt.want)
}
}
}
1 change: 1 addition & 0 deletions pkg/osvscanner/fixtures/example-git/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Example commit
1 change: 1 addition & 0 deletions pkg/osvscanner/fixtures/example-git/git-hidden/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/main
5 changes: 5 additions & 0 deletions pkg/osvscanner/fixtures/example-git/git-hidden/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
1 change: 1 addition & 0 deletions pkg/osvscanner/fixtures/example-git/git-hidden/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Binary file added pkg/osvscanner/fixtures/example-git/git-hidden/index
Binary file not shown.
6 changes: 6 additions & 0 deletions pkg/osvscanner/fixtures/example-git/git-hidden/info/exclude
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
1 change: 1 addition & 0 deletions pkg/osvscanner/fixtures/example-git/git-hidden/logs/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000000000000000000000000000000 862ac4bd2703b622e85f29f55a2fd8cd6caf8182 Rex P <[email protected]> 1696388400 +1100 commit (initial): Example commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000000000000000000000000000000 862ac4bd2703b622e85f29f55a2fd8cd6caf8182 Rex P <[email protected]> 1696388400 +1100 commit (initial): Example commit
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
862ac4bd2703b622e85f29f55a2fd8cd6caf8182
8 changes: 8 additions & 0 deletions pkg/osvscanner/fixtures/git-scan-queries.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"queries": [
{
"commit": "862ac4bd2703b622e85f29f55a2fd8cd6caf8182",
"package": {}
}
]
}
44 changes: 44 additions & 0 deletions pkg/osvscanner/osvscanner_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package osvscanner

import (
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/osv-scanner/internal/testutility"
"github.com/google/osv-scanner/pkg/config"
"github.com/google/osv-scanner/pkg/models"
"github.com/google/osv-scanner/pkg/osv"
"github.com/google/osv-scanner/pkg/reporter"
)

Expand Down Expand Up @@ -72,3 +74,45 @@ func Test_filterResults(t *testing.T) {
})
}
}

func Test_scanGit(t *testing.T) {
t.Parallel()

type args struct {
r reporter.Reporter
query *osv.BatchedQuery
repoDir string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Example Git repo",
args: args{
r: &reporter.VoidReporter{},
query: &osv.BatchedQuery{},
repoDir: "fixtures/example-git",
},
wantErr: false,
},
}

err := os.Rename("fixtures/example-git/git-hidden", "fixtures/example-git/.git")
if err != nil {
t.Errorf("can't find git-hidden folder")
}

for _, tt := range tests {
if err := scanGit(tt.args.r, tt.args.query, tt.args.repoDir); (err != nil) != tt.wantErr {
t.Errorf("scanGit() error = %v, wantErr %v", err, tt.wantErr)
}
testutility.CreateJSONFixture(t, "fixtures/git-scan-queries.txt", tt.args.query)
}

err = os.Rename("fixtures/example-git/.git", "fixtures/example-git/git-hidden")
if err != nil {
t.Errorf("can't find .git folder")
}
}

0 comments on commit 4d86a90

Please sign in to comment.