github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/security/crypto/bech32/bech32_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  // Copyright (c) 2013-2017 The btcsuite developers
    19  // Copyright (c) 2016-2017 The Lightning Network Developers
    20  // Copyright (c) 2019 Google LLC
    21  //
    22  // Permission to use, copy, modify, and distribute this software for any
    23  // purpose with or without fee is hereby granted, provided that the above
    24  // copyright notice and this permission notice appear in all copies.
    25  //
    26  // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    27  // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    28  // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    29  // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    30  // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    31  // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    32  // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    33  
    34  package bech32_test
    35  
    36  import (
    37  	"strings"
    38  	"testing"
    39  
    40  	"github.com/zntrio/harp/v2/pkg/sdk/security/crypto/bech32"
    41  )
    42  
    43  func TestBech32(t *testing.T) {
    44  	tests := []struct {
    45  		str   string
    46  		valid bool
    47  	}{
    48  		{"A12UEL5L", true},
    49  		{"a12uel5l", true},
    50  		{"an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", true},
    51  		{"abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", true},
    52  		{"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", true},
    53  		{"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", true},
    54  
    55  		// invalid checksum
    56  		{"split1checkupstagehandshakeupstreamerranterredcaperred2y9e2w", false},
    57  		// invalid character (space) in hrp
    58  		{"s lit1checkupstagehandshakeupstreamerranterredcaperredp8hs2p", false},
    59  		{"split1cheo2y9e2w", false}, // invalid character (o) in data part
    60  		{"split1a2y9w", false},      // too short data part
    61  		{"1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, // empty hrp
    62  		// invalid character (DEL) in hrp
    63  		{"spl" + string(rune(127)) + "t1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false},
    64  		// too long
    65  		{"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", false},
    66  
    67  		// BIP 173 invalid vectors.
    68  		{"an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", false},
    69  		{"pzry9x0s0muk", false},
    70  		{"1pzry9x0s0muk", false},
    71  		{"x1b4n0q5v", false},
    72  		{"li1dgmt3", false},
    73  		{"de1lg7wt\xff", false},
    74  		{"A1G7SGD8", false},
    75  		{"10a06t8", false},
    76  		{"1qzzfhee", false},
    77  	}
    78  
    79  	for _, test := range tests {
    80  		str := test.str
    81  		hrp, decoded, err := bech32.Decode(str)
    82  		if !test.valid {
    83  			// Invalid string decoding should result in error.
    84  			if err == nil {
    85  				t.Errorf("expected decoding to fail for invalid string %v", test.str)
    86  			}
    87  			continue
    88  		}
    89  
    90  		// Valid string decoding should result in no error.
    91  		if err != nil {
    92  			t.Errorf("expected string to be valid bech32: %v", err)
    93  		}
    94  
    95  		// Check that it encodes to the same string.
    96  		encoded, err := bech32.Encode(hrp, decoded)
    97  		if err != nil {
    98  			t.Errorf("encoding failed: %v", err)
    99  		}
   100  		if encoded != str {
   101  			t.Errorf("expected data to encode to %v, but got %v", str, encoded)
   102  		}
   103  
   104  		// Flip a bit in the string an make sure it is caught.
   105  		pos := strings.LastIndexAny(str, "1")
   106  		flipped := str[:pos+1] + string((str[pos+1] ^ 1)) + str[pos+2:]
   107  		if _, _, err = bech32.Decode(flipped); err == nil {
   108  			t.Error("expected decoding to fail")
   109  		}
   110  	}
   111  }