func verifyLogging(t *testing.T, checkString string) { fd, err := os.Open("output.log") mt.AssertTrue(t, err == nil) var buf [4096]byte n, err := fd.Read(buf[:]) mt.AssertTrue(t, err == nil) mt.AssertTrue(t, n < 4096) mt.AssertStringEqual(t, string(buf[0:n]), checkString) }
func executeNetworkTest(t *testing.T, mode int) { imageFd, err := os.OpenFile(dummy, os.O_WRONLY|mode, 0777) mt.AssertNoError(t, err) const imageString string = "CORRECT UPDATE" n, err := imageFd.Write([]byte(imageString)) mt.AssertNoError(t, err) mt.AssertTrue(t, n == len(imageString)) imageFd.Close() newRunner := &testRunnerMulti{} newRunner.cmdlines = StringPointerList( "mount ", "fw_printenv boot_part", "mount ", "fw_printenv boot_part", "fw_setenv upgrade_available 1", "fw_setenv boot_part 3", "fw_setenv bootcount 0") mount_output := baseMountDevice + "2 on / type ext4 (rw)\n" + "proc on /proc type proc (rw,noexec,nosuid,nodev)\n" + baseMountDevice + "1 on /boot type ext4 (rw)\n" newRunner.outputs = []string{ mount_output, "boot_part=2", mount_output, "boot_part=2", "", "", ""} newRunner.ret_codes = []int{ 0, 0, 0, 0, 0, 0, 0} runner = newRunner httpString := fmt.Sprintf("http://localhost:%s/%s", testPortString, dummy) err = doMain([]string{"-rootfs", httpString}) if err != nil { if mode == os.O_TRUNC { // This update should fail. mt.AssertErrorSubstring(t, err, "Less than") return } t.Fatalf("Updating image failed: %s", err.Error()) } else { if mode == os.O_TRUNC { t.Fatal("Update should have failed") } } mt.AssertTrue(t, checkFileOverlapEqual(t, baseMountDevice+"3", dummy)) fd, err := os.Open(baseMountDevice + "3") mt.AssertNoError(t, err) buf := new([len(imageString)]byte) n, err = fd.Read(buf[:]) mt.AssertNoError(t, err) mt.AssertTrue(t, n == len(imageString)) mt.AssertStringEqual(t, string(buf[:]), imageString) fd.Close() }
func TestPartitionsAPI(t *testing.T) { prepareMockDevices(t) defer cleanupMockDevices() // Test various parts of the partitions API. newRunner := &testRunnerMulti{} newRunner.cmdlines = StringPointerList( "mount ", "fw_printenv boot_part", "mount ", "fw_printenv boot_part", "mount ", "fw_printenv boot_part", "mount ", "fw_printenv boot_part", "mount ", "fw_printenv boot_part", "mount ", "fw_printenv boot_part") mount_output3 := baseMountDevice + "3 on / type ext4 (rw)\n" + "proc on /proc type proc (rw,noexec,nosuid,nodev)\n" + baseMountDevice + "1 on /boot type ext4 (rw)\n" mount_output4 := baseMountDevice + "4 on / type ext4 (rw)\n" + "proc on /proc type proc (rw,noexec,nosuid,nodev)\n" + baseMountDevice + "1 on /boot type ext4 (rw)\n" newRunner.outputs = []string{ mount_output3, "boot_part=3", mount_output3, "boot_part=3", mount_output4, "boot_part=4", mount_output4, "boot_part=3", mount_output4, "boot_part=4", mount_output4, "boot_part=3"} newRunner.ret_codes = []int{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} runner = newRunner statter.(*testStatter).active = "3" defer func() { statter.(*testStatter).active = "2" }() part, err := getActivePartition() mt.AssertTrue(t, err == nil) mt.AssertStringEqual(t, part, baseMountDevice+"3") part, err = getInactivePartition() mt.AssertTrue(t, err == nil) mt.AssertStringEqual(t, part, baseMountDevice+"2") statter.(*testStatter).active = "4" part, err = getActivePartition() mt.AssertTrue(t, err != nil) part, err = getActivePartition() mt.AssertTrue(t, err != nil) part, err = getInactivePartition() mt.AssertTrue(t, err != nil) part, err = getInactivePartition() mt.AssertTrue(t, err != nil) rootStat, err := statter.Stat("/") if err != nil { t.Fatal("Should never happen") } root := rootStat.Sys().(*syscall.Stat_t) mt.AssertTrue(t, isMountedRoot("no-such-file", root) == false) }