| 1 | package rootstate |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestPersistentMountLineage(t *testing.T) { |
| 6 | base := "10 1 8:1 / / rw - ext4 /dev/root rw\n" |
| 7 | bound := base + "20 10 8:1 /opt /opt rw - ext4 /dev/root rw\n" |
| 8 | got, err := persistentMountLineage(bound, 20) |
| 9 | if err != nil || got != "/opt\t/opt\n/\t/\n" { |
| 10 | t.Fatal(got, err) |
| 11 | } |
| 12 | renumbered := "110 1 8:7 / / rw - ext4 /dev/other rw\n120 110 8:7 /opt /opt rw - ext4 /dev/other rw\n" |
| 13 | again, err := persistentMountLineage(renumbered, 120) |
| 14 | if err != nil || again != got { |
| 15 | t.Fatal(again, err) |
| 16 | } |
| 17 | stacked, err := persistentMountLineage(bound+"30 20 8:1 /opt /opt rw - ext4 /dev/root rw\n", 30) |
| 18 | if err != nil || stacked == got { |
| 19 | t.Fatal(stacked, err) |
| 20 | } |
| 21 | for _, malformed := range []string{ |
| 22 | "20 99 8:1 /opt /opt rw - ext4 /dev/root rw\n", |
| 23 | "20 20 8:1 / / rw - ext4 /dev/root rw\n", |
| 24 | } { |
| 25 | if _, err := persistentMountLineage(malformed, 20); err == nil { |
| 26 | t.Fatal("invalid ancestry accepted", malformed) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestPersistentMountSubstitutionWithRecycledNumbers(t *testing.T) { |
| 32 | 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"} |
| 33 | after := before |
| 34 | after.MountLineage = "/opt\t/opt\n" + before.MountLineage |
| 35 | if before.Same(after) { |
| 36 | t.Fatal("stacked mount accepted with recycled mount and namespace numbers") |
| 37 | } |
| 38 | after = before |
| 39 | after.Mount++ |
| 40 | if !before.Same(after) { |
| 41 | t.Fatal("recyclable mount number established identity") |
| 42 | } |
| 43 | after.MountUnique++ |
| 44 | if before.Same(after) { |
| 45 | t.Fatal("replacement mount accepted within the same boot and namespace") |
| 46 | } |
| 47 | after.NamespaceUnique++ |
| 48 | if !before.Same(after) { |
| 49 | t.Fatal("recreated unchanged topology refused") |
| 50 | } |
| 51 | after.Boot = "reboot" |
| 52 | after.Device++ |
| 53 | if !before.Same(after) { |
| 54 | t.Fatal("reboot refused") |
| 55 | } |
| 56 | for _, missing := range []string{"mount", "namespace"} { |
| 57 | bad := before |
| 58 | if missing == "mount" { |
| 59 | bad.MountUnique = 0 |
| 60 | } else { |
| 61 | bad.NamespaceUnique = 0 |
| 62 | } |
| 63 | if before.Same(bad) || bad.Same(bad) { |
| 64 | t.Fatal("lineage without unique boot evidence accepted", missing) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestPersistentMountPrivateParent(t *testing.T) { |
| 70 | 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"} |
| 71 | after := before |
| 72 | after.MountNamespace = "host" |
| 73 | after.NamespaceUnique++ |
| 74 | after.MountUnique++ |
| 75 | after.MountLineage = "/data/raw\t/data/view\n/\t/\n" |
| 76 | if !before.Same(after) { |
| 77 | t.Fatal("private parent mount hid the unchanged child mount") |
| 78 | } |
| 79 | sameNamespace := before |
| 80 | sameNamespace.MountNamespace = after.MountNamespace |
| 81 | sameNamespace.NamespaceUnique = after.NamespaceUnique |
| 82 | sameNamespace.MountUnique = after.MountUnique |
| 83 | if sameNamespace.Same(after) { |
| 84 | t.Fatal("removed ancestor mount accepted within the same boot and namespace") |
| 85 | } |
| 86 | if after.Same(before) { |
| 87 | t.Fatal("added ancestor mount accepted") |
| 88 | } |
| 89 | substituted := before |
| 90 | substituted.MountLineage = "/data/raw\t/data/view\n/other\t/data\n/\t/\n" |
| 91 | if before.Same(substituted) { |
| 92 | t.Fatal("substituted ancestor mount accepted") |
| 93 | } |
| 94 | after.MountLineage = "/data/raw\t/data/view\n" + after.MountLineage |
| 95 | if before.Same(after) { |
| 96 | t.Fatal("substitution at recorded mountpoint accepted") |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestPersistentMountPrivateParentWithOffset(t *testing.T) { |
| 101 | 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"} |
| 102 | after := before |
| 103 | after.MountNamespace = "host" |
| 104 | after.NamespaceUnique++ |
| 105 | after.MountUnique++ |
| 106 | after.MountLineage = "/disk/tmp/fixture/raw\t/tmp/fixture/view\n/disk/tmp\t/tmp\n/\t/\n" |
| 107 | if !before.Same(after) { |
| 108 | t.Fatal("private self-bind beneath an offset bind refused") |
| 109 | } |
| 110 | for _, row := range []string{"/disk/other\t/tmp/fixture", "/tmp/fixture\t/tmp/fixture", "/disk/tmp/fixture\t/elsewhere", "@root:unknown\t/tmp/fixture"} { |
| 111 | bad := before |
| 112 | bad.MountLineage = "/disk/tmp/fixture/raw\t/tmp/fixture/view\n" + row + "\n/disk/tmp\t/tmp\n/\t/\n" |
| 113 | if bad.Same(after) { |
| 114 | t.Fatal("unproven parent accepted", row) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |