Skip to content

Commit

Permalink
Issue#261: Add fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
td-bn committed Dec 13, 2022
1 parent d46fd8a commit 7d21e01
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions did/key_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package did

import (
"testing"

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

func FuzzGenerateAndDecode(f *testing.F) {
keytypes := GetSupportedDIDKeyTypes()
f.Fuzz(func(t *testing.T, a uint) {
kt := keytypes[a%uint(len(keytypes))]

privKey, didKey, err := GenerateDIDKey(kt)
assert.NotEmpty(t, privKey)

expectedLdKeyType, _ := KeyTypeToLDKeyType(kt)

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

func FuzzGenerateAndResolve(f *testing.F) {
keytypes := GetSupportedDIDKeyTypes()
resolvers := []Resolution{KeyResolver{}, WebResolver{}, PKHResolver{}, PeerResolver{}}
resolver, _ := NewResolver(resolvers...)

f.Fuzz(func(t *testing.T, a uint) {
kt := keytypes[a%uint(len(keytypes))]

_, 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)
})
}

0 comments on commit 7d21e01

Please sign in to comment.