github.com/wangyougui/gf/v2@v2.6.5/encoding/gbase64/gbase64_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/wangyougui/gf.
     6  
     7  package gbase64_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/encoding/gbase64"
    13  	"github.com/wangyougui/gf/v2/test/gtest"
    14  )
    15  
    16  type testPair struct {
    17  	decoded, encoded string
    18  }
    19  
    20  var pairs = []testPair{
    21  	// RFC 3548 examples
    22  	{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
    23  	{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
    24  	{"\x14\xfb\x9c\x03", "FPucAw=="},
    25  
    26  	// RFC 4648 examples
    27  	{"", ""},
    28  	{"f", "Zg=="},
    29  	{"fo", "Zm8="},
    30  	{"foo", "Zm9v"},
    31  	{"foob", "Zm9vYg=="},
    32  	{"fooba", "Zm9vYmE="},
    33  	{"foobar", "Zm9vYmFy"},
    34  
    35  	// Wikipedia examples
    36  	{"sure.", "c3VyZS4="},
    37  	{"sure", "c3VyZQ=="},
    38  	{"sur", "c3Vy"},
    39  	{"su", "c3U="},
    40  	{"leasure.", "bGVhc3VyZS4="},
    41  	{"easure.", "ZWFzdXJlLg=="},
    42  	{"asure.", "YXN1cmUu"},
    43  	{"sure.", "c3VyZS4="},
    44  }
    45  
    46  func Test_Basic(t *testing.T) {
    47  	gtest.C(t, func(t *gtest.T) {
    48  		for k := range pairs {
    49  			// Encode
    50  			t.Assert(gbase64.Encode([]byte(pairs[k].decoded)), []byte(pairs[k].encoded))
    51  			t.Assert(gbase64.EncodeToString([]byte(pairs[k].decoded)), pairs[k].encoded)
    52  			t.Assert(gbase64.EncodeString(pairs[k].decoded), pairs[k].encoded)
    53  
    54  			// Decode
    55  			r1, _ := gbase64.Decode([]byte(pairs[k].encoded))
    56  			t.Assert(r1, []byte(pairs[k].decoded))
    57  
    58  			r2, _ := gbase64.DecodeString(pairs[k].encoded)
    59  			t.Assert(r2, []byte(pairs[k].decoded))
    60  
    61  			r3, _ := gbase64.DecodeToString(pairs[k].encoded)
    62  			t.Assert(r3, pairs[k].decoded)
    63  		}
    64  	})
    65  }
    66  
    67  func Test_File(t *testing.T) {
    68  	path := gtest.DataPath("test")
    69  	expect := "dGVzdA=="
    70  	gtest.C(t, func(t *gtest.T) {
    71  		b, err := gbase64.EncodeFile(path)
    72  		t.AssertNil(err)
    73  		t.Assert(string(b), expect)
    74  	})
    75  	gtest.C(t, func(t *gtest.T) {
    76  		s, err := gbase64.EncodeFileToString(path)
    77  		t.AssertNil(err)
    78  		t.Assert(s, expect)
    79  	})
    80  }
    81  
    82  func Test_File_Error(t *testing.T) {
    83  	path := "none-exist-file"
    84  	expect := ""
    85  	gtest.C(t, func(t *gtest.T) {
    86  		b, err := gbase64.EncodeFile(path)
    87  		t.AssertNE(err, nil)
    88  		t.Assert(string(b), expect)
    89  	})
    90  	gtest.C(t, func(t *gtest.T) {
    91  		s, err := gbase64.EncodeFileToString(path)
    92  		t.AssertNE(err, nil)
    93  		t.Assert(s, expect)
    94  	})
    95  }