| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package simulator |
| |
| import ( |
| "crypto/rand" |
| "fmt" |
| "fuchsia.googlesource.com/testing/qemu" |
| "os" |
| "path/filepath" |
| "testing" |
| ) |
| |
| func zbiPath(t *testing.T) string { |
| ex, err := os.Executable() |
| if err != nil { |
| t.Fatal(err) |
| return "" |
| } |
| exPath := filepath.Dir(ex) |
| // TODO(47555): get the path from a build API instead. |
| return filepath.Join(exPath, "../obj/build/images/recovery/recovery-eng.zbi") |
| } |
| |
| func randomTokenAsString() string { |
| b := make([]byte, 32) |
| _, err := rand.Read(b) |
| if err != nil { |
| panic(err) |
| } |
| |
| ret := "" |
| for i := 0; i < 32; i++ { |
| ret += fmt.Sprintf("%x", b[i]) |
| } |
| return ret |
| } |
| |
| // TestUnpack checks that we can unpack qemu. |
| func TestBoot(t *testing.T) { |
| distro, err := qemu.Unpack() |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer distro.Delete() |
| |
| arch, err := distro.TargetCPU() |
| if err != nil { |
| t.Fatal(err) |
| } |
| |
| i := distro.Create(qemu.Params{ |
| Arch: arch, |
| ZBI: zbiPath(t), |
| // This test uses additional memory on ASAN builds than normal. |
| Memory: 3072, |
| }) |
| |
| i.Start() |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer i.Kill() |
| |
| i.WaitForLogMessage("recovery: started") |
| } |
| |
| // TestSerial verifies that the serial shell is enabled for recovery-eng. |
| func TestSerialShellEnabled(t *testing.T) { |
| distro, err := qemu.Unpack() |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer distro.Delete() |
| |
| arch, err := distro.TargetCPU() |
| if err != nil { |
| t.Fatal(err) |
| } |
| |
| i := distro.Create(qemu.Params{ |
| Arch: arch, |
| ZBI: zbiPath(t), |
| AppendCmdline: "devmgr.log-to-debuglog", |
| // This test uses additional memory on ASAN builds than normal. |
| Memory: 3072, |
| }) |
| |
| i.Start() |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer i.Kill() |
| |
| i.WaitForLogMessage("Launching /boot/bin/sh (sh:console)") |
| tokenFromSerial := randomTokenAsString() |
| i.RunCommand("echo '" + tokenFromSerial + "'") |
| i.WaitForLogMessage(tokenFromSerial) |
| } |