github.com/zhongdalu/gf@v1.0.0/g/encoding/gbase64/gbase64_test.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 package gbase64_test 7 8 import ( 9 "testing" 10 11 "github.com/zhongdalu/gf/g/encoding/gbase64" 12 "github.com/zhongdalu/gf/g/test/gtest" 13 ) 14 15 type testpair struct { 16 decoded, encoded string 17 } 18 19 var pairs = []testpair{ 20 // RFC 3548 examples 21 {"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"}, 22 {"\x14\xfb\x9c\x03\xd9", "FPucA9k="}, 23 {"\x14\xfb\x9c\x03", "FPucAw=="}, 24 25 // RFC 4648 examples 26 {"", ""}, 27 {"f", "Zg=="}, 28 {"fo", "Zm8="}, 29 {"foo", "Zm9v"}, 30 {"foob", "Zm9vYg=="}, 31 {"fooba", "Zm9vYmE="}, 32 {"foobar", "Zm9vYmFy"}, 33 34 // Wikipedia examples 35 {"sure.", "c3VyZS4="}, 36 {"sure", "c3VyZQ=="}, 37 {"sur", "c3Vy"}, 38 {"su", "c3U="}, 39 {"leasure.", "bGVhc3VyZS4="}, 40 {"easure.", "ZWFzdXJlLg=="}, 41 {"asure.", "YXN1cmUu"}, 42 {"sure.", "c3VyZS4="}, 43 } 44 45 func TestBase64(t *testing.T) { 46 gtest.Case(t, func() { 47 for k := range pairs { 48 // []byte 49 gtest.Assert(gbase64.Encode([]byte(pairs[k].decoded)), []byte(pairs[k].encoded)) 50 e1, _ := gbase64.Decode([]byte(pairs[k].encoded)) 51 gtest.Assert(e1, []byte(pairs[k].decoded)) 52 53 // string 54 gtest.Assert(gbase64.EncodeString([]byte(pairs[k].decoded)), pairs[k].encoded) 55 e2, _ := gbase64.DecodeString(pairs[k].encoded) 56 gtest.Assert(e2, []byte(pairs[k].decoded)) 57 } 58 }) 59 }