Skip to content

Commit

Permalink
search: add Fuzz test for Similarity
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 24, 2025
1 parent 6924af3 commit 3526225
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/search/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package search

import (
"encoding/json"
"testing"
)

func FuzzSimilarity(f *testing.F) {
// Setup the fuzz corpus
corpusAdd(f, Entity[Value]{})
corpusAdd(f, Entity[Value]{
Person: &Person{},
})
corpusAdd(f, Entity[Value]{
Business: &Business{},
})
corpusAdd(f, Entity[Value]{
Organization: &Organization{},
})
corpusAdd(f, Entity[Value]{
Aircraft: &Aircraft{},
})
corpusAdd(f, Entity[Value]{
Vessel: &Vessel{},
})
corpusAdd(f, Entity[Value]{
SanctionsInfo: &SanctionsInfo{},
})
corpusAdd(f, Entity[Value]{
Addresses: []Address{{}},
CryptoAddresses: []CryptoAddress{{}},
Affiliations: []Affiliation{{}},
HistoricalInfo: []HistoricalInfo{{}},
})

// Run the fuzz loop
f.Fuzz(func(t *testing.T, queryData []byte, indexData []byte) {
var query Entity[Value]
err := json.Unmarshal(queryData, &query)
if err != nil {
return
}

var index Entity[Value]
err = json.Unmarshal(indexData, &index)
if err != nil {
return
}

Similarity(query, index)
Similarity(index, query)
})
}

func corpusAdd(f *testing.F, entity Entity[Value]) {
f.Helper()

bs, err := json.Marshal(entity)
if err != nil {
f.Fatal(err)
}
f.Add(bs, bs)
}

0 comments on commit 3526225

Please sign in to comment.