2017-07-14 23:50:42 +00:00
|
|
|
package pack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-07-14 23:47:28 +00:00
|
|
|
"encoding/binary"
|
2017-07-14 23:50:42 +00:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-07-14 23:46:58 +00:00
|
|
|
func TestDecodeIndexV1InvalidFanout(t *testing.T) {
|
2017-07-24 18:05:41 +00:00
|
|
|
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth-1)))
|
2017-07-14 23:46:58 +00:00
|
|
|
|
|
|
|
assert.Equal(t, ErrShortFanout, err)
|
|
|
|
assert.Nil(t, idx)
|
|
|
|
}
|
|
|
|
|
2017-07-14 23:47:28 +00:00
|
|
|
func TestDecodeIndexV2(t *testing.T) {
|
2017-07-24 18:05:41 +00:00
|
|
|
buf := make([]byte, 0, indexV2Width+indexFanoutWidth)
|
2017-07-14 23:47:28 +00:00
|
|
|
buf = append(buf, 0xff, 0x74, 0x4f, 0x63)
|
|
|
|
buf = append(buf, 0x0, 0x0, 0x0, 0x2)
|
2017-07-24 18:05:41 +00:00
|
|
|
for i := 0; i < indexFanoutEntries; i++ {
|
2017-07-14 23:47:28 +00:00
|
|
|
x := make([]byte, 4)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint32(x, uint32(3))
|
|
|
|
|
|
|
|
buf = append(buf, x...)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, err := DecodeIndex(bytes.NewReader(buf))
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 3, idx.Count())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeIndexV2InvalidFanout(t *testing.T) {
|
2017-07-24 18:05:41 +00:00
|
|
|
buf := make([]byte, 0, indexV2Width+indexFanoutWidth-indexFanoutEntryWidth)
|
2017-07-14 23:47:28 +00:00
|
|
|
buf = append(buf, 0xff, 0x74, 0x4f, 0x63)
|
|
|
|
buf = append(buf, 0x0, 0x0, 0x0, 0x2)
|
2017-07-24 18:05:41 +00:00
|
|
|
buf = append(buf, make([]byte, indexFanoutWidth-1)...)
|
2017-07-14 23:47:28 +00:00
|
|
|
|
|
|
|
idx, err := DecodeIndex(bytes.NewReader(buf))
|
|
|
|
|
2017-07-24 17:22:32 +00:00
|
|
|
assert.Nil(t, idx)
|
|
|
|
assert.Equal(t, ErrShortFanout, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeIndexV1(t *testing.T) {
|
2017-07-24 18:05:41 +00:00
|
|
|
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth)))
|
2017-07-24 17:20:56 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 0, idx.Count())
|
2017-07-14 23:47:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 23:50:42 +00:00
|
|
|
func TestDecodeIndexUnsupportedVersion(t *testing.T) {
|
|
|
|
buf := make([]byte, 0, 4+4)
|
|
|
|
buf = append(buf, 0xff, 0x74, 0x4f, 0x63)
|
|
|
|
buf = append(buf, 0x0, 0x0, 0x0, 0x3)
|
|
|
|
|
|
|
|
idx, err := DecodeIndex(bytes.NewReader(buf))
|
|
|
|
|
|
|
|
assert.EqualError(t, err, "git/odb/pack: unsupported version: 3")
|
|
|
|
assert.Nil(t, idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeIndexEmptyContents(t *testing.T) {
|
|
|
|
idx, err := DecodeIndex(bytes.NewReader(make([]byte, 0)))
|
|
|
|
|
|
|
|
assert.Equal(t, io.EOF, err)
|
|
|
|
assert.Nil(t, idx)
|
2017-07-24 17:22:32 +00:00
|
|
|
}
|