You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DateKeys/locator/fuzz_test.go

86 lines
2.4 KiB

package locator_test
import (
"bytes"
"encoding/hex"
"testing"
"g.activething.com/go/DateKeys/extension"
"g.activething.com/go/DateKeys/internal/testkit"
"g.activething.com/go/DateKeys/locator"
)
// FuzzUnmarshal reads the plaintext of a locator: it never panics, and what
// it accepts is canonical, so that encoding it again gives the same bytes.
func FuzzUnmarshal(f *testing.F) {
var v testkit.LocatorVectorFile
if err := testkit.ReadJSON("../testdata/vectors/locator.json", &v); err != nil {
f.Fatal(err)
}
for _, s := range []string{v.Plaintext, v.Mixed.Plaintext} {
if b, err := hex.DecodeString(s); err == nil {
f.Add(b)
}
}
for _, c := range v.PlaintextCases {
if b, err := hex.DecodeString(c.Plaintext); err == nil {
f.Add(b)
}
}
f.Add([]byte{0xa0})
f.Fuzz(func(t *testing.T, b []byte) {
l, err := locator.Unmarshal(b)
if err != nil {
return
}
for _, a := range l.Usable() {
if locator.CheckURI(a.URI) != nil || a.Host() == "" {
t.Fatalf("a usable address %q that the rules refuse", a.URI)
}
}
})
}
// FuzzParseInfo reads the data of datekeys.capsule: it never panics, and its
// only normative code is ERR_EXTENSION_DATA_INVALID (spec §54, §57).
func FuzzParseInfo(f *testing.F) {
var v testkit.LocatorVectorFile
if err := testkit.ReadJSON("../testdata/vectors/locator.json", &v); err != nil {
f.Fatal(err)
}
if b, err := hex.DecodeString(v.Extension); err == nil {
f.Add(b)
}
for _, c := range v.ExtensionCases {
if b, err := hex.DecodeString(c.Data); err == nil {
f.Add(b)
}
}
f.Fuzz(func(t *testing.T, b []byte) {
_, err := locator.ParseInfo(extension.Extension{ID: extension.CapsuleID, Version: 1, Data: b})
if err != nil && !bytes.Contains([]byte(err.Error()), []byte("ERR_EXTENSION_DATA_INVALID")) {
t.Fatalf("an error without its code: %v", err)
}
})
}
// FuzzCheckURI checks an address: it never panics, and a host that it shows
// is in the address as it is written, with no decoding.
func FuzzCheckURI(f *testing.F) {
var v testkit.LocatorVectorFile
if err := testkit.ReadJSON("../testdata/vectors/locator.json", &v); err != nil {
f.Fatal(err)
}
for _, c := range v.URICases {
f.Add(c.URI)
}
f.Fuzz(func(t *testing.T, uri string) {
if locator.CheckURI(uri) != nil {
return
}
if h := (locator.Address{URI: uri}).Host(); h == "" || !bytes.Contains([]byte(uri), []byte(h)) {
t.Fatalf("%q: host %q", uri, h)
}
})
}

Powered by TurnKey Linux.