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
|
package packed_test
import (
"bytes"
"errors"
"os"
"strings"
"testing"
"codeberg.org/lindenii/furgit/internal/testgit"
"codeberg.org/lindenii/furgit/objectid"
"codeberg.org/lindenii/furgit/objectstore"
"codeberg.org/lindenii/furgit/objectstore/packed"
)
func TestPackedStoreReadAgainstGit(t *testing.T) {
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
testRepo, ids := createPackedFixtureRepo(t, algo)
store := openPackedStore(t, testRepo.Dir(), algo)
for _, id := range ids {
id := id
t.Run(id.String(), func(t *testing.T) {
wantType, wantBody, wantRaw := expectedRawObject(t, testRepo, id)
gotHeaderType, gotHeaderSize, err := store.ReadHeader(id)
if err != nil {
t.Fatalf("ReadHeader: %v", err)
}
if gotHeaderType != wantType {
t.Fatalf("ReadHeader type = %v, want %v", gotHeaderType, wantType)
}
if gotHeaderSize != int64(len(wantBody)) {
t.Fatalf("ReadHeader size = %d, want %d", gotHeaderSize, len(wantBody))
}
gotRaw, err := store.ReadBytesFull(id)
if err != nil {
t.Fatalf("ReadBytesFull: %v", err)
}
if !bytes.Equal(gotRaw, wantRaw) {
t.Fatalf("ReadBytesFull mismatch")
}
gotType, gotBody, err := store.ReadBytesContent(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 mismatch")
}
fullReader, err := store.ReadReaderFull(id)
if err != nil {
t.Fatalf("ReadReaderFull: %v", err)
}
if got := mustReadAllAndClose(t, fullReader); !bytes.Equal(got, wantRaw) {
t.Fatalf("ReadReaderFull mismatch")
}
contentType, contentSize, contentReader, err := store.ReadReaderContent(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))
}
if got := mustReadAllAndClose(t, contentReader); !bytes.Equal(got, wantBody) {
t.Fatalf("ReadReaderContent mismatch")
}
})
}
})
}
func TestPackedStoreErrors(t *testing.T) {
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
testRepo, _ := createPackedFixtureRepo(t, algo)
store := openPackedStore(t, testRepo.Dir(), algo)
notFoundID, err := objectid.ParseHex(algo, strings.Repeat("0", algo.HexLen()))
if err != nil {
t.Fatalf("ParseHex(notFound): %v", err)
}
if _, err := store.ReadBytesFull(notFoundID); !errors.Is(err, objectstore.ErrObjectNotFound) {
t.Fatalf("ReadBytesFull not-found error = %v", err)
}
if _, _, err := store.ReadBytesContent(notFoundID); !errors.Is(err, objectstore.ErrObjectNotFound) {
t.Fatalf("ReadBytesContent not-found error = %v", err)
}
if _, err := store.ReadReaderFull(notFoundID); !errors.Is(err, objectstore.ErrObjectNotFound) {
t.Fatalf("ReadReaderFull not-found error = %v", err)
}
if _, _, _, err := store.ReadReaderContent(notFoundID); !errors.Is(err, objectstore.ErrObjectNotFound) {
t.Fatalf("ReadReaderContent not-found error = %v", err)
}
if _, _, err := store.ReadHeader(notFoundID); !errors.Is(err, objectstore.ErrObjectNotFound) {
t.Fatalf("ReadHeader not-found error = %v", err)
}
var otherAlgo objectid.Algorithm
for _, candidate := range objectid.SupportedAlgorithms() {
if candidate != algo {
otherAlgo = candidate
break
}
}
if otherAlgo != objectid.AlgorithmUnknown {
mismatchID, err := objectid.ParseHex(otherAlgo, strings.Repeat("0", otherAlgo.HexLen()))
if err != nil {
t.Fatalf("ParseHex(mismatch): %v", err)
}
if _, err := store.ReadBytesFull(mismatchID); err == nil || !strings.Contains(err.Error(), "algorithm mismatch") {
t.Fatalf("ReadBytesFull algorithm-mismatch error = %v", err)
}
}
})
}
func TestPackedStoreNewValidation(t *testing.T) {
testRepo, _ := createPackedFixtureRepo(t, objectid.AlgorithmSHA1)
store := openPackedStore(t, testRepo.Dir(), objectid.AlgorithmSHA1)
if err := store.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
if err := store.Close(); err != nil {
t.Fatalf("Close second: %v", err)
}
}
func TestPackedStoreInvalidAlgorithm(t *testing.T) {
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectid.AlgorithmSHA1, Bare: true})
root, err := os.OpenRoot(testRepo.Dir())
if err != nil {
t.Fatalf("OpenRoot(%q): %v", testRepo.Dir(), err)
}
t.Cleanup(func() { _ = root.Close() })
if _, err := packed.New(root, objectid.AlgorithmUnknown); !errors.Is(err, objectid.ErrInvalidAlgorithm) {
t.Fatalf("packed.New invalid algorithm error = %v", err)
}
}
|