github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/basen/basen_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Copyright (c) 2014 Casey Marshall. See LICENSE file for details. 6 7 package basen_test 8 9 import ( 10 "testing" 11 12 "github.com/turingchain2020/turingchain/wallet/bipwallet/basen" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 /* 17 func Test(t *testing.T) { gc.TestingT(t) } 18 19 type Suite struct{} 20 21 var _ = gc.Suite(&Suite{}) 22 23 func (s *Suite) TestRoundTrip62(c *gc.C) { 24 testCases := []struct { 25 enc *basen.Encoding 26 val []byte 27 rep string 28 }{ 29 {basen.Base62, []byte{1}, "1"}, 30 {basen.Base62, []byte{61}, "z"}, 31 {basen.Base62, []byte{62}, "10"}, 32 {basen.Base62, big.NewInt(int64(3844)).Bytes(), "100"}, 33 {basen.Base62, big.NewInt(int64(3843)).Bytes(), "zz"}, 34 35 {basen.Base58, big.NewInt(int64(10002343)).Bytes(), "Tgmc"}, 36 {basen.Base58, big.NewInt(int64(1000)).Bytes(), "if"}, 37 {basen.Base58, big.NewInt(int64(0)).Bytes(), ""}, 38 } 39 40 for _, testCase := range testCases { 41 rep := testCase.enc.EncodeToString(testCase.val) 42 c.Check(rep, gc.Equals, testCase.rep) 43 44 val, err := testCase.enc.DecodeString(testCase.rep) 45 c.Assert(err, gc.IsNil) 46 c.Check(val, gc.DeepEquals, testCase.val, gc.Commentf("%s", testCase.rep)) 47 } 48 } 49 50 func (s *Suite) TestRand256(c *gc.C) { 51 for i := 0; i < 100; i++ { 52 v := basen.Base62.MustRandom(32) 53 // Should be 43 chars or less because math.log(2**256, 62) == 42.994887413002736 54 c.Assert(len(v) < 44, gc.Equals, true) 55 } 56 } 57 58 func (s *Suite) TestStringN(c *gc.C) { 59 var val []byte 60 var err error 61 62 val, err = basen.Base58.DecodeStringN("", 4) 63 c.Assert(err, gc.IsNil) 64 c.Assert(val, gc.DeepEquals, []byte{0, 0, 0, 0}) 65 66 // ensure round-trip with padding is right 67 val, err = basen.Base62.DecodeStringN("10", 4) 68 c.Assert(err, gc.IsNil) 69 c.Assert(val, gc.DeepEquals, []byte{0, 0, 0, 62}) 70 rep := basen.Base62.EncodeToString(val) 71 c.Assert(rep, gc.Equals, "10") 72 } 73 74 func (s *Suite) TestNoMultiByte(c *gc.C) { 75 c.Assert(func() { basen.NewEncoding("世界") }, gc.PanicMatches, 76 "multi-byte characters not supported") 77 } 78 */ 79 80 func TestBase58(t *testing.T) { 81 s := basen.Base58.MustRandom(10) 82 assert.False(t, len(s) == 0) 83 84 b, err := basen.Base58.DecodeString(s) 85 assert.NoError(t, err) 86 assert.True(t, len(b) <= 10) 87 s = basen.Base58.EncodeToString([]byte{0, 1, 2, 3, 4, 5}) 88 b, err = basen.Base58.DecodeString(s) 89 assert.NoError(t, err) 90 assert.Equal(t, 5, len(b)) 91 b, err = basen.Base58.DecodeStringN(s, 12) 92 assert.NoError(t, err) 93 assert.Equal(t, 12, len(b)) 94 95 assert.True(t, basen.Base58.Base() == 58) 96 }