152 lines
5.9 KiB
Go
152 lines
5.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestManifestRechecksActualContent(t *testing.T) {
|
|
s := testServer(t)
|
|
p := project(t, s)
|
|
call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "r", Files: map[string]File{"a.txt": file("source")}})
|
|
os.WriteFile(filepath.Join(s.work(p), "a.txt"), []byte("changed by build"), 0600)
|
|
w := call(t, s, "GET", "/projects/"+p+"/manifest", nil)
|
|
var actual Project
|
|
json.Unmarshal(w.Body.Bytes(), &actual)
|
|
if len(actual.Files) != 0 {
|
|
t.Fatal("advertised stale hash, causing missing content")
|
|
}
|
|
if w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "r2", Files: map[string]File{"a.txt": file("source")}}); w.Code != 200 {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
}
|
|
func TestChangedWorkspaceRootRetainsHistoryButBlocksSourceAccess(t *testing.T) {
|
|
s:=testServer(t);oldRoot:=t.TempDir();s.workspaceRoot=oldRoot
|
|
work:=filepath.Join(oldRoot,"old-project");os.MkdirAll(work,0700)
|
|
w:=call(t,s,"POST","/projects",WorkspaceRequest{WorkspacePath:work})
|
|
if w.Code!=200 {t.Fatal(w.Body.String())}
|
|
var allocated map[string]string;json.Unmarshal(w.Body.Bytes(),&allocated);p:=allocated["id"]
|
|
b:=&Build{ID:id(),Project:p,Status:"success",Logs:[]string{},Artifacts:[]Artifact{}}
|
|
s.builds[b.ID]=b
|
|
s.workspaceRoot=t.TempDir()
|
|
if w=call(t,s,"GET","/projects/"+p+"/builds",nil);w.Code!=200{t.Fatal("history blocked by root change",w.Body.String())}
|
|
if w=call(t,s,"GET","/builds/"+b.ID,nil);w.Code!=200{t.Fatal("build record unavailable")}
|
|
if w=call(t,s,"GET","/projects/"+p+"/manifest",nil);w.Code!=400{t.Fatal("out-of-root source readable")}
|
|
if w=call(t,s,"POST","/projects/"+p+"/sync",SyncRequest{Revision:"new",Files:map[string]File{"file.txt":file("data")}});w.Code!=400{t.Fatal("out-of-root source writable")}
|
|
if _,e:=os.Stat(filepath.Join(work,"file.txt"));!os.IsNotExist(e){t.Fatal("source changed")}
|
|
}
|
|
func TestExistingWorkspaceBoundaryAndGitProtection(t *testing.T) {
|
|
if _, e := exec.LookPath("git"); e != nil {
|
|
t.Skip("git unavailable")
|
|
}
|
|
s := testServer(t)
|
|
s.workspaceRoot = t.TempDir()
|
|
work := filepath.Join(s.workspaceRoot, "中文 project")
|
|
os.MkdirAll(work, 0700)
|
|
git := func(args ...string) {
|
|
t.Helper()
|
|
if out, e := exec.Command("git", append([]string{"-C", work}, args...)...).CombinedOutput(); e != nil {
|
|
t.Fatalf("git: %v %s", e, out)
|
|
}
|
|
}
|
|
git("init", "-q")
|
|
os.WriteFile(filepath.Join(work, "a.txt"), []byte("base"), 0600)
|
|
os.WriteFile(filepath.Join(work, ".gitignore"), []byte("ignored/\n"), 0600)
|
|
git("add", ".")
|
|
git("-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-qm", "base")
|
|
snapshot, e := gitSnapshot(work)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
req := WorkspaceRequest{WorkspacePath: work, Git: snapshot}
|
|
w := call(t, s, "POST", "/projects/check", req)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `"canSync":true`) {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
if len(s.projects) != 0 {
|
|
t.Fatal("manual check mutated server projects")
|
|
}
|
|
mismatch := *snapshot
|
|
mismatch.Head = "different"
|
|
req.Git = &mismatch
|
|
w = call(t, s, "POST", "/projects/check", req)
|
|
if !strings.Contains(w.Body.String(), `"canSync":false`) {
|
|
t.Fatal("mismatched HEAD accepted")
|
|
}
|
|
req.Git = snapshot
|
|
w = call(t, s, "POST", "/projects", req)
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
var allocated map[string]string
|
|
json.Unmarshal(w.Body.Bytes(), &allocated)
|
|
p := allocated["id"]
|
|
if s.work(p) != work {
|
|
t.Fatal("selected existing path not used")
|
|
}
|
|
// Local dirty content is intentional; the server must accept subsequent syncs of its own snapshot.
|
|
files := map[string]File{}
|
|
for path, f := range s.projects[p].Files {
|
|
files[path] = f
|
|
}
|
|
files["a.txt"] = file("local edit")
|
|
w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "new", Files: files, Git: snapshot})
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
req.ProjectID = p
|
|
w = call(t, s, "POST", "/projects/check", req)
|
|
if !strings.Contains(w.Body.String(), `"canSync":true`) {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
os.WriteFile(filepath.Join(work, "a.txt"), []byte("remote edit"), 0600)
|
|
w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "next", Files: files, Git: snapshot})
|
|
if w.Code != 409 {
|
|
t.Fatal("remote edit overwritten", w.Body.String())
|
|
}
|
|
if w = call(t, s, "POST", "/projects/"+p+"/builds", map[string]string{"revision": "new", "task": ":app:assembleDebug"}); w.Code != 409 {
|
|
t.Fatal("built changed source under stale revision", w.Body.String())
|
|
}
|
|
req.WorkspacePath = t.TempDir()
|
|
if w = call(t, s, "POST", "/projects/check", req); w.Code != 400 {
|
|
t.Fatal("allowed root escaped")
|
|
}
|
|
req.WorkspacePath = filepath.Join(s.workspaceRoot, "missing")
|
|
if w = call(t, s, "POST", "/projects/check", req); w.Code != 400 {
|
|
t.Fatal("missing folder accepted")
|
|
}
|
|
// Directory browsing lists folders within the configured root without binding them.
|
|
w = call(t, s, "GET", "/directories", nil)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), "中文 project") {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
if w = call(t, s, "GET", "/directories?path="+filepath.ToSlash(t.TempDir()), nil); w.Code != 400 {
|
|
t.Fatal("directory browser escaped root")
|
|
}
|
|
}
|
|
func TestNativeBuildCachesCannotBeSyncedOrDeleted(t *testing.T) {
|
|
s := testServer(t)
|
|
p := project(t, s)
|
|
path := "app/.cxx/Debug/arm64-v8a/.ninja_deps"
|
|
if validPath(path) {
|
|
t.Fatal("native cache accepted")
|
|
}
|
|
os.MkdirAll(filepath.Dir(filepath.Join(s.work(p), filepath.FromSlash(path))), 0700)
|
|
os.WriteFile(filepath.Join(s.work(p), filepath.FromSlash(path)), []byte("server cache"), 0600)
|
|
s.projects[p].Files[path] = File{Hash: digest([]byte("old upload"))}
|
|
w := call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "clean", Files: map[string]File{}})
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
if _, e := os.Stat(filepath.Join(s.work(p), filepath.FromSlash(path))); e != nil {
|
|
t.Fatal("server cache deleted", e)
|
|
}
|
|
if len(s.projects[p].Files) != 0 {
|
|
t.Fatal("cache stayed in source manifest")
|
|
}
|
|
}
|