aboutsummaryrefslogtreecommitdiff
path: root/objectstore/loose/read_test.go
blob: 68b2d75bbed7ee7d13ac0c261d5b9d31eaf9881c (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package loose_test

import (
	"bytes"
	"errors"
	"os"
	"strings"
	"testing"

	"codeberg.org/lindenii/furgit/internal/testgit"
	objectid "codeberg.org/lindenii/furgit/object/id"
	"codeberg.org/lindenii/furgit/objectstore"
	"codeberg.org/lindenii/furgit/objectstore/loose"
	objecttype "codeberg.org/lindenii/furgit/object/type"
)

func TestLooseStoreReadAgainstGit(t *testing.T) {
	t.Parallel()
	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
		blobID := testRepo.HashObject(t, "blob", []byte("blob body\n"))
		_, treeID, commitID := testRepo.MakeCommit(t, "subject\n\nbody")
		tagID := testRepo.TagAnnotated(t, "v1", commitID, "tag message")

		store := openLooseStore(t, testRepo, algo)

		tests := []struct {
			name string
			id   objectid.ObjectID
		}{
			{name: "blob", id: blobID},
			{name: "tree", id: treeID},
			{name: "commit", id: commitID},
			{name: "tag", id: tagID},
		}

		for _, tt := range tests {
			t.Run(tt.name, func(t *testing.T) {
				wantType, wantBody, wantRaw := expectedRawObject(t, testRepo, tt.id)

				gotRaw, err := store.ReadBytesFull(tt.id)
				if err != nil {
					t.Fatalf("ReadBytesFull: %v", err)
				}

				if !bytes.Equal(gotRaw, wantRaw) {
					t.Fatalf("ReadBytesFull mismatch")
				}

				gotType, gotBody, err := store.ReadBytesContent(tt.id)
				if err != nil {
					t.Fatalf("ReadBytesContent: %v", err)
				}

				if gotType != wantType {
					t.Fatalf("ReadBytesContent type = %v, want %v", gotType, wantType)
				}

				if !bytes.Equal(gotBody, wantBody) {
					t.Fatalf("ReadBytesContent body mismatch")
				}

				headType, headSize, err := store.ReadHeader(tt.id)
				if err != nil {
					t.Fatalf("ReadHeader: %v", err)
				}

				if headType != wantType {
					t.Fatalf("ReadHeader type = %v, want %v", headType, wantType)
				}

				if headSize != int64(len(wantBody)) {
					t.Fatalf("ReadHeader size = %d, want %d", headSize, len(wantBody))
				}

				fullReader, err := store.ReadReaderFull(tt.id)
				if err != nil {
					t.Fatalf("ReadReaderFull: %v", err)
				}

				got := mustReadAllAndClose(t, fullReader)
				if !bytes.Equal(got, wantRaw) {
					t.Fatalf("ReadReaderFull stream mismatch")
				}

				contentType, contentSize, contentReader, err := store.ReadReaderContent(tt.id)
				if err != nil {
					t.Fatalf("ReadReaderContent: %v", err)
				}

				if contentType != wantType {
					t.Fatalf("ReadReaderContent type = %v, want %v", contentType, wantType)
				}

				if contentSize != int64(len(wantBody)) {
					t.Fatalf("ReadReaderContent size = %d, want %d", contentSize, len(wantBody))
				}

				got = mustReadAllAndClose(t, contentReader)
				if !bytes.Equal(got, wantBody) {
					t.Fatalf("ReadReaderContent stream mismatch")
				}
			})
		}
	})
}

func TestLooseStoreErrors(t *testing.T) {
	t.Parallel()
	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
		store := openLooseStore(t, testRepo, algo)

		notFoundID, err := objectid.ParseHex(algo, strings.Repeat("0", algo.HexLen()))
		if err != nil {
			t.Fatalf("ParseHex(notFoundID): %v", err)
		}

		_, err = store.ReadBytesFull(notFoundID)
		if !errors.Is(err, objectstore.ErrObjectNotFound) {
			t.Fatalf("ReadBytesFull not-found error = %v", err)
		}

		_, _, err = store.ReadBytesContent(notFoundID)
		if !errors.Is(err, objectstore.ErrObjectNotFound) {
			t.Fatalf("ReadBytesContent not-found error = %v", err)
		}

		_, err = store.ReadReaderFull(notFoundID)
		if !errors.Is(err, objectstore.ErrObjectNotFound) {
			t.Fatalf("ReadReaderFull not-found error = %v", err)
		}

		_, _, _, err = store.ReadReaderContent(notFoundID)
		if !errors.Is(err, objectstore.ErrObjectNotFound) {
			t.Fatalf("ReadReaderContent not-found error = %v", err)
		}

		_, _, err = store.ReadHeader(notFoundID)
		if !errors.Is(err, objectstore.ErrObjectNotFound) {
			t.Fatalf("ReadHeader not-found error = %v", err)
		}

		var otherAlgo objectid.Algorithm
		if algo == objectid.AlgorithmSHA1 {
			otherAlgo = objectid.AlgorithmSHA256
		} else {
			otherAlgo = objectid.AlgorithmSHA1
		}

		otherID, err := objectid.ParseHex(otherAlgo, strings.Repeat("1", otherAlgo.HexLen()))
		if err != nil {
			t.Fatalf("ParseHex(otherID): %v", err)
		}

		_, err = store.ReadBytesFull(otherID)
		if err == nil || !strings.Contains(err.Error(), "algorithm mismatch") {
			t.Fatalf("ReadBytesFull algorithm-mismatch error = %v", err)
		}
	})
}

func TestLooseStoreNewValidation(t *testing.T) {
	t.Parallel()

	root, err := os.OpenRoot(t.TempDir())
	if err != nil {
		t.Fatalf("OpenRoot: %v", err)
	}

	defer func() { _ = root.Close() }()

	_, err = loose.New(root, objectid.AlgorithmUnknown)
	if err == nil {
		t.Fatalf("loose.New(root, unknown) expected error")
	}
}

func TestLooseStoreReadHeaderDoesNotVerifyAdler32(t *testing.T) {
	t.Parallel()
	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
		store := openLooseStore(t, testRepo, algo)

		content := []byte("header-only-check\n")

		id, err := store.WriteBytesContent(objecttype.TypeBlob, content)
		if err != nil {
			t.Fatalf("WriteBytesContent: %v", err)
		}

		corruptLooseObjectTrailer(t, testRepo, id)

		ty, size, err := store.ReadHeader(id)
		if err != nil {
			t.Fatalf("ReadHeader: %v", err)
		}

		if ty != objecttype.TypeBlob {
			t.Fatalf("ReadHeader type = %v, want %v", ty, objecttype.TypeBlob)
		}

		if size != int64(len(content)) {
			t.Fatalf("ReadHeader size = %d, want %d", size, len(content))
		}

		_, err = store.ReadBytesFull(id)
		if err == nil {
			t.Fatalf("ReadBytesFull on corrupted trailer succeeded")
		}
	})
}