2020-02-15 22:13:50 +00:00
|
|
|
package dehub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dehub/sigcred"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2020-02-29 20:02:25 +00:00
|
|
|
"gopkg.in/src-d/go-git.v4"
|
2020-02-15 22:13:50 +00:00
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
func TestChangeCommitVerify(t *testing.T) {
|
2020-02-15 22:13:50 +00:00
|
|
|
type step struct {
|
|
|
|
msg string
|
|
|
|
msgHead string // defaults to msg
|
|
|
|
tree map[string]string
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
descr string
|
|
|
|
steps []step
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
descr: "single commit",
|
|
|
|
steps: []step{
|
|
|
|
{
|
|
|
|
msg: "first commit",
|
|
|
|
tree: map[string]string{"a": "0", "b": "1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "multiple commits",
|
|
|
|
steps: []step{
|
|
|
|
{
|
|
|
|
msg: "first commit",
|
|
|
|
tree: map[string]string{"a": "0", "b": "1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: "second commit, changing a",
|
|
|
|
tree: map[string]string{"a": "1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: "third commit, empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: "fourth commit, adding c, removing b",
|
|
|
|
tree: map[string]string{"b": "", "c": "2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "big body commits",
|
|
|
|
steps: []step{
|
|
|
|
{
|
|
|
|
msg: "first commit, single line but with newline\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: "second commit, single line but with two newlines\n\n",
|
|
|
|
msgHead: "second commit, single line but with two newlines\n\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: "third commit, multi-line with one newline\nanother line!",
|
|
|
|
msgHead: "third commit, multi-line with one newline\n\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: "fourth commit, multi-line with two newlines\n\nanother line!",
|
|
|
|
msgHead: "fourth commit, multi-line with two newlines\n\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.descr, func(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
|
|
|
for _, step := range test.steps {
|
|
|
|
h.stage(step.tree)
|
|
|
|
account := h.cfg.Accounts[0]
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
changeCommit, hash := h.changeCommit(step.msg, account.ID, h.sig)
|
2020-03-04 19:14:54 +00:00
|
|
|
if err := h.repo.VerifyChangeCommit(MainRefName, hash); err != nil {
|
2020-02-15 22:13:50 +00:00
|
|
|
t.Fatalf("could not verify hash %v: %v", hash, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := h.repo.GitRepo.CommitObject(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve commit %v: %v", hash, err)
|
|
|
|
} else if step.msgHead == "" {
|
|
|
|
step.msgHead = strings.TrimSpace(step.msg) + "\n\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(commit.Message, step.msgHead) {
|
|
|
|
t.Fatalf("commit message %q does not start with expected head %q", commit.Message, step.msgHead)
|
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
var actualChangeCommit ChangeCommit
|
|
|
|
if err := actualChangeCommit.UnmarshalText([]byte(commit.Message)); err != nil {
|
2020-02-15 22:13:50 +00:00
|
|
|
t.Fatalf("error unmarshaling commit body: %v", err)
|
2020-02-21 05:06:13 +00:00
|
|
|
} else if !reflect.DeepEqual(actualChangeCommit, changeCommit) {
|
|
|
|
t.Fatalf("returned change commit:\n%s\ndoes not match actual one:\n%s",
|
|
|
|
spew.Sdump(changeCommit), spew.Sdump(actualChangeCommit))
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigChange(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
|
|
|
|
|
|
|
var hashes []plumbing.Hash
|
|
|
|
|
|
|
|
// commit the initial staged changes, which merely include the config and
|
|
|
|
// public key
|
2020-02-21 05:06:13 +00:00
|
|
|
_, hash := h.changeCommit("commit configuration", h.cfg.Accounts[0].ID, h.sig)
|
2020-02-15 22:13:50 +00:00
|
|
|
hashes = append(hashes, hash)
|
|
|
|
|
2020-02-29 20:02:25 +00:00
|
|
|
// create a new account and add it to the configuration. That commit should
|
|
|
|
// not be verifiable, though
|
2020-02-15 22:13:50 +00:00
|
|
|
newSig, newPubKeyBody := sigcred.SignifierPGPTmp(h.rand)
|
|
|
|
h.cfg.Accounts = append(h.cfg.Accounts, Account{
|
|
|
|
ID: "toot",
|
|
|
|
Signifiers: []sigcred.Signifier{{PGPPublicKey: &sigcred.SignifierPGP{
|
|
|
|
Body: string(newPubKeyBody),
|
|
|
|
}}},
|
|
|
|
})
|
2020-02-29 20:02:25 +00:00
|
|
|
h.cfg.AccessControls[0].ChangeAccessControls[0].Condition.Signature.AccountIDs = []string{"root", "toot"}
|
|
|
|
h.cfg.AccessControls[0].ChangeAccessControls[0].Condition.Signature.Count = "1"
|
2020-02-15 22:13:50 +00:00
|
|
|
|
|
|
|
cfgBody, err := yaml.Marshal(h.cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
h.stage(map[string]string{ConfigPath: string(cfgBody)})
|
2020-02-29 20:02:25 +00:00
|
|
|
_, badHash := h.changeCommit("add toot user", h.cfg.Accounts[1].ID, newSig)
|
2020-02-15 22:13:50 +00:00
|
|
|
|
2020-03-04 19:14:54 +00:00
|
|
|
if err := h.repo.VerifyChangeCommit(MainRefName, badHash); err == nil {
|
2020-02-29 20:02:25 +00:00
|
|
|
t.Fatal("toot user shouldn't be able to add itself to config")
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
2020-02-29 20:02:25 +00:00
|
|
|
h.reset(hash, git.HardReset)
|
2020-02-15 22:13:50 +00:00
|
|
|
|
|
|
|
// now add with the root user, this should work.
|
2020-02-29 20:02:25 +00:00
|
|
|
h.stage(map[string]string{ConfigPath: string(cfgBody)})
|
2020-02-21 05:06:13 +00:00
|
|
|
_, hash = h.changeCommit("add toot user", h.cfg.Accounts[0].ID, h.sig)
|
2020-02-15 22:13:50 +00:00
|
|
|
hashes = append(hashes, hash)
|
|
|
|
|
|
|
|
// _now_ the toot user should be able to do things.
|
|
|
|
h.stage(map[string]string{"foo/bar": "what a cool file"})
|
2020-02-21 05:06:13 +00:00
|
|
|
_, hash = h.changeCommit("add a cool file", h.cfg.Accounts[1].ID, newSig)
|
2020-02-15 22:13:50 +00:00
|
|
|
hashes = append(hashes, hash)
|
|
|
|
|
|
|
|
for i, hash := range hashes {
|
2020-03-04 19:14:54 +00:00
|
|
|
if err := h.repo.VerifyChangeCommit(MainRefName, hash); err != nil {
|
2020-02-15 22:13:50 +00:00
|
|
|
t.Fatalf("commit %d (%v) should have been verified but wasn't: %v", i, hash, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|