// Test network updates, very similar to TestMockRootfs, but using network as // the transport for the image. func TestNetworkRootfs(t *testing.T) { prepareMockDevices(t) defer cleanupMockDevices() var server http.Server server.Handler = http.FileServer(http.Dir(".")) addr := ":" + testPortString listen, err := net.Listen("tcp", addr) mt.AssertNoError(t, err) defer listen.Close() go server.Serve(listen) // Do this test twice, once with a valid update, and once with a too // short/broken update. for _, mode := range []int{0, os.O_TRUNC}[:] { executeNetworkTest(t, mode) } }
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() }