aboutsummaryrefslogtreecommitdiff
path: root/securecookie_test.go
diff options
context:
space:
mode:
authorCorey Daley <cdaley@redhat.com>2023-07-31 22:18:18 +0300
committerGitHub <noreply@github.com>2023-07-31 22:18:18 +0300
commit22eae5c820537b29d36814c94c2c70c08e391d71 (patch)
tree3d2c4c7aa6c13b9c877cc9c4f41175a4650c71a9 /securecookie_test.go
parent4ce52525b6a4243a02356b62abf6b2ef9038ff52 (diff)
Update go version & add verification/testing tools (#81)
<!-- For Work In Progress Pull Requests, please use the Draft PR feature, see https://github.blog/2019-02-14-introducing-draft-pull-requests/ for further details. For a timely review/response, please avoid force-pushing additional commits if your PR already received reviews or comments. Before submitting a Pull Request, please ensure that you have: - 📖 Read the Contributing guide: https://github.com/gorilla/.github/blob/main/CONTRIBUTING.md - 📖 Read the Code of Conduct: https://github.com/gorilla/.github/blob/main/CODE_OF_CONDUCT.md - Provide tests for your changes. - Use descriptive commit messages. - Comment your code where appropriate. - Squash your commits - Update any related documentation. - Add gorilla/pull-request-reviewers as a Reviewer --> ## What type of PR is this? (check all applicable) - [ ] Refactor - [ ] Feature - [ ] Bug Fix - [x] Optimization - [ ] Documentation Update ## Description ## Related Tickets & Documents <!-- For pull requests that relate or close an issue, please include them below. We like to follow [Github's guidance on linking issues to pull requests](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue). For example having the text: "closes #1234" would connect the current pull request to issue 1234. And when we merge the pull request, Github will automatically close the issue. --> - Related Issue # - Closes # ## Added/updated tests? - [ ] Yes - [ ] No, and this is why: _please replace this line with details on why tests have not been included_ - [ ] I need help with writing tests ## Run verifications and test - [ ] `make verify` is passing - [ ] `make test` is passing
Diffstat (limited to 'securecookie_test.go')
-rw-r--r--securecookie_test.go38
1 files changed, 37 insertions, 1 deletions
diff --git a/securecookie_test.go b/securecookie_test.go
index c32ff33..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)
@@ -306,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)
+ }
+ })
+}