Files
RemoteCompilation/server/access_test.go
T

120 lines
4.2 KiB
Go

package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
func adminTest(t *testing.T, s *Server, args ...string) string {
t.Helper()
var out bytes.Buffer
if e := runAdmin(append([]string{"--data", s.root}, args...), &out); e != nil {
t.Fatal(e)
}
return out.String()
}
func TestAdminMigrationAndHotReload(t *testing.T) {
s := testServer(t)
p := project(t, s)
if e := s.initAccessPolicy(filepath.Join(s.root, "access.json")); e != nil {
t.Fatal(e)
}
policy, e := readAccessPolicy(s.accessFile)
if e != nil || len(policy.WorkspaceRoots) != 1 || len(policy.TaskRules) != 1 {
t.Fatal(policy, e)
}
added := t.TempDir()
work := filepath.Join(added, "project")
os.MkdirAll(work, 0700)
adminTest(t, s, "root", "add", added)
adminTest(t, s, "root", "add", added)
if w := call(t, s, "GET", "/health", nil); w.Code != 200 {
t.Fatal(w.Body.String())
}
if _, e := s.validateWorkspace(work); e != nil {
t.Fatal(e)
}
if len(s.allowedWorkspaceRoots()) != 2 {
t.Fatal("duplicate or lost startup root")
}
adminTest(t, s, "task", "add", ":app:assemble*")
for _, task := range []string{":app:assembleUiuiDebug", ":app:assembleNewFlavorRelease"} {
w := call(t, s, "POST", "/projects/"+p+"/builds", map[string]string{"task": task, "revision": "invalid"})
if w.Code != 409 {
t.Fatal("rule not applied without restart", w.Code, w.Body.String())
}
}
if s.taskAllowed(":other:assembleDebug") || s.taskAllowed(":app:assembleDebug --scan") || s.taskAllowed(":app:clean") {
t.Fatal("rule expanded beyond intended scope")
}
adminTest(t, s, "task", "remove", ":app:assemble*")
w := call(t, s, "POST", "/projects/"+p+"/builds", map[string]string{"task": ":app:assembleUiuiDebug", "revision": "invalid"})
if w.Code != 400 || !strings.Contains(w.Body.String(), " admin ") || !strings.Contains(w.Body.String(), ":app:assembleUiuiDebug") {
t.Fatal("missing actionable task error", w.Body.String())
}
adminTest(t, s, "root", "remove", added)
call(t, s, "GET", "/health", nil)
if _, e = s.validateWorkspace(work); e == nil || !strings.Contains(e.Error(), "root add") {
t.Fatal("removed root still allowed or missing hint", e)
}
if !strings.Contains(adminTest(t, s, "task", "list"), ":app:assembleDebug") {
t.Fatal("list lost migrated task")
}
// A restart must read the CLI-managed state rather than overwrite it from flags.
s2 := testServer(t)
s2.root = s.root
s2.workspaceRoot = t.TempDir()
if e = s2.initAccessPolicy(s.accessFile); e != nil {
t.Fatal(e)
}
if len(s2.allowedWorkspaceRoots()) != 1 || s2.allowedWorkspaceRoots()[0] != policy.WorkspaceRoots[0] {
t.Fatal("restart overwrote policy")
}
}
func TestAdminRejectsInvalidAndLockedEdits(t *testing.T) {
s := testServer(t)
if e := s.initAccessPolicy(filepath.Join(s.root, "access.json")); e != nil {
t.Fatal(e)
}
before, _ := os.ReadFile(s.accessFile)
for _, rule := range []string{"*", ":app:*", ":app:clean*", ":app:assembleDebug --scan", "task&command"} {
if e := runAdmin([]string{"--data", s.root, "task", "add", rule}, &bytes.Buffer{}); e == nil {
t.Fatal("invalid rule accepted", rule)
}
}
if e := runAdmin([]string{"--data", s.root, "root", "add", "relative"}, &bytes.Buffer{}); e == nil {
t.Fatal("relative root accepted")
}
os.Mkdir(s.accessFile+".lock", 0700)
if e := runAdmin([]string{"--data", s.root, "task", "add", ":app:assembleXDebug"}, &bytes.Buffer{}); e == nil {
t.Fatal("lock bypassed")
}
os.Remove(s.accessFile + ".lock")
after, _ := os.ReadFile(s.accessFile)
if !bytes.Equal(before, after) {
t.Fatal("rejected commands changed config")
}
}
func TestPolicyCorruptionFailsClosedAndEmptyRootsDoNotFallback(t *testing.T) {
s := testServer(t)
if e := s.initAccessPolicy(filepath.Join(s.root, "access.json")); e != nil {
t.Fatal(e)
}
os.WriteFile(s.accessFile, []byte("broken"), 0600)
if w := call(t, s, "GET", "/health", nil); w.Code != 503 {
t.Fatal("invalid policy silently ignored", w.Code)
}
if e := writeAccessPolicy(s.accessFile, AccessPolicy{[]string{}, []string{}}); e != nil {
t.Fatal(e)
}
if w := call(t, s, "GET", "/directories", nil); w.Code != 400 {
t.Fatal("empty policy must not panic or restore defaults", w.Code)
}
if len(s.allowedWorkspaceRoots()) != 0 || s.taskAllowed(":app:assembleDebug") {
t.Fatal("removed defaults resurrected")
}
}