File: source_test.go

package info (click to toggle)
golang-golang-x-vuln 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 4,400 kB
  • sloc: sh: 161; asm: 40; makefile: 7
file content (79 lines) | stat: -rw-r--r-- 1,558 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2023 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.

package client

import (
	"context"
	"os"
	"testing"
)

func TestGet(t *testing.T) {
	tcs := []struct {
		endpoint string
	}{
		{
			endpoint: "index/db",
		},
		{
			endpoint: "index/modules",
		},
		{
			endpoint: "ID/GO-2021-0068",
		},
	}
	for _, tc := range tcs {
		test := func(t *testing.T, s source) {
			got, err := s.get(context.Background(), tc.endpoint)
			if err != nil {
				t.Fatal(err)
			}
			want, err := os.ReadFile(testVulndb + "/" + tc.endpoint + ".json")
			if err != nil {
				t.Fatal(err)
			}
			if string(got) != string(want) {
				t.Errorf("get(%s) = %s, want %s", tc.endpoint, got, want)
			}
		}
		testAllSourceTypes(t, test)
	}
}

// testAllSourceTypes runs a given test for all source types.
func testAllSourceTypes(t *testing.T, test func(t *testing.T, s source)) {
	t.Run("http", func(t *testing.T) {
		srv := newTestServer(testVulndb)
		hs := newHTTPSource(srv.URL, &Options{HTTPClient: srv.Client()})
		test(t, hs)
	})

	t.Run("local", func(t *testing.T) {
		test(t, newLocalSource(testVulndb))
	})

	t.Run("in-memory", func(t *testing.T) {
		testEntries, err := entries(testIDs)
		if err != nil {
			t.Fatal(err)
		}

		ms, err := newInMemorySource(testEntries)
		if err != nil {
			t.Fatal(err)
		}

		test(t, ms)
	})

	t.Run("hybrid", func(t *testing.T) {
		hs, err := newHybridSource(testFlatVulndb)
		if err != nil {
			t.Fatal(err)
		}

		test(t, hs)
	})
}