aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-06-07 08:49:48 +0000
committerGravatar Runxi Yu2026-06-07 08:49:48 +0000
commit33846c09ff1457bef25c65e4ea4b276de9b7e6dc (patch)
tree9dc84a10709a9a1a7c407ebd7da7547f02286179
parentobject/commit: Test author/committer dates in parse tests (diff)
signatureNo signature
Add malformed and roundtrip tests
-rw-r--r--object/commit/malformed_test.go128
-rw-r--r--object/commit/roundtrip_test.go181
2 files changed, 309 insertions, 0 deletions
diff --git a/object/commit/malformed_test.go b/object/commit/malformed_test.go
new file mode 100644
index 00000000..f76b93e6
--- /dev/null
+++ b/object/commit/malformed_test.go
@@ -0,0 +1,128 @@
+package commit_test
+
+import (
+ "errors"
+ "strings"
+ "testing"
+
+ "lindenii.org/go/furgit/object/commit"
+ "lindenii.org/go/furgit/object/id"
+)
+
+func TestParseMalformed(t *testing.T) {
+ t.Parallel()
+
+ for _, objectFormat := range id.SupportedObjectFormats() {
+ t.Run(objectFormat.String(), func(t *testing.T) {
+ t.Parallel()
+
+ tree := strings.Repeat("1", objectFormat.HexLen())
+ parent := strings.Repeat("2", objectFormat.HexLen())
+ shortTree := strings.Repeat("3", objectFormat.HexLen()-2)
+ author := "author Test Author <author@example.org> 1234567890 +0000\n"
+ committer := "committer Test Committer <committer@example.org> 1234567890 +0000\n"
+ valid := "tree " + tree + "\n" + author + committer + "\nmessage\n"
+
+ for _, tc := range []struct {
+ name string
+ body string
+ }{
+ {
+ name: "empty",
+ body: "",
+ },
+ {
+ name: "missing-tree",
+ body: author + committer + "\nmessage\n",
+ },
+ {
+ name: "malformed-tree",
+ body: "tree not-an-oid\n" + author + committer + "\nmessage\n",
+ },
+ {
+ name: "short-tree",
+ body: "tree " + shortTree + "\n" + author + committer + "\nmessage\n",
+ },
+ {
+ name: "parent-before-tree",
+ body: "parent " + parent + "\n" + valid,
+ },
+ {
+ name: "malformed-parent",
+ body: "tree " + tree + "\nparent not-an-oid\n" + author + committer + "\nmessage\n",
+ },
+ {
+ name: "missing-author",
+ body: "tree " + tree + "\n" + committer + "\nmessage\n",
+ },
+ {
+ name: "malformed-author-missing-lt",
+ body: "tree " + tree + "\nauthor Test Author author@example.org> 1234567890 +0000\n" + committer + "\nmessage\n",
+ },
+ {
+ name: "malformed-author-missing-gt",
+ body: "tree " + tree + "\nauthor Test Author <author@example.org 1234567890 +0000\n" + committer + "\nmessage\n",
+ },
+ {
+ name: "malformed-author-missing-timestamp",
+ body: "tree " + tree + "\nauthor Test Author <author@example.org> +0000\n" + committer + "\nmessage\n",
+ },
+ {
+ name: "malformed-author-bad-timezone",
+ body: "tree " + tree + "\nauthor Test Author <author@example.org> 1234567890 UTC\n" + committer + "\nmessage\n",
+ },
+ {
+ name: "missing-committer",
+ body: "tree " + tree + "\n" + author + "\nmessage\n",
+ },
+ {
+ name: "malformed-committer-missing-lt",
+ body: "tree " + tree + "\n" + author + "committer Test Committer committer@example.org> 1234567890 +0000\n\nmessage\n",
+ },
+ {
+ name: "malformed-committer-missing-gt",
+ body: "tree " + tree + "\n" + author + "committer Test Committer <committer@example.org 1234567890 +0000\n\nmessage\n",
+ },
+ {
+ name: "malformed-committer-missing-timestamp",
+ body: "tree " + tree + "\n" + author + "committer Test Committer <committer@example.org> +0000\n\nmessage\n",
+ },
+ {
+ name: "malformed-committer-bad-timezone",
+ body: "tree " + tree + "\n" + author + "committer Test Committer <committer@example.org> 1234567890 UTC\n\nmessage\n",
+ },
+ {
+ name: "missing-blank-line",
+ body: "tree " + tree + "\n" + author + committer,
+ },
+ {
+ name: "header-without-space",
+ body: "tree " + tree + "\n" + author + committer + "encoding\n\nmessage\n",
+ },
+ {
+ name: "unknown-header-before-required-fields",
+ body: "tree " + tree + "\nencoding UTF-8\n" + author + committer + "\nmessage\n",
+ },
+ {
+ name: "duplicate-tree",
+ body: "tree " + tree + "\ntree " + tree + "\n" + author + committer + "\nmessage\n",
+ },
+ {
+ name: "duplicate-author",
+ body: "tree " + tree + "\n" + author + author + committer + "\nmessage\n",
+ },
+ {
+ name: "duplicate-committer",
+ body: "tree " + tree + "\n" + author + committer + committer + "\nmessage\n",
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ _, err := commit.Parse([]byte(tc.body), objectFormat)
+ if !errors.Is(err, commit.ErrInvalidCommit) {
+ t.Fatalf("Parse error = %v, want ErrInvalidCommit", err)
+ }
+ })
+ }
+ })
+ }
+}
diff --git a/object/commit/roundtrip_test.go b/object/commit/roundtrip_test.go
new file mode 100644
index 00000000..eeeb1f69
--- /dev/null
+++ b/object/commit/roundtrip_test.go
@@ -0,0 +1,181 @@
+package commit_test
+
+import (
+ "bytes"
+ "slices"
+ "strings"
+ "testing"
+
+ "lindenii.org/go/furgit/internal/testgit"
+ "lindenii.org/go/furgit/object/commit"
+ "lindenii.org/go/furgit/object/id"
+ "lindenii.org/go/furgit/object/signature"
+ "lindenii.org/go/furgit/object/typ"
+)
+
+func TestRoundTrip(t *testing.T) {
+ t.Parallel()
+
+ for _, objectFormat := range id.SupportedObjectFormats() {
+ t.Run(objectFormat.String(), func(t *testing.T) {
+ t.Parallel()
+
+ repo, err := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectFormat})
+ if err != nil {
+ t.Fatalf("NewRepo: %v", err)
+ }
+
+ blobID, err := repo.HashObject(t, typ.TypeBlob, strings.NewReader("roundtrip\n"))
+ if err != nil {
+ t.Fatalf("HashObject(blob): %v", err)
+ }
+
+ treeID, err := repo.MkTree(t, []testgit.MkTreeEntry{
+ {Mode: "100644", Type: typ.TypeBlob, OID: blobID, Name: "roundtrip.txt"},
+ })
+ if err != nil {
+ t.Fatalf("MkTree: %v", err)
+ }
+
+ parent1, err := repo.CommitTree(t, treeID, testgit.CommitTreeOptions{
+ Message: "parent one",
+ Author: testgit.CommitTreeIdentity{
+ Name: "Parent Author",
+ Email: "parent-author@example.org",
+ },
+ Committer: testgit.CommitTreeIdentity{
+ Name: "Parent Committer",
+ Email: "parent-committer@example.org",
+ },
+ AuthorDate: "1234567890 +0000",
+ CommitterDate: "1234567890 +0000",
+ })
+ if err != nil {
+ t.Fatalf("CommitTree(parent1): %v", err)
+ }
+
+ parent2, err := repo.CommitTree(t, treeID, testgit.CommitTreeOptions{
+ Message: "parent two",
+ Author: testgit.CommitTreeIdentity{
+ Name: "Parent Author",
+ Email: "parent-author@example.org",
+ },
+ Committer: testgit.CommitTreeIdentity{
+ Name: "Parent Committer",
+ Email: "parent-committer@example.org",
+ },
+ AuthorDate: "1234567891 +0000",
+ CommitterDate: "1234567891 +0000",
+ }, parent1)
+ if err != nil {
+ t.Fatalf("CommitTree(parent2): %v", err)
+ }
+
+ want := &commit.Commit{
+ Tree: treeID,
+ Parents: []id.ObjectID{parent1, parent2},
+ Author: signature.Signature{
+ Name: []byte("Round Trip Author"),
+ Email: []byte("roundtrip-author@example.org"),
+ WhenUnix: 1234567000,
+ OffsetMinutes: -210,
+ },
+ Committer: signature.Signature{
+ Name: []byte("Round Trip Committer"),
+ Email: []byte("roundtrip-committer@example.org"),
+ WhenUnix: 1234567999,
+ OffsetMinutes: 330,
+ },
+ Message: []byte("roundtrip subject\n\nroundtrip body\n\n"),
+ ChangeID: "zyxwvutsrqponmlk",
+ ExtraHeaders: []commit.ExtraHeader{
+ {Key: "encoding", Value: []byte("UTF-8")},
+ {Key: "x-test-header", Value: []byte("value")},
+ },
+ }
+
+ rawBody, err := want.AppendWithoutHeader(nil)
+ if err != nil {
+ t.Fatalf("AppendWithoutHeader: %v", err)
+ }
+
+ roundTripID, err := repo.HashObject(t, typ.TypeCommit, bytes.NewReader(rawBody))
+ if err != nil {
+ t.Fatalf("HashObject(commit): %v", err)
+ }
+
+ if err := repo.Fsck(t, testgit.FsckOptions{
+ Strict: true,
+ NoDangling: true,
+ }, roundTripID); err != nil {
+ t.Fatalf("Fsck: %v", err)
+ }
+
+ gitBody, err := repo.CatFile(t, typ.TypeCommit, roundTripID)
+ if err != nil {
+ t.Fatalf("CatFile: %v", err)
+ }
+
+ got, err := commit.Parse(gitBody, objectFormat)
+ if err != nil {
+ t.Fatalf("Parse: %v", err)
+ }
+
+ assertCommitEqual(t, got, want)
+ })
+ }
+}
+
+func assertCommitEqual(t *testing.T, got *commit.Commit, want *commit.Commit) {
+ t.Helper()
+
+ if got.Tree != want.Tree {
+ t.Fatalf("tree = %s, want %s", got.Tree, want.Tree)
+ }
+
+ if !slices.Equal(got.Parents, want.Parents) {
+ t.Fatalf("parents = %v, want %v", got.Parents, want.Parents)
+ }
+
+ assertSignatureEqual(t, "author", got.Author, want.Author)
+ assertSignatureEqual(t, "committer", got.Committer, want.Committer)
+
+ if !bytes.Equal(got.Message, want.Message) {
+ t.Fatalf("message = %q, want %q", got.Message, want.Message)
+ }
+
+ if got.ChangeID != want.ChangeID {
+ t.Fatalf("change id = %q, want %q", got.ChangeID, want.ChangeID)
+ }
+
+ if len(got.ExtraHeaders) != len(want.ExtraHeaders) {
+ t.Fatalf("extra header count = %d, want %d", len(got.ExtraHeaders), len(want.ExtraHeaders))
+ }
+
+ for i, wantHeader := range want.ExtraHeaders {
+ gotHeader := got.ExtraHeaders[i]
+ if gotHeader.Key != wantHeader.Key {
+ t.Fatalf("extra header[%d] key = %q, want %q", i, gotHeader.Key, wantHeader.Key)
+ }
+ if !bytes.Equal(gotHeader.Value, wantHeader.Value) {
+ t.Fatalf("extra header[%d] value = %q, want %q", i, gotHeader.Value, wantHeader.Value)
+ }
+ }
+}
+
+func assertSignatureEqual(t *testing.T, name string, got signature.Signature, want signature.Signature) {
+ t.Helper()
+
+ if !bytes.Equal(got.Name, want.Name) {
+ t.Fatalf("%s name = %q, want %q", name, got.Name, want.Name)
+ }
+ if !bytes.Equal(got.Email, want.Email) {
+ t.Fatalf("%s email = %q, want %q", name, got.Email, want.Email)
+ }
+ if got.WhenUnix != want.WhenUnix {
+ t.Fatalf("%s time = %d, want %d", name, got.WhenUnix, want.WhenUnix)
+ }
+ if got.OffsetMinutes != want.OffsetMinutes {
+ t.Fatalf("%s offset = %d, want %d", name, got.OffsetMinutes, want.OffsetMinutes)
+ }
+}