You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinaryDeltaInputStreamTest.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C) 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util.io;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.InputStream;
  16. import java.util.zip.InflaterInputStream;
  17. import org.junit.Test;
  18. /**
  19. * Crude tests for the {@link BinaryDeltaInputStream} using delta diffs
  20. * generated by C git.
  21. */
  22. public class BinaryDeltaInputStreamTest {
  23. private InputStream getBinaryHunk(String name) {
  24. return this.getClass().getResourceAsStream(name);
  25. }
  26. @Test
  27. public void testBinaryDelta() throws Exception {
  28. // Prepare our test data
  29. byte[] data = new byte[8192];
  30. for (int i = 0; i < data.length; i++) {
  31. data[i] = (byte) (255 - (i % 256));
  32. }
  33. // Same, but with five 'x' inserted in the middle.
  34. int middle = data.length / 2;
  35. byte[] newData = new byte[data.length + 5];
  36. System.arraycopy(data, 0, newData, 0, middle);
  37. for (int i = 0; i < 5; i++) {
  38. newData[middle + i] = 'x';
  39. }
  40. System.arraycopy(data, middle, newData, middle + 5, middle);
  41. // delta1.forward has the instructions
  42. // @formatter:off
  43. // COPY 0 4096
  44. // INSERT 5 xxxxx
  45. // COPY 0 4096
  46. // @formatter:on
  47. // Note that the way we built newData could be expressed as
  48. // @formatter:off
  49. // COPY 0 4096
  50. // INSERT 5 xxxxx
  51. // COPY 4096 4096
  52. // @formatter:on
  53. try (ByteArrayOutputStream out = new ByteArrayOutputStream();
  54. BinaryDeltaInputStream input = new BinaryDeltaInputStream(data,
  55. new InflaterInputStream(new BinaryHunkInputStream(
  56. getBinaryHunk("delta1.forward"))))) {
  57. byte[] buf = new byte[1024];
  58. int n;
  59. while ((n = input.read(buf)) >= 0) {
  60. out.write(buf, 0, n);
  61. }
  62. assertArrayEquals(newData, out.toByteArray());
  63. assertTrue(input.isFullyConsumed());
  64. }
  65. // delta1.reverse has the instructions
  66. // @formatter:off
  67. // COPY 0 4096
  68. // COPY 256 3840
  69. // COPY 256 256
  70. // @formatter:on
  71. // Note that there are alternatives, for instance
  72. // @formatter:off
  73. // COPY 0 4096
  74. // COPY 4101 4096
  75. // @formatter:on
  76. // or
  77. // @formatter:off
  78. // COPY 0 4096
  79. // COPY 0 4096
  80. // @formatter:on
  81. try (ByteArrayOutputStream out = new ByteArrayOutputStream();
  82. BinaryDeltaInputStream input = new BinaryDeltaInputStream(
  83. newData,
  84. new InflaterInputStream(new BinaryHunkInputStream(
  85. getBinaryHunk("delta1.reverse"))))) {
  86. long expectedSize = input.getExpectedResultSize();
  87. assertEquals(data.length, expectedSize);
  88. byte[] buf = new byte[1024];
  89. int n;
  90. while ((n = input.read(buf)) >= 0) {
  91. out.write(buf, 0, n);
  92. }
  93. assertArrayEquals(data, out.toByteArray());
  94. assertTrue(input.isFullyConsumed());
  95. }
  96. }
  97. }