github.com/klaytn/klaytn@v1.12.1/blockchain/state_transition_test.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package blockchain
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/klaytn/klaytn/blockchain/types"
    25  	"github.com/klaytn/klaytn/blockchain/vm"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestGetVMerrFromReceiptStatus(t *testing.T) {
    30  	testData := []struct {
    31  		status           uint
    32  		expectMatchError error
    33  	}{
    34  		{types.ReceiptStatusFailed, ErrInvalidReceiptStatus},
    35  		{types.ReceiptStatusLast, ErrInvalidReceiptStatus},
    36  		{types.ReceiptStatusSuccessful, nil},
    37  		{types.ReceiptStatusErrDefault, ErrVMDefault},
    38  	}
    39  
    40  	for _, tc := range testData {
    41  		result := ExecutionResult{VmExecutionStatus: tc.status}
    42  		assert.Equal(t, tc.expectMatchError, result.Unwrap())
    43  	}
    44  }
    45  
    46  func TestGetReceiptStatusFromVMerr(t *testing.T) {
    47  	status := getReceiptStatusFromErrTxFailed(nil)
    48  	expectedStatus := types.ReceiptStatusSuccessful
    49  	if status != expectedStatus {
    50  		t.Fatalf("Invalid receipt status, want %d, got %d", expectedStatus, status)
    51  	}
    52  
    53  	status = getReceiptStatusFromErrTxFailed(vm.ErrMaxCodeSizeExceeded)
    54  	expectedStatus = types.ReceiptStatuserrMaxCodeSizeExceed
    55  	if status != expectedStatus {
    56  		t.Fatalf("Invalid receipt status, want %d, got %d", expectedStatus, status)
    57  	}
    58  
    59  	// Unknown VM error
    60  	status = getReceiptStatusFromErrTxFailed(errors.New("Unknown VM error"))
    61  	expectedStatus = types.ReceiptStatusErrDefault
    62  	if status != expectedStatus {
    63  		t.Fatalf("Invalid receipt status, want %d, got %d", expectedStatus, status)
    64  	}
    65  }
    66  
    67  // TestPrintErrorCodeTable prints the error code table in a format of a markdown table.
    68  func TestPrintErrorCodeTable(t *testing.T) {
    69  	if testing.Verbose() {
    70  		fmt.Println("| ErrorCode | Description |")
    71  		fmt.Println("|---|---|")
    72  		for i := uint(types.ReceiptStatusErrDefault); i < types.ReceiptStatusLast; i++ {
    73  			fmt.Printf("|0x%02x|%s|\n", i, receiptstatus2errTxFailed[i])
    74  		}
    75  	}
    76  }