From 90eb6248feb2f8d3112aa5d14eb491b15413fe38 Mon Sep 17 00:00:00 2001 From: Rajkumar Gosavi Date: Tue, 24 Oct 2023 22:10:36 +0530 Subject: [PATCH] add UT for coverage --- consensus/export_test.go | 8 ++ consensus/update_test.go | 158 +++++++++++++++++++++++++++++++++++ consensus/validation_test.go | 39 +++++++++ 3 files changed, 205 insertions(+) create mode 100644 consensus/export_test.go diff --git a/consensus/export_test.go b/consensus/export_test.go new file mode 100644 index 00000000..83a6aa8c --- /dev/null +++ b/consensus/export_test.go @@ -0,0 +1,8 @@ +package consensus + +// NewWork creates a Work instances for testing +func NewWork(n [32]byte) *Work { + return &Work{ + n: n, + } +} diff --git a/consensus/update_test.go b/consensus/update_test.go index 948ca8f7..bcc30b90 100644 --- a/consensus/update_test.go +++ b/consensus/update_test.go @@ -239,3 +239,161 @@ func TestApplyBlock(t *testing.T) { dbStore.RevertBlock(cs, ru) checkRevertElements(ru, addedSCEs, spentSCEs, addedSFEs, spentSFEs) } + +/* +func TestWork_UnmarshalText(t *testing.T) { + type fields struct { + n [32]byte + } + type args struct { + b []byte + } + + negTen := big.NewInt(-111111) + + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "failure math/big: cannot unmarshal \"\" into a *big.Int", + fields: fields{}, + args: args{}, + wantErr: true, + }, + { + name: "failure value cannot be negative", + fields: fields{ + n: types.HashBytes(negTen.Bytes()), + }, + args: args{ + b: negTen.Bytes(), + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := consensus.NewWork(tt.fields.n) + if err := w.UnmarshalText(tt.args.b); (err != nil) != tt.wantErr { + t.Log("!!!", tt.args.b, "!@", "@@@", string(tt.fields.n[:])) + t.Errorf("Work.UnmarshalText() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} +*/ + +func TestWorkMarshalText(t *testing.T) { + type fields struct { + n [32]byte + } + one := [32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + tests := []struct { + name string + fields fields + want []byte + wantErr bool + }{ + { + name: "success", + fields: fields{ + n: one, + }, + want: []byte{49}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := consensus.NewWork(tt.fields.n) + got, err := w.MarshalText() + if (err != nil) != tt.wantErr { + t.Errorf("Work.MarshalText() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Log(string(got), "!!!", string(tt.want)) + t.Errorf("Work.MarshalText() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestWork_MarshalJSON(t *testing.T) { + type fields struct { + n [32]byte + } + one := [32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + + tests := []struct { + name string + fields fields + want []byte + wantErr bool + }{ + { + name: "success", + fields: fields{ + n: one, + }, + want: []byte(`"1"`), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := consensus.NewWork(tt.fields.n) + got, err := w.MarshalJSON() + if (err != nil) != tt.wantErr { + t.Errorf("Work.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Work.MarshalJSON() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRevertBlock(t *testing.T) { + type args struct { + s consensus.State + b types.Block + bs consensus.V1BlockSupplement + } + tests := []struct { + name string + args args + want consensus.RevertUpdate + }{ + { + name: "panic", + args: args{ + s: consensus.State{ + Index: types.ChainIndex{ + ID: types.BlockID{1}, + }, + }, + b: types.Block{ + ParentID: types.BlockID{10}, + }, + }, + want: consensus.RevertUpdate{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Error("should have panicked") + } + }() + if got := consensus.RevertBlock(tt.args.s, tt.args.b, tt.args.bs); !reflect.DeepEqual(got, tt.want) { + t.Errorf("RevertBlock() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/consensus/validation_test.go b/consensus/validation_test.go index a6040672..3e73426d 100644 --- a/consensus/validation_test.go +++ b/consensus/validation_test.go @@ -2,6 +2,7 @@ package consensus_test import ( "bytes" + "errors" "math" "testing" "time" @@ -1250,3 +1251,41 @@ func TestValidateV2Block(t *testing.T) { } } } + +func TestValidateV2Transaction(t *testing.T) { + type args struct { + ms *consensus.MidState + txn types.V2Transaction + } + tests := []struct { + name string + args args + wantErr error + }{ + { + name: "failure - v2 transactions are not allowed", + args: args{ + ms: consensus.NewMidState(consensus.State{ + Index: types.ChainIndex{Height: uint64(0)}, + Network: &consensus.Network{ + HardforkV2: struct { + AllowHeight uint64 "json:\"allowHeight\"" + RequireHeight uint64 "json:\"requireHeight\"" + }{ + AllowHeight: uint64(2), + }, + }, + }), + txn: types.V2Transaction{}, + }, + wantErr: errors.New("v2 transactions are not allowed until v2 hardfork begins"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := consensus.ValidateV2Transaction(tt.args.ms, tt.args.txn); (err != nil) && err.Error() != tt.wantErr.Error() { + t.Errorf("ValidateV2Transaction() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}