github.com/klaytn/klaytn@v1.10.2/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 ) 27 28 func TestGetVMerrFromReceiptStatus(t *testing.T) { 29 err := GetVMerrFromReceiptStatus(types.ReceiptStatusFailed) 30 expectedErr := ErrInvalidReceiptStatus 31 if err.Error() != expectedErr.Error() { 32 t.Fatalf("Invalid err, want %s, got %s", expectedErr, err) 33 } 34 35 // Invalid ReceiptStatus 36 err = GetVMerrFromReceiptStatus(types.ReceiptStatusLast) 37 expectedErr = ErrInvalidReceiptStatus 38 if err.Error() != expectedErr.Error() { 39 t.Fatalf("Invalid err, want %s, got %s", expectedErr, err) 40 } 41 42 err = GetVMerrFromReceiptStatus(types.ReceiptStatusSuccessful) 43 if err != nil { 44 t.Fatalf("Invalid err, want nil, got %s", err) 45 } 46 47 err = GetVMerrFromReceiptStatus(types.ReceiptStatusErrDefault) 48 expectedErr = ErrVMDefault 49 if err.Error() != expectedErr.Error() { 50 t.Fatalf("Invalid err, want %s, got %s", expectedErr, err) 51 } 52 } 53 54 func TestGetReceiptStatusFromVMerr(t *testing.T) { 55 status := getReceiptStatusFromErrTxFailed(nil) 56 expectedStatus := types.ReceiptStatusSuccessful 57 if status != expectedStatus { 58 t.Fatalf("Invalid receipt status, want %d, got %d", expectedStatus, status) 59 } 60 61 status = getReceiptStatusFromErrTxFailed(vm.ErrMaxCodeSizeExceeded) 62 expectedStatus = types.ReceiptStatuserrMaxCodeSizeExceed 63 if status != expectedStatus { 64 t.Fatalf("Invalid receipt status, want %d, got %d", expectedStatus, status) 65 } 66 67 // Unknown VM error 68 status = getReceiptStatusFromErrTxFailed(errors.New("Unknown VM error")) 69 expectedStatus = types.ReceiptStatusErrDefault 70 if status != expectedStatus { 71 t.Fatalf("Invalid receipt status, want %d, got %d", expectedStatus, status) 72 } 73 } 74 75 // TestPrintErrorCodeTable prints the error code table in a format of a markdown table. 76 func TestPrintErrorCodeTable(t *testing.T) { 77 if testing.Verbose() { 78 fmt.Println("| ErrorCode | Description |") 79 fmt.Println("|---|---|") 80 for i := uint(types.ReceiptStatusErrDefault); i < types.ReceiptStatusLast; i++ { 81 fmt.Printf("|0x%02x|%s|\n", i, receiptstatus2errTxFailed[i]) 82 } 83 } 84 }