Skip to content

Commit

Permalink
format fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
pascaldekloe committed Jul 23, 2024
1 parent ab8a1b5 commit 27af6fb
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions info/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package info

import (
"fmt"
"strings"
"testing"
)

Expand Down Expand Up @@ -45,3 +46,39 @@ func TestGoldenFormats(t *testing.T) {
}
}
}

func FuzzDataUnitFormat(f *testing.F) {
f.Fuzz(func(t *testing.T, typeID byte, enc byte, cause byte, origAddr byte, comAddr byte, info []byte) {
u := DataUnit[OrigAddr8, ComAddr8, ObjAddr8]{
Type: TypeID(typeID),
Enc: Enc(enc),
Cause: Cause(cause),
Orig: OrigAddr8{origAddr},
Addr: ComAddr8{comAddr},
Info: info,
}
got := fmt.Sprintf("%s\n%#s", &u, u)
a, b, _ := strings.Cut(got, "\n")
if a == "" || b == "" {
t.Fatalf("got %q and %q", a, b)
}

// Check the ending first to be reasonable sure of a complete formatting.
switch a[len(a)-1] {
case '.', '?', '!':
if a[len(a)-1] != b[len(b)-1] {
t.Errorf("last character differs between %q and %q", a, b)
}
default:
t.Errorf("last character not '.', '?' or '!' of %q (with %q)", a, b)
}

// ASDUs have a fixed data-unit identifier.
switch DUI, _, _ := strings.Cut(a, ":"); {
case DUI == "":
t.Errorf("want data-unit identifier terminated by ':' in %q", a)
case strings.Count(DUI, " ") != 3:
t.Errorf("data-uint identifier %q of %q does not have 4 fields", DUI, a)
}
})
}

0 comments on commit 27af6fb

Please sign in to comment.