aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/csource/options_test.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-11-17 17:57:02 +0100
committerDmitry Vyukov <dvyukov@google.com>2017-11-17 17:57:51 +0100
commitfba338cd51aa2894bdd97b6943c27236df6b98d9 (patch)
tree6dcdb70f4e75568255befcf488923c7503fc969e /pkg/csource/options_test.go
parent3fb087023a62213a128683d3ef52ff52d32b07fe (diff)
pkg/csource: add function to parse serialized options
Also move options and options tests into a separate file, add serialization function.
Diffstat (limited to 'pkg/csource/options_test.go')
-rw-r--r--pkg/csource/options_test.go131
1 files changed, 131 insertions, 0 deletions
diff --git a/pkg/csource/options_test.go b/pkg/csource/options_test.go
new file mode 100644
index 000000000..f3f42a6fc
--- /dev/null
+++ b/pkg/csource/options_test.go
@@ -0,0 +1,131 @@
+// Copyright 2017 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+package csource
+
+import (
+ "fmt"
+ "reflect"
+ "testing"
+)
+
+//{Threaded:true Collide:true Repeat:true Procs:1 Sandbox:none Fault:false FaultCall:-1 FaultNth:0 EnableTun:true UseTmpDir:true HandleSegv:true WaitRepeat:true Debug:false Repro:false}
+
+func TestParseOptions(t *testing.T) {
+ for _, opts := range allOptionsSingle() {
+ data := opts.Serialize()
+ got, err := DeserializeOptions(data)
+ if err != nil {
+ t.Fatalf("failed to deserialize %q: %v", data, err)
+ }
+ if !reflect.DeepEqual(got, opts) {
+ t.Fatalf("opts changed, got:\n%+v\nwant:\n%+v", got, opts)
+ }
+ }
+}
+
+func TestParseOptionsCanned(t *testing.T) {
+ // Dashboard stores csource options with syzkaller reproducers,
+ // so we need to be able to parse old formats.
+ canned := map[string]Options{
+ "{Threaded:true Collide:true Repeat:true Procs:1 Sandbox:none Fault:false FaultCall:-1 FaultNth:0 EnableTun:true UseTmpDir:true HandleSegv:true WaitRepeat:true Debug:false Repro:false}": Options{
+ Threaded: true,
+ Collide: true,
+ Repeat: true,
+ Procs: 1,
+ Sandbox: "none",
+ Fault: false,
+ FaultCall: -1,
+ FaultNth: 0,
+ EnableTun: true,
+ UseTmpDir: true,
+ HandleSegv: true,
+ WaitRepeat: true,
+ Debug: false,
+ Repro: false,
+ },
+ "{Threaded:true Collide:true Repeat:true Procs:1 Sandbox: Fault:false FaultCall:-1 FaultNth:0 EnableTun:true UseTmpDir:true HandleSegv:true WaitRepeat:true Debug:false Repro:false}": Options{
+ Threaded: true,
+ Collide: true,
+ Repeat: true,
+ Procs: 1,
+ Sandbox: "",
+ Fault: false,
+ FaultCall: -1,
+ FaultNth: 0,
+ EnableTun: true,
+ UseTmpDir: true,
+ HandleSegv: true,
+ WaitRepeat: true,
+ Debug: false,
+ Repro: false,
+ },
+ }
+ for data, want := range canned {
+ got, err := DeserializeOptions([]byte(data))
+ if err != nil {
+ t.Fatalf("failed to deserialize %q: %v", data, err)
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("deserialize %q\ngot:\n%+v\nwant:\n%+v", data, got, want)
+ }
+ }
+}
+
+func allOptionsSingle() []Options {
+ var opts []Options
+ fields := reflect.TypeOf(Options{}).NumField()
+ for i := 0; i < fields; i++ {
+ opts = append(opts, enumerateField(Options{}, i)...)
+ }
+ return opts
+}
+
+func allOptionsPermutations() []Options {
+ opts := []Options{Options{}}
+ fields := reflect.TypeOf(Options{}).NumField()
+ for i := 0; i < fields; i++ {
+ var newOpts []Options
+ for _, opt := range opts {
+ newOpts = append(newOpts, enumerateField(opt, i)...)
+ }
+ opts = newOpts
+ }
+ return opts
+}
+
+func enumerateField(opt Options, field int) []Options {
+ var opts []Options
+ s := reflect.ValueOf(&opt).Elem()
+ fldName := s.Type().Field(field).Name
+ fld := s.Field(field)
+ if fldName == "Sandbox" {
+ for _, sandbox := range []string{"", "none", "setuid", "namespace"} {
+ fld.SetString(sandbox)
+ opts = append(opts, opt)
+ }
+ } else if fldName == "Procs" {
+ for _, procs := range []int64{1, 4} {
+ fld.SetInt(procs)
+ opts = append(opts, opt)
+ }
+ } else if fldName == "FaultCall" {
+ opts = append(opts, opt)
+ } else if fldName == "FaultNth" {
+ opts = append(opts, opt)
+ } else if fld.Kind() == reflect.Bool {
+ for _, v := range []bool{false, true} {
+ fld.SetBool(v)
+ opts = append(opts, opt)
+ }
+ } else {
+ panic(fmt.Sprintf("field '%v' is not boolean", fldName))
+ }
+ var checked []Options
+ for _, opt := range opts {
+ if err := opt.Check(); err == nil {
+ checked = append(checked, opt)
+ }
+ }
+ return checked
+}