OSDN Git Service

new repo
[bytom/vapor.git] / common / bech32 / bech32_test.go
1 package bech32
2
3 import (
4         "strings"
5         "testing"
6 )
7
8 func TestBech32(t *testing.T) {
9         tests := []struct {
10                 str   string
11                 valid bool
12         }{
13                 {"A12UEL5L", true},
14                 {"an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", true},
15                 {"abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", true},
16                 {"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", true},
17                 {"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", true},
18                 {"split1checkupstagehandshakeupstreamerranterredcaperred2y9e2w", false},                                // invalid checksum
19                 {"s lit1checkupstagehandshakeupstreamerranterredcaperredp8hs2p", false},                                // invalid character (space) in hrp
20                 {"spl" + string(127) + "t1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false},              // invalid character (DEL) in hrp
21                 {"split1cheo2y9e2w", false},                                                                            // invalid character (o) in data part
22                 {"split1a2y9w", false},                                                                                 // too short data part
23                 {"1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false},                                     // empty hrp
24                 {"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", false}, // too long
25         }
26
27         for _, test := range tests {
28                 str := test.str
29                 hrp, decoded, err := Bech32Decode(str)
30                 if !test.valid {
31                         // Invalid string decoding should result in error.
32                         if err == nil {
33                                 t.Error("expected decoding to fail for "+
34                                         "invalid string %v", test.str)
35                         }
36                         continue
37                 }
38
39                 // Valid string decoding should result in no error.
40                 if err != nil {
41                         t.Errorf("expected string to be valid bech32: %v", err)
42                 }
43
44                 // Check that it encodes to the same string
45                 encoded, err := Bech32Encode(hrp, decoded)
46                 if err != nil {
47                         t.Errorf("encoding failed: %v", err)
48                 }
49
50                 if encoded != strings.ToLower(str) {
51                         t.Errorf("expected data to encode to %v, but got %v",
52                                 str, encoded)
53                 }
54
55                 // Flip a bit in the string an make sure it is caught.
56                 pos := strings.LastIndexAny(str, "1")
57                 flipped := str[:pos+1] + string((str[pos+1] ^ 1)) + str[pos+2:]
58                 _, _, err = Bech32Decode(flipped)
59                 if err == nil {
60                         t.Error("expected decoding to fail")
61                 }
62         }
63 }