Пример #1
0
func TestRootfsUpdateNoDeviceMatch(t *testing.T) {
	prepareMockDevices(t)
	defer cleanupMockDevices()

	// Try to query active partition again, when U-Boot and mount don't
	// agree.

	newRunner := &testRunnerMulti{}

	newRunner.cmdlines = StringPointerList(
		"mount ")

	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}

	newRunner.ret_codes = []int{
		0}

	runner = newRunner
	statter.(*testStatter).active = "5"
	_, err := getActivePartition()
	statter.(*testStatter).active = "2"
	if err == nil {
		t.Fatal("A valid partition should not have been detected")
	}
	mt.AssertErrorSubstring(t, err, "device ID")
}
Пример #2
0
func TestMissingArgs(t *testing.T) {
	if err := doMain([]string{}); err == nil {
		t.Fatal("Calling doMain() with no arguments does not " +
			"produce error")
	} else {
		mt.AssertErrorSubstring(t, err, errMsgNoArgumentsGiven.Error())
	}
}
Пример #3
0
func TestRootfsUpdateFailingUBoot(t *testing.T) {
	prepareMockDevices(t)
	defer cleanupMockDevices()

	// Try to execute rootfs operation with the dummy file, and have U-Boot
	// fail.
	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,
		1,
		0,
		0}

	runner = newRunner
	if err := doMain([]string{"-rootfs", dummy}); err == nil {
		t.Fatal("Enabling the partition should have failed")
	} else {
		mt.AssertErrorSubstring(t, err, "Unable to activate partition")
	}
}
Пример #4
0
func TestRootfsUpdateUBootDisagreement(t *testing.T) {
	prepareMockDevices(t)
	defer cleanupMockDevices()

	// Try to query active partition again, when U-Boot and mount don't
	// agree.

	newRunner := &testRunnerMulti{}

	newRunner.cmdlines = StringPointerList(
		"mount ",
		"fw_printenv boot_part")

	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=3"}

	newRunner.ret_codes = []int{
		0,
		0}

	runner = newRunner
	err := doMain([]string{"-rootfs", dummy})
	if err == nil {
		t.Fatal("Updating image should have failed " +
			"(mount and U-Boot don't agree on boot " +
			"partition)")
	}
	mt.AssertTrue(t, !checkFileOverlapEqual(t, baseMountDevice+"3", dummy))
	mt.AssertErrorSubstring(t, err,
		"agree")
}
Пример #5
0
func TestLoggingOptions(t *testing.T) {
	if err := doMain([]string{"-commit", "-log-level", "crap"}); err == nil {
		t.Fatal("'crap' log level should have given error")
	} else {
		// Should have a reference to log level.
		mt.AssertErrorSubstring(t, err, "Level")
	}

	if err := doMain([]string{"-info", "-log-level", "debug"}); err == nil {
		t.Fatal("Incompatible log levels should have given error")
	} else {
		mt.AssertErrorSubstring(t, err,
			errMsgIncompatibleLogOptions.Error())
	}

	var buf bytes.Buffer
	oldOutput := log.Log.Out
	log.SetOutput(&buf)
	defer log.SetOutput(oldOutput)

	// Ignore errors for now, we just want to know if the logging level was
	// applied.
	log.SetLevel(log.DebugLevel)
	doMain([]string{"-log-level", "panic"})
	log.Debugln("Should not show")
	doMain([]string{"-debug"})
	log.Debugln("Should show")
	doMain([]string{"-info"})
	log.Debugln("Should also not show")

	mt.AssertTrue(t, strings.Index(buf.String(), "Should show") >= 0)
	mt.AssertTrue(t, strings.Index(buf.String(), "Should not show") < 0)
	mt.AssertTrue(t, strings.Index(buf.String(), "Should also not show") < 0)

	doMain([]string{"-log-modules", "main_test,MyModule"})
	log.Errorln("Module filter should show main_test")
	log.PushModule("MyModule")
	log.Errorln("Module filter should show MyModule")
	log.PushModule("MyOtherModule")
	log.Errorln("Module filter should not show MyOtherModule")
	log.PopModule()
	log.PopModule()

	mt.AssertTrue(t, strings.Index(buf.String(),
		"Module filter should show main_test") >= 0)
	mt.AssertTrue(t, strings.Index(buf.String(),
		"Module filter should show MyModule") >= 0)
	mt.AssertTrue(t, strings.Index(buf.String(),
		"Module filter should not show MyOtherModule") < 0)

	doMain([]string{"-log-file", "test.log"})
	log.Errorln("Should be in log file")
	fd, err := os.Open("test.log")
	mt.AssertTrue(t, err == nil)

	var bytebuf [4096]byte
	n, err := fd.Read(bytebuf[:])
	mt.AssertTrue(t, err == nil)
	mt.AssertTrue(t, strings.Index(string(bytebuf[0:n]),
		"Should be in log file") >= 0)

	err = doMain([]string{"-no-syslog"})
	// Just check that the flag can be specified.
	mt.AssertTrue(t, err != nil)
	mt.AssertTrue(t, strings.Index(err.Error(), "syslog") < 0)
}
Пример #6
0
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()
}