2020-03-04 23:34:02 +00:00
|
|
|
package dehub
|
|
|
|
|
|
|
|
import (
|
2020-03-22 23:56:00 +00:00
|
|
|
"dehub/sigcred"
|
2020-03-04 23:34:02 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2020-03-22 23:56:00 +00:00
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
2020-03-04 23:34:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestChangeCommitVerify(t *testing.T) {
|
|
|
|
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]
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
gitCommit := h.changeCommit(step.msg, account.ID, h.sig)
|
|
|
|
if step.msgHead == "" {
|
2020-03-04 23:34:02 +00:00
|
|
|
step.msgHead = strings.TrimSpace(step.msg) + "\n\n"
|
|
|
|
}
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
if !strings.HasPrefix(gitCommit.GitCommit.Message, step.msgHead) {
|
|
|
|
t.Fatalf("commit message %q does not start with expected head %q",
|
|
|
|
gitCommit.GitCommit.Message, step.msgHead)
|
2020-03-04 23:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var actualCommit Commit
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
if err := actualCommit.UnmarshalText([]byte(gitCommit.GitCommit.Message)); err != nil {
|
2020-03-04 23:34:02 +00:00
|
|
|
t.Fatalf("error unmarshaling commit body: %v", err)
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
} else if !reflect.DeepEqual(actualCommit, gitCommit.Commit) {
|
2020-03-04 23:34:02 +00:00
|
|
|
t.Fatalf("returned change commit:\n%s\ndoes not match actual one:\n%s",
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
spew.Sdump(gitCommit.Commit), spew.Sdump(actualCommit))
|
2020-03-04 23:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 23:56:00 +00:00
|
|
|
|
|
|
|
func TestCombineCommitChanges(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
|
|
|
|
|
|
|
// commit initial config, so the root user can modify it in the next commit
|
|
|
|
h.changeCommit("initial commit", h.cfg.Accounts[0].ID, h.sig)
|
|
|
|
|
|
|
|
// add a toot user and modify the access controls such that both accounts
|
|
|
|
// are required for the main branch
|
|
|
|
tootSig, tootPubKeyBody := sigcred.SignifierPGPTmp("toot", h.rand)
|
|
|
|
h.cfg.Accounts = append(h.cfg.Accounts, Account{
|
|
|
|
ID: "toot",
|
|
|
|
Signifiers: []sigcred.Signifier{{PGPPublicKey: &sigcred.SignifierPGP{
|
|
|
|
Body: string(tootPubKeyBody),
|
|
|
|
}}},
|
|
|
|
})
|
|
|
|
err := yaml.Unmarshal([]byte(`
|
|
|
|
- action: allow
|
|
|
|
filters:
|
|
|
|
- type: branch
|
|
|
|
pattern: main
|
|
|
|
- type: commit_type
|
|
|
|
commit_type: change
|
|
|
|
- type: signature
|
|
|
|
any_account: true
|
|
|
|
count: 2
|
|
|
|
|
|
|
|
- action: allow
|
|
|
|
filters:
|
|
|
|
- type: not
|
|
|
|
filter:
|
|
|
|
type: branch
|
|
|
|
pattern: main
|
|
|
|
- type: signature
|
|
|
|
any_account: true
|
|
|
|
count: 1
|
|
|
|
|
|
|
|
- action: deny
|
|
|
|
`), &h.cfg.AccessControls)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.stageCfg()
|
|
|
|
tootCommit := h.changeCommit("add toot", h.cfg.Accounts[0].ID, h.sig)
|
|
|
|
|
|
|
|
// make a single change commit in another branch using root. Then add a
|
|
|
|
// credential using toot, and combine them onto main.
|
|
|
|
otherBranch := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(otherBranch)
|
|
|
|
h.stage(map[string]string{"foo": "bar"})
|
|
|
|
fooCommit := h.changeCommit("add foo file", h.cfg.Accounts[0].ID, h.sig)
|
|
|
|
|
|
|
|
// now adding a credential commit from toot should work
|
|
|
|
credCommitObj, err := h.repo.NewCommitCredential(fooCommit.Interface.GetHash())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
credCommit := h.tryCommit(true, credCommitObj, h.cfg.Accounts[1].ID, tootSig)
|
2020-03-23 22:49:01 +00:00
|
|
|
|
|
|
|
allCommits, err := h.repo.GetGitCommitRange(
|
|
|
|
tootCommit.GitCommit.Hash,
|
|
|
|
credCommit.GitCommit.Hash,
|
2020-03-22 23:56:00 +00:00
|
|
|
)
|
2020-03-23 22:49:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error getting commits: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
combinedCommit, err := h.repo.CombineCommitChanges(allCommits, MainRefName)
|
2020-03-22 23:56:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// that new commit should have both credentials
|
|
|
|
creds := combinedCommit.Commit.Common.Credentials
|
|
|
|
if len(creds) != 2 {
|
|
|
|
t.Fatalf("combined commit has %d credentials, not 2", len(creds))
|
|
|
|
} else if creds[0].AccountID != "root" {
|
|
|
|
t.Fatalf("combined commit first credential should be from root, is from %q", creds[0].AccountID)
|
|
|
|
} else if creds[1].AccountID != "toot" {
|
|
|
|
t.Fatalf("combined commit second credential should be from toot, is from %q", creds[1].AccountID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// double check that the HEAD commit of main got properly set
|
|
|
|
h.checkout(MainRefName)
|
|
|
|
mainHead, err := h.repo.GetGitHead()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if mainHead.GitCommit.Hash != combinedCommit.GitCommit.Hash {
|
|
|
|
t.Fatalf("mainHead's should be pointed at %s but is pointed at %s",
|
|
|
|
combinedCommit.GitCommit.Hash, mainHead.GitCommit.Hash)
|
|
|
|
} else if err = h.repo.VerifyCommits(MainRefName, []GitCommit{combinedCommit}); err != nil {
|
|
|
|
t.Fatalf("unable to verify combined commit: %v", err)
|
2020-03-23 22:49:01 +00:00
|
|
|
} else if author := combinedCommit.GitCommit.Author.Name; author != "root" {
|
|
|
|
t.Fatalf("unexpected author value %q", author)
|
2020-03-22 23:56:00 +00:00
|
|
|
}
|
|
|
|
}
|