Home | History | Annotate | Download | only in mgrconfig
      1 // Copyright 2017 syzkaller project authors. All rights reserved.
      2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
      3 
      4 package mgrconfig
      5 
      6 import (
      7 	"path/filepath"
      8 	"testing"
      9 
     10 	"github.com/google/syzkaller/pkg/config"
     11 	"github.com/google/syzkaller/vm/gce"
     12 	"github.com/google/syzkaller/vm/qemu"
     13 )
     14 
     15 func TestCanned(t *testing.T) {
     16 	files, err := filepath.Glob(filepath.Join("testdata", "*.cfg"))
     17 	if err != nil || len(files) == 0 {
     18 		t.Fatalf("failed to read input files: %v", err)
     19 	}
     20 	for _, file := range files {
     21 		t.Run(file, func(t *testing.T) {
     22 			cfg := new(Config)
     23 			if err := config.LoadFile(file, cfg); err != nil {
     24 				t.Fatal(err)
     25 			}
     26 			var vmCfg interface{}
     27 			switch cfg.Type {
     28 			case "qemu":
     29 				vmCfg = new(qemu.Config)
     30 			case "gce":
     31 				vmCfg = new(gce.Config)
     32 			default:
     33 				t.Fatalf("unknown VM type: %v", cfg.Type)
     34 			}
     35 			if err := config.LoadData(cfg.VM, vmCfg); err != nil {
     36 				t.Fatalf("failed to load %v config: %v", cfg.Type, err)
     37 			}
     38 		})
     39 	}
     40 }
     41 
     42 func TestMatchSyscall(t *testing.T) {
     43 	tests := []struct {
     44 		pattern string
     45 		call    string
     46 		result  bool
     47 	}{
     48 		{"foo", "foo", true},
     49 		{"foo", "bar", false},
     50 		{"foo", "foo$BAR", true},
     51 		{"foo*", "foo", true},
     52 		{"foo*", "foobar", true},
     53 		{"foo*", "foo$BAR", true},
     54 		{"foo$*", "foo", false},
     55 		{"foo$*", "foo$BAR", true},
     56 	}
     57 	for i, test := range tests {
     58 		res := matchSyscall(test.call, test.pattern)
     59 		if res != test.result {
     60 			t.Errorf("#%v: pattern=%q call=%q want=%v got=%v",
     61 				i, test.pattern, test.call, test.result, res)
     62 		}
     63 	}
     64 }
     65