...

Source file src/github.com/decred/dcrd/dcrec/secp256k1/v4/error_test.go

Documentation: github.com/decred/dcrd/dcrec/secp256k1/v4

     1  // Copyright (c) 2020 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package secp256k1
     6  
     7  import (
     8  	"errors"
     9  	"testing"
    10  )
    11  
    12  // TestErrorKindStringer tests the stringized output for the ErrorKind type.
    13  func TestErrorKindStringer(t *testing.T) {
    14  	tests := []struct {
    15  		in   ErrorKind
    16  		want string
    17  	}{
    18  		{ErrPubKeyInvalidLen, "ErrPubKeyInvalidLen"},
    19  		{ErrPubKeyInvalidFormat, "ErrPubKeyInvalidFormat"},
    20  		{ErrPubKeyXTooBig, "ErrPubKeyXTooBig"},
    21  		{ErrPubKeyYTooBig, "ErrPubKeyYTooBig"},
    22  		{ErrPubKeyNotOnCurve, "ErrPubKeyNotOnCurve"},
    23  		{ErrPubKeyMismatchedOddness, "ErrPubKeyMismatchedOddness"},
    24  	}
    25  
    26  	for i, test := range tests {
    27  		result := test.in.Error()
    28  		if result != test.want {
    29  			t.Errorf("#%d: got: %s want: %s", i, result, test.want)
    30  			continue
    31  		}
    32  	}
    33  }
    34  
    35  // TestError tests the error output for the Error type.
    36  func TestError(t *testing.T) {
    37  	tests := []struct {
    38  		in   Error
    39  		want string
    40  	}{{
    41  		Error{Description: "some error"},
    42  		"some error",
    43  	}, {
    44  		Error{Description: "human-readable error"},
    45  		"human-readable error",
    46  	}}
    47  
    48  	for i, test := range tests {
    49  		result := test.in.Error()
    50  		if result != test.want {
    51  			t.Errorf("#%d: got: %s want: %s", i, result, test.want)
    52  			continue
    53  		}
    54  	}
    55  }
    56  
    57  // TestErrorKindIsAs ensures both ErrorKind and Error can be identified as being
    58  // a specific error kind via errors.Is and unwrapped via errors.As.
    59  func TestErrorKindIsAs(t *testing.T) {
    60  	tests := []struct {
    61  		name      string
    62  		err       error
    63  		target    error
    64  		wantMatch bool
    65  		wantAs    ErrorKind
    66  	}{{
    67  		name:      "ErrPubKeyInvalidLen == ErrPubKeyInvalidLen",
    68  		err:       ErrPubKeyInvalidLen,
    69  		target:    ErrPubKeyInvalidLen,
    70  		wantMatch: true,
    71  		wantAs:    ErrPubKeyInvalidLen,
    72  	}, {
    73  		name:      "Error.ErrPubKeyInvalidLen == ErrPubKeyInvalidLen",
    74  		err:       makeError(ErrPubKeyInvalidLen, ""),
    75  		target:    ErrPubKeyInvalidLen,
    76  		wantMatch: true,
    77  		wantAs:    ErrPubKeyInvalidLen,
    78  	}, {
    79  		name:      "Error.ErrPubKeyInvalidLen == Error.ErrPubKeyInvalidLen",
    80  		err:       makeError(ErrPubKeyInvalidLen, ""),
    81  		target:    makeError(ErrPubKeyInvalidLen, ""),
    82  		wantMatch: true,
    83  		wantAs:    ErrPubKeyInvalidLen,
    84  	}, {
    85  		name:      "ErrPubKeyInvalidFormat != ErrPubKeyInvalidLen",
    86  		err:       ErrPubKeyInvalidFormat,
    87  		target:    ErrPubKeyInvalidLen,
    88  		wantMatch: false,
    89  		wantAs:    ErrPubKeyInvalidFormat,
    90  	}, {
    91  		name:      "Error.ErrPubKeyInvalidFormat != ErrPubKeyInvalidLen",
    92  		err:       makeError(ErrPubKeyInvalidFormat, ""),
    93  		target:    ErrPubKeyInvalidLen,
    94  		wantMatch: false,
    95  		wantAs:    ErrPubKeyInvalidFormat,
    96  	}, {
    97  		name:      "ErrPubKeyInvalidFormat != Error.ErrPubKeyInvalidLen",
    98  		err:       ErrPubKeyInvalidFormat,
    99  		target:    makeError(ErrPubKeyInvalidLen, ""),
   100  		wantMatch: false,
   101  		wantAs:    ErrPubKeyInvalidFormat,
   102  	}, {
   103  		name:      "Error.ErrPubKeyInvalidFormat != Error.ErrPubKeyInvalidLen",
   104  		err:       makeError(ErrPubKeyInvalidFormat, ""),
   105  		target:    makeError(ErrPubKeyInvalidLen, ""),
   106  		wantMatch: false,
   107  		wantAs:    ErrPubKeyInvalidFormat,
   108  	}}
   109  
   110  	for _, test := range tests {
   111  		// Ensure the error matches or not depending on the expected result.
   112  		result := errors.Is(test.err, test.target)
   113  		if result != test.wantMatch {
   114  			t.Errorf("%s: incorrect error identification -- got %v, want %v",
   115  				test.name, result, test.wantMatch)
   116  			continue
   117  		}
   118  
   119  		// Ensure the underlying error code can be unwrapped and is the expected
   120  		// code.
   121  		var kind ErrorKind
   122  		if !errors.As(test.err, &kind) {
   123  			t.Errorf("%s: unable to unwrap to error code", test.name)
   124  			continue
   125  		}
   126  		if kind != test.wantAs {
   127  			t.Errorf("%s: unexpected unwrapped error code -- got %v, want %v",
   128  				test.name, kind, test.wantAs)
   129  			continue
   130  		}
   131  	}
   132  }
   133  

View as plain text