Skip to content

Commit 1e0e44a

Browse files
🐛 Bug fixing: recurring results of the scorecard fuzzing check for go built-in fuzzers (#2101)
* save * save * save * save * save
1 parent 8118e5d commit 1e0e44a

File tree

3 files changed

+138
-8
lines changed

3 files changed

+138
-8
lines changed

checks/raw/fuzzing.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func Fuzzing(c *checker.CheckRequest) (checker.FuzzingData, error) {
9898
return checker.FuzzingData{}, fmt.Errorf("cannot get langs of repo: %w", err)
9999
}
100100
prominentLangs := getProminentLanguages(langs)
101-
102101
for _, lang := range prominentLangs {
103102
usingFuzzFunc, files, e := checkFuzzFunc(c, lang)
104103
if e != nil {
@@ -224,18 +223,20 @@ func getProminentLanguages(langs []clients.Language) []clients.LanguageName {
224223
return nil
225224
}
226225
totalLoC := 0
226+
// Use a map to record languages and their lines of code to drop potential duplicates.
227+
langMap := map[clients.LanguageName]int{}
227228
for _, l := range langs {
228229
totalLoC += l.NumLines
230+
langMap[l.Name] += l.NumLines
229231
}
230-
// Var avgLoC calculates the average lines of code in the current repo,
231-
// and it can stay as an int, no need for a float value.
232+
// Calculate the average lines of code in the current repo.
233+
// This var can stay as an int, no need for a precise float value.
232234
avgLoC := totalLoC / numLangs
233-
234235
// Languages that have lines of code above average will be considered prominent.
235236
ret := []clients.LanguageName{}
236-
for _, l := range langs {
237-
if l.NumLines >= avgLoC {
238-
lang := clients.LanguageName(strings.ToLower(string(l.Name)))
237+
for lName, loC := range langMap {
238+
if loC >= avgLoC {
239+
lang := clients.LanguageName(strings.ToLower(string(lName)))
239240
ret = append(ret, lang)
240241
}
241242
}

checks/raw/fuzzing_test.go

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ func Test_checkFuzzFunc(t *testing.T) {
242242
fileContent string
243243
}{
244244
{
245-
// TODO: more test cases needed. @aidenwang9867
246245
name: "Test_checkFuzzFunc failure",
247246
want: false,
248247
wantErr: false,
@@ -286,3 +285,112 @@ func Test_checkFuzzFunc(t *testing.T) {
286285
})
287286
}
288287
}
288+
289+
func Test_getProminentLanguages(t *testing.T) {
290+
t.Parallel()
291+
//nolint
292+
tests := []struct {
293+
name string
294+
languages []clients.Language
295+
expected []clients.LanguageName
296+
}{
297+
{
298+
name: "case1",
299+
languages: []clients.Language{
300+
{
301+
Name: clients.Go,
302+
NumLines: 1000,
303+
},
304+
{
305+
Name: clients.Python,
306+
NumLines: 40,
307+
}, {
308+
Name: clients.JavaScript,
309+
NumLines: 800,
310+
},
311+
},
312+
expected: []clients.LanguageName{
313+
clients.Go, clients.JavaScript,
314+
},
315+
},
316+
{
317+
// This test case simulates the situation when the GitHub language API returns
318+
// duplicated languages, but we can still drop them and get the correct result.
319+
name: "case2: drop duplicates",
320+
languages: []clients.Language{
321+
{
322+
Name: clients.Go,
323+
NumLines: 1000,
324+
},
325+
{
326+
Name: clients.Python,
327+
NumLines: 40,
328+
}, {
329+
Name: clients.JavaScript,
330+
NumLines: 800,
331+
},
332+
{
333+
Name: clients.Go,
334+
NumLines: 1000,
335+
},
336+
{
337+
Name: clients.Python,
338+
NumLines: 40,
339+
}, {
340+
Name: clients.JavaScript,
341+
NumLines: 800,
342+
},
343+
{
344+
Name: clients.Go,
345+
NumLines: 1000,
346+
},
347+
{
348+
Name: clients.Python,
349+
NumLines: 40,
350+
}, {
351+
Name: clients.JavaScript,
352+
NumLines: 800,
353+
},
354+
},
355+
expected: []clients.LanguageName{
356+
clients.Go, clients.JavaScript,
357+
},
358+
},
359+
}
360+
for _, tt := range tests {
361+
tt := tt
362+
t.Run(tt.name, func(t *testing.T) {
363+
t.Parallel()
364+
got := getProminentLanguages(tt.languages)
365+
if !unorderedEqual(got, tt.expected) {
366+
t.Errorf(
367+
"got (%s) != expected (%s)",
368+
got, tt.expected,
369+
)
370+
}
371+
372+
})
373+
}
374+
}
375+
376+
func unorderedEqual(l1, l2 []clients.LanguageName) bool {
377+
if len(l1) != len(l2) {
378+
return false
379+
}
380+
l1Map, l2Map := map[clients.LanguageName]bool{}, map[clients.LanguageName]bool{}
381+
for _, l := range l1 {
382+
l1Map[l] = true
383+
}
384+
for _, l := range l2 {
385+
l2Map[l] = true
386+
if !l1Map[l] {
387+
return false
388+
}
389+
}
390+
for k := range l1Map {
391+
if !l2Map[k] {
392+
return false
393+
}
394+
}
395+
return true
396+
}

e2e/fuzzing_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/ossf/scorecard/v4/checker"
2424
"github.com/ossf/scorecard/v4/checks"
25+
"github.com/ossf/scorecard/v4/checks/raw"
2526
"github.com/ossf/scorecard/v4/clients"
2627
"github.com/ossf/scorecard/v4/clients/githubrepo"
2728
scut "github.com/ossf/scorecard/v4/utests"
@@ -113,6 +114,26 @@ var _ = Describe("E2E TEST:"+checks.CheckFuzzing, func() {
113114
Expect(repoClient.Close()).Should(BeNil())
114115
Expect(ossFuzzRepoClient.Close()).Should(BeNil())
115116
})
117+
It("Should return an expected number of GoBuiltInFuzzers", func() {
118+
dl := scut.TestDetailLogger{}
119+
repo, err := githubrepo.MakeGithubRepo("ossf-tests/scorecard-check-fuzzing-golang")
120+
Expect(err).Should(BeNil())
121+
repoClient := githubrepo.CreateGithubRepoClient(context.Background(), logger)
122+
err = repoClient.InitRepo(repo, clients.HeadSHA)
123+
Expect(err).Should(BeNil())
124+
ossFuzzRepoClient, err := githubrepo.CreateOssFuzzRepoClient(context.Background(), logger)
125+
Expect(err).Should(BeNil())
126+
req := checker.CheckRequest{
127+
Ctx: context.Background(),
128+
RepoClient: repoClient,
129+
OssFuzzRepo: ossFuzzRepoClient,
130+
Repo: repo,
131+
Dlogger: &dl,
132+
}
133+
rawData, err := raw.Fuzzing(&req)
134+
Expect(err).Should(BeNil())
135+
Expect(len(rawData.Fuzzers) == 1).Should(BeTrue())
136+
})
116137
It("Should return no fuzzing", func() {
117138
dl := scut.TestDetailLogger{}
118139
repo, err := githubrepo.MakeGithubRepo("ossf-tests/scorecard-check-packaging-e2e")

0 commit comments

Comments
 (0)