| 1 | // Copyright (c) 2013-2017 The btcsuite developers |
| 2 | // Copyright (c) 2016-2017 The Lightning Network Developers |
| 3 | // Copyright (c) 2019 The age Authors |
| 4 | // |
| 5 | // Permission to use, copy, modify, and distribute this software for any |
| 6 | // purpose with or without fee is hereby granted, provided that the above |
| 7 | // copyright notice and this permission notice appear in all copies. |
| 8 | // |
| 9 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | |
| 17 | package bech32_test |
| 18 | |
| 19 | import ( |
| 20 | "strings" |
| 21 | "testing" |
| 22 | |
| 23 | "filippo.io/age/internal/bech32" |
| 24 | ) |
| 25 | |
| 26 | func TestBech32(t *testing.T) { |
| 27 | tests := []struct { |
| 28 | str string |
| 29 | valid bool |
| 30 | }{ |
| 31 | {"A12UEL5L", true}, // empty |
| 32 | {"a12uel5l", true}, |
| 33 | {"an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", true}, |
| 34 | {"abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", true}, |
| 35 | {"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", true}, |
| 36 | {"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", true}, |
| 37 | |
| 38 | // invalid checksum |
| 39 | {"split1checkupstagehandshakeupstreamerranterredcaperred2y9e2w", false}, |
| 40 | // invalid character (space) in hrp |
| 41 | {"s lit1checkupstagehandshakeupstreamerranterredcaperredp8hs2p", false}, |
| 42 | {"split1cheo2y9e2w", false}, // invalid character (o) in data part |
| 43 | {"split1a2y9w", false}, // too short data part |
| 44 | {"1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, // empty hrp |
| 45 | // invalid character (DEL) in hrp |
| 46 | {"spl" + string(rune(127)) + "t1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, |
| 47 | |
| 48 | // long vectors that we do accept despite the spec, see Issue 453 |
| 49 | {"long10pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7r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enumber1andtheexcludedcharactersbio1569pvx", true}, |
| 51 | |
| 52 | // BIP 173 invalid vectors. |
| 53 | {"pzry9x0s0muk", false}, |
| 54 | {"1pzry9x0s0muk", false}, |
| 55 | {"x1b4n0q5v", false}, |
| 56 | {"li1dgmt3", false}, |
| 57 | {"de1lg7wt\xff", false}, |
| 58 | {"A1G7SGD8", false}, |
| 59 | {"10a06t8", false}, |
| 60 | {"1qzzfhee", false}, |
| 61 | } |
| 62 | |
| 63 | for _, test := range tests { |
| 64 | str := test.str |
| 65 | hrp, decoded, err := bech32.Decode(str) |
| 66 | if !test.valid { |
| 67 | // Invalid string decoding should result in error. |
| 68 | if err == nil { |
| 69 | t.Errorf("expected decoding to fail for invalid string %v", test.str) |
| 70 | } |
| 71 | continue |
| 72 | } |
| 73 | |
| 74 | // Valid string decoding should result in no error. |
| 75 | if err != nil { |
| 76 | t.Errorf("expected string to be valid bech32: %v", err) |
| 77 | } |
| 78 | |
| 79 | // Check that it encodes to the same string. |
| 80 | encoded, err := bech32.Encode(hrp, decoded) |
| 81 | if err != nil { |
| 82 | t.Errorf("encoding failed: %v", err) |
| 83 | } |
| 84 | if encoded != str { |
| 85 | t.Errorf("expected data to encode to %v, but got %v", str, encoded) |
| 86 | } |
| 87 | |
| 88 | // Flip a bit in the string an make sure it is caught. |
| 89 | pos := strings.LastIndexAny(str, "1") |
| 90 | flipped := str[:pos+1] + string((str[pos+1] ^ 1)) + str[pos+2:] |
| 91 | if _, _, err = bech32.Decode(flipped); err == nil { |
| 92 | t.Error("expected decoding to fail") |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestDecodeShortDataPart(t *testing.T) { |
| 98 | kelvin := string(rune(0x212A)) |
| 99 | for _, s := range []string{ |
| 100 | "AA3100AC" + kelvin, |
| 101 | "BK1" + kelvin + "0JFM", |
| 102 | "AQM1KZCML", |
| 103 | } { |
| 104 | func() { |
| 105 | defer func() { |
| 106 | if r := recover(); r != nil { |
| 107 | t.Errorf("Decode(%+q) panicked: %v", s, r) |
| 108 | } |
| 109 | }() |
| 110 | if _, _, err := bech32.Decode(s); err == nil { |
| 111 | t.Errorf("Decode(%+q) = nil error, want error", s) |
| 112 | } |
| 113 | }() |
| 114 | } |
| 115 | } |