From 677d2f88bf4b0de5f92c7b8bb99369fe79aa83c9 Mon Sep 17 00:00:00 2001 From: Enrique Jose Avila Asapche Date: Thu, 30 Sep 2021 02:52:58 +0100 Subject: [PATCH] test tha produces an error for decoding incarnation (#2742) --- core/types/accounts/account.go | 1 - core/types/accounts/account_test.go | 43 +++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/core/types/accounts/account.go b/core/types/accounts/account.go index a65411231..5b8a767d8 100644 --- a/core/types/accounts/account.go +++ b/core/types/accounts/account.go @@ -528,7 +528,6 @@ func (a *Account) DecodeForStorage(enc []byte) error { } func DecodeIncarnationFromStorage(enc []byte) (uint64, error) { - if len(enc) == 0 { return 0, nil } diff --git a/core/types/accounts/account_test.go b/core/types/accounts/account_test.go index 3e38cd9a4..5d260f4e5 100644 --- a/core/types/accounts/account_test.go +++ b/core/types/accounts/account_test.go @@ -190,7 +190,7 @@ func isAccountsEqual(t *testing.T, src, dst Account) { } } -func testIncarnationForEmptyAccount(t *testing.T) { +func TestIncarnationForEmptyAccount(t *testing.T) { a := Account{ Initialised: true, Nonce: 100, @@ -202,34 +202,39 @@ func testIncarnationForEmptyAccount(t *testing.T) { encodedAccount := make([]byte, a.EncodingLengthForStorage()) a.EncodeForStorage(encodedAccount) - var decodedIncarnation uint64 + if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil { t.Fatal("Can't decode the incarnation", err, encodedAccount) } - decodedIncarnation, _ = DecodeIncarnationFromStorage(encodedAccount) + decodedIncarnation, err := DecodeIncarnationFromStorage(encodedAccount) + if err != nil { + t.Fatal("Can't decode the incarnation", err, encodedAccount) + } isIncarnationEqual(t, a.Incarnation, decodedIncarnation) } -func testEmptyIncarnationForEmptyAccount2(t *testing.T) { +func TestEmptyIncarnationForEmptyAccount2(t *testing.T) { a := Account{} encodedAccount := make([]byte, a.EncodingLengthForStorage()) a.EncodeForStorage(encodedAccount) - var decodedIncarnation uint64 if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil { t.Fatal("Can't decode the incarnation", err, encodedAccount) } - decodedIncarnation, _ = DecodeIncarnationFromStorage(encodedAccount) + decodedIncarnation, err := DecodeIncarnationFromStorage(encodedAccount) + if err != nil { + t.Fatal("Can't decode the incarnation", err, encodedAccount) + } isIncarnationEqual(t, a.Incarnation, decodedIncarnation) } -func testIncarnationWithNonEmptyAccount(t *testing.T) { +func TestIncarnationWithNonEmptyAccount(t *testing.T) { a := Account{ Initialised: true, Nonce: 2, @@ -242,18 +247,20 @@ func testIncarnationWithNonEmptyAccount(t *testing.T) { encodedAccount := make([]byte, a.EncodingLengthForStorage()) a.EncodeForStorage(encodedAccount) - var decodedIncarnation uint64 if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil { t.Fatal("Can't decode the incarnation", err, encodedAccount) } - decodedIncarnation, _ = DecodeIncarnationFromStorage(encodedAccount) + decodedIncarnation, err := DecodeIncarnationFromStorage(encodedAccount) + if err != nil { + t.Fatal("Can't decode the incarnation", err, encodedAccount) + } isIncarnationEqual(t, a.Incarnation, decodedIncarnation) } -func testIncarnationWithNoIncarnation(t *testing.T) { +func TestIncarnationWithNoIncarnation(t *testing.T) { a := Account{ Initialised: true, Nonce: 2, @@ -266,17 +273,29 @@ func testIncarnationWithNoIncarnation(t *testing.T) { encodedAccount := make([]byte, a.EncodingLengthForStorage()) a.EncodeForStorage(encodedAccount) - var decodedIncarnation uint64 if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil { t.Fatal("Can't decode the incarnation", err, encodedAccount) } - decodedIncarnation, _ = DecodeIncarnationFromStorage(encodedAccount) + decodedIncarnation, err := DecodeIncarnationFromStorage(encodedAccount) + if err != nil { + t.Fatal("Can't decode the incarnation", err, encodedAccount) + } isIncarnationEqual(t, a.Incarnation, decodedIncarnation) } +func TestIncarnationWithInvalidEncodedAccount(t *testing.T) { + + var failingSlice = []byte{1, 12} + + if incarnation, err := DecodeIncarnationFromStorage(failingSlice); err == nil { + t.Fatal("decoded the incarnation", incarnation, failingSlice) + } + +} + func isIncarnationEqual(t *testing.T, initialIncarnation uint64, decodedIncarnation uint64) { if initialIncarnation != decodedIncarnation { t.Fatal("Can't decode the incarnation", initialIncarnation, decodedIncarnation)