| 1 |
package pack |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/sha1" |
| 6 |
"encoding/binary" |
| 7 |
"io" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
) |
| 12 |
|
| 13 |
func TestDecodeIndexV2(t *testing.T) { |
| 14 |
buf := make([]byte, 0, indexV2Width+indexFanoutWidth) |
| 15 |
buf = append(buf, 0xff, 0x74, 0x4f, 0x63) |
| 16 |
buf = append(buf, 0x0, 0x0, 0x0, 0x2) |
| 17 |
for i := 0; i < indexFanoutEntries; i++ { |
| 18 |
x := make([]byte, 4) |
| 19 |
|
| 20 |
binary.BigEndian.PutUint32(x, uint32(3)) |
| 21 |
|
| 22 |
buf = append(buf, x...) |
| 23 |
} |
| 24 |
|
| 25 |
idx, err := DecodeIndex(bytes.NewReader(buf), sha1.New()) |
| 26 |
|
| 27 |
assert.NoError(t, err) |
| 28 |
assert.EqualValues(t, 3, idx.Count()) |
| 29 |
} |
| 30 |
|
| 31 |
func TestDecodeIndexV2InvalidFanout(t *testing.T) { |
| 32 |
buf := make([]byte, 0, indexV2Width+indexFanoutWidth-indexFanoutEntryWidth) |
| 33 |
buf = append(buf, 0xff, 0x74, 0x4f, 0x63) |
| 34 |
buf = append(buf, 0x0, 0x0, 0x0, 0x2) |
| 35 |
buf = append(buf, make([]byte, indexFanoutWidth-1)...) |
| 36 |
|
| 37 |
idx, err := DecodeIndex(bytes.NewReader(buf), sha1.New()) |
| 38 |
|
| 39 |
assert.Equal(t, ErrShortFanout, err) |
| 40 |
assert.Nil(t, idx) |
| 41 |
} |
| 42 |
|
| 43 |
func TestDecodeIndexV1(t *testing.T) { |
| 44 |
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth)), sha1.New()) |
| 45 |
|
| 46 |
assert.NoError(t, err) |
| 47 |
assert.EqualValues(t, 0, idx.Count()) |
| 48 |
} |
| 49 |
|
| 50 |
func TestDecodeIndexV1InvalidFanout(t *testing.T) { |
| 51 |
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth-1)), sha1.New()) |
| 52 |
|
| 53 |
assert.Equal(t, ErrShortFanout, err) |
| 54 |
assert.Nil(t, idx) |
| 55 |
} |
| 56 |
|
| 57 |
func TestDecodeIndexUnsupportedVersion(t *testing.T) { |
| 58 |
buf := make([]byte, 0, 4+4) |
| 59 |
buf = append(buf, 0xff, 0x74, 0x4f, 0x63) |
| 60 |
buf = append(buf, 0x0, 0x0, 0x0, 0x3) |
| 61 |
|
| 62 |
idx, err := DecodeIndex(bytes.NewReader(buf), sha1.New()) |
| 63 |
|
| 64 |
assert.EqualError(t, err, "gitobj/pack: unsupported version: 3") |
| 65 |
assert.Nil(t, idx) |
| 66 |
} |
| 67 |
|
| 68 |
func TestDecodeIndexEmptyContents(t *testing.T) { |
| 69 |
idx, err := DecodeIndex(bytes.NewReader(make([]byte, 0)), sha1.New()) |
| 70 |
|
| 71 |
assert.Equal(t, io.EOF, err) |
| 72 |
assert.Nil(t, idx) |
| 73 |
} |