Add input format checker in engine
This commit is contained in:
parent
d12ff18dd9
commit
2989b23b93
@ -5,11 +5,16 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"regexp"
|
||||
|
||||
"git.defalsify.org/festive/resource"
|
||||
"git.defalsify.org/festive/state"
|
||||
"git.defalsify.org/festive/vm"
|
||||
)
|
||||
|
||||
var (
|
||||
inputRegex = regexp.MustCompile("^[a-zA-Z0-9].*$")
|
||||
)
|
||||
//
|
||||
//type Config struct {
|
||||
// FlagCount uint32
|
||||
@ -45,6 +50,13 @@ func(en *Engine) Init(sym string, ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkInput(input []byte) error {
|
||||
if !inputRegex.Match(input) {
|
||||
return fmt.Errorf("Invalid input format: %s", input)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exec processes user input against the current state of the virtual machine environment.
|
||||
//
|
||||
// If successfully executed, output of the last execution is available using the WriteResult call.
|
||||
@ -56,7 +68,11 @@ func(en *Engine) Init(sym string, ctx context.Context) error {
|
||||
// - no current bytecode is available
|
||||
// - input processing against bytcode failed
|
||||
func (en *Engine) Exec(input []byte, ctx context.Context) (bool, error) {
|
||||
err := en.st.SetInput(input)
|
||||
err := checkInput(input)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
err = en.st.SetInput(input)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -120,3 +120,19 @@ it has more lines
|
||||
t.Fatalf("expected\n\t%s\ngot:\n\t%s\n", expect, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEngineExecInvalidInput(t *testing.T) {
|
||||
st := state.NewState(17).WithCacheSize(1024)
|
||||
generateTestData(t)
|
||||
ctx := context.TODO()
|
||||
rs := NewFsWrapper(dataDir, &st)
|
||||
en := NewEngine(&st, &rs)
|
||||
err := en.Init("root", ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = en.Exec([]byte("_foo"), ctx)
|
||||
if err == nil {
|
||||
t.Fatalf("expected fail on invalid input")
|
||||
}
|
||||
}
|
||||
|
@ -89,8 +89,6 @@ func TestRun(t *testing.T) {
|
||||
|
||||
b := NewLine(nil, MOVE, []string{"foo"}, nil, nil)
|
||||
b = NewLine(b, HALT, nil, nil, nil)
|
||||
//b := []byte{0x00, MOVE, 0x03}
|
||||
//b = append(b, []byte("foo")...)
|
||||
_, err := Run(b, &st, &rs, context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("run error: %v", err)
|
||||
|
Loading…
Reference in New Issue
Block a user