8 func TestBech32(t *testing.T) {
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
27 for _, test := range tests {
29 hrp, decoded, err := Bech32Decode(str)
31 // Invalid string decoding should result in error.
33 t.Error("expected decoding to fail for "+
34 "invalid string %v", test.str)
39 // Valid string decoding should result in no error.
41 t.Errorf("expected string to be valid bech32: %v", err)
44 // Check that it encodes to the same string
45 encoded, err := Bech32Encode(hrp, decoded)
47 t.Errorf("encoding failed: %v", err)
50 if encoded != strings.ToLower(str) {
51 t.Errorf("expected data to encode to %v, but got %v",
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)
60 t.Error("expected decoding to fail")