Functional test for injections in core/rawdb

This commit is contained in:
philip-morlier 2023-06-01 13:14:08 -07:00
parent 6e8c38b994
commit 679d028484
3 changed files with 69 additions and 0 deletions

View File

@ -46,6 +46,9 @@ func newFreezerBatch(f *Freezer) *freezerBatch {
func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) error {
// begin PluGeth injection
PluginTrackUpdate(num, kind, item)
if injectionCalled != nil {
return nil
}
// end PluGeth injection
return batch.tables[kind].Append(num, item)
}
@ -54,6 +57,9 @@ func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) err
func (batch *freezerBatch) AppendRaw(kind string, num uint64, item []byte) error {
// being PluGeth injection
PluginTrackUpdate(num, kind, item)
if injectionCalled != nil {
return nil
}
// end PluGeth injection
return batch.tables[kind].AppendRaw(num, item)
}

View File

@ -0,0 +1,50 @@
package rawdb
import (
"fmt"
"os"
"testing"
"github.com/ethereum/go-ethereum/ethdb"
)
func TestAncientsInjections(t *testing.T) {
test_dir_path := "./injection_test_dir"
f, _ := NewFreezer(test_dir_path, "plugeth hook test", false, uint32(0), map[string]bool{"test": false})
t.Run(fmt.Sprintf("test ModifyAncients"), func(t *testing.T) {
called := false
injectionCalled = &called
_, _ = f.ModifyAncients(func (ethdb.AncientWriteOp) error {return nil})
if *injectionCalled != true {
t.Fatalf("pluginCommitUpdate injection in ModifyAncients not called")
}
})
os.RemoveAll(test_dir_path)
fb := newFreezerBatch(f)
t.Run(fmt.Sprintf("test Append"), func(t *testing.T) {
var item interface{}
called := false
injectionCalled = &called
_ = fb.Append("kind", uint64(0), item)
if *injectionCalled != true {
t.Fatalf("PluginTrackUpdate injection in Append not called")
}
})
t.Run(fmt.Sprintf("test AppendRaw"), func(t *testing.T) {
called := false
injectionCalled = &called
_ = fb.AppendRaw("kind", uint64(100), []byte{})
if *injectionCalled != true {
t.Fatalf("PluginTrackUpdate injection in AppendRaw not called")
}
})
}

View File

@ -11,9 +11,16 @@ import (
var (
freezerUpdates map[uint64]map[string]interface{}
lock sync.Mutex
injectionCalled *bool
)
func PluginTrackUpdate(num uint64, kind string, value interface{}) {
if injectionCalled != nil {
called := true
injectionCalled = &called
}
lock.Lock()
defer lock.Unlock()
if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) }
@ -26,6 +33,12 @@ func PluginTrackUpdate(num uint64, kind string, value interface{}) {
}
func pluginCommitUpdate(num uint64) {
if injectionCalled != nil {
called := true
injectionCalled = &called
}
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting CommitUpdate, but default PluginLoader has not been initialized")
return