github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/others/bech32/bech32_test.go (about)

     1  package bech32
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  var (
     9  	valid_checksum_bech32 = []string{
    10  		"A12UEL5L",
    11  		"a12uel5l",
    12  		"an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
    13  		"abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
    14  		"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j",
    15  		"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
    16  		"?1ezyfcl",
    17  	}
    18  
    19  	valid_checksum_bech32m = []string{
    20  		"A1LQFN3A",
    21  		"a1lqfn3a",
    22  		"an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11sg7hg6",
    23  		"abcdef1l7aum6echk45nj3s0wdvt2fg8x9yrzpqzd3ryx",
    24  		"11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllludsr8",
    25  		"split1checkupstagehandshakeupstreamerranterredcaperredlc445v",
    26  		"?1v759aa",
    27  	}
    28  
    29  	invalid_checksum_bech32 = []string{
    30  		" 1nwldj5",
    31  		"\x7f1axkwrx",
    32  		"\x801eym55h",
    33  		"an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
    34  		"pzry9x0s0muk",
    35  		"1pzry9x0s0muk",
    36  		"x1b4n0q5v",
    37  		"li1dgmt3",
    38  		"de1lg7wt\xff",
    39  		"A1G7SGD8",
    40  		"10a06t8",
    41  		"1qzzfhee",
    42  	}
    43  
    44  	invalid_checksum_bech32m = []string{
    45  		" 1xj0phk",
    46  		"\x7F1g6xzxy",
    47  		"\x801vctc34",
    48  		"an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4",
    49  		"qyrz8wqd2c9m",
    50  		"1qyrz8wqd2c9m",
    51  		"y1b0jsk6g",
    52  		"lt1igcx5c0",
    53  		"in1muywd",
    54  		"mm1crxm3i",
    55  		"au1s5cgom",
    56  		"M1VUXWEZ",
    57  		"16plkw9",
    58  		"1p2gdwpf",
    59  	}
    60  )
    61  
    62  func TestValidChecksumB32(t *testing.T) {
    63  	for _, s := range valid_checksum_bech32 {
    64  		hrp, data, bech32m := Decode(s)
    65  		if data == nil || hrp == "" || bech32m {
    66  			t.Error("Decode fails: ", s)
    67  		} else {
    68  			rebuild := Encode(hrp, data, false)
    69  			if rebuild == "" {
    70  				t.Error("Encode fails: ", s)
    71  			} else {
    72  				if !strings.EqualFold(s, rebuild) {
    73  					t.Error("Encode produces incorrect result: ", s)
    74  				}
    75  			}
    76  		}
    77  	}
    78  }
    79  
    80  func TestValidChecksumB32m(t *testing.T) {
    81  	for _, s := range valid_checksum_bech32m {
    82  		hrp, data, bech32m := Decode(s)
    83  		if data == nil || hrp == "" || !bech32m {
    84  			t.Error("Decode fails: ", s)
    85  		} else {
    86  			rebuild := Encode(hrp, data, true)
    87  			if rebuild == "" {
    88  				t.Error("Encode fails: ", s)
    89  			} else {
    90  				if !strings.EqualFold(s, rebuild) {
    91  					t.Error("Encode produces incorrect result: ", s)
    92  				}
    93  			}
    94  		}
    95  	}
    96  }
    97  
    98  func TestInvalidChecksumB32(t *testing.T) {
    99  	for _, s := range invalid_checksum_bech32 {
   100  		hrp, data, bech32m := Decode(s)
   101  		if data != nil || hrp != "" || bech32m {
   102  			t.Error("Decode succeeds on invalid string: ", s)
   103  		}
   104  	}
   105  }
   106  
   107  func TestInvalidChecksumB32m(t *testing.T) {
   108  	for _, s := range invalid_checksum_bech32m {
   109  		hrp, data, bech32m := Decode(s)
   110  		if data != nil || hrp != "" || bech32m {
   111  			t.Error("Decode succeeds on invalid string: ", s)
   112  		}
   113  	}
   114  }