aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/BinaryDeltaInputStreamTest.java
blob: d9297fcd9c3b609408370e508444eadcff1b1928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.util.io;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.zip.InflaterInputStream;

import org.junit.Test;

/**
 * Crude tests for the {@link BinaryDeltaInputStream} using delta diffs
 * generated by C git.
 */
public class BinaryDeltaInputStreamTest {

	private InputStream getBinaryHunk(String name) {
		return this.getClass().getResourceAsStream(name);
	}

	@Test
	public void testBinaryDelta() throws Exception {
		// Prepare our test data
		byte[] data = new byte[8192];
		for (int i = 0; i < data.length; i++) {
			data[i] = (byte) (255 - (i % 256));
		}
		// Same, but with five 'x' inserted in the middle.
		int middle = data.length / 2;
		byte[] newData = new byte[data.length + 5];
		System.arraycopy(data, 0, newData, 0, middle);
		for (int i = 0; i < 5; i++) {
			newData[middle + i] = 'x';
		}
		System.arraycopy(data, middle, newData, middle + 5, middle);
		// delta1.forward has the instructions
		// @formatter:off
		// COPY 0 4096
		// INSERT 5 xxxxx
		// COPY 0 4096
		// @formatter:on
		// Note that the way we built newData could be expressed as
		// @formatter:off
		// COPY 0 4096
		// INSERT 5 xxxxx
		// COPY 4096 4096
		// @formatter:on
		try (ByteArrayOutputStream out = new ByteArrayOutputStream();
				BinaryDeltaInputStream input = new BinaryDeltaInputStream(data,
						new InflaterInputStream(new BinaryHunkInputStream(
								getBinaryHunk("delta1.forward"))))) {
			byte[] buf = new byte[1024];
			int n;
			while ((n = input.read(buf)) >= 0) {
				out.write(buf, 0, n);
			}
			assertArrayEquals(newData, out.toByteArray());
			assertTrue(input.isFullyConsumed());
		}
		// delta1.reverse has the instructions
		// @formatter:off
		// COPY 0 4096
		// COPY 256 3840
		// COPY 256 256
		// @formatter:on
		// Note that there are alternatives, for instance
		// @formatter:off
		// COPY 0 4096
		// COPY 4101 4096
		// @formatter:on
		// or
		// @formatter:off
		// COPY 0 4096
		// COPY 0 4096
		// @formatter:on
		try (ByteArrayOutputStream out = new ByteArrayOutputStream();
				BinaryDeltaInputStream input = new BinaryDeltaInputStream(
						newData,
						new InflaterInputStream(new BinaryHunkInputStream(
								getBinaryHunk("delta1.reverse"))))) {
			long expectedSize = input.getExpectedResultSize();
			assertEquals(data.length, expectedSize);
			byte[] buf = new byte[1024];
			int n;
			while ((n = input.read(buf)) >= 0) {
				out.write(buf, 0, n);
			}
			assertArrayEquals(data, out.toByteArray());
			assertTrue(input.isFullyConsumed());
		}
	}
}