diff options
author | Hank Shen <swh@admpub.com> | 2023-10-09 07:42:42 +0300 |
---|---|---|
committer | Hank Shen <swh@admpub.com> | 2023-10-09 07:42:42 +0300 |
commit | a9590b45840539390424b8bbab01079c03d59db4 (patch) | |
tree | d0a515e4bf11230d12c0e4572ea6aa032ebe84e0 /securecookie_test.go | |
parent | 69560e37559624d8ff8027cdb12c0cb39e00210a (diff) | |
parent | 22eae5c820537b29d36814c94c2c70c08e391d71 (diff) |
Merge branch 'main' of github.com:/gorilla/securecookie
Diffstat (limited to 'securecookie_test.go')
-rw-r--r-- | securecookie_test.go | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/securecookie_test.go b/securecookie_test.go index 2b0f8a1..72905ae 100644 --- a/securecookie_test.go +++ b/securecookie_test.go @@ -13,6 +13,8 @@ import ( "reflect" "strings" "testing" + + fuzz "github.com/google/gofuzz" ) // Asserts that cookieError and MultiError are Error implementations. @@ -238,7 +240,7 @@ func TestMultiError(t *testing.T) { if len(err.(MultiError)) != 2 { t.Errorf("Expected 2 errors, got %s.", err) } else { - if strings.Index(err.Error(), "hash key is not set") == -1 { + if !strings.Contains(err.Error(), "hash key is not set") { t.Errorf("Expected missing hash key error, got %s.", err.Error()) } ourErr, ok := err.(Error) @@ -268,15 +270,22 @@ func TestMultiNoCodecs(t *testing.T) { } func TestMissingKey(t *testing.T) { - s1 := New(nil, nil) - - var dst []byte - err := s1.Decode("sid", "value", &dst) - if err != errHashKeyNotSet { - t.Fatalf("Expected %#v, got %#v", errHashKeyNotSet, err) + emptyKeys := [][]byte{ + nil, + []byte(""), } - if err2, ok := err.(Error); !ok || !err2.IsUsage() { - t.Errorf("Expected missing hash key to be IsUsage(); was %#v", err) + + for _, key := range emptyKeys { + s1 := New(key, nil) + + var dst []byte + err := s1.Decode("sid", "value", &dst) + if err != errHashKeyNotSet { + t.Fatalf("Expected %#v, got %#v", errHashKeyNotSet, err) + } + if err2, ok := err.(Error); !ok || !err2.IsUsage() { + t.Errorf("Expected missing hash key to be IsUsage(); was %#v", err) + } } } @@ -299,3 +308,37 @@ func TestCustomType(t *testing.T) { t.Fatalf("Expected %#v, got %#v", src, dst) } } + +type Cookie struct { + B bool + I int + S string +} + +func FuzzEncodeDecode(f *testing.F) { + fuzzer := fuzz.New() + s1 := New([]byte("12345"), []byte("1234567890123456")) + s1.maxLength = 0 + + for i := 0; i < 100000; i++ { + var c Cookie + fuzzer.Fuzz(&c) + f.Add(c.B, c.I, c.S) + } + + f.Fuzz(func(t *testing.T, b bool, i int, s string) { + c := Cookie{b, i, s} + encoded, err := s1.Encode("sid", c) + if err != nil { + t.Errorf("Encode failed: %v", err) + } + dc := Cookie{} + err = s1.Decode("sid", encoded, &dc) + if err != nil { + t.Errorf("Decode failed: %v", err) + } + if dc != c { + t.Fatalf("Expected %v, got %v.", s, dc) + } + }) +} |