| 1 |
package pack |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
) |
| 8 |
|
| 9 |
func TestChainDeltaUnpackCopiesFromBase(t *testing.T) { |
| 10 |
c := &ChainDelta{ |
| 11 |
base: &ChainSimple{ |
| 12 |
X: []byte{0x0, 0x1, 0x2, 0x3}, |
| 13 |
}, |
| 14 |
delta: []byte{ |
| 15 |
0x04, // Source size: 4. |
| 16 |
0x03, // Destination size: 3. |
| 17 |
|
| 18 |
0x80 | 0x01 | 0x10, // Copy, omask=0001, smask=0001. |
| 19 |
0x1, // Offset: 1. |
| 20 |
0x3, // Size: 3. |
| 21 |
}, |
| 22 |
} |
| 23 |
|
| 24 |
data, err := c.Unpack() |
| 25 |
assert.NoError(t, err) |
| 26 |
assert.Equal(t, []byte{0x1, 0x2, 0x3}, data) |
| 27 |
} |
| 28 |
|
| 29 |
func TestChainDeltaUnpackAddsToBase(t *testing.T) { |
| 30 |
c := &ChainDelta{ |
| 31 |
base: &ChainSimple{ |
| 32 |
X: make([]byte, 0), |
| 33 |
}, |
| 34 |
delta: []byte{ |
| 35 |
0x0, // Source size: 0. |
| 36 |
0x3, // Destination size: 3. |
| 37 |
|
| 38 |
0x3, // Add, size=3. |
| 39 |
|
| 40 |
0x1, 0x2, 0x3, // Contents: ... |
| 41 |
}, |
| 42 |
} |
| 43 |
|
| 44 |
data, err := c.Unpack() |
| 45 |
assert.NoError(t, err) |
| 46 |
assert.Equal(t, []byte{0x1, 0x2, 0x3}, data) |
| 47 |
} |
| 48 |
|
| 49 |
func TestChainDeltaWithMultipleInstructions(t *testing.T) { |
| 50 |
c := &ChainDelta{ |
| 51 |
base: &ChainSimple{ |
| 52 |
X: []byte{'H', 'e', 'l', 'l', 'o', '!', '\n'}, |
| 53 |
}, |
| 54 |
delta: []byte{ |
| 55 |
0x07, // Source size: 7. |
| 56 |
0x0e, // Destination size: 14. |
| 57 |
|
| 58 |
0x80 | 0x01 | 0x10, // Copy, omask=0001, smask=0001. |
| 59 |
0x0, // Offset: 1. |
| 60 |
0x5, // Size: 5. |
| 61 |
|
| 62 |
0x7, // Add, size=7. |
| 63 |
',', ' ', 'w', 'o', 'r', 'l', 'd', // Contents: ... |
| 64 |
|
| 65 |
0x80 | 0x01 | 0x10, // Copy, omask=0001, smask=0001. |
| 66 |
0x05, // Offset: 5. |
| 67 |
0x02, // Size: 2. |
| 68 |
}, |
| 69 |
} |
| 70 |
|
| 71 |
data, err := c.Unpack() |
| 72 |
assert.NoError(t, err) |
| 73 |
assert.Equal(t, []byte("Hello, world!\n"), data) |
| 74 |
} |
| 75 |
|
| 76 |
func TestChainDeltaWithInvalidDeltaInstruction(t *testing.T) { |
| 77 |
c := &ChainDelta{ |
| 78 |
base: &ChainSimple{ |
| 79 |
X: make([]byte, 0), |
| 80 |
}, |
| 81 |
delta: []byte{ |
| 82 |
0x0, // Source size: 0. |
| 83 |
0x1, // Destination size: 3. |
| 84 |
|
| 85 |
0x0, // Invalid instruction. |
| 86 |
}, |
| 87 |
} |
| 88 |
|
| 89 |
data, err := c.Unpack() |
| 90 |
assert.EqualError(t, err, "gitobj/pack: invalid delta data") |
| 91 |
assert.Nil(t, data) |
| 92 |
} |
| 93 |
|
| 94 |
func TestChainDeltaWithExtraInstructions(t *testing.T) { |
| 95 |
c := &ChainDelta{ |
| 96 |
base: &ChainSimple{ |
| 97 |
X: make([]byte, 0), |
| 98 |
}, |
| 99 |
delta: []byte{ |
| 100 |
0x0, // Source size: 0. |
| 101 |
0x3, // Destination size: 3. |
| 102 |
|
| 103 |
0x4, // Add, size=4 (invalid). |
| 104 |
|
| 105 |
0x1, 0x2, 0x3, 0x4, // Contents: ... |
| 106 |
}, |
| 107 |
} |
| 108 |
|
| 109 |
data, err := c.Unpack() |
| 110 |
assert.EqualError(t, err, "gitobj/pack: invalid delta data") |
| 111 |
assert.Nil(t, data) |
| 112 |
} |