update tests

This commit is contained in:
Bobby DeSimone 2019-07-12 15:46:05 -07:00
parent bade7461ca
commit 5b2f6ecd2f
No known key found for this signature in database
GPG key ID: AEE4CF12FE86D07E
2 changed files with 24 additions and 31 deletions

View file

@ -130,22 +130,22 @@ func (s *CookieStore) makeCSRFCookie(req *http.Request, value string, expiration
func (s *CookieStore) SetCookie(w http.ResponseWriter, cookie *http.Cookie) { func (s *CookieStore) SetCookie(w http.ResponseWriter, cookie *http.Cookie) {
if len(cookie.String()) <= MaxChunkSize { if len(cookie.String()) <= MaxChunkSize {
http.SetCookie(w, cookie) http.SetCookie(w, cookie)
} else { return
chunks := chunk(cookie.Value, MaxChunkSize) }
for i, c := range chunks { chunks := chunk(cookie.Value, MaxChunkSize)
// start with a copy of our original cookie for i, c := range chunks {
nc := *cookie // start with a copy of our original cookie
if i == 0 { nc := *cookie
// if this is the first cookie, add our canary byte if i == 0 {
nc.Value = fmt.Sprintf("%s%s", string(ChunkedCanaryByte), c) // if this is the first cookie, add our canary byte
} else { nc.Value = fmt.Sprintf("%s%s", string(ChunkedCanaryByte), c)
// subsequent parts will be postfixed with their part number } else {
nc.Name = fmt.Sprintf("%s_%d", cookie.Name, i) // subsequent parts will be postfixed with their part number
nc.Value = fmt.Sprintf("%s", c) nc.Name = fmt.Sprintf("%s_%d", cookie.Name, i)
} nc.Value = fmt.Sprintf("%s", c)
log.Info().Interface("new cookie", nc).Msg("SetCookie: chunked")
http.SetCookie(w, &nc)
} }
log.Info().Interface("new cookie", nc).Msg("SetCookie: chunked")
http.SetCookie(w, &nc)
} }
} }

View file

@ -1,7 +1,9 @@
package sessions package sessions
import ( import (
"crypto/rand"
"errors" "errors"
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
@ -204,6 +206,10 @@ func TestCookieStore_SaveSession(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
hugeString := make([]byte, 4097)
if _, err := rand.Read(hugeString); err != nil {
t.Fatal(err)
}
tests := []struct { tests := []struct {
name string name string
sessionState *SessionState sessionState *SessionState
@ -211,22 +217,9 @@ func TestCookieStore_SaveSession(t *testing.T) {
wantErr bool wantErr bool
wantLoadErr bool wantLoadErr bool
}{ }{
{"good", {"good", &SessionState{AccessToken: "token1234", RefreshToken: "refresh4321", RefreshDeadline: time.Now().Add(1 * time.Hour).Truncate(time.Second).UTC(), Email: "user@domain.com", User: "user"}, cipher, false, false},
&SessionState{ {"bad cipher", &SessionState{AccessToken: "token1234", RefreshToken: "refresh4321", RefreshDeadline: time.Now().Add(1 * time.Hour).Truncate(time.Second).UTC(), Email: "user@domain.com", User: "user"}, mockCipher{}, true, true},
AccessToken: "token1234", {"huge cookie", &SessionState{AccessToken: fmt.Sprintf("%x", hugeString), RefreshToken: "refresh4321", RefreshDeadline: time.Now().Add(1 * time.Hour).Truncate(time.Second).UTC(), Email: "user@domain.com", User: "user"}, cipher, false, false},
RefreshToken: "refresh4321",
RefreshDeadline: time.Now().Add(1 * time.Hour).Truncate(time.Second).UTC(),
Email: "user@domain.com",
User: "user",
}, cipher, false, false},
{"bad cipher",
&SessionState{
AccessToken: "token1234",
RefreshToken: "refresh4321",
RefreshDeadline: time.Now().Add(1 * time.Hour).Truncate(time.Second).UTC(),
Email: "user@domain.com",
User: "user",
}, mockCipher{}, true, true},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {