Skip to content

Commit

Permalink
[bugfix] Fix NopDecoder
Browse files Browse the repository at this point in the history
* Fixes #41
  • Loading branch information
Craig Peterson authored and elithrar committed Oct 3, 2016
1 parent c13558c commit fa5329f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
8 changes: 4 additions & 4 deletions securecookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var (
errTimestampExpired = cookieError{typ: decodeError, msg: "expired timestamp"}
errDecryptionFailed = cookieError{typ: decodeError, msg: "the value could not be decrypted"}
errValueNotByte = cookieError{typ: decodeError, msg: "value not a []byte."}
errValueNotBytePtr = cookieError{typ: decodeError, msg: "value not a pointer to []byte."}

// ErrMacInvalid indicates that cookie decoding failed because the HMAC
// could not be extracted and verified. Direct use of this error
Expand Down Expand Up @@ -474,12 +475,11 @@ func (e NopEncoder) Serialize(src interface{}) ([]byte, error) {

// Deserialize passes a []byte through as-is.
func (e NopEncoder) Deserialize(src []byte, dst interface{}) error {
if _, ok := dst.([]byte); ok {
dst = src
if dat, ok := dst.(*[]byte); ok {
*dat = src
return nil
}

return errValueNotByte
return errValueNotBytePtr
}

// Encoding -------------------------------------------------------------------
Expand Down
27 changes: 27 additions & 0 deletions securecookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,33 @@ func TestJSONSerialization(t *testing.T) {
}
}

func TestNopSerialization(t *testing.T) {
cookieData := "fooobar123"
sz := NopEncoder{}

if _, err := sz.Serialize(cookieData); err != errValueNotByte {
t.Fatal("Expected error passing string")
}
dat, err := sz.Serialize([]byte(cookieData))
if err != nil {
t.Fatal(err)
}
if (string(dat)) != cookieData {
t.Fatal("Expected serialized data to be same as source")
}

var dst []byte
if err = sz.Deserialize(dat, dst); err != errValueNotBytePtr {
t.Fatal("Expect error unless you pass a *[]byte")
}
if err = sz.Deserialize(dat, &dst); err != nil {
t.Fatal(err)
}
if (string(dst)) != cookieData {
t.Fatal("Expected deserialized data to be same as source")
}
}

func TestEncoding(t *testing.T) {
for _, value := range testStrings {
encoded := encode([]byte(value))
Expand Down

0 comments on commit fa5329f

Please sign in to comment.