Files
RemoteCompilation/server/workspace_roots_test.go

71 lines
2.0 KiB
Go

package main
import (
"encoding/json"
"net/url"
"os"
"path/filepath"
"testing"
)
func TestMultipleWorkspaceRootsBrowseAndIsolation(t *testing.T) {
s := testServer(t)
one := t.TempDir()
two := t.TempDir()
s.workspaceRoots = []string{one, two, one}
for _, root := range []string{one, two} {
project := filepath.Join(root, "project")
os.MkdirAll(project, 0700)
if _, e := s.validateWorkspace(project); e != nil {
t.Fatal("allowed project rejected", e)
}
w := call(t, s, "GET", "/directories?path="+url.QueryEscape(root), nil)
if w.Code != 200 {
t.Fatal(w.Body.String())
}
var listing struct {
Parent string `json:"parent"`
Root string `json:"root"`
}
json.Unmarshal(w.Body.Bytes(), &listing)
if listing.Root != root || listing.Parent != "@roots" {
t.Fatal("root browser cannot return to allowed-root list", listing)
}
}
w := call(t, s, "GET", "/directories", nil)
var list struct {
Roots []string `json:"roots"`
Directories []map[string]string `json:"directories"`
Selectable bool `json:"selectable"`
}
json.Unmarshal(w.Body.Bytes(), &list)
if w.Code != 200 || len(list.Roots) != 2 || len(list.Directories) != 2 || list.Selectable {
t.Fatal("invalid virtual root list", w.Body.String())
}
if w = call(t, s, "GET", "/directories?path=%40roots", nil); w.Code != 200 {
t.Fatal("root list back navigation failed")
}
outside := t.TempDir()
if _, e := s.validateWorkspace(outside); e == nil {
t.Fatal("unconfigured sibling allowed")
}
if w = call(t, s, "GET", "/directories?path="+url.QueryEscape(outside), nil); w.Code != 400 {
t.Fatal("browser escaped allowed roots")
}
for _, root := range []string{one, two} {
if _, e := s.validateWorkspace(root); e == nil {
t.Fatal("allowed parent used as project")
}
}
var flags workspaceRootFlags
if e := flags.Set(one); e != nil {
t.Fatal(e)
}
if e := flags.Set(two); e != nil {
t.Fatal(e)
}
if len(flags) != 2 {
t.Fatal("repeated root flag overwrote prior value")
}
}