aboutsummaryrefslogtreecommitdiff
path: root/object/commit/malformed_test.go
blob: f76b93e6a34f1b36a4dbaea43faf4c7702f32db1 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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)
					}
				})
			}
		})
	}
}