| 1 | |
| 2 | |
| 3 | package reversesshfs |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | |
| 15 | func TestRootedOwnRemovalNotRelayed(t *testing.T) { |
| 16 | c, root, h := setupRootedHandlers(t, false) |
| 17 | p := func(s string) string { return filepath.Join(root, s) } |
| 18 | create := func(s string) { |
| 19 | t.Helper() |
| 20 | f, err := c.Create(p(s)) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | f.Close() |
| 25 | } |
| 26 | create("f") |
| 27 | if err := c.Remove(p("f")); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if h.expectRemove(p("f")) { |
| 31 | t.Fatal("the host's event for the guest's own removal was relayed") |
| 32 | } |
| 33 | create("f") |
| 34 | if err := c.Remove(p("f")); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if _, err := os.Lstat(p("f")); err == nil { |
| 38 | t.Fatal("rm f; touch f; rm f left the file on the host") |
| 39 | } |
| 40 | |
| 41 | |
| 42 | create("log") |
| 43 | if err := c.PosixRename(p("log"), p("log.1")); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | create("log") |
| 47 | if h.expectRemove(p("log")) { |
| 48 | t.Fatal("the host's event for the guest's rename was relayed") |
| 49 | } |
| 50 | if err := c.Remove(p("log")); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if _, err := os.Lstat(p("log")); err == nil { |
| 54 | t.Fatal("the new log was not removed") |
| 55 | } |
| 56 | |
| 57 | |
| 58 | now := time.Now() |
| 59 | h.now = func() time.Time { return now.Add(ownRemovalTTL + time.Second) } |
| 60 | if !h.expectRemove(p("log")) { |
| 61 | t.Error("not relayed after the window") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | func TestRootedFailedRemovalNotOwn(t *testing.T) { |
| 67 | c, root, h := setupRootedHandlers(t, false) |
| 68 | p := func(s string) string { return filepath.Join(root, s) } |
| 69 | if err := os.MkdirAll(p("d/sub"), 0o755); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if err := c.RemoveDirectory(p("d")); err == nil { |
| 73 | t.Fatal("a folder with something in it was removed") |
| 74 | } |
| 75 | if !h.expectRemove(p("d")) { |
| 76 | t.Error("a removal that failed hid the host's deletion") |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | func TestRootedTokenDroppedByCreate(t *testing.T) { |
| 82 | c, root, h := setupRootedHandlers(t, false) |
| 83 | p := func(s string) string { return filepath.Join(root, s) } |
| 84 | if !h.expectRemove(p("new")) { |
| 85 | t.Fatal("not expected") |
| 86 | } |
| 87 | f, err := c.Create(p("new")) |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | f.Close() |
| 92 | if err := c.Remove(p("new")); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if _, err := os.Lstat(p("new")); err == nil { |
| 96 | t.Fatal("the removal was swallowed though the guest created the file") |
| 97 | } |
| 98 | |
| 99 | |
| 100 | for name, create := range map[string]func(string) error{ |
| 101 | "mkdir": func(s string) error { return c.Mkdir(s) }, |
| 102 | "symlink": func(s string) error { return c.Symlink("x", s) }, |
| 103 | "rename": func(s string) error { return c.PosixRename(p("src/main.go"), s) }, |
| 104 | } { |
| 105 | target := p("t-" + name) |
| 106 | h.expectRemove(target) |
| 107 | if err := create(target); err != nil { |
| 108 | t.Fatal(name, err) |
| 109 | } |
| 110 | if err := c.Remove(target); err != nil { |
| 111 | if err := c.RemoveDirectory(target); err != nil { |
| 112 | t.Fatal(name, err) |
| 113 | } |
| 114 | } |
| 115 | if _, err := os.Lstat(target); err == nil { |
| 116 | t.Errorf("%s: the removal was swallowed", name) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | func TestExpectRemoveMany(t *testing.T) { |
| 123 | _, root, h := setupRootedHandlers(t, false) |
| 124 | start := time.Now() |
| 125 | const n = 200000 |
| 126 | for i := 0; i < n; i++ { |
| 127 | h.expectRemove(filepath.Join(root, "src", fmt.Sprint(i))) |
| 128 | } |
| 129 | if d := time.Since(start); d > 5*time.Second { |
| 130 | t.Fatalf("%d tokens took %v", n, d) |
| 131 | } |
| 132 | |
| 133 | now := time.Now() |
| 134 | h.now = func() time.Time { return now.Add(noopRemovalTTL + time.Second) } |
| 135 | h.expectRemove(filepath.Join(root, "x")) |
| 136 | h.mu.Lock() |
| 137 | defer h.mu.Unlock() |
| 138 | if len(h.relay.tokens.m) != 1 { |
| 139 | t.Errorf("%d tokens left", len(h.relay.tokens.m)) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestExpiringKeyAddedAgain(t *testing.T) { |
| 144 | e := newExpiring(10 * time.Second) |
| 145 | t0 := time.Now() |
| 146 | e.add("k", t0) |
| 147 | e.add("k", t0.Add(8*time.Second)) |
| 148 | |
| 149 | if !e.has("k", t0.Add(12*time.Second)) { |
| 150 | t.Error("key added again was purged by its first entry") |
| 151 | } |
| 152 | if e.has("k", t0.Add(19*time.Second)) { |
| 153 | t.Error("key not expired") |
| 154 | } |
| 155 | if len(e.m) != 0 { |
| 156 | t.Error("left over") |
| 157 | } |
| 158 | } |