| 1 |
package idxfile_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/base64" |
| 6 |
"encoding/binary" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/go-git/go-git/v5/plumbing" |
| 13 |
. "github.com/go-git/go-git/v5/plumbing/format/idxfile" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
|
| 16 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 17 |
. "gopkg.in/check.v1" |
| 18 |
) |
| 19 |
|
| 20 |
func Test(t *testing.T) { TestingT(t) } |
| 21 |
|
| 22 |
type IdxfileSuite struct { |
| 23 |
fixtures.Suite |
| 24 |
} |
| 25 |
|
| 26 |
var _ = Suite(&IdxfileSuite{}) |
| 27 |
|
| 28 |
func (s *IdxfileSuite) TestDecode(c *C) { |
| 29 |
f := fixtures.Basic().One() |
| 30 |
|
| 31 |
d := NewDecoder(f.Idx()) |
| 32 |
idx := new(MemoryIndex) |
| 33 |
err := d.Decode(idx) |
| 34 |
c.Assert(err, IsNil) |
| 35 |
|
| 36 |
count, _ := idx.Count() |
| 37 |
c.Assert(count, Equals, int64(31)) |
| 38 |
|
| 39 |
hash := plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea") |
| 40 |
ok, err := idx.Contains(hash) |
| 41 |
c.Assert(err, IsNil) |
| 42 |
c.Assert(ok, Equals, true) |
| 43 |
|
| 44 |
offset, err := idx.FindOffset(hash) |
| 45 |
c.Assert(err, IsNil) |
| 46 |
c.Assert(offset, Equals, int64(615)) |
| 47 |
|
| 48 |
crc32, err := idx.FindCRC32(hash) |
| 49 |
c.Assert(err, IsNil) |
| 50 |
c.Assert(crc32, Equals, uint32(3645019190)) |
| 51 |
|
| 52 |
c.Assert(fmt.Sprintf("%x", idx.IdxChecksum), Equals, "fb794f1ec720b9bc8e43257451bd99c4be6fa1c9") |
| 53 |
c.Assert(fmt.Sprintf("%x", idx.PackfileChecksum), Equals, f.PackfileHash) |
| 54 |
} |
| 55 |
|
| 56 |
func (s *IdxfileSuite) TestDecode64bitsOffsets(c *C) { |
| 57 |
f := bytes.NewBufferString(fixtureLarge4GB) |
| 58 |
|
| 59 |
idx := new(MemoryIndex) |
| 60 |
|
| 61 |
d := NewDecoder(base64.NewDecoder(base64.StdEncoding, f)) |
| 62 |
err := d.Decode(idx) |
| 63 |
c.Assert(err, IsNil) |
| 64 |
|
| 65 |
expected := map[string]uint64{ |
| 66 |
"303953e5aa461c203a324821bc1717f9b4fff895": 12, |
| 67 |
"5296768e3d9f661387ccbff18c4dea6c997fd78c": 142, |
| 68 |
"03fc8d58d44267274edef4585eaeeb445879d33f": 1601322837, |
| 69 |
"8f3ceb4ea4cb9e4a0f751795eb41c9a4f07be772": 2646996529, |
| 70 |
"e0d1d625010087f79c9e01ad9d8f95e1628dda02": 3452385606, |
| 71 |
"90eba326cdc4d1d61c5ad25224ccbf08731dd041": 3707047470, |
| 72 |
"bab53055add7bc35882758a922c54a874d6b1272": 5323223332, |
| 73 |
"1b8995f51987d8a449ca5ea4356595102dc2fbd4": 5894072943, |
| 74 |
"35858be9c6f5914cbe6768489c41eb6809a2bceb": 5924278919, |
| 75 |
} |
| 76 |
|
| 77 |
iter, err := idx.Entries() |
| 78 |
c.Assert(err, IsNil) |
| 79 |
|
| 80 |
var entries int |
| 81 |
for { |
| 82 |
e, err := iter.Next() |
| 83 |
if err == io.EOF { |
| 84 |
break |
| 85 |
} |
| 86 |
c.Assert(err, IsNil) |
| 87 |
entries++ |
| 88 |
|
| 89 |
c.Assert(expected[e.Hash.String()], Equals, e.Offset) |
| 90 |
} |
| 91 |
|
| 92 |
c.Assert(entries, Equals, len(expected)) |
| 93 |
} |
| 94 |
|
| 95 |
const fixtureLarge4GB = `/3RPYwAAAAIAAAAAAAAAAAAAAAAAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEA |
| 96 |
AAABAAAAAQAAAA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AAIAAAACAAAAAgAAAAIAAAACAAAAAgAAAAIAAAACAAAAAgAAAAIAAAACAAAAAgAAAAIAAAA |
| 110 |
CAAAAAgAAAAIAAAACAAAAAgAAAAIAAAACAAAAAgAAAAIAAAACAAAAAgAAAAIAAAACAAAAAkAAAAJ |
| 111 |
AAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkA |
| 112 |
AAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAA |
| 113 |
AAkAAAAJA/yNWNRCZydO3vRYXq7rRFh50z8biZX1GYfYpEnKXqQ1ZZUQLcL71DA5U+WqRhwgOjJI |
| 114 |
IbwXF/m0//iVNYWL6cb1kUy+Z2hInEHraAmivOtSlnaOPZ9mE4fMv/GMTepsmX/XjI88606ky55K |
| 115 |
D3UXletByaTwe+dykOujJs3E0dYcWtJSJMy/CHMd0EG6tTBVrde8NYgnWKkixUqHTWsScuDR1iUB |
| 116 |
AIf3nJ4BrZ2PleFijdoCkp36qiGHwFa8NHxMnInZ0s3CKEKmHe+KcZPzuqwmm44GvqGAX3I/VYAA |
| 117 |
AAAAAAAMgAAAAQAAAI6AAAACgAAAA4AAAASAAAAFAAAAAV9Qam8AAAABYR1ShwAAAACdxfYxAAAA |
| 118 |
ANz1Di4AAAABPUnxJAAAAADNxzlGr6vCJpIFz4XaG/fi/f9C9zgQ8ptKSQpfQ1NMJBGTDTxxYGGp |
| 119 |
ch2xUA== |
| 120 |
` |
| 121 |
|
| 122 |
func BenchmarkDecode(b *testing.B) { |
| 123 |
f := fixtures.Basic().One() |
| 124 |
fixture, err := io.ReadAll(f.Idx()) |
| 125 |
if err != nil { |
| 126 |
b.Errorf("unexpected error reading idx file: %s", err) |
| 127 |
} |
| 128 |
|
| 129 |
defer fixtures.Clean() |
| 130 |
|
| 131 |
for i := 0; i < b.N; i++ { |
| 132 |
f := bytes.NewBuffer(fixture) |
| 133 |
idx := new(MemoryIndex) |
| 134 |
d := NewDecoder(f) |
| 135 |
if err := d.Decode(idx); err != nil { |
| 136 |
b.Errorf("unexpected error decoding: %s", err) |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func TestDecodeErrors(t *testing.T) { |
| 142 |
t.Parallel() |
| 143 |
|
| 144 |
idx := fixtures.Basic().One().Idx() |
| 145 |
t.Cleanup(func() { idx.Close() }) |
| 146 |
validIdx, err := io.ReadAll(idx) |
| 147 |
require.NoError(t, err) |
| 148 |
|
| 149 |
tests := []struct { |
| 150 |
name string |
| 151 |
input func() []byte |
| 152 |
wantErr error |
| 153 |
wantErrContains string |
| 154 |
}{ |
| 155 |
{ |
| 156 |
name: "empty input", |
| 157 |
input: func() []byte { return nil }, |
| 158 |
wantErr: io.EOF, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "wrong magic", |
| 162 |
input: func() []byte { return []byte{0, 0, 0, 0, 0, 0, 0, 2} }, |
| 163 |
wantErr: ErrMalformedIdxFile, |
| 164 |
}, |
| 165 |
{ |
| 166 |
name: "truncated header", |
| 167 |
input: func() []byte { return []byte{255, 't'} }, |
| 168 |
wantErr: io.ErrUnexpectedEOF, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "unsupported version 1", |
| 172 |
input: func() []byte { |
| 173 |
var buf bytes.Buffer |
| 174 |
buf.Write([]byte{255, 't', 'O', 'c'}) |
| 175 |
binary.Write(&buf, binary.BigEndian, uint32(1)) |
| 176 |
return buf.Bytes() |
| 177 |
}, |
| 178 |
wantErr: ErrUnsupportedVersion, |
| 179 |
wantErrContains: "v1", |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "unsupported version 3", |
| 183 |
input: func() []byte { |
| 184 |
var buf bytes.Buffer |
| 185 |
buf.Write([]byte{255, 't', 'O', 'c'}) |
| 186 |
binary.Write(&buf, binary.BigEndian, uint32(3)) |
| 187 |
return buf.Bytes() |
| 188 |
}, |
| 189 |
wantErr: ErrUnsupportedVersion, |
| 190 |
wantErrContains: "v3", |
| 191 |
}, |
| 192 |
{ |
| 193 |
name: "truncated fanout table", |
| 194 |
input: func() []byte { |
| 195 |
buf := idxV2Header() |
| 196 |
// Only 10 fanout entries instead of 256. |
| 197 |
for range 10 { |
| 198 |
buf = binary.BigEndian.AppendUint32(buf, 0) |
| 199 |
} |
| 200 |
return buf |
| 201 |
}, |
| 202 |
wantErr: io.EOF, |
| 203 |
}, |
| 204 |
{ |
| 205 |
name: "non-monotonic fanout at entry 1", |
| 206 |
input: func() []byte { |
| 207 |
buf := idxV2Header() |
| 208 |
// entry[0]=5, entry[1]=3 (decrease), rest=5 |
| 209 |
buf = append(buf, writeFanout(5, map[int]uint32{0: 5, 1: 3})...) |
| 210 |
return buf |
| 211 |
}, |
| 212 |
wantErr: ErrMalformedIdxFile, |
| 213 |
wantErrContains: "not monotonically non-decreasing", |
| 214 |
}, |
| 215 |
{ |
| 216 |
name: "non-monotonic fanout at last entry", |
| 217 |
input: func() []byte { |
| 218 |
buf := idxV2Header() |
| 219 |
// all entries = 10, except entry[255] = 5 |
| 220 |
buf = append(buf, writeFanout(10, map[int]uint32{255: 5})...) |
| 221 |
return buf |
| 222 |
}, |
| 223 |
wantErr: ErrMalformedIdxFile, |
| 224 |
wantErrContains: "not monotonically non-decreasing", |
| 225 |
}, |
| 226 |
{ |
| 227 |
name: "truncated object names", |
| 228 |
input: func() []byte { |
| 229 |
buf := idxV2Header() |
| 230 |
// Fanout claims 1 object, but no name data follows. |
| 231 |
buf = append(buf, writeFanout(1, nil)...) |
| 232 |
return buf |
| 233 |
}, |
| 234 |
wantErr: io.EOF, |
| 235 |
}, |
| 236 |
{ |
| 237 |
name: "checksum mismatch", |
| 238 |
input: func() []byte { |
| 239 |
corrupted := make([]byte, len(validIdx)) |
| 240 |
copy(corrupted, validIdx) |
| 241 |
// Flip the last byte of the idx checksum. |
| 242 |
corrupted[len(corrupted)-1] ^= 0xff |
| 243 |
return corrupted |
| 244 |
}, |
| 245 |
wantErr: ErrMalformedIdxFile, |
| 246 |
wantErrContains: "checksum mismatch", |
| 247 |
}, |
| 248 |
} |
| 249 |
|
| 250 |
for _, tt := range tests { |
| 251 |
t.Run(tt.name, func(t *testing.T) { |
| 252 |
t.Parallel() |
| 253 |
|
| 254 |
idx := new(MemoryIndex) |
| 255 |
d := NewDecoder(bytes.NewReader(tt.input())) |
| 256 |
|
| 257 |
err := d.Decode(idx) |
| 258 |
require.Error(t, err) |
| 259 |
if tt.wantErr != nil { |
| 260 |
require.ErrorIs(t, err, tt.wantErr) |
| 261 |
} |
| 262 |
if tt.wantErrContains != "" { |
| 263 |
require.ErrorContains(t, err, tt.wantErrContains) |
| 264 |
} |
| 265 |
}) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
// writeFanout writes a 256-entry fanout table where every entry is set to total, |
| 270 |
// except for overrides specified as index→value pairs applied afterwards. |
| 271 |
func writeFanout(total uint32, overrides map[int]uint32) []byte { |
| 272 |
var buf bytes.Buffer |
| 273 |
entries := [256]uint32{} |
| 274 |
for i := range entries { |
| 275 |
entries[i] = total |
| 276 |
} |
| 277 |
for k, v := range overrides { |
| 278 |
entries[k] = v |
| 279 |
} |
| 280 |
for _, v := range entries { |
| 281 |
binary.Write(&buf, binary.BigEndian, v) |
| 282 |
} |
| 283 |
return buf.Bytes() |
| 284 |
} |
| 285 |
|
| 286 |
// idxV2Header returns the 8-byte idx v2 header (magic + version). |
| 287 |
func idxV2Header() []byte { |
| 288 |
var buf bytes.Buffer |
| 289 |
buf.Write([]byte{255, 't', 'O', 'c'}) |
| 290 |
binary.Write(&buf, binary.BigEndian, uint32(2)) |
| 291 |
return buf.Bytes() |
| 292 |
} |
| 293 |
|
| 294 |
// TestDecoderSizeFormulaBoundary exercises the [minSize, maxSize] range |
| 295 |
// enforced by the size formula when the input exposes Stat. The |
| 296 |
// boundary is asymmetric for nr > 1: each extra 8-byte offset64 slot |
| 297 |
// extends maxSize by 8. Inputs at the edge of the legal range must |
| 298 |
// pass the size check (failing later with the trailing checksum |
| 299 |
// mismatch, since the payload is zero-filled); inputs one byte |
| 300 |
// outside must be rejected with a size-related error. |
| 301 |
func TestDecoderSizeFormulaBoundary(t *testing.T) { |
| 302 |
t.Parallel() |
| 303 |
|
| 304 |
const hashsz = 20 // SHA-1 |
| 305 |
const headerLen = 8 + 4*256 |
| 306 |
minSize := func(nr int64) int64 { |
| 307 |
return headerLen + nr*(hashsz+8) + 2*hashsz |
| 308 |
} |
| 309 |
maxSize := func(nr int64) int64 { |
| 310 |
m := minSize(nr) |
| 311 |
if nr > 0 { |
| 312 |
m += (nr - 1) * 8 |
| 313 |
} |
| 314 |
return m |
| 315 |
} |
| 316 |
build := func(nr uint32, total int64) []byte { |
| 317 |
buf := append(idxV2Header(), writeFanout(nr, nil)...) |
| 318 |
if int64(len(buf)) > total { |
| 319 |
t.Fatalf("header+fanout exceeds requested total: %d > %d", len(buf), total) |
| 320 |
} |
| 321 |
return append(buf, make([]byte, total-int64(len(buf)))...) |
| 322 |
} |
| 323 |
|
| 324 |
tests := []struct { |
| 325 |
name string |
| 326 |
nr uint32 |
| 327 |
size int64 |
| 328 |
passesLen bool // true: size check passes (later parsing may fail) |
| 329 |
}{ |
| 330 |
{"nr=1, at minSize", 1, minSize(1), true}, |
| 331 |
{"nr=1, one byte below minSize", 1, minSize(1) - 1, false}, |
| 332 |
{"nr=1, one byte above maxSize", 1, maxSize(1) + 1, false}, |
| 333 |
{"nr=2, at minSize", 2, minSize(2), true}, |
| 334 |
{"nr=2, at maxSize", 2, maxSize(2), true}, |
| 335 |
{"nr=2, one byte below minSize", 2, minSize(2) - 1, false}, |
| 336 |
{"nr=2, one byte above maxSize", 2, maxSize(2) + 1, false}, |
| 337 |
} |
| 338 |
|
| 339 |
for _, tt := range tests { |
| 340 |
t.Run(tt.name, func(t *testing.T) { |
| 341 |
t.Parallel() |
| 342 |
|
| 343 |
idx := new(MemoryIndex) |
| 344 |
d := NewDecoder(fromBytes(build(tt.nr, tt.size))) |
| 345 |
err := d.Decode(idx) |
| 346 |
require.Error(t, err) |
| 347 |
require.ErrorIs(t, err, ErrMalformedIdxFile) |
| 348 |
if tt.passesLen { |
| 349 |
// Payload is zero-filled, so we reach the trailing |
| 350 |
// checksum comparison and fail there. The size check |
| 351 |
// itself must not fire. |
| 352 |
require.ErrorContains(t, err, "checksum mismatch") |
| 353 |
} else { |
| 354 |
require.ErrorContains(t, err, "inconsistent with object count") |
| 355 |
} |
| 356 |
}) |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// TestDecoderRejectsInconsistentObjectCount asserts that a fanout |
| 361 |
// table whose object count would overflow the size formula is |
| 362 |
// rejected when the input exposes Stat. |
| 363 |
func TestDecoderRejectsInconsistentObjectCount(t *testing.T) { |
| 364 |
t.Parallel() |
| 365 |
|
| 366 |
// Header (\xff t O c) + version 2 + fanout where fanout[0..255] all |
| 367 |
// claim 0x4C4C4C4C objects. The byte count cannot possibly accommodate |
| 368 |
// that many object names. |
| 369 |
var buf bytes.Buffer |
| 370 |
buf.Write([]byte{0xff, 't', 'O', 'c', 0, 0, 0, 2}) |
| 371 |
for range 256 { |
| 372 |
_ = binary.Write(&buf, binary.BigEndian, uint32(0x4C4C4C4C)) |
| 373 |
} |
| 374 |
|
| 375 |
idx := new(MemoryIndex) |
| 376 |
d := NewDecoder(fromBytes(buf.Bytes())) |
| 377 |
err := d.Decode(idx) |
| 378 |
if !errors.Is(err, ErrMalformedIdxFile) { |
| 379 |
t.Fatalf("expected ErrMalformedIdxFile, got %v", err) |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
// TestDecoderSizeCheckSkippedForBareReader documents the |
| 384 |
// backward-compat path: when the input does not expose Stat (here a |
| 385 |
// raw [bytes.Reader]), the size formula is not applied, and a |
| 386 |
// truncated payload surfaces as the underlying read error. The |
| 387 |
// production call sites in `storage/filesystem` always pass a |
| 388 |
// `billy.File` whose concrete type has Stat, so the formula does run |
| 389 |
// there. |
| 390 |
func TestDecoderSizeCheckSkippedForBareReader(t *testing.T) { |
| 391 |
t.Parallel() |
| 392 |
|
| 393 |
buf := append(idxV2Header(), writeFanout(1, nil)...) |
| 394 |
|
| 395 |
idx := new(MemoryIndex) |
| 396 |
d := NewDecoder(bytes.NewReader(buf)) |
| 397 |
err := d.Decode(idx) |
| 398 |
require.ErrorIs(t, err, io.EOF) |
| 399 |
} |