schnorrerror_test.go raw

   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 schnorr
   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  		{ErrInvalidHashLen, "ErrInvalidHashLen"},
  19  		{ErrSecretKeyIsZero, "ErrSecretKeyIsZero"},
  20  		{ErrSchnorrHashValue, "ErrSchnorrHashValue"},
  21  		{ErrPubKeyNotOnCurve, "ErrPubKeyNotOnCurve"},
  22  		{ErrSigRYIsOdd, "ErrSigRYIsOdd"},
  23  		{ErrSigRNotOnCurve, "ErrSigRNotOnCurve"},
  24  		{ErrUnequalRValues, "ErrUnequalRValues"},
  25  		{ErrSigTooShort, "ErrSigTooShort"},
  26  		{ErrSigTooLong, "ErrSigTooLong"},
  27  		{ErrSigRTooBig, "ErrSigRTooBig"},
  28  		{ErrSigSTooBig, "ErrSigSTooBig"},
  29  	}
  30  	for i, test := range tests {
  31  		result := test.in.Error()
  32  		if result != test.want {
  33  			t.Errorf("#%d: got: %s want: %s", i, result, test.want)
  34  			continue
  35  		}
  36  	}
  37  }
  38  
  39  // TestError tests the error output for the Error type.
  40  func TestError(t *testing.T) {
  41  	tests := []struct {
  42  		in   Error
  43  		want string
  44  	}{
  45  		{
  46  			Error{Description: "some error"},
  47  			"some error",
  48  		}, {
  49  			Error{Description: "human-readable error"},
  50  			"human-readable error",
  51  		},
  52  	}
  53  
  54  	for i, test := range tests {
  55  		result := test.in.Error()
  56  		if result != test.want {
  57  			t.Errorf("#%d: got: %s want: %s", i, result, test.want)
  58  			continue
  59  		}
  60  	}
  61  }
  62  
  63  // TestErrorKindIsAs ensures both ErrorKind and Error can be identified
  64  // as being a specific error via errors.Is and unwrapped via errors.As.
  65  func TestErrorKindIsAs(t *testing.T) {
  66  	tests := []struct {
  67  		name      string
  68  		err       error
  69  		target    error
  70  		wantMatch bool
  71  		wantAs    ErrorKind
  72  	}{
  73  		{
  74  			name:      "ErrInvalidHashLen == ErrInvalidHashLen",
  75  			err:       ErrInvalidHashLen,
  76  			target:    ErrInvalidHashLen,
  77  			wantMatch: true,
  78  			wantAs:    ErrInvalidHashLen,
  79  		}, {
  80  			name:      "Error.ErrInvalidHashLen == ErrInvalidHashLen",
  81  			err:       signatureError(ErrInvalidHashLen, ""),
  82  			target:    ErrInvalidHashLen,
  83  			wantMatch: true,
  84  			wantAs:    ErrInvalidHashLen,
  85  		}, {
  86  			name:      "Error.ErrInvalidHashLen == Error.ErrInvalidHashLen",
  87  			err:       signatureError(ErrInvalidHashLen, ""),
  88  			target:    signatureError(ErrInvalidHashLen, ""),
  89  			wantMatch: true,
  90  			wantAs:    ErrInvalidHashLen,
  91  		}, {
  92  			name:      "ErrSecretKeyIsZero != ErrInvalidHashLen",
  93  			err:       ErrSecretKeyIsZero,
  94  			target:    ErrInvalidHashLen,
  95  			wantMatch: false,
  96  			wantAs:    ErrSecretKeyIsZero,
  97  		}, {
  98  			name:      "Error.ErrSecretKeyIsZero != ErrInvalidHashLen",
  99  			err:       signatureError(ErrSecretKeyIsZero, ""),
 100  			target:    ErrInvalidHashLen,
 101  			wantMatch: false,
 102  			wantAs:    ErrSecretKeyIsZero,
 103  		}, {
 104  			name:      "ErrSecretKeyIsZero != Error.ErrInvalidHashLen",
 105  			err:       ErrSecretKeyIsZero,
 106  			target:    signatureError(ErrInvalidHashLen, ""),
 107  			wantMatch: false,
 108  			wantAs:    ErrSecretKeyIsZero,
 109  		}, {
 110  			name:      "Error.ErrSecretKeyIsZero != Error.ErrInvalidHashLen",
 111  			err:       signatureError(ErrSecretKeyIsZero, ""),
 112  			target:    signatureError(ErrInvalidHashLen, ""),
 113  			wantMatch: false,
 114  			wantAs:    ErrSecretKeyIsZero,
 115  		},
 116  	}
 117  	for _, test := range tests {
 118  		// Ensure the error matches or not depending on the expected result.
 119  		result := errors.Is(test.err, test.target)
 120  		if result != test.wantMatch {
 121  			t.Errorf(
 122  				"%s: incorrect error identification -- got %v, want %v",
 123  				test.name, result, test.wantMatch,
 124  			)
 125  			continue
 126  		}
 127  		// Ensure the underlying error kind can be unwrapped and is the
 128  		// expected code.
 129  		var code ErrorKind
 130  		if !errors.As(test.err, &code) {
 131  			t.Errorf("%s: unable to unwrap to error", test.name)
 132  			continue
 133  		}
 134  		if !errors.Is(code, test.wantAs) {
 135  			t.Errorf(
 136  				"%s: unexpected unwrapped error -- got %v, want %v",
 137  				test.name, code, test.wantAs,
 138  			)
 139  			continue
 140  		}
 141  	}
 142  }
 143