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
|
package memory
import (
"bytes"
"testing"
"codeberg.org/lindenii/furgit/internal/testgit"
objectheader "codeberg.org/lindenii/furgit/object/header"
objectid "codeberg.org/lindenii/furgit/object/id"
objecttype "codeberg.org/lindenii/furgit/object/type"
)
func TestStoreWriteReaderContent(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
store := New(algo)
content := []byte("memory-content\n")
gotID, err := store.WriteReaderContent(objecttype.TypeBlob, int64(len(content)), bytes.NewReader(content))
if err != nil {
t.Fatalf("WriteReaderContent: %v", err)
}
wantID := algo.Sum(buildRawObject(objecttype.TypeBlob, content))
if gotID != wantID {
t.Fatalf("WriteReaderContent id = %s, want %s", gotID, wantID)
}
gotType, gotContent, err := store.ReadBytesContent(gotID)
if err != nil {
t.Fatalf("ReadBytesContent: %v", err)
}
if gotType != objecttype.TypeBlob {
t.Fatalf("ReadBytesContent type = %v, want %v", gotType, objecttype.TypeBlob)
}
if !bytes.Equal(gotContent, content) {
t.Fatalf("ReadBytesContent content mismatch")
}
})
}
func TestStoreWriteReaderFull(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
store := New(algo)
content := []byte("memory-full\n")
raw := buildRawObject(objecttype.TypeBlob, content)
gotID, err := store.WriteReaderFull(bytes.NewReader(raw))
if err != nil {
t.Fatalf("WriteReaderFull: %v", err)
}
wantID := algo.Sum(raw)
if gotID != wantID {
t.Fatalf("WriteReaderFull id = %s, want %s", gotID, wantID)
}
gotRaw, err := store.ReadBytesFull(gotID)
if err != nil {
t.Fatalf("ReadBytesFull: %v", err)
}
if !bytes.Equal(gotRaw, raw) {
t.Fatalf("ReadBytesFull mismatch")
}
})
}
func TestStoreWriteBytes(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
store := New(algo)
content := []byte("memory-bytes\n")
gotID, err := store.WriteBytesContent(objecttype.TypeBlob, content)
if err != nil {
t.Fatalf("WriteBytesContent: %v", err)
}
wantID := algo.Sum(buildRawObject(objecttype.TypeBlob, content))
if gotID != wantID {
t.Fatalf("WriteBytesContent id = %s, want %s", gotID, wantID)
}
raw := buildRawObject(objecttype.TypeBlob, content)
gotID2, err := store.WriteBytesFull(raw)
if err != nil {
t.Fatalf("WriteBytesFull: %v", err)
}
if gotID2 != wantID {
t.Fatalf("WriteBytesFull id = %s, want %s", gotID2, wantID)
}
})
}
func TestStoreWriteReaderValidationErrors(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
t.Run("content overflow", func(t *testing.T) {
t.Parallel()
store := New(algo)
_, err := store.WriteReaderContent(objecttype.TypeBlob, 1, bytes.NewReader([]byte("hello")))
if err == nil {
t.Fatalf("expected error after overflow")
}
})
t.Run("content short", func(t *testing.T) {
t.Parallel()
store := New(algo)
_, err := store.WriteReaderContent(objecttype.TypeBlob, 5, bytes.NewReader([]byte("x")))
if err == nil {
t.Fatalf("expected error for short content")
}
})
t.Run("full malformed header", func(t *testing.T) {
t.Parallel()
store := New(algo)
_, err := store.WriteReaderFull(bytes.NewReader([]byte("not-a-header")))
if err == nil {
t.Fatalf("expected error for malformed header")
}
})
t.Run("full size mismatch", func(t *testing.T) {
t.Parallel()
store := New(algo)
_, err := store.WriteReaderFull(bytes.NewReader([]byte("blob 1\x00hello")))
if err == nil {
t.Fatalf("expected error after mismatch")
}
})
t.Run("bytes malformed header", func(t *testing.T) {
t.Parallel()
store := New(algo)
_, err := store.WriteBytesFull([]byte("not-a-header"))
if err == nil {
t.Fatalf("expected error for malformed byte header")
}
})
})
}
func TestBuildRawObjectMatchesObjectHeaderEncode(t *testing.T) {
t.Parallel()
content := []byte("body")
raw := buildRawObject(objecttype.TypeBlob, content)
header, ok := objectheader.Encode(objecttype.TypeBlob, int64(len(content)))
if !ok {
t.Fatalf("objectheader.Encode failed")
}
want := append(append([]byte(nil), header...), content...)
if !bytes.Equal(raw, want) {
t.Fatalf("buildRawObject mismatch")
}
}
|