| 1 |
package index |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"crypto" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/go-git/go-git/v5/plumbing/hash" |
| 16 |
"github.com/go-git/go-git/v5/utils/binary" |
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
|
| 20 |
"github.com/go-git/go-git/v5/plumbing" |
| 21 |
"github.com/go-git/go-git/v5/plumbing/filemode" |
| 22 |
|
| 23 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 24 |
. "gopkg.in/check.v1" |
| 25 |
) |
| 26 |
|
| 27 |
func Test(t *testing.T) { TestingT(t) } |
| 28 |
|
| 29 |
type IndexSuite struct { |
| 30 |
fixtures.Suite |
| 31 |
} |
| 32 |
|
| 33 |
var _ = Suite(&IndexSuite{}) |
| 34 |
|
| 35 |
func (s *IndexSuite) TestDecode(c *C) { |
| 36 |
f, err := fixtures.Basic().One().DotGit().Open("index") |
| 37 |
c.Assert(err, IsNil) |
| 38 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 39 |
|
| 40 |
idx := &Index{} |
| 41 |
d := NewDecoder(f) |
| 42 |
err = d.Decode(idx) |
| 43 |
c.Assert(err, IsNil) |
| 44 |
|
| 45 |
c.Assert(idx.Version, Equals, uint32(2)) |
| 46 |
c.Assert(idx.Entries, HasLen, 9) |
| 47 |
} |
| 48 |
|
| 49 |
func (s *IndexSuite) TestDecodeEntries(c *C) { |
| 50 |
f, err := fixtures.Basic().One().DotGit().Open("index") |
| 51 |
c.Assert(err, IsNil) |
| 52 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 53 |
|
| 54 |
idx := &Index{} |
| 55 |
d := NewDecoder(f) |
| 56 |
err = d.Decode(idx) |
| 57 |
c.Assert(err, IsNil) |
| 58 |
|
| 59 |
c.Assert(idx.Entries, HasLen, 9) |
| 60 |
|
| 61 |
e := idx.Entries[0] |
| 62 |
|
| 63 |
c.Assert(e.CreatedAt.Unix(), Equals, int64(1480626693)) |
| 64 |
c.Assert(e.CreatedAt.Nanosecond(), Equals, 498593596) |
| 65 |
c.Assert(e.ModifiedAt.Unix(), Equals, int64(1480626693)) |
| 66 |
c.Assert(e.ModifiedAt.Nanosecond(), Equals, 498593596) |
| 67 |
c.Assert(e.Dev, Equals, uint32(39)) |
| 68 |
c.Assert(e.Inode, Equals, uint32(140626)) |
| 69 |
c.Assert(e.UID, Equals, uint32(1000)) |
| 70 |
c.Assert(e.GID, Equals, uint32(100)) |
| 71 |
c.Assert(e.Size, Equals, uint32(189)) |
| 72 |
c.Assert(e.Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88") |
| 73 |
c.Assert(e.Name, Equals, ".gitignore") |
| 74 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 75 |
|
| 76 |
e = idx.Entries[1] |
| 77 |
c.Assert(e.Name, Equals, "CHANGELOG") |
| 78 |
} |
| 79 |
|
| 80 |
func (s *IndexSuite) TestDecodeCacheTree(c *C) { |
| 81 |
f, err := fixtures.Basic().One().DotGit().Open("index") |
| 82 |
c.Assert(err, IsNil) |
| 83 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 84 |
|
| 85 |
idx := &Index{} |
| 86 |
d := NewDecoder(f) |
| 87 |
err = d.Decode(idx) |
| 88 |
c.Assert(err, IsNil) |
| 89 |
|
| 90 |
c.Assert(idx.Entries, HasLen, 9) |
| 91 |
c.Assert(idx.Cache.Entries, HasLen, 5) |
| 92 |
|
| 93 |
for i, expected := range expectedEntries { |
| 94 |
c.Assert(idx.Cache.Entries[i].Path, Equals, expected.Path) |
| 95 |
c.Assert(idx.Cache.Entries[i].Entries, Equals, expected.Entries) |
| 96 |
c.Assert(idx.Cache.Entries[i].Trees, Equals, expected.Trees) |
| 97 |
c.Assert(idx.Cache.Entries[i].Hash.String(), Equals, expected.Hash.String()) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
var expectedEntries = []TreeEntry{ |
| 102 |
{Path: "", Entries: 9, Trees: 4, Hash: plumbing.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c")}, |
| 103 |
{Path: "go", Entries: 1, Trees: 0, Hash: plumbing.NewHash("a39771a7651f97faf5c72e08224d857fc35133db")}, |
| 104 |
{Path: "php", Entries: 1, Trees: 0, Hash: plumbing.NewHash("586af567d0bb5e771e49bdd9434f5e0fb76d25fa")}, |
| 105 |
{Path: "json", Entries: 2, Trees: 0, Hash: plumbing.NewHash("5a877e6a906a2743ad6e45d99c1793642aaf8eda")}, |
| 106 |
{Path: "vendor", Entries: 1, Trees: 0, Hash: plumbing.NewHash("cf4aa3b38974fb7d81f367c0830f7d78d65ab86b")}, |
| 107 |
} |
| 108 |
|
| 109 |
func (s *IndexSuite) TestDecodeMergeConflict(c *C) { |
| 110 |
f, err := fixtures.Basic().ByTag("merge-conflict").One().DotGit().Open("index") |
| 111 |
c.Assert(err, IsNil) |
| 112 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 113 |
|
| 114 |
idx := &Index{} |
| 115 |
d := NewDecoder(f) |
| 116 |
err = d.Decode(idx) |
| 117 |
c.Assert(err, IsNil) |
| 118 |
|
| 119 |
c.Assert(idx.Version, Equals, uint32(2)) |
| 120 |
c.Assert(idx.Entries, HasLen, 13) |
| 121 |
|
| 122 |
expected := []struct { |
| 123 |
Stage Stage |
| 124 |
Hash string |
| 125 |
}{ |
| 126 |
{AncestorMode, "880cd14280f4b9b6ed3986d6671f907d7cc2a198"}, |
| 127 |
{OurMode, "d499a1a0b79b7d87a35155afd0c1cce78b37a91c"}, |
| 128 |
{TheirMode, "14f8e368114f561c38e134f6e68ea6fea12d77ed"}, |
| 129 |
} |
| 130 |
|
| 131 |
// staged files |
| 132 |
for i, e := range idx.Entries[4:7] { |
| 133 |
c.Assert(e.Stage, Equals, expected[i].Stage) |
| 134 |
c.Assert(e.CreatedAt.IsZero(), Equals, true) |
| 135 |
c.Assert(e.ModifiedAt.IsZero(), Equals, true) |
| 136 |
c.Assert(e.Dev, Equals, uint32(0)) |
| 137 |
c.Assert(e.Inode, Equals, uint32(0)) |
| 138 |
c.Assert(e.UID, Equals, uint32(0)) |
| 139 |
c.Assert(e.GID, Equals, uint32(0)) |
| 140 |
c.Assert(e.Size, Equals, uint32(0)) |
| 141 |
c.Assert(e.Hash.String(), Equals, expected[i].Hash) |
| 142 |
c.Assert(e.Name, Equals, "go/example.go") |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func (s *IndexSuite) TestDecodeExtendedV3(c *C) { |
| 147 |
f, err := fixtures.Basic().ByTag("intent-to-add").One().DotGit().Open("index") |
| 148 |
c.Assert(err, IsNil) |
| 149 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 150 |
|
| 151 |
idx := &Index{} |
| 152 |
d := NewDecoder(f) |
| 153 |
err = d.Decode(idx) |
| 154 |
c.Assert(err, IsNil) |
| 155 |
|
| 156 |
c.Assert(idx.Version, Equals, uint32(3)) |
| 157 |
c.Assert(idx.Entries, HasLen, 11) |
| 158 |
|
| 159 |
c.Assert(idx.Entries[6].Name, Equals, "intent-to-add") |
| 160 |
c.Assert(idx.Entries[6].IntentToAdd, Equals, true) |
| 161 |
c.Assert(idx.Entries[6].SkipWorktree, Equals, false) |
| 162 |
} |
| 163 |
|
| 164 |
func (s *IndexSuite) TestDecodeResolveUndo(c *C) { |
| 165 |
f, err := fixtures.Basic().ByTag("resolve-undo").One().DotGit().Open("index") |
| 166 |
c.Assert(err, IsNil) |
| 167 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 168 |
|
| 169 |
idx := &Index{} |
| 170 |
d := NewDecoder(f) |
| 171 |
err = d.Decode(idx) |
| 172 |
c.Assert(err, IsNil) |
| 173 |
|
| 174 |
c.Assert(idx.Version, Equals, uint32(2)) |
| 175 |
c.Assert(idx.Entries, HasLen, 8) |
| 176 |
|
| 177 |
ru := idx.ResolveUndo |
| 178 |
c.Assert(ru.Entries, HasLen, 2) |
| 179 |
c.Assert(ru.Entries[0].Path, Equals, "go/example.go") |
| 180 |
c.Assert(ru.Entries[0].Stages, HasLen, 3) |
| 181 |
c.Assert(ru.Entries[0].Stages[AncestorMode], Not(Equals), plumbing.ZeroHash) |
| 182 |
c.Assert(ru.Entries[0].Stages[OurMode], Not(Equals), plumbing.ZeroHash) |
| 183 |
c.Assert(ru.Entries[0].Stages[TheirMode], Not(Equals), plumbing.ZeroHash) |
| 184 |
c.Assert(ru.Entries[1].Path, Equals, "haskal/haskal.hs") |
| 185 |
c.Assert(ru.Entries[1].Stages, HasLen, 2) |
| 186 |
c.Assert(ru.Entries[1].Stages[OurMode], Not(Equals), plumbing.ZeroHash) |
| 187 |
c.Assert(ru.Entries[1].Stages[TheirMode], Not(Equals), plumbing.ZeroHash) |
| 188 |
} |
| 189 |
|
| 190 |
func (s *IndexSuite) TestDecodeV4(c *C) { |
| 191 |
f, err := fixtures.Basic().ByTag("index-v4").One().DotGit().Open("index") |
| 192 |
c.Assert(err, IsNil) |
| 193 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 194 |
|
| 195 |
idx := &Index{} |
| 196 |
d := NewDecoder(f) |
| 197 |
err = d.Decode(idx) |
| 198 |
c.Assert(err, IsNil) |
| 199 |
|
| 200 |
c.Assert(idx.Version, Equals, uint32(4)) |
| 201 |
c.Assert(idx.Entries, HasLen, 11) |
| 202 |
|
| 203 |
names := []string{ |
| 204 |
".gitignore", "CHANGELOG", "LICENSE", "binary.jpg", "go/example.go", |
| 205 |
"haskal/haskal.hs", "intent-to-add", "json/long.json", |
| 206 |
"json/short.json", "php/crappy.php", "vendor/foo.go", |
| 207 |
} |
| 208 |
|
| 209 |
for i, e := range idx.Entries { |
| 210 |
c.Assert(e.Name, Equals, names[i]) |
| 211 |
} |
| 212 |
|
| 213 |
c.Assert(idx.Entries[6].Name, Equals, "intent-to-add") |
| 214 |
c.Assert(idx.Entries[6].IntentToAdd, Equals, true) |
| 215 |
c.Assert(idx.Entries[6].SkipWorktree, Equals, false) |
| 216 |
} |
| 217 |
|
| 218 |
func (s *IndexSuite) TestDecodeEndOfIndexEntry(c *C) { |
| 219 |
f, err := fixtures.Basic().ByTag("end-of-index-entry").One().DotGit().Open("index") |
| 220 |
c.Assert(err, IsNil) |
| 221 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 222 |
|
| 223 |
idx := &Index{} |
| 224 |
d := NewDecoder(f) |
| 225 |
err = d.Decode(idx) |
| 226 |
c.Assert(err, IsNil) |
| 227 |
|
| 228 |
c.Assert(idx.Version, Equals, uint32(2)) |
| 229 |
c.Assert(idx.EndOfIndexEntry, NotNil) |
| 230 |
c.Assert(idx.EndOfIndexEntry.Offset, Equals, uint32(716)) |
| 231 |
c.Assert(idx.EndOfIndexEntry.Hash.String(), Equals, "922e89d9ffd7cefce93a211615b2053c0f42bd78") |
| 232 |
} |
| 233 |
|
| 234 |
func (s *IndexSuite) readSimpleIndex(c *C) *Index { |
| 235 |
f, err := fixtures.Basic().One().DotGit().Open("index") |
| 236 |
c.Assert(err, IsNil) |
| 237 |
defer func() { c.Assert(f.Close(), IsNil) }() |
| 238 |
|
| 239 |
idx := &Index{} |
| 240 |
d := NewDecoder(f) |
| 241 |
err = d.Decode(idx) |
| 242 |
c.Assert(err, IsNil) |
| 243 |
|
| 244 |
return idx |
| 245 |
} |
| 246 |
|
| 247 |
func (s *IndexSuite) buildIndexWithExtension(c *C, signature string, data string) []byte { |
| 248 |
idx := s.readSimpleIndex(c) |
| 249 |
|
| 250 |
buf := bytes.NewBuffer(nil) |
| 251 |
e := NewEncoder(buf) |
| 252 |
|
| 253 |
err := e.encode(idx, false) |
| 254 |
c.Assert(err, IsNil) |
| 255 |
err = e.encodeRawExtension(signature, []byte(data)) |
| 256 |
c.Assert(err, IsNil) |
| 257 |
|
| 258 |
err = e.encodeFooter() |
| 259 |
c.Assert(err, IsNil) |
| 260 |
|
| 261 |
return buf.Bytes() |
| 262 |
} |
| 263 |
|
| 264 |
func (s *IndexSuite) TestDecodeUnknownOptionalExt(c *C) { |
| 265 |
f := bytes.NewReader(s.buildIndexWithExtension(c, "TEST", "testdata")) |
| 266 |
|
| 267 |
idx := &Index{} |
| 268 |
d := NewDecoder(f) |
| 269 |
err := d.Decode(idx) |
| 270 |
c.Assert(err, IsNil) |
| 271 |
} |
| 272 |
|
| 273 |
func (s *IndexSuite) TestDecodeUnknownMandatoryExt(c *C) { |
| 274 |
f := bytes.NewReader(s.buildIndexWithExtension(c, "test", "testdata")) |
| 275 |
|
| 276 |
idx := &Index{} |
| 277 |
d := NewDecoder(f) |
| 278 |
err := d.Decode(idx) |
| 279 |
c.Assert(err, ErrorMatches, ErrUnknownExtension.Error()) |
| 280 |
} |
| 281 |
|
| 282 |
func (s *IndexSuite) TestDecodeTruncatedExt(c *C) { |
| 283 |
idx := s.readSimpleIndex(c) |
| 284 |
|
| 285 |
buf := bytes.NewBuffer(nil) |
| 286 |
e := NewEncoder(buf) |
| 287 |
|
| 288 |
err := e.encode(idx, false) |
| 289 |
c.Assert(err, IsNil) |
| 290 |
|
| 291 |
_, err = e.w.Write([]byte("TEST")) |
| 292 |
c.Assert(err, IsNil) |
| 293 |
|
| 294 |
err = binary.WriteUint32(e.w, uint32(100)) |
| 295 |
c.Assert(err, IsNil) |
| 296 |
|
| 297 |
_, err = e.w.Write([]byte("truncated")) |
| 298 |
c.Assert(err, IsNil) |
| 299 |
|
| 300 |
err = e.encodeFooter() |
| 301 |
c.Assert(err, IsNil) |
| 302 |
|
| 303 |
idx = &Index{} |
| 304 |
d := NewDecoder(buf) |
| 305 |
err = d.Decode(idx) |
| 306 |
c.Assert(err, ErrorMatches, io.EOF.Error()) |
| 307 |
} |
| 308 |
|
| 309 |
func (s *IndexSuite) TestDecodeInvalidHash(c *C) { |
| 310 |
idx := s.readSimpleIndex(c) |
| 311 |
|
| 312 |
buf := bytes.NewBuffer(nil) |
| 313 |
e := NewEncoder(buf) |
| 314 |
|
| 315 |
err := e.encode(idx, false) |
| 316 |
c.Assert(err, IsNil) |
| 317 |
|
| 318 |
err = e.encodeRawExtension("TEST", []byte("testdata")) |
| 319 |
c.Assert(err, IsNil) |
| 320 |
|
| 321 |
h := hash.New(crypto.SHA1) |
| 322 |
err = binary.Write(e.w, h.Sum(nil)) |
| 323 |
c.Assert(err, IsNil) |
| 324 |
|
| 325 |
idx = &Index{} |
| 326 |
d := NewDecoder(buf) |
| 327 |
err = d.Decode(idx) |
| 328 |
c.Assert(err, ErrorMatches, ErrInvalidChecksum.Error()) |
| 329 |
} |
| 330 |
|
| 331 |
func TestDecodeV4StripLength(t *testing.T) { |
| 332 |
t.Parallel() |
| 333 |
|
| 334 |
tests := []struct { |
| 335 |
name string |
| 336 |
stripLen byte |
| 337 |
// firstEntry selects which entry's strip length to corrupt. |
| 338 |
// When true the first entry's varint is patched (lastEntry == nil path). |
| 339 |
// When false the second entry's varint is patched (lastEntry != nil path). |
| 340 |
firstEntry bool |
| 341 |
wantErr error |
| 342 |
}{ |
| 343 |
{ |
| 344 |
name: "SHA1: strip length equals name length", |
| 345 |
stripLen: 3, // len("abc") — strips entire name, valid |
| 346 |
}, |
| 347 |
{ |
| 348 |
name: "SHA1: strip length exceeds name length by one", |
| 349 |
stripLen: 4, // len("abc")+1 — one past the end |
| 350 |
wantErr: ErrMalformedIndexFile, |
| 351 |
}, |
| 352 |
{ |
| 353 |
name: "SHA1: strip length far exceeds name length", |
| 354 |
stripLen: 100, |
| 355 |
wantErr: ErrMalformedIndexFile, |
| 356 |
}, |
| 357 |
{ |
| 358 |
name: "SHA1: non-zero strip length on first entry", |
| 359 |
stripLen: 1, |
| 360 |
firstEntry: true, |
| 361 |
wantErr: ErrMalformedIndexFile, |
| 362 |
}, |
| 363 |
} |
| 364 |
|
| 365 |
for _, tc := range tests { |
| 366 |
t.Run(tc.name, func(t *testing.T) { |
| 367 |
t.Parallel() |
| 368 |
|
| 369 |
var h1, h2 plumbing.Hash |
| 370 |
idx := &Index{ |
| 371 |
Version: 4, |
| 372 |
Entries: []*Entry{ |
| 373 |
{ |
| 374 |
CreatedAt: time.Now(), |
| 375 |
ModifiedAt: time.Now(), |
| 376 |
Name: "abc", |
| 377 |
Hash: h1, |
| 378 |
Size: 1, |
| 379 |
}, |
| 380 |
{ |
| 381 |
CreatedAt: time.Now(), |
| 382 |
ModifiedAt: time.Now(), |
| 383 |
Name: "abd", |
| 384 |
Hash: h2, |
| 385 |
Size: 2, |
| 386 |
}, |
| 387 |
}, |
| 388 |
} |
| 389 |
|
| 390 |
buf := bytes.NewBuffer(nil) |
| 391 |
enc := NewEncoder(buf) |
| 392 |
err := enc.Encode(idx) |
| 393 |
require.NoError(t, err) |
| 394 |
|
| 395 |
raw := buf.Bytes() |
| 396 |
|
| 397 |
hashSize := crypto.SHA1.Size() |
| 398 |
h := crypto.SHA1.New() |
| 399 |
|
| 400 |
// In a V4 index, entries are sorted and prefix-compressed. After the |
| 401 |
// 12-byte header (DIRC + version + count), entry 1 ("abc") is: |
| 402 |
// 40 (10×uint32) + hashSize (hash) + 2 (flags) + 1 (varint 0) + 4 ("abc\x00") |
| 403 |
// Entry 2 starts after entry 1. Its fixed header is 40 + hashSize + 2 bytes, |
| 404 |
// followed by the strip length varint. |
| 405 |
entryFixedLen := 40 + hashSize + 2 |
| 406 |
entry1StripOffset := 12 + entryFixedLen |
| 407 |
entry1Len := entryFixedLen + 1 + 4 // + varint(0) + "abc\x00" |
| 408 |
entry2StripOffset := 12 + entry1Len + entryFixedLen |
| 409 |
|
| 410 |
var stripLenOffset int |
| 411 |
if tc.firstEntry { |
| 412 |
stripLenOffset = entry1StripOffset |
| 413 |
} else { |
| 414 |
stripLenOffset = entry2StripOffset |
| 415 |
} |
| 416 |
require.Less(t, stripLenOffset, len(raw)-hashSize, "strip length offset must be within data") |
| 417 |
|
| 418 |
// Variable-width int encoding: a single byte with value < 128 encodes directly. |
| 419 |
raw[stripLenOffset] = tc.stripLen |
| 420 |
|
| 421 |
// Recompute the checksum over the modified content. |
| 422 |
h.Reset() |
| 423 |
h.Write(raw[:len(raw)-hashSize]) |
| 424 |
copy(raw[len(raw)-hashSize:], h.Sum(nil)) |
| 425 |
|
| 426 |
dec := NewDecoder(bytes.NewReader(raw)) |
| 427 |
out := &Index{} |
| 428 |
err = dec.Decode(out) |
| 429 |
if tc.wantErr != nil { |
| 430 |
assert.ErrorIs(t, err, tc.wantErr) |
| 431 |
} else { |
| 432 |
assert.NoError(t, err) |
| 433 |
} |
| 434 |
}) |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
func TestDecodeNameLength0xFFF(t *testing.T) { |
| 439 |
t.Parallel() |
| 440 |
|
| 441 |
tests := []struct { |
| 442 |
name string |
| 443 |
version uint32 |
| 444 |
names []string // must be in sorted order |
| 445 |
}{ |
| 446 |
{ |
| 447 |
name: "V2/SHA1: name 4094 bytes (below 0xFFF, fixed-length read)", |
| 448 |
version: 2, |
| 449 |
names: []string{strings.Repeat("x", 4094)}, |
| 450 |
}, |
| 451 |
{ |
| 452 |
name: "V2/SHA1: name 4095 bytes (== 0xFFF, triggers NUL scan)", |
| 453 |
version: 2, |
| 454 |
names: []string{strings.Repeat("x", 4095)}, |
| 455 |
}, |
| 456 |
{ |
| 457 |
name: "V2/SHA1: name 4096 bytes (just above 0xFFF)", |
| 458 |
version: 2, |
| 459 |
names: []string{strings.Repeat("x", 4096)}, |
| 460 |
}, |
| 461 |
{ |
| 462 |
name: "V2/SHA1: name 5000 bytes (well above 0xFFF)", |
| 463 |
version: 2, |
| 464 |
names: []string{strings.Repeat("x", 5000)}, |
| 465 |
}, |
| 466 |
{ |
| 467 |
name: "V2/SHA1: long name then short name", |
| 468 |
version: 2, |
| 469 |
names: []string{strings.Repeat("a", 5000), "zzz"}, |
| 470 |
}, |
| 471 |
{ |
| 472 |
name: "V2/SHA1: short name then long name", |
| 473 |
version: 2, |
| 474 |
names: []string{"aaa", strings.Repeat("z", 5000)}, |
| 475 |
}, |
| 476 |
{ |
| 477 |
name: "V3/SHA1: name 4095 bytes", |
| 478 |
version: 3, |
| 479 |
names: []string{strings.Repeat("x", 4095)}, |
| 480 |
}, |
| 481 |
} |
| 482 |
|
| 483 |
for _, tc := range tests { |
| 484 |
t.Run(tc.name, func(t *testing.T) { |
| 485 |
t.Parallel() |
| 486 |
|
| 487 |
entries := make([]*Entry, len(tc.names)) |
| 488 |
for i, name := range tc.names { |
| 489 |
var eh plumbing.Hash |
| 490 |
entries[i] = &Entry{ |
| 491 |
CreatedAt: time.Now(), |
| 492 |
ModifiedAt: time.Now(), |
| 493 |
Name: name, |
| 494 |
Hash: eh, |
| 495 |
Size: uint32(i + 1), |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
idx := &Index{Version: tc.version, Entries: entries} |
| 500 |
|
| 501 |
buf := bytes.NewBuffer(nil) |
| 502 |
err := NewEncoder(buf).Encode(idx) |
| 503 |
require.NoError(t, err) |
| 504 |
|
| 505 |
output := &Index{} |
| 506 |
err = NewDecoder(bytes.NewReader(buf.Bytes())).Decode(output) |
| 507 |
require.NoError(t, err) |
| 508 |
|
| 509 |
require.Len(t, output.Entries, len(tc.names)) |
| 510 |
for i, name := range tc.names { |
| 511 |
assert.Equal(t, name, output.Entries[i].Name, |
| 512 |
"entry %d name mismatch", i) |
| 513 |
} |
| 514 |
}) |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
// TestDecodeNameLength0xFFFPatchedFlags verifies the decoder's NUL-scan |
| 519 |
// fallback by patching a short entry's flags to 0xFFF. The decoder must |
| 520 |
// recover the correct name by scanning for the NUL terminator in the |
| 521 |
// padding bytes, matching C Git's strlen(name) fallback. |
| 522 |
func TestDecodeNameLength0xFFFPatchedFlags(t *testing.T) { |
| 523 |
t.Parallel() |
| 524 |
|
| 525 |
var eh plumbing.Hash |
| 526 |
idx := &Index{ |
| 527 |
Version: 2, |
| 528 |
Entries: []*Entry{{ |
| 529 |
CreatedAt: time.Now(), |
| 530 |
ModifiedAt: time.Now(), |
| 531 |
Name: "hello", |
| 532 |
Hash: eh, |
| 533 |
Size: 42, |
| 534 |
}}, |
| 535 |
} |
| 536 |
|
| 537 |
buf := bytes.NewBuffer(nil) |
| 538 |
err := NewEncoder(buf).Encode(idx) |
| 539 |
require.NoError(t, err) |
| 540 |
|
| 541 |
raw := buf.Bytes() |
| 542 |
|
| 543 |
// Flags are at: 12 (header) + 40 (10×uint32) + hashSize. |
| 544 |
hashSize := crypto.SHA1.Size() |
| 545 |
flagsOff := 12 + 40 + hashSize |
| 546 |
origFlags := uint16(raw[flagsOff])<<8 | uint16(raw[flagsOff+1]) |
| 547 |
require.Equal(t, uint16(len("hello")), origFlags&nameMask, |
| 548 |
"original flags should encode the actual name length") |
| 549 |
|
| 550 |
// Overwrite the lower 12 bits to 0xFFF, forcing the NUL-scan path. |
| 551 |
raw[flagsOff] = (raw[flagsOff] & 0xF0) | 0x0F |
| 552 |
raw[flagsOff+1] = 0xFF |
| 553 |
|
| 554 |
// Recompute the trailing checksum over the modified content. |
| 555 |
h := crypto.SHA1.New() |
| 556 |
h.Write(raw[:len(raw)-hashSize]) |
| 557 |
copy(raw[len(raw)-hashSize:], h.Sum(nil)) |
| 558 |
|
| 559 |
output := &Index{} |
| 560 |
err = NewDecoder(bytes.NewReader(raw)).Decode(output) |
| 561 |
require.NoError(t, err) |
| 562 |
|
| 563 |
require.Len(t, output.Entries, 1) |
| 564 |
assert.Equal(t, "hello", output.Entries[0].Name) |
| 565 |
assert.Equal(t, uint32(42), output.Entries[0].Size) |
| 566 |
} |
| 567 |
|
| 568 |
func TestDecodeAllIndexFixtures(t *testing.T) { |
| 569 |
t.Parallel() |
| 570 |
|
| 571 |
fix := fixtures.ByTag(".git") |
| 572 |
require.NotEmpty(t, fix) |
| 573 |
|
| 574 |
want := map[uint32]struct{}{ |
| 575 |
2: {}, |
| 576 |
3: {}, |
| 577 |
4: {}, |
| 578 |
} |
| 579 |
got := map[uint32]struct{}{} |
| 580 |
|
| 581 |
for i, f := range fix { //nolint: paralleltest // breaks fixtures |
| 582 |
t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 583 |
f, err := f.DotGit().Open("index") |
| 584 |
if errors.Is(err, os.ErrNotExist) { |
| 585 |
return |
| 586 |
} |
| 587 |
|
| 588 |
require.NoError(t, err) |
| 589 |
defer func() { require.NoError(t, f.Close()) }() |
| 590 |
|
| 591 |
idx := &Index{} |
| 592 |
d := NewDecoder(f) |
| 593 |
err = d.Decode(idx) |
| 594 |
require.NoError(t, err) |
| 595 |
|
| 596 |
got[idx.Version] = struct{}{} |
| 597 |
}) |
| 598 |
} |
| 599 |
|
| 600 |
assert.Equal(t, want, got, "not all wanted index versions found") |
| 601 |
} |
| 602 |
|
| 603 |
func TestTreeExtensionInvalidatedEntry(t *testing.T) { |
| 604 |
t.Parallel() |
| 605 |
|
| 606 |
// TREE extension payload: three entries where the middle one is |
| 607 |
// invalidated (entry_count == -1). The on-disk format per entry is: |
| 608 |
// <path>\0<entry_count> <subtree_nr>\n[<OID> only if entry_count >= 0] |
| 609 |
// |
| 610 |
// Before the fix, an invalidated entry returned before consuming the |
| 611 |
// subtree_nr and newline, leaving stale bytes in the stream that |
| 612 |
// corrupted every subsequent entry. |
| 613 |
h := crypto.SHA1.New() |
| 614 |
hashSize := h.Size() |
| 615 |
|
| 616 |
var buf bytes.Buffer |
| 617 |
|
| 618 |
// Entry 1 (root, valid): path="", entry_count=5, subtrees=2 |
| 619 |
buf.WriteByte('\x00') |
| 620 |
buf.WriteString("5 2\n") |
| 621 |
rootHash := make([]byte, hashSize) |
| 622 |
rootHash[0] = 0xaa |
| 623 |
buf.Write(rootHash) |
| 624 |
|
| 625 |
// Entry 2 (invalidated): path="stale", entry_count=-1, subtrees=0 |
| 626 |
// No OID follows an invalidated entry. |
| 627 |
buf.WriteString("stale\x00") |
| 628 |
buf.WriteString("-1 0\n") |
| 629 |
|
| 630 |
// Entry 3 (valid): path="good", entry_count=2, subtrees=0 |
| 631 |
buf.WriteString("good\x00") |
| 632 |
buf.WriteString("2 0\n") |
| 633 |
goodHash := make([]byte, hashSize) |
| 634 |
goodHash[0] = 0xbb |
| 635 |
buf.Write(goodHash) |
| 636 |
|
| 637 |
r := bufio.NewReader(&buf) |
| 638 |
d := &treeExtensionDecoder{r} |
| 639 |
tree := &Tree{} |
| 640 |
err := d.Decode(tree) |
| 641 |
require.NoError(t, err) |
| 642 |
|
| 643 |
// The invalidated entry is skipped; only the two valid entries remain. |
| 644 |
require.Len(t, tree.Entries, 2) |
| 645 |
|
| 646 |
assert.Equal(t, "", tree.Entries[0].Path) |
| 647 |
assert.Equal(t, 5, tree.Entries[0].Entries) |
| 648 |
assert.Equal(t, 2, tree.Entries[0].Trees) |
| 649 |
assert.Equal(t, rootHash, tree.Entries[0].Hash[:]) |
| 650 |
|
| 651 |
assert.Equal(t, "good", tree.Entries[1].Path) |
| 652 |
assert.Equal(t, 2, tree.Entries[1].Entries) |
| 653 |
assert.Equal(t, 0, tree.Entries[1].Trees) |
| 654 |
assert.Equal(t, goodHash, tree.Entries[1].Hash[:]) |
| 655 |
} |