You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
435 lines
13 KiB
435 lines
13 KiB
4 years ago
|
package dehub
|
||
|
|
||
|
import (
|
||
4 years ago
|
"errors"
|
||
|
"regexp"
|
||
4 years ago
|
"testing"
|
||
4 years ago
|
|
||
4 years ago
|
"dehub.dev/src/dehub.git/accessctl"
|
||
|
"dehub.dev/src/dehub.git/sigcred"
|
||
|
"gopkg.in/src-d/go-git.v4"
|
||
4 years ago
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
||
4 years ago
|
)
|
||
|
|
||
|
func TestConfigChange(t *testing.T) {
|
||
|
h := newHarness(t)
|
||
4 years ago
|
rootSig := h.stageNewAccount("root", false)
|
||
4 years ago
|
|
||
4 years ago
|
var commits []Commit
|
||
4 years ago
|
|
||
|
// commit the initial staged changes, which merely include the config and
|
||
|
// public key
|
||
4 years ago
|
commit := h.assertCommitChange(verifyShouldSucceed, "commit configuration", rootSig)
|
||
|
commits = append(commits, commit)
|
||
4 years ago
|
|
||
4 years ago
|
// create a new account and add it to the configuration. That commit should
|
||
|
// not be verifiable, though
|
||
4 years ago
|
tootSig := h.stageNewAccount("toot", false)
|
||
4 years ago
|
h.stageCfg()
|
||
4 years ago
|
h.assertCommitChange(verifyShouldFail, "add toot user", tootSig)
|
||
4 years ago
|
|
||
|
// now add with the root user, this should work.
|
||
4 years ago
|
h.stageCfg()
|
||
4 years ago
|
commit = h.assertCommitChange(verifyShouldSucceed, "add toot user", rootSig)
|
||
|
commits = append(commits, commit)
|
||
4 years ago
|
|
||
|
// _now_ the toot user should be able to do things.
|
||
|
h.stage(map[string]string{"foo/bar": "what a cool file"})
|
||
4 years ago
|
commit = h.assertCommitChange(verifyShouldSucceed, "add a cool file", tootSig)
|
||
|
commits = append(commits, commit)
|
||
4 years ago
|
|
||
4 years ago
|
if err := h.proj.VerifyCommits(MainRefName, commits); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
4 years ago
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
func TestMainAncestryRequirement(t *testing.T) {
|
||
|
otherBranch := plumbing.NewBranchReferenceName("other")
|
||
|
t.Run("empty repo", func(t *testing.T) {
|
||
|
h := newHarness(t)
|
||
4 years ago
|
rootSig := h.stageNewAccount("root", false)
|
||
4 years ago
|
h.checkout(otherBranch)
|
||
|
|
||
|
// stage and try to add to the "other" branch, it shouldn't work though
|
||
|
h.stageCfg()
|
||
4 years ago
|
h.assertCommitChange(verifyShouldFail, "starting new branch at other", rootSig)
|
||
4 years ago
|
})
|
||
|
|
||
|
t.Run("new branch, single commit", func(t *testing.T) {
|
||
|
h := newHarness(t)
|
||
4 years ago
|
rootSig := h.stageNewAccount("root", false)
|
||
4 years ago
|
h.assertCommitChange(verifyShouldSucceed, "add cfg", rootSig)
|
||
4 years ago
|
|
||
|
// set HEAD to this other branch which doesn't really exist
|
||
|
ref := plumbing.NewSymbolicReference(plumbing.HEAD, otherBranch)
|
||
4 years ago
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
||
4 years ago
|
h.t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
h.stageCfg()
|
||
4 years ago
|
h.assertCommitChange(verifyShouldFail, "starting new branch at other", rootSig)
|
||
4 years ago
|
})
|
||
|
}
|
||
4 years ago
|
|
||
|
func TestAnonymousCommits(t *testing.T) {
|
||
|
h := newHarness(t)
|
||
4 years ago
|
anonSig := h.stageNewAccount("anon", true)
|
||
|
|
||
|
h.stageAccessControls(`
|
||
|
- action: allow
|
||
|
filters:
|
||
|
- type: signature
|
||
|
any: true
|
||
|
`)
|
||
4 years ago
|
h.assertCommitChange(verifyShouldSucceed, "this will work", anonSig)
|
||
|
}
|
||
|
|
||
|
func TestNonFastForwardCommits(t *testing.T) {
|
||
|
h := newHarness(t)
|
||
|
rootSig := h.stageNewAccount("root", false)
|
||
|
initCommit := h.assertCommitChange(verifyShouldSucceed, "init", rootSig)
|
||
|
|
||
|
// add another commit
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
fooCommit := h.assertCommitChange(verifyShouldSucceed, "foo", rootSig)
|
||
|
|
||
4 years ago
|
commitOn := func(hash plumbing.Hash, msg string) Commit {
|
||
4 years ago
|
ref := plumbing.NewHashReference(plumbing.HEAD, hash)
|
||
4 years ago
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
||
4 years ago
|
h.t.Fatal(err)
|
||
4 years ago
|
} else if commitChange, err := h.proj.NewPayloadChange("bar"); err != nil {
|
||
4 years ago
|
h.t.Fatal(err)
|
||
4 years ago
|
} else if commitChange, err = h.proj.AccreditPayload(commitChange, rootSig); err != nil {
|
||
4 years ago
|
h.t.Fatal(err)
|
||
4 years ago
|
} else if gitCommit, err := h.proj.Commit(commitChange); err != nil {
|
||
4 years ago
|
h.t.Fatal(err)
|
||
|
} else {
|
||
|
return gitCommit
|
||
|
}
|
||
|
panic("can't get here")
|
||
|
}
|
||
|
|
||
|
// checkout initCommit directly, make a new commit on top of it, and try to
|
||
|
// verify that (this is too fancy for the harness, must be done manually).
|
||
|
h.stage(map[string]string{"bar": "bar"})
|
||
4 years ago
|
barCommit := commitOn(initCommit.Hash, "bar")
|
||
|
err := h.proj.VerifyCommits(MainRefName, []Commit{barCommit})
|
||
4 years ago
|
if !errors.As(err, new(accessctl.ErrCommitRequestDenied)) {
|
||
|
h.t.Fatalf("expected ErrCommitRequestDenied, got: %v", err)
|
||
|
}
|
||
|
|
||
|
// check main back out (fooCommit should be checked out), and modify the
|
||
|
// config to allow nonFF commits, and add another bogus commit on top.
|
||
|
h.checkout(MainRefName)
|
||
|
h.stageAccessControls(`
|
||
|
- action: allow
|
||
|
filters:
|
||
|
- type: commit_attributes
|
||
|
non_fast_forward: true`)
|
||
|
h.stageCfg()
|
||
|
allowNonFFCommit := h.assertCommitChange(verifyShouldSucceed, "allow non-ff", rootSig)
|
||
|
|
||
|
h.stage(map[string]string{"foo": "foo foo"})
|
||
|
h.assertCommitChange(verifyShouldSucceed, "foo foo", rootSig)
|
||
|
|
||
|
// checking out allowNonFFCommit directly and performing a nonFF commit
|
||
|
// should work now.
|
||
|
h.stage(map[string]string{"baz": "baz"})
|
||
4 years ago
|
bazCommit := commitOn(allowNonFFCommit.Hash, "baz")
|
||
|
if err = h.proj.VerifyCommits(MainRefName, []Commit{bazCommit}); err != nil {
|
||
4 years ago
|
h.t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// verifying the full history should also work
|
||
4 years ago
|
gitCommits := []Commit{initCommit, fooCommit, allowNonFFCommit, bazCommit}
|
||
|
if err = h.proj.VerifyCommits(MainRefName, gitCommits); err != nil {
|
||
4 years ago
|
h.t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestCanSetBranchHEADTo(t *testing.T) {
|
||
|
type toTest struct {
|
||
|
// branchName and hash are the arguments passed into
|
||
|
// VerifyCanSetBranchHEADTo.
|
||
|
branchName plumbing.ReferenceName
|
||
|
hash plumbing.Hash
|
||
|
|
||
|
// if set then the branch will have its HEAD reset to this hash prior to
|
||
|
// calling VerifyCanSetBranchHEADTo.
|
||
|
resetTo plumbing.Hash
|
||
|
}
|
||
|
|
||
|
type test struct {
|
||
|
descr string
|
||
4 years ago
|
init func(h *harness, rootSig sigcred.Signifier) toTest
|
||
4 years ago
|
|
||
|
// If true then the verify call is expected to fail. The string is a
|
||
|
// regex which should match the unwrapped error returned.
|
||
|
expErr string
|
||
|
}
|
||
|
|
||
|
tests := []test{
|
||
|
{
|
||
|
descr: "creation of main",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
// checkout other and build on top of that, so that when
|
||
|
// VerifyCanSetBranchHEADTo is called main won't exist.
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
h.checkout(other)
|
||
|
|
||
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
return toTest{
|
||
|
branchName: MainRefName,
|
||
4 years ago
|
hash: initCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
descr: "main ff",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
nextCommit := h.assertCommitChange(verifySkip, "next", rootSig)
|
||
|
return toTest{
|
||
|
branchName: MainRefName,
|
||
4 years ago
|
hash: nextCommit.Hash,
|
||
|
resetTo: initCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
descr: "new branch, no main",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
// checkout other and build on top of that, so that when
|
||
|
// VerifyCanSetBranchHEADTo is called main won't exist.
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
h.checkout(other)
|
||
|
|
||
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
return toTest{
|
||
|
branchName: plumbing.NewBranchReferenceName("other2"),
|
||
4 years ago
|
hash: initCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
expErr: `^cannot verify commits in branch "refs/heads/other2" when no main branch exists$`,
|
||
|
},
|
||
|
{
|
||
|
// this case isn't generally possible, unless someone manually
|
||
|
// creates a branch in an empty repo on the remote
|
||
|
descr: "existing branch, no main",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
// checkout other and build on top of that, so that when
|
||
|
// VerifyCanSetBranchHEADTo is called main won't exist.
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
h.checkout(other)
|
||
|
|
||
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
fooCommit := h.assertCommitChange(verifySkip, "foo", rootSig)
|
||
|
|
||
|
return toTest{
|
||
|
branchName: other,
|
||
4 years ago
|
hash: fooCommit.Hash,
|
||
|
resetTo: initCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
expErr: `^cannot verify commits in branch "refs/heads/other" when no main branch exists$`,
|
||
|
},
|
||
|
{
|
||
|
descr: "new branch, not ancestor of main",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
|
||
|
// create new branch with no HEAD, and commit on that.
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
ref := plumbing.NewSymbolicReference(plumbing.HEAD, other)
|
||
4 years ago
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
h.stageCfg()
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
badInitCommit := h.assertCommitChange(verifySkip, "a different init", rootSig)
|
||
|
return toTest{
|
||
|
branchName: plumbing.NewBranchReferenceName("other2"),
|
||
4 years ago
|
hash: badInitCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
expErr: `^commit "[0-9a-f]+" must be direct descendant of root commit of "main" \("[0-9a-f]+"\)$`,
|
||
|
},
|
||
|
{
|
||
|
// this case isn't generally possible, unless someone manually
|
||
|
// creates a branch in an empty repo on the remote
|
||
|
descr: "existing branch, not ancestor of main",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
|
||
|
// create new branch with no HEAD, and commit on that.
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
ref := plumbing.NewSymbolicReference(plumbing.HEAD, other)
|
||
4 years ago
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
h.stageCfg()
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
badInitCommit := h.assertCommitChange(verifySkip, "a different init", rootSig)
|
||
|
|
||
|
h.stage(map[string]string{"bar": "bar"})
|
||
|
barCommit := h.assertCommitChange(verifySkip, "bar", rootSig)
|
||
|
|
||
|
return toTest{
|
||
|
branchName: other,
|
||
4 years ago
|
hash: barCommit.Hash,
|
||
|
resetTo: badInitCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
expErr: `^commit "[0-9a-f]+" must be direct descendant of root commit of "main" \("[0-9a-f]+"\)$`,
|
||
|
},
|
||
|
{
|
||
|
descr: "new branch off of main",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
|
||
|
h.checkout(other)
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
fooCommit := h.assertCommitChange(verifySkip, "foo", rootSig)
|
||
|
|
||
|
return toTest{
|
||
|
branchName: other,
|
||
4 years ago
|
hash: fooCommit.Hash,
|
||
|
resetTo: initCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
descr: "new branch off of older main commit",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
h.assertCommitChange(verifySkip, "foo", rootSig)
|
||
|
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
h.checkout(other)
|
||
4 years ago
|
h.reset(initCommit.Hash, git.HardReset)
|
||
4 years ago
|
h.stage(map[string]string{"bar": "bar"})
|
||
|
barCommit := h.assertCommitChange(verifySkip, "bar", rootSig)
|
||
|
|
||
|
return toTest{
|
||
|
branchName: other,
|
||
4 years ago
|
hash: barCommit.Hash,
|
||
|
resetTo: initCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
descr: "branch ff",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
h.checkout(other)
|
||
|
|
||
4 years ago
|
var commits []Commit
|
||
4 years ago
|
for _, str := range []string{"foo", "bar", "baz", "biz", "buz"} {
|
||
|
h.stage(map[string]string{str: str})
|
||
|
commit := h.assertCommitChange(verifySkip, str, rootSig)
|
||
|
commits = append(commits, commit)
|
||
|
}
|
||
|
|
||
|
return toTest{
|
||
|
branchName: other,
|
||
4 years ago
|
hash: commits[len(commits)-1].Hash,
|
||
|
resetTo: commits[0].Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
descr: "main nonff",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
h.assertCommitChange(verifySkip, "foo", rootSig)
|
||
|
|
||
|
// start another branch back at init and make a new commit on it
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
h.checkout(other)
|
||
4 years ago
|
h.reset(initCommit.Hash, git.HardReset)
|
||
4 years ago
|
h.stage(map[string]string{"bar": "bar"})
|
||
|
barCommit := h.assertCommitChange(verifySkip, "bar", rootSig)
|
||
|
|
||
|
return toTest{
|
||
|
branchName: MainRefName,
|
||
4 years ago
|
hash: barCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
expErr: `^commit matched and denied by this access control:`,
|
||
|
},
|
||
|
{
|
||
|
descr: "branch nonff",
|
||
4 years ago
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
||
4 years ago
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
||
|
|
||
|
other := plumbing.NewBranchReferenceName("other")
|
||
|
h.checkout(other)
|
||
|
h.stage(map[string]string{"foo": "foo"})
|
||
|
fooCommit := h.assertCommitChange(verifySkip, "foo", rootSig)
|
||
|
h.stage(map[string]string{"bar": "bar"})
|
||
|
h.assertCommitChange(verifySkip, "bar", rootSig)
|
||
|
|
||
|
other2 := plumbing.NewBranchReferenceName("other2")
|
||
|
h.checkout(other2)
|
||
4 years ago
|
h.reset(fooCommit.Hash, git.HardReset)
|
||
4 years ago
|
h.stage(map[string]string{"baz": "baz"})
|
||
|
bazCommit := h.assertCommitChange(verifySkip, "baz", rootSig)
|
||
|
|
||
|
return toTest{
|
||
|
branchName: other,
|
||
4 years ago
|
hash: bazCommit.Hash,
|
||
4 years ago
|
}
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, test := range tests {
|
||
|
t.Run(test.descr, func(t *testing.T) {
|
||
|
h := newHarness(t)
|
||
|
rootSig := h.stageNewAccount("root", false)
|
||
|
toTest := test.init(h, rootSig)
|
||
|
|
||
|
if toTest.resetTo != plumbing.ZeroHash {
|
||
|
ref := plumbing.NewHashReference(toTest.branchName, toTest.resetTo)
|
||
4 years ago
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
err := h.proj.VerifyCanSetBranchHEADTo(toTest.branchName, toTest.hash)
|
||
4 years ago
|
if test.expErr == "" {
|
||
|
if err != nil {
|
||
|
t.Fatalf("unexpected error: %v", err)
|
||
|
}
|
||
|
return
|
||
|
} else if err == nil {
|
||
|
t.Fatal("expected verification to fail")
|
||
|
}
|
||
|
|
||
|
ogErr := err
|
||
|
for {
|
||
|
if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil {
|
||
|
err = unwrappedErr
|
||
|
} else {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
errRegex := regexp.MustCompile(test.expErr)
|
||
|
if !errRegex.MatchString(err.Error()) {
|
||
|
t.Fatalf("\nexpected error of form %q\nbut got: %v", test.expErr, ogErr)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
4 years ago
|
}
|