github.com/cockroachdb/errors@v1.11.1/errbase/adapters_errno_test.go (about)

     1  // Copyright 2019 The Cockroach 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
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License.
    14  
    15  // +build !plan9
    16  
    17  package errbase_test
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"syscall"
    23  	"testing"
    24  
    25  	"github.com/cockroachdb/errors/errbase"
    26  	"github.com/cockroachdb/errors/errorspb"
    27  	"github.com/cockroachdb/errors/oserror"
    28  	"github.com/cockroachdb/errors/testutils"
    29  	"github.com/gogo/protobuf/types"
    30  )
    31  
    32  func TestAdaptErrno(t *testing.T) {
    33  	tt := testutils.T{T: t}
    34  
    35  	// Arbitrary values of errno on a given platform are preserved
    36  	// exactly when decoded on the same platform.
    37  	origErr := syscall.Errno(123)
    38  	newErr := network(t, origErr)
    39  	tt.Check(reflect.DeepEqual(newErr, origErr))
    40  
    41  	// Common values of errno preserve their properties
    42  	// across a network encode/decode even though they
    43  	// may not decode to the same type.
    44  	for i := 0; i < 2000; i++ {
    45  		origErr := syscall.Errno(i)
    46  		enc := errbase.EncodeError(context.Background(), origErr)
    47  
    48  		// Trick the decoder into thinking the error comes from a different platform.
    49  		details := &enc.Error.(*errorspb.EncodedError_Leaf).Leaf.Details
    50  		var d types.DynamicAny
    51  		if err := types.UnmarshalAny(details.FullDetails, &d); err != nil {
    52  			t.Fatal(err)
    53  		}
    54  		errnoDetails := d.Message.(*errorspb.ErrnoPayload)
    55  		errnoDetails.Arch = "OTHER"
    56  		any, err := types.MarshalAny(errnoDetails)
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  		details.FullDetails = any
    61  
    62  		// Now decode the error. This produces an OpaqueErrno payload.
    63  		dec := errbase.DecodeError(context.Background(), enc)
    64  		if _, ok := dec.(*errbase.OpaqueErrno); !ok {
    65  			t.Fatalf("expected OpaqueErrno, got %T", dec)
    66  		}
    67  
    68  		// Now check that the properties have been preserved properly.
    69  		tt.CheckEqual(oserror.IsPermission(origErr), oserror.IsPermission(dec))
    70  		tt.CheckEqual(oserror.IsExist(origErr), oserror.IsExist(dec))
    71  		tt.CheckEqual(oserror.IsNotExist(origErr), oserror.IsNotExist(dec))
    72  		tt.CheckEqual(oserror.IsTimeout(origErr), oserror.IsTimeout(dec))
    73  	}
    74  }