test tha produces an error for decoding incarnation (#2742)

This commit is contained in:
Enrique Jose Avila Asapche 2021-09-30 02:52:58 +01:00 committed by GitHub
parent 3f10758faf
commit 677d2f88bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 13 deletions

View File

@ -528,7 +528,6 @@ func (a *Account) DecodeForStorage(enc []byte) error {
} }
func DecodeIncarnationFromStorage(enc []byte) (uint64, error) { func DecodeIncarnationFromStorage(enc []byte) (uint64, error) {
if len(enc) == 0 { if len(enc) == 0 {
return 0, nil return 0, nil
} }

View File

@ -190,7 +190,7 @@ func isAccountsEqual(t *testing.T, src, dst Account) {
} }
} }
func testIncarnationForEmptyAccount(t *testing.T) { func TestIncarnationForEmptyAccount(t *testing.T) {
a := Account{ a := Account{
Initialised: true, Initialised: true,
Nonce: 100, Nonce: 100,
@ -202,34 +202,39 @@ func testIncarnationForEmptyAccount(t *testing.T) {
encodedAccount := make([]byte, a.EncodingLengthForStorage()) encodedAccount := make([]byte, a.EncodingLengthForStorage())
a.EncodeForStorage(encodedAccount) a.EncodeForStorage(encodedAccount)
var decodedIncarnation uint64
if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil { if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil {
t.Fatal("Can't decode the incarnation", err, encodedAccount) 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) isIncarnationEqual(t, a.Incarnation, decodedIncarnation)
} }
func testEmptyIncarnationForEmptyAccount2(t *testing.T) { func TestEmptyIncarnationForEmptyAccount2(t *testing.T) {
a := Account{} a := Account{}
encodedAccount := make([]byte, a.EncodingLengthForStorage()) encodedAccount := make([]byte, a.EncodingLengthForStorage())
a.EncodeForStorage(encodedAccount) a.EncodeForStorage(encodedAccount)
var decodedIncarnation uint64
if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil { if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil {
t.Fatal("Can't decode the incarnation", err, encodedAccount) 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) isIncarnationEqual(t, a.Incarnation, decodedIncarnation)
} }
func testIncarnationWithNonEmptyAccount(t *testing.T) { func TestIncarnationWithNonEmptyAccount(t *testing.T) {
a := Account{ a := Account{
Initialised: true, Initialised: true,
Nonce: 2, Nonce: 2,
@ -242,18 +247,20 @@ func testIncarnationWithNonEmptyAccount(t *testing.T) {
encodedAccount := make([]byte, a.EncodingLengthForStorage()) encodedAccount := make([]byte, a.EncodingLengthForStorage())
a.EncodeForStorage(encodedAccount) a.EncodeForStorage(encodedAccount)
var decodedIncarnation uint64
if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil { if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil {
t.Fatal("Can't decode the incarnation", err, encodedAccount) 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) isIncarnationEqual(t, a.Incarnation, decodedIncarnation)
} }
func testIncarnationWithNoIncarnation(t *testing.T) { func TestIncarnationWithNoIncarnation(t *testing.T) {
a := Account{ a := Account{
Initialised: true, Initialised: true,
Nonce: 2, Nonce: 2,
@ -266,17 +273,29 @@ func testIncarnationWithNoIncarnation(t *testing.T) {
encodedAccount := make([]byte, a.EncodingLengthForStorage()) encodedAccount := make([]byte, a.EncodingLengthForStorage())
a.EncodeForStorage(encodedAccount) a.EncodeForStorage(encodedAccount)
var decodedIncarnation uint64
if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil { if _, err := DecodeIncarnationFromStorage(encodedAccount); err != nil {
t.Fatal("Can't decode the incarnation", err, encodedAccount) 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) 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) { func isIncarnationEqual(t *testing.T, initialIncarnation uint64, decodedIncarnation uint64) {
if initialIncarnation != decodedIncarnation { if initialIncarnation != decodedIncarnation {
t.Fatal("Can't decode the incarnation", initialIncarnation, decodedIncarnation) t.Fatal("Can't decode the incarnation", initialIncarnation, decodedIncarnation)