istio.io/istio@v0.0.0-20240520182934-d79c90f27776/security/pkg/pki/error/error_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package error
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"google.golang.org/grpc/codes"
    22  )
    23  
    24  func TestError(t *testing.T) {
    25  	testCases := map[string]struct {
    26  		eType   ErrType
    27  		err     error
    28  		message string
    29  		code    codes.Code
    30  	}{
    31  		"CA_NOT_READY": {
    32  			eType:   CANotReady,
    33  			err:     fmt.Errorf("test error1"),
    34  			message: "CA_NOT_READY",
    35  			code:    codes.Internal,
    36  		},
    37  		"CSR_ERROR": {
    38  			eType:   CSRError,
    39  			err:     fmt.Errorf("test error2"),
    40  			message: "CSR_ERROR",
    41  			code:    codes.InvalidArgument,
    42  		},
    43  		"TTL_ERROR": {
    44  			eType:   TTLError,
    45  			err:     fmt.Errorf("test error3"),
    46  			message: "TTL_ERROR",
    47  			code:    codes.InvalidArgument,
    48  		},
    49  		"CERT_GEN_ERROR": {
    50  			eType:   CertGenError,
    51  			err:     fmt.Errorf("test error4"),
    52  			message: "CERT_GEN_ERROR",
    53  			code:    codes.Internal,
    54  		},
    55  		"UNKNOWN": {
    56  			eType:   -1,
    57  			err:     fmt.Errorf("test error5"),
    58  			message: "UNKNOWN",
    59  			code:    codes.Internal,
    60  		},
    61  	}
    62  
    63  	for k, tc := range testCases {
    64  		caErr := NewError(tc.eType, tc.err)
    65  		if caErr.Error() != tc.err.Error() {
    66  			t.Errorf("[%s] unexpected error: '%s' VS (expected)'%s'", k, caErr.Error(), tc.err.Error())
    67  		}
    68  		if caErr.ErrorType() != tc.message {
    69  			t.Errorf("[%s] unexpected error type message: '%s' VS (expected)'%s'", k, caErr.ErrorType(), tc.message)
    70  		}
    71  		if caErr.HTTPErrorCode() != tc.code {
    72  			t.Errorf("[%s] unexpected error HTTP code: '%d' VS (expected)'%d'", k, caErr.HTTPErrorCode(), tc.code)
    73  		}
    74  	}
    75  }