-
Notifications
You must be signed in to change notification settings - Fork 450
/
fuzzing_test.go
146 lines (138 loc) · 3.67 KB
/
fuzzing_test.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2020 Security Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package checks
import (
"errors"
"testing"
"github.com/golang/mock/gomock"
"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/clients"
mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
sce "github.com/ossf/scorecard/v4/errors"
scut "github.com/ossf/scorecard/v4/utests"
)
// TestFuzzing is a test function for Fuzzing.
func TestFuzzing(t *testing.T) {
t.Parallel()
//nolint
tests := []struct {
name string
want checker.CheckResult
langs map[string]int
response clients.SearchResponse
wantErr bool
wantFuzzErr bool
fileName []string
fileContent string
expected scut.TestReturn
}{
{
name: "empty response",
response: clients.SearchResponse{},
langs: map[string]int{
"go": 300,
},
wantErr: false,
},
{
name: "hits 1",
response: clients.SearchResponse{
Hits: 1,
},
langs: map[string]int{
"go": 100,
"java": 70,
},
wantErr: false,
want: checker.CheckResult{Score: 10},
expected: scut.TestReturn{
NumberOfWarn: 0,
NumberOfDebug: 0,
NumberOfInfo: 0,
Score: 10,
},
},
{
name: "nil response",
langs: map[string]int{
"python": 256,
},
wantErr: true,
want: checker.CheckResult{Score: -1},
expected: scut.TestReturn{
Error: sce.ErrScorecardInternal,
NumberOfWarn: 0,
NumberOfDebug: 0,
NumberOfInfo: 0,
Score: -1,
},
},
{
name: "min score since lang not supported",
langs: map[string]int{
"not_supported_lang": 1490,
},
wantFuzzErr: false,
want: checker.CheckResult{Score: 0},
},
{
name: "error",
wantFuzzErr: true,
want: checker.CheckResult{},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockFuzz := mockrepo.NewMockRepoClient(ctrl)
mockFuzz.EXPECT().URI().Return("github.com/ossf/scorecard").AnyTimes()
mockFuzz.EXPECT().Search(gomock.Any()).
DoAndReturn(func(q clients.SearchRequest) (clients.SearchResponse, error) {
if tt.wantErr {
//nolint
return clients.SearchResponse{}, errors.New("error")
}
return tt.response, nil
}).AnyTimes()
mockFuzz.EXPECT().ListProgrammingLanguages().Return(tt.langs, nil).AnyTimes()
mockFuzz.EXPECT().ListFiles(gomock.Any()).Return(tt.fileName, nil).AnyTimes()
mockFuzz.EXPECT().GetFileContent(gomock.Any()).DoAndReturn(func(f string) (string, error) {
if tt.wantErr {
//nolint
return "", errors.New("error")
}
return tt.fileContent, nil
}).AnyTimes()
dl := scut.TestDetailLogger{}
req := checker.CheckRequest{
RepoClient: mockFuzz,
OssFuzzRepo: mockFuzz,
Dlogger: &dl,
}
if tt.wantFuzzErr {
req.OssFuzzRepo = nil
}
result := Fuzzing(&req)
if (result.Error != nil) != tt.wantErr {
t.Errorf("Fuzzing() error = %v, wantErr %v", result.Error, tt.wantErr)
return
}
if !scut.ValidateTestReturn(t, tt.name, &tt.expected, &result, &dl) {
t.Fatalf(tt.name, tt.expected)
}
})
}
}