Skip to content

Commit

Permalink
Issue#261: Add fuzz test, enhance test suite for key.go (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
td-bn authored Dec 19, 2022
1 parent e2badfb commit 0802858
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:

- name: Test
run: mage citest

- name: Fuzz tests
run: mage fuzz

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
56 changes: 56 additions & 0 deletions did/key_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package did

import (
"testing"

"github.com/stretchr/testify/assert"
)

var mockPubKeys = []string{
"b9c5714089478a327f09197987f16f9e5d936e8a", "5f246d7d19aa612d6718d27c1da1ee66859586b0", "7d2d43e63666f45b40316b44212325625dbaeb40", "1c1f02f1640e52b313f2d504b3c0c7ee8ad61108", "69c5888ecd21287fbdac5a43d1558bf73c51e38b",
}

func FuzzCreateAndDecode(f *testing.F) {
keytypes := GetSupportedDIDKeyTypes()
ktLen := len(keytypes)

for i, pk := range mockPubKeys {
f.Add(i, []byte(pk))
}
f.Fuzz(func(t *testing.T, ktSeed int, pubKey []byte) {
kt := keytypes[(ktSeed%ktLen+ktLen)%ktLen]

didKey, err := CreateDIDKey(kt, pubKey)
assert.NoError(t, err)

recvPubKey, _, _, err := didKey.Decode()
assert.NoError(t, err)
assert.Equal(t, pubKey, recvPubKey)
})
}

func FuzzCreateAndResolve(f *testing.F) {
keytypes := GetSupportedDIDKeyTypes()
ktLen := len(keytypes)

resolvers := []Resolution{KeyResolver{}, WebResolver{}, PKHResolver{}, PeerResolver{}}
resolver, _ := NewResolver(resolvers...)

for i, pk := range mockPubKeys {
f.Add(i, []byte(pk))
}

f.Fuzz(func(t *testing.T, ktSeed int, pubKey []byte) {
kt := keytypes[(ktSeed%ktLen+ktLen)%ktLen]

didKey, err := CreateDIDKey(kt, pubKey)
assert.NoError(t, err)

doc, err := resolver.Resolve(didKey.String())
if err != nil {
t.Skip()
}
assert.NotEmpty(t, doc)
assert.Equal(t, didKey.String(), doc.DIDDocument.ID)
})
}
31 changes: 31 additions & 0 deletions did/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,37 @@ func TestExpand(t *testing.T) {
})
}

func TestGenerateAndDecode(t *testing.T) {
for _, kt := range GetSupportedDIDKeyTypes() {
privKey, didKey, err := GenerateDIDKey(kt)
assert.NotEmpty(t, privKey)
assert.NoError(t, err)

expectedLLKeyType, _ := KeyTypeToLDKeyType(kt)

pubKey, ldKeyType, cryptoKeyType, err := didKey.Decode()
assert.NoError(t, err)
assert.NotEmpty(t, pubKey)
assert.Equal(t, ldKeyType, expectedLLKeyType)
assert.Equal(t, cryptoKeyType, kt)
}
}

func TestGenerateAndResolve(t *testing.T) {
resolvers := []Resolution{KeyResolver{}, WebResolver{}, PKHResolver{}, PeerResolver{}}
resolver, _ := NewResolver(resolvers...)

for _, kt := range GetSupportedDIDKeyTypes() {
_, didKey, err := GenerateDIDKey(kt)
assert.NoError(t, err)

doc, err := resolver.Resolve(didKey.String())
assert.NoError(t, err)
assert.NotEmpty(t, doc)
assert.Equal(t, didKey.String(), doc.DIDDocument.ID)
}
}

func TestDIDKeySignVerify(t *testing.T) {
t.Run("Test Ed25519 did:key", func(t *testing.T) {
privKey, didKey, err := GenerateDIDKey(crypto.Ed25519)
Expand Down
74 changes: 74 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ package main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"io/fs"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -43,6 +47,10 @@ func Test() error {
return runTests()
}

func Fuzz() error {
return runFuzzTests()
}

func runTests(extraTestArgs ...string) error {
args := []string{"test"}
if mg.Verbose() {
Expand All @@ -61,6 +69,72 @@ func runTests(extraTestArgs ...string) error {
return err
}

func runFuzzTests(extraTestArgs ...string) error {
dirs := []string{"./did"}

for _, dir := range dirs {
functionNames, _ := getFuzzTests(dir)

for _, testName := range functionNames {
args := []string{"test"}
if mg.Verbose() {
args = append(args, "-v")
}
args = append(args, dir)
args = append(args, fmt.Sprintf("-run=%s", testName))
args = append(args, fmt.Sprintf("-fuzz=%s", testName))
args = append(args, "-fuzztime=10s")
testEnv := map[string]string{
"CGO_ENABLED": "1",
"GO111MODULE": "on",
}
writer := ColorizeTestStdout()
fmt.Printf("%+v\n", args)
_, err := sh.Exec(testEnv, writer, os.Stderr, Go, args...)
if err != nil {
return err
}
}

}
return nil
}

func getFuzzTests(src string) ([]string, error) {
// src is the input for which we want to inspect the AST.
var testFilePaths []string
filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
if !strings.HasSuffix(path, "test.go") {
return nil
}
testFilePaths = append(testFilePaths, path)
return nil
})

// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
var testNames []string
for _, filename := range testFilePaths {
// Pass in nil to automatically parse the file
f, err := parser.ParseFile(fset, filename, nil, 0)
if err != nil {
panic(err)
}
ast.FileExports(f)
ast.FilterFile(f, func(s string) bool {
p := strings.HasPrefix(s, "Fuzz")
if p {
testNames = append(testNames, s)
}
return p
})
}
return testNames, nil
}

func Deps() error {
return brewInstall("golangci-lint")
}
Expand Down

0 comments on commit 0802858

Please sign in to comment.