Skip to content

Commit

Permalink
🐛 Bug fixing: recurring results of the scorecard fuzzing check for go…
Browse files Browse the repository at this point in the history
… built-in fuzzers (#2101)

* save

* save

* save

* save

* save
  • Loading branch information
aidenwang9867 authored Jul 28, 2022
1 parent 8118e5d commit 1e0e44a
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 8 deletions.
15 changes: 8 additions & 7 deletions checks/raw/fuzzing.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func Fuzzing(c *checker.CheckRequest) (checker.FuzzingData, error) {
return checker.FuzzingData{}, fmt.Errorf("cannot get langs of repo: %w", err)
}
prominentLangs := getProminentLanguages(langs)

for _, lang := range prominentLangs {
usingFuzzFunc, files, e := checkFuzzFunc(c, lang)
if e != nil {
Expand Down Expand Up @@ -224,18 +223,20 @@ func getProminentLanguages(langs []clients.Language) []clients.LanguageName {
return nil
}
totalLoC := 0
// Use a map to record languages and their lines of code to drop potential duplicates.
langMap := map[clients.LanguageName]int{}
for _, l := range langs {
totalLoC += l.NumLines
langMap[l.Name] += l.NumLines
}
// Var avgLoC calculates the average lines of code in the current repo,
// and it can stay as an int, no need for a float value.
// Calculate the average lines of code in the current repo.
// This var can stay as an int, no need for a precise float value.
avgLoC := totalLoC / numLangs

// Languages that have lines of code above average will be considered prominent.
ret := []clients.LanguageName{}
for _, l := range langs {
if l.NumLines >= avgLoC {
lang := clients.LanguageName(strings.ToLower(string(l.Name)))
for lName, loC := range langMap {
if loC >= avgLoC {
lang := clients.LanguageName(strings.ToLower(string(lName)))
ret = append(ret, lang)
}
}
Expand Down
110 changes: 109 additions & 1 deletion checks/raw/fuzzing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ func Test_checkFuzzFunc(t *testing.T) {
fileContent string
}{
{
// TODO: more test cases needed. @aidenwang9867
name: "Test_checkFuzzFunc failure",
want: false,
wantErr: false,
Expand Down Expand Up @@ -286,3 +285,112 @@ func Test_checkFuzzFunc(t *testing.T) {
})
}
}

func Test_getProminentLanguages(t *testing.T) {
t.Parallel()
//nolint
tests := []struct {
name string
languages []clients.Language
expected []clients.LanguageName
}{
{
name: "case1",
languages: []clients.Language{
{
Name: clients.Go,
NumLines: 1000,
},
{
Name: clients.Python,
NumLines: 40,
}, {
Name: clients.JavaScript,
NumLines: 800,
},
},
expected: []clients.LanguageName{
clients.Go, clients.JavaScript,
},
},
{
// This test case simulates the situation when the GitHub language API returns
// duplicated languages, but we can still drop them and get the correct result.
name: "case2: drop duplicates",
languages: []clients.Language{
{
Name: clients.Go,
NumLines: 1000,
},
{
Name: clients.Python,
NumLines: 40,
}, {
Name: clients.JavaScript,
NumLines: 800,
},
{
Name: clients.Go,
NumLines: 1000,
},
{
Name: clients.Python,
NumLines: 40,
}, {
Name: clients.JavaScript,
NumLines: 800,
},
{
Name: clients.Go,
NumLines: 1000,
},
{
Name: clients.Python,
NumLines: 40,
}, {
Name: clients.JavaScript,
NumLines: 800,
},
},
expected: []clients.LanguageName{
clients.Go, clients.JavaScript,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := getProminentLanguages(tt.languages)
if !unorderedEqual(got, tt.expected) {
t.Errorf(
"got (%s) != expected (%s)",
got, tt.expected,
)
}

})
}
}

func unorderedEqual(l1, l2 []clients.LanguageName) bool {
if len(l1) != len(l2) {
return false
}
l1Map, l2Map := map[clients.LanguageName]bool{}, map[clients.LanguageName]bool{}
for _, l := range l1 {
l1Map[l] = true
}
for _, l := range l2 {
l2Map[l] = true
if !l1Map[l] {
return false
}
}
for k := range l1Map {
if !l2Map[k] {
return false
}
}
return true
}
21 changes: 21 additions & 0 deletions e2e/fuzzing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/checks"
"github.com/ossf/scorecard/v4/checks/raw"
"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/clients/githubrepo"
scut "github.com/ossf/scorecard/v4/utests"
Expand Down Expand Up @@ -113,6 +114,26 @@ var _ = Describe("E2E TEST:"+checks.CheckFuzzing, func() {
Expect(repoClient.Close()).Should(BeNil())
Expect(ossFuzzRepoClient.Close()).Should(BeNil())
})
It("Should return an expected number of GoBuiltInFuzzers", func() {
dl := scut.TestDetailLogger{}
repo, err := githubrepo.MakeGithubRepo("ossf-tests/scorecard-check-fuzzing-golang")
Expect(err).Should(BeNil())
repoClient := githubrepo.CreateGithubRepoClient(context.Background(), logger)
err = repoClient.InitRepo(repo, clients.HeadSHA)
Expect(err).Should(BeNil())
ossFuzzRepoClient, err := githubrepo.CreateOssFuzzRepoClient(context.Background(), logger)
Expect(err).Should(BeNil())
req := checker.CheckRequest{
Ctx: context.Background(),
RepoClient: repoClient,
OssFuzzRepo: ossFuzzRepoClient,
Repo: repo,
Dlogger: &dl,
}
rawData, err := raw.Fuzzing(&req)
Expect(err).Should(BeNil())
Expect(len(rawData.Fuzzers) == 1).Should(BeTrue())
})
It("Should return no fuzzing", func() {
dl := scut.TestDetailLogger{}
repo, err := githubrepo.MakeGithubRepo("ossf-tests/scorecard-check-packaging-e2e")
Expand Down

0 comments on commit 1e0e44a

Please sign in to comment.