diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-04-12 13:56:41 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-04-12 13:56:41 +0900 |
| commit | 6309469e933a31a300fbf16d8e77f48dcee402d3 (patch) | |
| tree | 8f8a72ee02b3ca15b104a6e55c9379e20f8d7e8a /internal/app/id_test.go | |
| parent | 0d7c1a9a4356614f035225aeb24e66421879a99b (diff) | |
app/instance: wrap internal implementation
This reduces the scope of the fst package, which was growing questionably large.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/app/id_test.go')
| -rw-r--r-- | internal/app/id_test.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/internal/app/id_test.go b/internal/app/id_test.go new file mode 100644 index 00000000..f928a482 --- /dev/null +++ b/internal/app/id_test.go @@ -0,0 +1,63 @@ +package app_test + +import ( + "errors" + "testing" + + . "git.gensokyo.uk/security/fortify/internal/app" +) + +func TestParseAppID(t *testing.T) { + t.Run("bad length", func(t *testing.T) { + if err := ParseAppID(new(ID), "meow"); !errors.Is(err, ErrInvalidLength) { + t.Errorf("ParseAppID: error = %v, wantErr = %v", err, ErrInvalidLength) + } + }) + + t.Run("bad byte", func(t *testing.T) { + wantErr := "invalid char '\\n' at byte 15" + if err := ParseAppID(new(ID), "02bc7f8936b2af6\n\ne2535cd71ef0bb7"); err == nil || err.Error() != wantErr { + t.Errorf("ParseAppID: error = %v, wantErr = %v", err, wantErr) + } + }) + + t.Run("fuzz 16 iterations", func(t *testing.T) { + for i := 0; i < 16; i++ { + testParseAppIDWithRandom(t) + } + }) +} + +func FuzzParseAppID(f *testing.F) { + for i := 0; i < 16; i++ { + id := new(ID) + if err := NewAppID(id); err != nil { + panic(err.Error()) + } + f.Add(id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11], id[12], id[13], id[14], id[15]) + } + + f.Fuzz(func(t *testing.T, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15 byte) { + testParseAppID(t, &ID{b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15}) + }) +} + +func testParseAppIDWithRandom(t *testing.T) { + id := new(ID) + if err := NewAppID(id); err != nil { + t.Fatalf("cannot generate app ID: %v", err) + } + testParseAppID(t, id) +} + +func testParseAppID(t *testing.T, id *ID) { + s := id.String() + got := new(ID) + if err := ParseAppID(got, s); err != nil { + t.Fatalf("cannot parse app ID: %v", err) + } + + if *got != *id { + t.Fatalf("ParseAppID(%#v) = \n%#v, want \n%#v", s, got, id) + } +} |
