Debian Codegraph @

plumbing/format/packfile/patch_delta_test.go

raw
1 package packfile
2
3 import (
4 "bytes"
5 "io"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10
11 "github.com/go-git/go-git/v5/plumbing"
12 )
13
14 func TestDecodeLEB128Overflow(t *testing.T) {
15 t.Parallel()
16
17 // Eleven continuation bytes is enough to push shift past the bit width
18 // of uint on either 32- or 64-bit platforms.
19 input := append(bytes.Repeat([]byte{0x80}, 11), 0x01)
20
21 _, _, err := decodeLEB128(input)
22 require.ErrorIs(t, err, ErrLengthOverflow)
23 }
24
25 func TestDecodeLEB128ByteReaderOverflow(t *testing.T) {
26 t.Parallel()
27
28 input := bytes.Repeat([]byte{0x80}, 11)
29
30 _, err := decodeLEB128ByteReader(bytes.NewReader(input))
31 require.ErrorIs(t, err, ErrLengthOverflow)
32 }
33
34 func TestDecodeLEB128(t *testing.T) {
35 t.Parallel()
36
37 tests := []struct {
38 name string
39 input []byte
40 want uint
41 wantRest []byte
42 }{
43 {
44 name: "single byte, small number",
45 input: []byte{0x01, 0xFF},
46 want: 1,
47 wantRest: []byte{0xFF},
48 },
49 {
50 name: "single byte, max value without continuation",
51 input: []byte{0x7F, 0xFF},
52 want: 127,
53 wantRest: []byte{0xFF},
54 },
55 {
56 name: "two bytes",
57 input: []byte{0x80, 0x01, 0xFF},
58 want: 128,
59 wantRest: []byte{0xFF},
60 },
61 {
62 name: "two bytes, larger number",
63 input: []byte{0xFF, 0x01, 0xFF},
64 want: 255,
65 wantRest: []byte{0xFF},
66 },
67 {
68 name: "three bytes",
69 input: []byte{0x80, 0x80, 0x01, 0xFF},
70 want: 16384,
71 wantRest: []byte{0xFF},
72 },
73 {
74 name: "empty remaining bytes",
75 input: []byte{0x01},
76 want: 1,
77 wantRest: []byte{},
78 },
79 {
80 name: "empty input",
81 input: []byte{},
82 want: 0,
83 wantRest: []byte{},
84 },
85 }
86
87 for _, tc := range tests {
88 tc := tc
89 t.Run(tc.name, func(t *testing.T) {
90 t.Parallel()
91
92 gotNum, gotRest, err := decodeLEB128(tc.input)
93 assert.NoError(t, err)
94 assert.Equal(t, tc.want, gotNum, "decoded number mismatch")
95 assert.Equal(t, tc.wantRest, gotRest, "remaining bytes mismatch")
96 })
97 }
98 }
99
100 // buildDelta assembles a delta byte stream from a (srcSz, targetSz)
101 // header and a sequence of pre-encoded operations.
102 func buildDelta(srcSz, targetSz int, ops ...[]byte) []byte {
103 var b bytes.Buffer
104 b.Write(deltaEncodeSize(srcSz))
105 b.Write(deltaEncodeSize(targetSz))
106 for _, op := range ops {
107 b.Write(op)
108 }
109 return b.Bytes()
110 }
111
112 // insertOp encodes a copy-from-delta op of the given payload.
113 func insertOp(data []byte) []byte {
114 return append([]byte{byte(len(data))}, data...)
115 }
116
117 // TestPatchDeltaRejectsOversizedCopies asserts that a delta whose
118 // individual copy operations each fit within the declared target size,
119 // but whose cumulative output would exceed it, is rejected before any
120 // write past the declared target size happens.
121 func TestPatchDeltaRejectsOversizedCopies(t *testing.T) {
122 t.Parallel()
123
124 src := bytes.Repeat([]byte("A"), 64)
125
126 cases := []struct {
127 name string
128 targetSz uint
129 delta []byte
130 }{
131 {
132 // Two copy-from-src ops, each individually fits but
133 // their sum (126) exceeds targetSz (64).
134 name: "copy-from-src cumulative overflow",
135 targetSz: 64,
136 delta: buildDelta(64, 64,
137 encodeCopyOperation(0, 63),
138 encodeCopyOperation(0, 63),
139 ),
140 },
141 {
142 // Two copy-from-delta ops, each fits but their sum (14)
143 // exceeds targetSz (10).
144 name: "copy-from-delta cumulative overflow",
145 targetSz: 10,
146 delta: buildDelta(64, 10,
147 insertOp(bytes.Repeat([]byte{'x'}, 7)),
148 insertOp(bytes.Repeat([]byte{'x'}, 7)),
149 ),
150 },
151 }
152
153 for _, tc := range cases {
154 t.Run(tc.name, func(t *testing.T) {
155 t.Parallel()
156
157 _, err := PatchDelta(src, tc.delta)
158 assert.ErrorIs(t, err, ErrInvalidDelta)
159
160 // The buffer fed to patchDelta must never be written past
161 // targetSz: that is the property the validation protects.
162 b := &bytes.Buffer{}
163 _ = patchDelta(b, src, tc.delta)
164 assert.LessOrEqual(t, uint(b.Len()), tc.targetSz,
165 "patchDelta wrote past the declared target size")
166 })
167 }
168 }
169
170 // TestReaderFromDeltaRejectsOversizedCopies covers the streaming
171 // counterpart and asserts that the reader surfaces ErrInvalidDelta to
172 // the consumer rather than silently truncating the stream when a
173 // crafted delta would write past the declared target size.
174 func TestReaderFromDeltaRejectsOversizedCopies(t *testing.T) {
175 t.Parallel()
176
177 src := bytes.Repeat([]byte("A"), 64)
178 base := &plumbing.MemoryObject{}
179 _, _ = base.Write(src)
180
181 delta := buildDelta(64, 64,
182 encodeCopyOperation(0, 63),
183 encodeCopyOperation(0, 63),
184 )
185
186 rc, err := ReaderFromDelta(base, io.NopCloser(bytes.NewReader(delta)))
187 assert.NoError(t, err)
188 out, err := io.ReadAll(rc)
189 assert.ErrorIs(t, err, ErrInvalidDelta)
190 assert.LessOrEqual(t, len(out), 64,
191 "ReaderFromDelta yielded more bytes than the declared target size")
192 }
193
194 // TestPatchDeltaRejectsTrailingBytes asserts that a delta whose
195 // operations exactly fill the declared target size but is followed by
196 // extra bytes is rejected, matching upstream's `data != top` post-loop
197 // sanity check.
198 func TestPatchDeltaRejectsTrailingBytes(t *testing.T) {
199 t.Parallel()
200
201 src := bytes.Repeat([]byte("A"), 64)
202 delta := buildDelta(64, 64,
203 encodeCopyOperation(0, 64),
204 []byte{0x00, 0x01, 0x02}, // unused trailing bytes
205 )
206
207 _, err := PatchDelta(src, delta)
208 assert.ErrorIs(t, err, ErrInvalidDelta)
209 }
210
211 // TestPatchDeltaAcceptsEmptyTarget asserts that a delta whose declared
212 // target size is zero and which carries no operations succeeds and
213 // produces an empty result, matching upstream's behaviour of treating
214 // `data == top && size == 0` as success.
215 func TestPatchDeltaAcceptsEmptyTarget(t *testing.T) {
216 t.Parallel()
217
218 src := []byte("hello")
219 delta := buildDelta(len(src), 0)
220
221 out, err := PatchDelta(src, delta)
222 assert.NoError(t, err)
223 assert.Empty(t, out)
224 }