Skip to content

Commit

Permalink
tests: add fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverpool committed Oct 17, 2024
1 parent 4dc6ced commit 4b773fc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ test-license:
reuse lint

fuzz:
go test -fuzztime=10s -fuzz=NewIBAN
go test -fuzztime=10s -fuzz=NewBIC
go test -fuzztime=10s -fuzz=FromIBAN ./bic
go test -fuzztime=10s -fuzz=NewISIN
go test -fuzztime=10s -fuzz=GenerateISIN
4 changes: 2 additions & 2 deletions isin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewISIN(s string, additionalRules ...ISINValidator) (ISIN, error) {
isin.NSIN = s[2:11]
isin.CheckDigit = int(s[11] - '0')
if isin.CheckDigit < 0 || isin.CheckDigit > 9 {
return isin, CheckDigitError{Given: isin.CheckDigit}
return isin, CheckDigitError{Given: isin.CheckDigit, Computed: -1}
}
c := isin.checkDigit()
if c != isin.CheckDigit {
Expand Down Expand Up @@ -113,6 +113,6 @@ func (e CheckDigitError) Error() string {
if e.Given < 0 || e.Given > 9 {
return "expected digit as last chart, got " + strconv.Quote(string(byte(e.Given+'0')))
}
// don't display the expected/actual digits, because the error is most likely somewhere else in the BBAN
// don't display the expected/actual digits, because the error is most likely somewhere else in the NSIN
return "wrong check digit"
}
83 changes: 83 additions & 0 deletions isin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package corfin

import (
"errors"
"strconv"
"testing"

"code.pfad.fr/check"
Expand Down Expand Up @@ -113,3 +114,85 @@ func TestInvalidISIN(t *testing.T) {
f("GB0002634947", 7, 6)
})
}

func FuzzNewISIN(f *testing.F) {
f.Add("US0378331005")
f.Add("DE0006231004")
f.Add("DE000BAY0017")
f.Add("XF0000C14922")
f.Add("NL0000729408")
f.Add("CH0031240127")
f.Add("US5949181045")
f.Add("US38259P5089")
f.Add("JP3946600008")
f.Add("DE000DZ21632")
f.Add("DE000DB7HWY7")
f.Add("DE000CM7VX13")
f.Add("CH0031240127")
f.Add("CA9861913023")
f.Fuzz(func(t *testing.T, s string) {
isin, err := NewISIN(s)
if err != nil {
var cerr CheckDigitError
var lerr LenError
if errors.As(err, &cerr) || errors.As(err, &lerr) {
return
}
t.Fatal(err)
}
isin2, err := NewISIN(isin.String())
check.Equal(t, nil, err).Fatal()
check.Equal(t, isin, isin2)
})
}

func FuzzGenerateISIN(f *testing.F) {
f.Add("US0378331005")
f.Add("DE0006231004")
f.Add("DE000BAY0017")
f.Add("XF0000C14922")
f.Add("NL0000729408")
f.Add("CH0031240127")
f.Add("US5949181045")
f.Add("US38259P5089")
f.Add("JP3946600008")
f.Add("DE000DZ21632")
f.Add("DE000DB7HWY7")
f.Add("DE000CM7VX13")
f.Add("CH0031240127")
f.Add("CA9861913023")
// wrong isins:
f.Add("CA9861913024") // will be fixed
f.Add("CA9861913024 ") // will be fixed
f.Add("CA986191302A") // not fixable: last char is not a digit
f.Add("CA9861913024BC") // not fixable: too long
f.Fuzz(func(t *testing.T, s string) {
isin, err := NewISIN(s)
if err == nil {
return
}
var lerr LenError
if errors.As(err, &lerr) {
// hard to fix
return
}
var cerr CheckDigitError
if !errors.As(err, &cerr) {
t.Fatal(err)
}
if cerr.Computed == -1 {
// last char is not a digit: not fixable
return
}
// fix the check digit
t.Log("before", s, cerr.Given)
t.Log("after", isin.String())
s = isin.String() // get the cleaned version
s = s[:len(s)-1] + strconv.Itoa(cerr.Computed)
isin, err = NewISIN(s)
if err != nil {
t.Fatal(err)
}
check.Equal(t, isin.String(), s)
})
}

0 comments on commit 4b773fc

Please sign in to comment.