-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix base64 error when input is multiple of 4 bytes
The base64Decode function was incorrectly re-adding padding when the input is a multiple of 4 bytes. These input lengths should have no padding, but it was adding 1, which led to an error: > illegal base64 data at input byte 4 The base64 package has options to omit padding using `WithPadding(base64.NoPadding)`[1], and also defines Raw versions of the pre-defined encoders[2]. This therefore updates the functions to use these encoders instead, which means they're now just simple wrappers. [1]https://pkg.go.dev/encoding/base64#Encoding.WithPadding [2]https://pkg.go.dev/encoding/base64#pkg-variables
- Loading branch information
Showing
2 changed files
with
39 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package itsdangerous | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestBase64(t *testing.T) { | ||
|
||
tests := []struct { | ||
value []byte | ||
encoded string | ||
}{ | ||
{value: []byte("a"), encoded: "YQ"}, | ||
{value: []byte("ab"), encoded: "YWI"}, | ||
{value: []byte("abc"), encoded: "YWJj"}, | ||
{value: []byte("abcd"), encoded: "YWJjZA"}, | ||
{value: []byte("abcde"), encoded: "YWJjZGU"}, | ||
{value: []byte("abcdef"), encoded: "YWJjZGVm"}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(string(test.value), func(t *testing.T) { | ||
actualEncoded := base64Encode(test.value) | ||
if actualEncoded != test.encoded { | ||
t.Errorf("base64Encode(%v) got %s; want %s", test.value, actualEncoded, test.encoded) | ||
} | ||
|
||
decoded, err := base64Decode(test.encoded) | ||
if err != nil { | ||
t.Errorf("base64Decode(%s) returned error: %s", test.encoded, err) | ||
} else if !reflect.DeepEqual(decoded, test.value) { | ||
t.Errorf("base64Decode(%s) got %v; want %v", test.encoded, decoded, test.value) | ||
} | ||
}) | ||
} | ||
} |