github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/git/odb/pack/index_v2_test.go (about)

     1  package pack
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var (
    12  	V2IndexHeader = []byte{
    13  		0xff, 0x74, 0x4f, 0x63,
    14  		0x00, 0x00, 0x00, 0x02,
    15  	}
    16  	V2IndexFanout = make([]uint32, indexFanoutEntries)
    17  
    18  	V2IndexNames = []byte{
    19  		0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
    20  		0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
    21  
    22  		0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2,
    23  		0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2,
    24  
    25  		0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
    26  		0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
    27  	}
    28  	V2IndexSmallSha  = V2IndexNames[0:20]
    29  	V2IndexMediumSha = V2IndexNames[20:40]
    30  	V2IndexLargeSha  = V2IndexNames[40:60]
    31  
    32  	V2IndexCRCs = []byte{
    33  		0x0, 0x0, 0x0, 0x0,
    34  		0x1, 0x1, 0x1, 0x1,
    35  		0x2, 0x2, 0x2, 0x2,
    36  	}
    37  
    38  	V2IndexOffsets = []byte{
    39  		0x00, 0x00, 0x00, 0x01,
    40  		0x00, 0x00, 0x00, 0x02,
    41  		0x80, 0x00, 0x00, 0x01, // use the second large offset
    42  
    43  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // filler data
    44  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // large offset
    45  	}
    46  
    47  	V2Index = &Index{
    48  		fanout:  V2IndexFanout,
    49  		version: new(V2),
    50  	}
    51  )
    52  
    53  func TestIndexV2EntryExact(t *testing.T) {
    54  	e, err := new(V2).Entry(V2Index, 1)
    55  
    56  	assert.NoError(t, err)
    57  	assert.EqualValues(t, 2, e.PackOffset)
    58  }
    59  
    60  func TestIndexV2EntryExtendedOffset(t *testing.T) {
    61  	e, err := new(V2).Entry(V2Index, 2)
    62  
    63  	assert.NoError(t, err)
    64  	assert.EqualValues(t, 3, e.PackOffset)
    65  }
    66  
    67  func TestIndexVersionWidthV2(t *testing.T) {
    68  	assert.EqualValues(t, 8, new(V2).Width())
    69  }
    70  
    71  func init() {
    72  	V2IndexFanout[1] = 1
    73  	V2IndexFanout[2] = 2
    74  	V2IndexFanout[3] = 3
    75  
    76  	for i := 3; i < len(V2IndexFanout); i++ {
    77  		V2IndexFanout[i] = 3
    78  	}
    79  
    80  	fanout := make([]byte, indexFanoutWidth)
    81  	for i, n := range V2IndexFanout {
    82  		binary.BigEndian.PutUint32(fanout[i*indexFanoutEntryWidth:], n)
    83  	}
    84  
    85  	buf := make([]byte, 0, indexOffsetV2Start+3*(indexObjectEntryV2Width)+indexObjectLargeOffsetWidth)
    86  	buf = append(buf, V2IndexHeader...)
    87  	buf = append(buf, fanout...)
    88  	buf = append(buf, V2IndexNames...)
    89  	buf = append(buf, V2IndexCRCs...)
    90  	buf = append(buf, V2IndexOffsets...)
    91  
    92  	V2Index.r = bytes.NewReader(buf)
    93  }