File: result_test.go

package info (click to toggle)
golang-golang-x-vuln 0.0~git20230201.4c848ed-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 992 kB
  • sloc: sh: 288; asm: 40; makefile: 7
file content (84 lines) | stat: -rw-r--r-- 2,407 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
80
81
82
83
84
// Copyright 2022 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 govulncheck

import (
	"go/token"
	"testing"
)

func TestStackFrame(t *testing.T) {
	for _, test := range []struct {
		sf       *StackFrame
		wantFunc string
		wantPos  string
	}{
		{
			&StackFrame{
				PkgPath:  "golang.org/x/vuln/vulncheck",
				FuncName: "Foo",
				Position: token.Position{Filename: "some/path/file.go", Line: 12},
			},
			"golang.org/x/vuln/vulncheck.Foo",
			"some/path/file.go:12",
		},
		{
			&StackFrame{
				PkgPath:  "golang.org/x/vuln/vulncheck",
				RecvType: "golang.org/x/vuln/vulncheck.Bar",
				FuncName: "Foo",
			},
			"golang.org/x/vuln/vulncheck.Bar.Foo",
			"",
		},
	} {
		if got := test.sf.Name(); got != test.wantFunc {
			t.Errorf("want %v func name; got %v", test.wantFunc, got)
		}
		if got := test.sf.Pos(); got != test.wantPos {
			t.Errorf("want %v call position; got %v", test.wantPos, got)
		}
	}
}

func TestVuln(t *testing.T) {
	// vuln creates a Vuln with symbol info syms.
	// Each element of syms is a pair <p, s> where
	// p is both the module and package path, and
	// s is the called symbol. If s is "", then
	// there is no called symbol.
	vuln := func(syms ...[2]string) *Vuln {
		v := &Vuln{}
		for _, sym := range syms {
			p := &Package{Path: sym[0]}
			v.Modules = append(v.Modules, &Module{
				Path:     sym[0],
				Packages: []*Package{p},
			})
			if symbol := sym[1]; symbol != "" {
				cs := CallStack{Symbol: symbol}
				p.CallStacks = []CallStack{cs}
			}
		}
		return v
	}

	for _, test := range []struct {
		desc string
		v    *Vuln
		want bool
	}{
		{"called - single module", vuln([2]string{"golang.org/p1", "Foo"}), true},
		{"called - multi modules", vuln([2]string{"golang.org/p1", "Foo"}, [2]string{"golang.org/p2", "Bar"}), true},
		// The following case is not expected to happen in practice, but we check it for sanity.
		{"called - mixed multi modules", vuln([2]string{"golang.org/p1", ""}, [2]string{"golang.org/p2", "Bar"}), true},
		{"not called - single module", vuln([2]string{"golang.org/p1", ""}), false},
		{"not called - multi modules", vuln([2]string{"golang.org/p1", ""}, [2]string{"golang.org/p2", ""}), false},
	} {
		if test.v.IsCalled() != test.want {
			t.Errorf("want called=%t for %v; got the opposite", test.want, test.desc)
		}
	}
}