package rootstate import "testing" func TestPersistentMountLineage(t *testing.T) { base := "10 1 8:1 / / rw - ext4 /dev/root rw\n" bound := base + "20 10 8:1 /opt /opt rw - ext4 /dev/root rw\n" got, err := persistentMountLineage(bound, 20) if err != nil || got != "/opt\t/opt\n/\t/\n" { t.Fatal(got, err) } renumbered := "110 1 8:7 / / rw - ext4 /dev/other rw\n120 110 8:7 /opt /opt rw - ext4 /dev/other rw\n" again, err := persistentMountLineage(renumbered, 120) if err != nil || again != got { t.Fatal(again, err) } stacked, err := persistentMountLineage(bound+"30 20 8:1 /opt /opt rw - ext4 /dev/root rw\n", 30) if err != nil || stacked == got { t.Fatal(stacked, err) } for _, malformed := range []string{ "20 99 8:1 /opt /opt rw - ext4 /dev/root rw\n", "20 20 8:1 / / rw - ext4 /dev/root rw\n", } { if _, err := persistentMountLineage(malformed, 20); err == nil { t.Fatal("invalid ancestry accepted", malformed) } } } func TestPersistentMountSubstitutionWithRecycledNumbers(t *testing.T) { before := FileIdentity{Boot: "boot", Filesystem: "fs", Handle: "handle", Inode: 2, Mount: 3, MountUnique: 3, NamespaceUnique: 1, MountNamespace: "mnt:[1]", MountRoot: "/opt", MountPoint: "/opt", MountLineage: "/opt\t/opt\n/\t/\n"} after := before after.MountLineage = "/opt\t/opt\n" + before.MountLineage if before.Same(after) { t.Fatal("stacked mount accepted with recycled mount and namespace numbers") } after = before after.Mount++ if !before.Same(after) { t.Fatal("recyclable mount number established identity") } after.MountUnique++ if before.Same(after) { t.Fatal("replacement mount accepted within the same boot and namespace") } after.NamespaceUnique++ if !before.Same(after) { t.Fatal("recreated unchanged topology refused") } after.Boot = "reboot" after.Device++ if !before.Same(after) { t.Fatal("reboot refused") } for _, missing := range []string{"mount", "namespace"} { bad := before if missing == "mount" { bad.MountUnique = 0 } else { bad.NamespaceUnique = 0 } if before.Same(bad) || bad.Same(bad) { t.Fatal("lineage without unique boot evidence accepted", missing) } } } func TestPersistentMountPrivateParent(t *testing.T) { before := FileIdentity{Boot: "boot", Filesystem: "fs", Handle: "handle", Inode: 2, Mount: 3, MountUnique: 3, NamespaceUnique: 1, MountNamespace: "private", MountRoot: "/data/raw", MountPoint: "/data/view", MountLineage: "/data/raw\t/data/view\n/data\t/data\n/\t/\n"} after := before after.MountNamespace = "host" after.NamespaceUnique++ after.MountUnique++ after.MountLineage = "/data/raw\t/data/view\n/\t/\n" if !before.Same(after) { t.Fatal("private parent mount hid the unchanged child mount") } sameNamespace := before sameNamespace.MountNamespace = after.MountNamespace sameNamespace.NamespaceUnique = after.NamespaceUnique sameNamespace.MountUnique = after.MountUnique if sameNamespace.Same(after) { t.Fatal("removed ancestor mount accepted within the same boot and namespace") } if after.Same(before) { t.Fatal("added ancestor mount accepted") } substituted := before substituted.MountLineage = "/data/raw\t/data/view\n/other\t/data\n/\t/\n" if before.Same(substituted) { t.Fatal("substituted ancestor mount accepted") } after.MountLineage = "/data/raw\t/data/view\n" + after.MountLineage if before.Same(after) { t.Fatal("substitution at recorded mountpoint accepted") } } func TestPersistentMountPrivateParentWithOffset(t *testing.T) { before := FileIdentity{Boot: "boot", Filesystem: "fs", Handle: "handle", Inode: 2, Mount: 3, MountUnique: 3, NamespaceUnique: 1, MountNamespace: "private", MountRoot: "/disk/tmp/fixture/raw", MountPoint: "/tmp/fixture/view", MountLineage: "/disk/tmp/fixture/raw\t/tmp/fixture/view\n/disk/tmp/fixture\t/tmp/fixture\n/disk/tmp\t/tmp\n/\t/\n"} after := before after.MountNamespace = "host" after.NamespaceUnique++ after.MountUnique++ after.MountLineage = "/disk/tmp/fixture/raw\t/tmp/fixture/view\n/disk/tmp\t/tmp\n/\t/\n" if !before.Same(after) { t.Fatal("private self-bind beneath an offset bind refused") } for _, row := range []string{"/disk/other\t/tmp/fixture", "/tmp/fixture\t/tmp/fixture", "/disk/tmp/fixture\t/elsewhere", "@root:unknown\t/tmp/fixture"} { bad := before bad.MountLineage = "/disk/tmp/fixture/raw\t/tmp/fixture/view\n" + row + "\n/disk/tmp\t/tmp\n/\t/\n" if bad.Same(after) { t.Fatal("unproven parent accepted", row) } } }