// Determines the script type and creates interpreter func (this *SandboxFilter) Init(config interface{}) (err error) { if this.sb != nil { return nil // no-op already initialized } this.sbc = config.(*SandboxConfig) this.sbc.ScriptFilename = pipeline.PrependShareDir(this.sbc.ScriptFilename) data_dir := pipeline.PrependBaseDir(DATA_DIR) if !fileExists(data_dir) { err = os.MkdirAll(data_dir, 0700) if err != nil { return } } switch this.sbc.ScriptType { case "lua": this.sb, err = lua.CreateLuaSandbox(this.sbc) if err != nil { return } default: return fmt.Errorf("unsupported script type: %s", this.sbc.ScriptType) } this.preservationFile = filepath.Join(data_dir, this.name+DATA_EXT) if this.sbc.PreserveData && fileExists(this.preservationFile) { err = this.sb.Init(this.preservationFile, "filter") } else { err = this.sb.Init("", "filter") } return }
func TestReadMessage(t *testing.T) { var sbc SandboxConfig captures := map[string]string{"exists": "found"} sbc.ScriptFilename = "./testsupport/read_message.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 msg := getTestMessage() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } r := sb.ProcessMessage(msg, captures) if r != 0 { t.Errorf("ProcessMessage should return 0, received %d", r) } r = sb.TimerEvent(time.Now().UnixNano()) if r != 0 { t.Errorf("read_message should return nil in timer_event") } sb.Destroy("") }
func TestInit(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/hello_world.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sbc.OutputLimit = 1024 sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } if STATUS_UNKNOWN != sb.Status() { t.Errorf("status should be %d, received %d", STATUS_UNKNOWN, sb.Status()) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } b := sb.Usage(TYPE_MEMORY, STAT_CURRENT) if b == 0 { t.Errorf("current memory should be >0, using %d", b) } b = sb.Usage(TYPE_MEMORY, STAT_MAXIMUM) if b == 0 { t.Errorf("maximum memory should be >0, using %d", b) } b = sb.Usage(TYPE_MEMORY, STAT_LIMIT) if b != sbc.MemoryLimit { t.Errorf("memory limit should be %d, using %d", sbc.MemoryLimit, b) } b = sb.Usage(TYPE_INSTRUCTIONS, STAT_CURRENT) if b != 9 { t.Errorf("current instructions should be 9, using %d", b) } b = sb.Usage(TYPE_INSTRUCTIONS, STAT_MAXIMUM) if b != 9 { t.Errorf("maximum instructions should be 9, using %d", b) } b = sb.Usage(TYPE_INSTRUCTIONS, STAT_LIMIT) if b != sbc.InstructionLimit { t.Errorf("instruction limit should be %d, using %d", sbc.InstructionLimit, b) } b = sb.Usage(TYPE_OUTPUT, STAT_CURRENT) if b != 12 { t.Errorf("current output should be 12, using %d", b) } b = sb.Usage(TYPE_OUTPUT, STAT_MAXIMUM) if b != 12 { t.Errorf("maximum output should be 12, using %d", b) } b = sb.Usage(TYPE_OUTPUT, STAT_LIMIT) if b != sbc.OutputLimit { t.Errorf("output limit should be %d, using %d", sbc.OutputLimit, b) } if STATUS_RUNNING != sb.Status() { t.Errorf("status should be %d, received %d", STATUS_RUNNING, sb.Status()) } sb.Destroy("") }
// Determines the script type and creates interpreter sandbox. func (this *SandboxFilter) Init(config interface{}) (err error) { if this.sb != nil { return nil // no-op already initialized } this.sbc = config.(*sandbox.SandboxConfig) this.sbc.ScriptFilename = GetHekaConfigDir(this.sbc.ScriptFilename) switch this.sbc.ScriptType { case "lua": this.sb, err = lua.CreateLuaSandbox(this.sbc) if err != nil { return err } default: return fmt.Errorf("unsupported script type: %s", this.sbc.ScriptType) } this.preservationFile = filepath.Join(filepath.Dir(this.sbc.ScriptFilename), this.name+".data") if this.sbc.PreserveData && fileExists(this.preservationFile) { err = this.sb.Init(this.preservationFile) } else { err = this.sb.Init("") } return err }
func TestMissingTimeEvent(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/hello_world.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sbc.OutputLimit = 1024 sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } r := sb.TimerEvent(time.Now().UnixNano()) if r == 0 { t.Errorf("TimerEvent() expected: 1, received: %d", r) } if STATUS_TERMINATED != sb.Status() { t.Errorf("status should be %d, received %d", STATUS_TERMINATED, sb.Status()) } r = sb.TimerEvent(time.Now().UnixNano()) // try to use the terminated plugin if r == 0 { t.Errorf("TimerEvent() expected: 1, received: %d", r) } sb.Destroy("") }
func (s *SandboxEncoder) Init(config interface{}) (err error) { conf := config.(*SandboxEncoderConfig) s.sbc = &sandbox.SandboxConfig{ ScriptType: conf.ScriptType, ScriptFilename: conf.ScriptFilename, ModuleDirectory: conf.ModuleDirectory, PreserveData: conf.PreserveData, MemoryLimit: conf.MemoryLimit, InstructionLimit: conf.InstructionLimit, OutputLimit: conf.OutputLimit, Profile: conf.Profile, Config: conf.Config, } s.sbc.ScriptFilename = pipeline.PrependShareDir(s.sbc.ScriptFilename) s.sampleDenominator = pipeline.Globals().SampleDenominator s.tz = time.UTC if tz, ok := s.sbc.Config["tz"]; ok { if s.tz, err = time.LoadLocation(tz.(string)); err != nil { return } } dataDir := pipeline.PrependBaseDir(sandbox.DATA_DIR) if !fileExists(dataDir) { if err = os.MkdirAll(dataDir, 0700); err != nil { return } } switch s.sbc.ScriptType { case "lua": s.sb, err = lua.CreateLuaSandbox(s.sbc) default: return fmt.Errorf("Unsupported script type: %s", s.sbc.ScriptType) } if err != nil { return fmt.Errorf("Sandbox creation failed: '%s'", err) } s.preservationFile = filepath.Join(dataDir, s.name+sandbox.DATA_EXT) if s.sbc.PreserveData && fileExists(s.preservationFile) { err = s.sb.Init(s.preservationFile, "encoder") } else { err = s.sb.Init("", "encoder") } if err != nil { return fmt.Errorf("Sandbox initialization failed: %s", err) } s.sb.InjectMessage(func(payload, payload_type, payload_name string) int { s.injected = true s.output = []byte(payload) return 0 }) s.sample = true s.cEncoder = client.NewProtobufEncoder(nil) return }
func TestLpeg(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/lpeg_csv.lua" sbc.MemoryLimit = 100000 sbc.InstructionLimit = 1000 sbc.OutputLimit = 8000 pack := getTestPack() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("", "") if err != nil { t.Errorf("%s", err) } sb.InjectMessage(func(p, pt, pn string) int { expected := `["1","string with spaces","quoted string, with comma and \"quoted\" text"]` if p != expected { t.Errorf("Output is incorrect, expected: \"%s\" received: \"%s\"", expected, p) } return 0 }) pack.Message.SetPayload("1,string with spaces,\"quoted string, with comma and \"\"quoted\"\" text\"") r := sb.ProcessMessage(pack) if r != 0 { t.Errorf("ProcessMessage should return 0, received %d %s", r, sb.LastError()) } sb.Destroy("") }
func TestReadMessage(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/read_message.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 pack := getTestPack() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("", "") if err != nil { t.Errorf("%s", err) } pack.MsgBytes = []byte("rawdata") r := sb.ProcessMessage(pack) if r != 0 { t.Errorf("ProcessMessage should return 0, received %d", r) } r = sb.TimerEvent(time.Now().UnixNano()) if r != 0 { t.Errorf("read_message should return nil in timer_event") } sb.Destroy("") }
func TestPreserve(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/serialize.lua" sbc.MemoryLimit = 64000 sbc.InstructionLimit = 1000 sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } output := "/tmp/serialize.lua.data" saved := "./testsupport/serialize.lua.data" err = sb.Destroy("/tmp/serialize.lua.data") if err != nil { t.Errorf("%s", err) } else { o, err := ioutil.ReadFile(output) if err != nil { t.Errorf("%s", err) } s, err := ioutil.ReadFile(saved) if err != nil { t.Errorf("%s", err) } if 0 != bytes.Compare(o, s) { t.Errorf("The preserved data does not match") } } }
func TestMissingProcessMessage(t *testing.T) { var sbc SandboxConfig var captures map[string]string sbc.ScriptFilename = "./testsupport/hello_world.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sbc.OutputLimit = 1024 msg := getTestMessage() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } r := sb.ProcessMessage(msg, captures) if r == 0 { t.Errorf("ProcessMessage() expected: 1, received: %d", r) } s := "process_message() function was not found" if sb.LastError() != s { t.Errorf("LastError() should be \"%s\", received: \"%s\"", s, sb.LastError()) } if STATUS_TERMINATED != sb.Status() { t.Errorf("status should be %d, received %d", STATUS_TERMINATED, sb.Status()) } r = sb.ProcessMessage(msg, captures) // try to use the terminated plugin if r == 0 { t.Errorf("ProcessMessage() expected: 1, received: %d", r) } sb.Destroy("") }
func TestRestore(t *testing.T) { var sbc SandboxConfig var captures map[string]string sbc.ScriptFilename = "./testsupport/simple_count.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sbc.OutputLimit = 1024 msg := getTestMessage() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("./testsupport/simple_count.lua.data") if err != nil { t.Errorf("%s", err) } sb.InjectMessage(func(p, pt, pn string) int { if p != "11" { t.Errorf("State was not restored") } return 0 }) r := sb.ProcessMessage(msg, captures) if r != 0 { t.Errorf("ProcessMessage should return 0, received %d", r) } sb.Destroy("") }
func TestPreserveFailure(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/serialize_failure.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } output := "/tmp/serialize_failure.lua.data" err = sb.Destroy(output) if err == nil { t.Errorf("The key of type 'function' should have failed") } else { expect := "Destroy() serialize_data cannot preserve type 'function'" if err.Error() != expect { t.Errorf("expected '%s' got '%s'", expect, err) } } _, err = os.Stat(output) if err == nil { t.Errorf("The output file should be removed on failure") } }
func TestFailedMessageInjection(t *testing.T) { var sbc SandboxConfig var captures map[string]string sbc.ScriptFilename = "./testsupport/loop.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sbc.OutputLimit = 1024 msg := getTestMessage() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } sb.InjectMessage(func(p, pt, pn string) int { return 1 }) r := sb.ProcessMessage(msg, captures) if r != 1 { t.Errorf("ProcessMessage should return 1, received %d", r) } if STATUS_TERMINATED != sb.Status() { t.Errorf("status should be %d, received %d", STATUS_TERMINATED, sb.Status()) } s := sb.LastError() errMsg := "process_message() inject_message() exceeded MaxMsgLoops" if s != errMsg { t.Errorf("error should be \"%s\", received \"%s\"", errMsg, s) } sb.Destroy("") }
func TestAPIErrors(t *testing.T) { msg := getTestMessage() tests := []string{ "inject_message() incorrect number of args", "output() no arg", "out of memory", "out of instructions", "operation on a nil", "invalid return", "no return", "read_message() incorrect number of args", "read_message() incorrect field name type", "read_message() negative field index", "read_message() negative array index", "output limit exceeded"} msgs := []string{ "process_message() ./testsupport/errors.lua:11: inject_message() takes a maximum of 2 arguments", "process_message() ./testsupport/errors.lua:13: output() must have at least one argument", "process_message() not enough memory", "process_message() instruction_limit exceeded", "process_message() ./testsupport/errors.lua:22: attempt to perform arithmetic on global 'x' (a nil value)", "process_message() must return a single numeric value", "process_message() must return a single numeric value", "process_message() ./testsupport/errors.lua:28: read_message() incorrect number of arguments", "process_message() ./testsupport/errors.lua:30: bad argument #1 to 'read_message' (string expected, got nil)", "process_message() ./testsupport/errors.lua:32: bad argument #2 to 'read_message' (field index must be >= 0)", "process_message() ./testsupport/errors.lua:34: bad argument #3 to 'read_message' (array index must be >= 0)", "process_message() ./testsupport/errors.lua:37: output_limit exceeded"} var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/errors.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sbc.OutputLimit = 128 for i, v := range tests { sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } msg.SetPayload(v) r := sb.ProcessMessage(msg) if r != 1 || STATUS_TERMINATED != sb.Status() { t.Errorf("test: %s status should be %d, received %d", v, STATUS_TERMINATED, sb.Status()) } s := sb.LastError() if s != msgs[i] { t.Errorf("test: %s error should be \"%s\", received \"%s\"", v, msgs[i], s) } sb.Destroy("") } }
func TestCreation(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/hello_world.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sbc.OutputLimit = 1024 sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } b := sb.Usage(TYPE_MEMORY, STAT_CURRENT) if b != 0 { t.Errorf("current memory should be 0, using %d", b) } b = sb.Usage(TYPE_MEMORY, STAT_MAXIMUM) if b != 0 { t.Errorf("maximum memory should be 0, using %d", b) } b = sb.Usage(TYPE_MEMORY, STAT_LIMIT) if b != sbc.MemoryLimit { t.Errorf("memory limit should be %d, using %d", sbc.MemoryLimit, b) } b = sb.Usage(TYPE_INSTRUCTIONS, STAT_CURRENT) if b != 0 { t.Errorf("current instructions should be 0, using %d", b) } b = sb.Usage(TYPE_INSTRUCTIONS, STAT_MAXIMUM) if b != 0 { t.Errorf("maximum instructions should be 0, using %d", b) } b = sb.Usage(TYPE_INSTRUCTIONS, STAT_LIMIT) if b != sbc.InstructionLimit { t.Errorf("instruction limit should be %d, using %d", sbc.InstructionLimit, b) } b = sb.Usage(TYPE_OUTPUT, STAT_CURRENT) if b != 0 { t.Errorf("current output should be 0, using %d", b) } b = sb.Usage(TYPE_OUTPUT, STAT_MAXIMUM) if b != 0 { t.Errorf("maximum output should be 0, using %d", b) } b = sb.Usage(TYPE_OUTPUT, STAT_LIMIT) if b != sbc.OutputLimit { t.Errorf("output limit should be %d, using %d", sbc.OutputLimit, b) } b = sb.Usage(TYPE_OUTPUT, 99) if b != 0 { t.Errorf("invalid index should return 0, received %d", b) } if sb.LastError() != "" { t.Errorf("LastError() should be empty, received: %s", sb.LastError()) } sb.Destroy("") }
func TestCreationTooManyInstructions(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/hello_world.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000001 sb, err := lua.CreateLuaSandbox(&sbc) if err == nil { t.Errorf("Sandbox creation should have failed on InstructionLimit") sb.Destroy("") } }
func BenchmarkSandboxCreateInitDestroyPreserve(b *testing.B) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/serialize.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 for i := 0; i < b.N; i++ { sb, _ := lua.CreateLuaSandbox(&sbc) sb.Init("") sb.Destroy("/tmp/serialize.lua.data") } }
func TestInjectMessageError(t *testing.T) { var sbc SandboxConfig tests := []string{ "error circular reference", "error escape overflow", "error mis-match field array", "error nil field", "error nil type arg", "error nil name arg", "error nil message", "error userdata output_limit", } errors := []string{ "process_message() ./testsupport/inject_message.lua:38: Cannot serialise, excessive nesting (1001)", "process_message() ./testsupport/inject_message.lua:44: strbuf max_size exceeded", "process_message() ./testsupport/inject_message.lua:50: inject_message() could not encode protobuf - array has mixed types", "process_message() ./testsupport/inject_message.lua:53: inject_message() could not encode protobuf - unsupported type: nil", "process_message() ./testsupport/inject_message.lua:55: bad argument #1 to 'inject_payload' (string expected, got nil)", "process_message() ./testsupport/inject_message.lua:57: bad argument #2 to 'inject_payload' (string expected, got nil)", "process_message() ./testsupport/inject_message.lua:59: inject_message() takes a single table argument", "process_message() ./testsupport/inject_message.lua:62: output_limit exceeded", } sbc.ScriptFilename = "./testsupport/inject_message.lua" sbc.MemoryLimit = 1000000 sbc.InstructionLimit = 1000 sbc.OutputLimit = 1024 pack := getTestPack() for i, v := range tests { sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("", "") if err != nil { t.Errorf("%s", err) } pack.Message.SetPayload(v) r := sb.ProcessMessage(pack) if r != 1 { t.Errorf("ProcessMessage test: %s should return 1, received %d", v, r) } else { if sb.LastError() != errors[i] { t.Errorf("Expected: \"%s\" received: \"%s\"", errors[i], sb.LastError()) } } sb.Destroy("") } }
func TestInjectMessageError(t *testing.T) { var sbc SandboxConfig tests := []string{ "error internal reference", "error circular reference", "error escape overflow", "error mis-match field array", "error nil field", "error nil type arg", "error nil name arg", "error incorrect number of args", } errors := []string{ "process_message() ./testsupport/inject_message.lua:46: table contains an internal or circular reference", "process_message() ./testsupport/inject_message.lua:51: table contains an internal or circular reference", "process_message() not enough memory", "process_message() ./testsupport/inject_message.lua:80: inject_message() cound not encode protobuf - array has mixed types", "process_message() ./testsupport/inject_message.lua:83: inject_message() cound not encode protobuf - unsupported type 0", "process_message() ./testsupport/inject_message.lua:85: bad argument #1 to 'inject_message' (string, table, or circular_buffer expected, got nil)", "process_message() ./testsupport/inject_message.lua:87: bad argument #2 to 'inject_message' (string expected, got nil)", "process_message() ./testsupport/inject_message.lua:89: inject_message() takes a maximum of 2 arguments", } sbc.ScriptFilename = "./testsupport/inject_message.lua" sbc.MemoryLimit = 100000 sbc.InstructionLimit = 1000 sbc.OutputLimit = 1024 msg := getTestMessage() for i, v := range tests { sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("") if err != nil { t.Errorf("%s", err) } msg.SetPayload(v) r := sb.ProcessMessage(msg) if r != 1 { t.Errorf("ProcessMessage should return 1, received %d", r) } else { if sb.LastError() != errors[i] { t.Errorf("Expected: \"%s\" received: \"%s\"", errors[i], sb.LastError()) } } sb.Destroy("") } }
func TestRestoreMissingData(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/simple_count.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("./testsupport/missing.data") if err != nil { t.Errorf("%s", err) } sb.Destroy("") }
func BenchmarkSandboxProcessMessageCounter(b *testing.B) { b.StopTimer() var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 msg := getTestMessage() sb, _ := lua.CreateLuaSandbox(&sbc) sb.Init("") b.StartTimer() for i := 0; i < b.N; i++ { sb.ProcessMessage(msg) } sb.Destroy("") }
func BenchmarkSandboxReadMessageField(b *testing.B) { b.StopTimer() var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/readfield.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 pack := getTestPack() sb, _ := lua.CreateLuaSandbox(&sbc) sb.Init("", "") b.StartTimer() for i := 0; i < b.N; i++ { sb.ProcessMessage(pack) } sb.Destroy("") }
func TestReadNilConfig(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/read_config_nil.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("", "") if err != nil { t.Errorf("%s", err) } sb.Destroy("") }
func BenchmarkSandboxReadMessageCapture(b *testing.B) { b.StopTimer() var sbc SandboxConfig captures := map[string]string{"exists": "found"} sbc.ScriptFilename = "./testsupport/readcapture.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 msg := getTestMessage() sb, _ := lua.CreateLuaSandbox(&sbc) sb.Init("") b.StartTimer() for i := 0; i < b.N; i++ { sb.ProcessMessage(msg, captures) } sb.Destroy("") }
func TestAlert(t *testing.T) { var sbc SandboxConfig tests := []string{ "alert1\nalert2\nalert3", "alert5", "alert8", } sbc.ScriptFilename = "./testsupport/alert.lua" sbc.ModuleDirectory = "./modules" sbc.MemoryLimit = 100000 sbc.InstructionLimit = 1000 sbc.OutputLimit = 8000 pack := getTestPack() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("", "") if err != nil { t.Errorf("%s", err) } cnt := 0 sb.InjectMessage(func(p, pt, pn string) int { if pt != "alert" { t.Errorf("Payload type, expected: \"alert\" received: \"%s\"", pt) } if p != tests[cnt] { t.Errorf("Output is incorrect, expected: \"%s\" received: \"%s\"", tests[cnt], p) } cnt++ return 0 }) for i, _ := range tests { pack.Message.SetTimestamp(int64(i)) r := sb.ProcessMessage(pack) if r != 0 { t.Errorf("ProcessMessage should return 0, received %d %s", r, sb.LastError()) } } sb.Destroy("") if cnt != len(tests) { t.Errorf("Executed %d test, expected %d", cnt, len(tests)) } }
func BenchmarkSandboxOutputMessageAsJSON(b *testing.B) { b.StopTimer() var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/inject_message.lua" sbc.MemoryLimit = 100000 sbc.InstructionLimit = 1000 sbc.OutputLimit = 64512 sb, _ := lua.CreateLuaSandbox(&sbc) sb.Init("", "") sb.InjectMessage(func(p, pt, pn string) int { return 0 }) b.StartTimer() for i := 0; i < b.N; i++ { sb.TimerEvent(2) } sb.Destroy("") }
func TestAnnotation(t *testing.T) { var sbc SandboxConfig tests := []string{ "{\"annotations\":[{\"text\":\"anomaly\",\"x\":1000,\"shortText\":\"A\",\"col\":1},{\"text\":\"anomaly2\",\"x\":5000,\"shortText\":\"A\",\"col\":2},{\"text\":\"maintenance\",\"x\":60000,\"shortText\":\"M\",\"col\":1}]}\n", "{\"annotations\":[{\"text\":\"maintenance\",\"x\":60000,\"shortText\":\"M\",\"col\":1}]}\n", "{\"annotations\":{}}\n", "ok", } sbc.ScriptFilename = "./testsupport/annotation.lua" sbc.ModuleDirectory = "./modules" sbc.MemoryLimit = 100000 sbc.InstructionLimit = 1000 sbc.OutputLimit = 8000 pack := getTestPack() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("", "") if err != nil { t.Errorf("%s", err) } cnt := 0 sb.InjectMessage(func(p, pt, pn string) int { if p != tests[cnt] { t.Errorf("Output is incorrect, expected: \"%s\" received: \"%s\"", tests[cnt], p) } cnt++ return 0 }) for i, _ := range tests { pack.Message.SetTimestamp(int64(i)) r := sb.ProcessMessage(pack) if r != 0 { t.Errorf("ProcessMessage should return 0, received %d %s", r, sb.LastError()) } } sb.Destroy("") if cnt != len(tests) { t.Errorf("Executed %d test, expected %d", cnt, len(tests)) } }
func (s *SandboxInput) Init(config interface{}) (err error) { s.sbc = config.(*SandboxConfig) globals := s.pConfig.Globals s.sbc.ScriptFilename = globals.PrependShareDir(s.sbc.ScriptFilename) s.sbc.InstructionLimit = 0 s.sbc.PluginType = "input" s.tz = time.UTC if tz, ok := s.sbc.Config["tz"]; ok { s.tz, err = time.LoadLocation(tz.(string)) if err != nil { return } } data_dir := globals.PrependBaseDir(DATA_DIR) if !fileExists(data_dir) { err = os.MkdirAll(data_dir, 0700) if err != nil { return } } switch s.sbc.ScriptType { case "lua": s.sb, err = lua.CreateLuaSandbox(s.sbc) if err != nil { return } default: return fmt.Errorf("unsupported script type: %s", s.sbc.ScriptType) } s.preservationFile = filepath.Join(data_dir, s.name+DATA_EXT) if s.sbc.PreserveData && fileExists(s.preservationFile) { err = s.sb.Init(s.preservationFile) } else { err = s.sb.Init("") } s.stopChan = make(chan struct{}) return }
func TestRestoreMissingData(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/simple_count.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("./testsupport/missing.data") if err == nil { t.Errorf("Init should fail on data load error") } else { expect := "Init() restore_global_data cannot open ./testsupport/missing.data: No such file or directory" if err.Error() != expect { t.Errorf("expected '%s' got '%s'", expect, err) } } sb.Destroy("") }
func TestReadNextField(t *testing.T) { var sbc SandboxConfig sbc.ScriptFilename = "./testsupport/read_next_field.lua" sbc.MemoryLimit = 32767 sbc.InstructionLimit = 1000 pack := getTestPack() sb, err := lua.CreateLuaSandbox(&sbc) if err != nil { t.Errorf("%s", err) } err = sb.Init("", "") if err != nil { t.Errorf("%s", err) } r := sb.ProcessMessage(pack) if r != 0 { t.Errorf("ProcessMessage should return 0, received %d %s", r, sb.LastError()) } sb.Destroy("") }