2021-12-18 02:11:51 +03:00
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
//go:build go1.17 && !windows
|
|
|
|
// +build go1.17,!windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-11 00:26:13 +03:00
|
|
|
"errors"
|
2022-09-14 02:09:01 +03:00
|
|
|
"fmt"
|
2021-12-18 02:11:51 +03:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-12-21 01:14:01 +03:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2022-08-11 00:26:13 +03:00
|
|
|
"sort"
|
2021-12-21 01:14:01 +03:00
|
|
|
"strings"
|
2021-12-18 02:11:51 +03:00
|
|
|
"testing"
|
2022-09-14 02:09:01 +03:00
|
|
|
"time"
|
2021-12-21 01:14:01 +03:00
|
|
|
|
2022-09-14 02:09:01 +03:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
"golang.org/x/vulndb/internal/database"
|
2021-12-21 01:14:01 +03:00
|
|
|
"golang.org/x/vulndb/internal/report"
|
2021-12-18 02:11:51 +03:00
|
|
|
)
|
|
|
|
|
2021-12-21 01:14:01 +03:00
|
|
|
func TestChecksBash(t *testing.T) {
|
2021-12-18 02:11:51 +03:00
|
|
|
bash, err := exec.LookPath("bash")
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("skipping: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(bash, "./checks.bash")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 01:14:01 +03:00
|
|
|
|
2022-08-11 00:26:13 +03:00
|
|
|
const (
|
2022-08-12 20:19:52 +03:00
|
|
|
reportsDir = "data/reports"
|
|
|
|
excludedDir = "data/excluded"
|
2022-08-11 00:26:13 +03:00
|
|
|
)
|
2021-12-21 01:14:01 +03:00
|
|
|
|
|
|
|
func TestLintReports(t *testing.T) {
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
t.Skipf("wasm builder does not have network access")
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
t.Skipf("android builder does not have access to reports/")
|
|
|
|
}
|
2022-08-11 00:26:13 +03:00
|
|
|
allFiles := make(map[string]string)
|
|
|
|
var reports []string
|
|
|
|
for _, dir := range []string{reportsDir, excludedDir} {
|
2022-09-14 09:13:32 +03:00
|
|
|
files, err := os.ReadDir(dir)
|
2022-08-11 00:26:13 +03:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
t.Fatalf("unable to read %v/: %s", dir, err)
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
2022-08-22 22:01:49 +03:00
|
|
|
if filepath.Ext(fi.Name()) != ".yaml" {
|
|
|
|
continue
|
|
|
|
}
|
2022-08-11 00:26:13 +03:00
|
|
|
fn := filepath.Join(dir, fi.Name())
|
|
|
|
if allFiles[fi.Name()] != "" {
|
|
|
|
t.Errorf("report appears in multiple locations: %v, %v", allFiles[fi.Name()], fn)
|
|
|
|
}
|
|
|
|
allFiles[fi.Name()] = fn
|
|
|
|
reports = append(reports, fn)
|
2021-12-21 01:14:01 +03:00
|
|
|
}
|
2022-08-11 00:26:13 +03:00
|
|
|
}
|
|
|
|
sort.Strings(reports)
|
|
|
|
for _, fn := range reports {
|
|
|
|
t.Run(fn, func(t *testing.T) {
|
|
|
|
r, err := report.Read(fn)
|
2021-12-21 01:14:01 +03:00
|
|
|
if err != nil {
|
2022-01-04 23:12:43 +03:00
|
|
|
t.Fatal(err)
|
2021-12-21 01:14:01 +03:00
|
|
|
}
|
2022-08-11 00:26:13 +03:00
|
|
|
switch filepath.Base(filepath.Dir(fn)) {
|
|
|
|
case reportsDir:
|
|
|
|
if r.Excluded != "" {
|
|
|
|
t.Errorf("report in %q must not have excluded set", reportsDir)
|
|
|
|
}
|
|
|
|
case excludedDir:
|
|
|
|
if r.Excluded == "" {
|
|
|
|
t.Errorf("report in %q must have excluded set", excludedDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lints := r.Lint(fn)
|
2021-12-21 01:14:01 +03:00
|
|
|
if len(lints) > 0 {
|
|
|
|
t.Errorf(strings.Join(lints, "\n"))
|
|
|
|
}
|
2022-09-14 02:09:01 +03:00
|
|
|
if r.Excluded == "" {
|
|
|
|
e1 := database.GenerateOSVEntry(fn, time.Time{}, r)
|
|
|
|
e2, err := database.ReadOSV(fmt.Sprintf("data/osv/%v.json", e1.ID))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(e1, e2, cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
t.Errorf("data/osv/%v.json does not match report:\n%v", e1.ID, diff)
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 01:14:01 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|