diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-09-14 15:03:18 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-09-14 15:15:06 +0200 |
| commit | 6c16e36a7bc8ca1fa66de37e6e70003e0f14e2e8 (patch) | |
| tree | 5966295691446f3fe51b59430425e28a42e39194 /pkg/host/machine_info_linux_test.go | |
| parent | 3e84253bf41d63c55f92679b1aab9102f2f4949a (diff) | |
pkg/host: make machine info tests linux-specific
TestScanCPUInfo does not build on !linux.
TestMachineInfoLinux builds, but does not do anything useful.
Diffstat (limited to 'pkg/host/machine_info_linux_test.go')
| -rw-r--r-- | pkg/host/machine_info_linux_test.go | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/pkg/host/machine_info_linux_test.go b/pkg/host/machine_info_linux_test.go new file mode 100644 index 000000000..a96ca232f --- /dev/null +++ b/pkg/host/machine_info_linux_test.go @@ -0,0 +1,145 @@ +// Copyright 2020 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 host + +import ( + "bufio" + "bytes" + "strings" + "testing" +) + +func TestMachineInfoLinux(t *testing.T) { + result, err := CollectMachineInfo() + if err != nil { + t.Fatal(err) + } + + scanner := bufio.NewScanner(bytes.NewReader(result)) + + for scanner.Scan() { + line := scanner.Text() + + if line == "[CPU Info]" { + checkCPUInfo(t, scanner) + } + if line == "[KVM]" { + checkKVMInfo(t, scanner) + } + } +} + +func checkCPUInfo(t *testing.T, scanner *bufio.Scanner) { + keys := make(map[string]bool) + for scanner.Scan() { + line := scanner.Text() + // End of CPU Info section. + if strings.HasPrefix(line, "-----") { + break + } + splitted := strings.Split(line, ":") + if len(splitted) != 2 { + t.Fatalf("the format of line \"%s\" is not correct", line) + } + key := strings.TrimSpace(splitted[0]) + keys[key] = true + } + + importantKeys := [][]string{ + {"vendor", "vendor_id", "CPU implementer"}, + {"model", "CPU part", "cpu model"}, + {"flags", "features", "Features", "ASEs implemented", "type"}, + } + for _, possibleNames := range importantKeys { + exists := false + for _, name := range possibleNames { + if keys[name] { + exists = true + break + } + } + if !exists { + t.Fatalf("one of {%s} should exists in the output, but not found", + strings.Join(possibleNames, ", ")) + } + } +} + +func checkKVMInfo(t *testing.T, scanner *bufio.Scanner) { + for scanner.Scan() { + line := scanner.Text() + if line == "" { + continue + } + if strings.HasPrefix(line, "-----") { + break + } + splitted := strings.Split(line, ":") + if len(splitted) != 2 { + t.Fatalf("the format of line \"%s\" is not correct", line) + } + key := strings.TrimSpace(splitted[0]) + if key == "" { + t.Fatalf("empty key") + } + if key[0] != '/' { + continue + } + + if !strings.HasPrefix(key, "/sys/module/kvm") { + t.Fatalf("the directory does not match /sys/module/kvm*") + } + } +} + +func TestScanCPUInfo(t *testing.T) { + input := `A: a +B: b + +C: c1 +D: d +C: c1 +D: d +C: c2 +D: d +` + + output := []struct { + key, val string + }{ + {"A", "a"}, + {"B", "b"}, + {"C", "c1, c1, c2"}, + {"D", "d"}, + } + scanner := bufio.NewScanner(strings.NewReader(input)) + buffer := new(bytes.Buffer) + scanCPUInfo(buffer, scanner) + result := bufio.NewScanner(buffer) + + idx := 0 + for result.Scan() { + line := result.Text() + splitted := strings.Split(line, ":") + if len(splitted) != 2 { + t.Fatalf("the format of line \"%s\" is not correct", line) + } + key := strings.TrimSpace(splitted[0]) + val := strings.TrimSpace(splitted[1]) + if idx >= len(output) { + t.Fatalf("additional line \"%s: %s\"", key, val) + } + expected := output[idx] + if key != expected.key || val != expected.val { + t.Fatalf("expected \"%s: %s\", got \"%s: %s\"", + expected.key, expected.val, key, val) + } + idx++ + } + if idx < len(output) { + expected := output[idx] + t.Fatalf("expected \"%s: %s\", got end of output", + expected.key, expected.val) + } +} |
