package migration import ( "encoding/json" "os" "path/filepath" "strings" "testing" ) func testConfig() Config { return Config{Source: Endpoint{Config: "/clusters/a/ceph.conf", Keyring: "/clusters/a/keyring", Filesystem: "cephfs", Group: "group", Subvolume: "source"}, Target: Endpoint{Config: "/clusters/b/ceph.conf", Keyring: "/clusters/b/keyring", Filesystem: "cephfs", Group: "group", Subvolume: "target"}} } func TestConfigRejectsUnsafePathsAndNames(t *testing.T) { for name, modify := range map[string]func(*Config){ "relative": func(c *Config) { c.Source.Config = "relative" }, "unclean": func(c *Config) { c.Target.Keyring = "/clusters/b/../b/keyring" }, "rsync-backend": func(c *Config) { c.Backend = "rsync" }, "flag-name": func(c *Config) { c.Target.Subvolume = "--force" }, "traversal": func(c *Config) { c.Source.Group = "../outside" }, "negative-rate": func(c *Config) { c.BandwidthKiB = -1 }, "overflow-rate": func(c *Config) { c.BandwidthKiB = 1 << 62 }, "bad-backend": func(c *Config) { c.Backend = "raw" }, "bad-workers": func(c *Config) { c.Workers = 65 }, "invalid-user": func(c *Config) { c.Source.User = "osd.0" }, } { t.Run(name, func(t *testing.T) { c := testConfig() modify(&c) if c.Validate() == nil { t.Fatal("accepted invalid config") } }) } if err := testConfig().Validate(); err != nil { t.Fatal(err) } } func TestNativeConfigAndResumeSettings(t *testing.T) { c := testConfig() c.Backend = "native" if err := c.Validate(); err != nil { t.Fatal(err) } implicit := c implicit.Backend = "" if !sameMigrationIdentity(c, implicit) { t.Fatal(`backend "" and "native" differ in resume identity`) } changed := c changed.Workers, changed.BandwidthKiB = 32, 100000 if !sameMigrationIdentity(c, changed) { t.Fatal("runtime settings invalidated resume identity") } changed.Target.Subvolume = "another" if sameMigrationIdentity(c, changed) { t.Fatal("accepted a different target") } } func TestStrictJSONConfig(t *testing.T) { b, _ := json.Marshal(testConfig()) for name, data := range map[string]string{"unknown": strings.TrimSuffix(string(b), "}") + ", \"typo\": true}", "trailing": string(b) + " {}", "truncated": string(b[:len(b)-2])} { t.Run(name, func(t *testing.T) { p := filepath.Join(t.TempDir(), "config.json") if err := os.WriteFile(p, []byte(data), 0600); err != nil { t.Fatal(err) } if _, err := ReadConfig(p); err == nil { t.Fatal("accepted invalid JSON config") } }) } } func TestPrivateDirectoriesNeverFollowsSymlink(t *testing.T) { root, outside := t.TempDir(), t.TempDir() if err := os.Symlink(outside, filepath.Join(root, "link")); err != nil { t.Fatal(err) } if err := privateDirectories(filepath.Join(root, "link", "new")); err == nil { t.Fatal("followed symlink") } if _, err := os.Stat(filepath.Join(outside, "new")); !os.IsNotExist(err) { t.Fatal("created directory through symlink") } }