gf/encoding/gbase64/gbase64_test.go

97 lines
2.3 KiB
Go
Raw Normal View History

2019-04-09 19:12:48 +08:00
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2019-04-09 19:12:48 +08:00
package gbase64_test
import (
"github.com/gogf/gf/debug/gdebug"
2019-07-02 17:30:18 +08:00
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/encoding/gbase64"
"github.com/gogf/gf/test/gtest"
2019-04-09 19:12:48 +08:00
)
2019-09-24 20:39:00 +08:00
type testPair struct {
2019-04-09 19:12:48 +08:00
decoded, encoded string
}
2019-09-24 20:39:00 +08:00
var pairs = []testPair{
2019-04-09 19:12:48 +08:00
// RFC 3548 examples
{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
{"\x14\xfb\x9c\x03", "FPucAw=="},
// RFC 4648 examples
{"", ""},
{"f", "Zg=="},
{"fo", "Zm8="},
{"foo", "Zm9v"},
{"foob", "Zm9vYg=="},
{"fooba", "Zm9vYmE="},
{"foobar", "Zm9vYmFy"},
// Wikipedia examples
{"sure.", "c3VyZS4="},
{"sure", "c3VyZQ=="},
{"sur", "c3Vy"},
{"su", "c3U="},
{"leasure.", "bGVhc3VyZS4="},
{"easure.", "ZWFzdXJlLg=="},
{"asure.", "YXN1cmUu"},
{"sure.", "c3VyZS4="},
}
func Test_Basic(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-07-02 17:30:18 +08:00
for k := range pairs {
2019-09-24 20:39:00 +08:00
// Encode
2020-03-19 22:56:12 +08:00
t.Assert(gbase64.Encode([]byte(pairs[k].decoded)), []byte(pairs[k].encoded))
t.Assert(gbase64.EncodeToString([]byte(pairs[k].decoded)), pairs[k].encoded)
t.Assert(gbase64.EncodeString(pairs[k].decoded), pairs[k].encoded)
2019-04-09 19:12:48 +08:00
2019-09-24 20:39:00 +08:00
// Decode
r1, _ := gbase64.Decode([]byte(pairs[k].encoded))
2020-03-19 22:56:12 +08:00
t.Assert(r1, []byte(pairs[k].decoded))
2019-09-24 20:39:00 +08:00
r2, _ := gbase64.DecodeString(pairs[k].encoded)
2020-03-19 22:56:12 +08:00
t.Assert(r2, []byte(pairs[k].decoded))
2019-09-24 20:39:00 +08:00
r3, _ := gbase64.DecodeToString(pairs[k].encoded)
2020-03-19 22:56:12 +08:00
t.Assert(r3, pairs[k].decoded)
2019-07-02 17:30:18 +08:00
}
})
2019-04-09 19:12:48 +08:00
}
func Test_File(t *testing.T) {
path := gdebug.TestDataPath("test")
expect := "dGVzdA=="
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
b, err := gbase64.EncodeFile(path)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(string(b), expect)
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
s, err := gbase64.EncodeFileToString(path)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(s, expect)
})
}
func Test_File_Error(t *testing.T) {
path := "none-exist-file"
expect := ""
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
b, err := gbase64.EncodeFile(path)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
t.Assert(string(b), expect)
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
s, err := gbase64.EncodeFileToString(path)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
t.Assert(s, expect)
})
}