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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (C) 2010, Google Inc. 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.lib;
  11. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertNotNull;
  15. import static org.junit.Assert.assertNotSame;
  16. import static org.junit.Assert.assertSame;
  17. import static org.junit.Assert.assertTrue;
  18. import static org.junit.Assert.fail;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import java.util.Arrays;
  23. import org.eclipse.jgit.errors.LargeObjectException;
  24. import org.eclipse.jgit.errors.MissingObjectException;
  25. import org.eclipse.jgit.junit.JGitTestUtil;
  26. import org.eclipse.jgit.junit.TestRng;
  27. import org.junit.Test;
  28. public class ObjectLoaderTest {
  29. private TestRng rng;
  30. private TestRng getRng() {
  31. if (rng == null)
  32. rng = new TestRng(JGitTestUtil.getName());
  33. return rng;
  34. }
  35. @Test
  36. public void testSmallObjectLoader() throws MissingObjectException,
  37. IOException {
  38. final byte[] act = getRng().nextBytes(512);
  39. final ObjectLoader ldr = new ObjectLoader.SmallObject(OBJ_BLOB, act);
  40. assertEquals(OBJ_BLOB, ldr.getType());
  41. assertEquals(act.length, ldr.getSize());
  42. assertFalse("not is large", ldr.isLarge());
  43. assertSame(act, ldr.getCachedBytes());
  44. assertSame(act, ldr.getCachedBytes(1));
  45. assertSame(act, ldr.getCachedBytes(Integer.MAX_VALUE));
  46. byte[] copy = ldr.getBytes();
  47. assertNotSame(act, copy);
  48. assertTrue("same content", Arrays.equals(act, copy));
  49. copy = ldr.getBytes(1);
  50. assertNotSame(act, copy);
  51. assertTrue("same content", Arrays.equals(act, copy));
  52. copy = ldr.getBytes(Integer.MAX_VALUE);
  53. assertNotSame(act, copy);
  54. assertTrue("same content", Arrays.equals(act, copy));
  55. ObjectStream in = ldr.openStream();
  56. assertNotNull("has stream", in);
  57. assertTrue("is small stream", in instanceof ObjectStream.SmallStream);
  58. assertEquals(OBJ_BLOB, in.getType());
  59. assertEquals(act.length, in.getSize());
  60. assertEquals(act.length, in.available());
  61. assertTrue("mark supported", in.markSupported());
  62. copy = new byte[act.length];
  63. assertEquals(act.length, in.read(copy));
  64. assertEquals(0, in.available());
  65. assertEquals(-1, in.read());
  66. assertTrue("same content", Arrays.equals(act, copy));
  67. ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  68. ldr.copyTo(tmp);
  69. assertTrue("same content", Arrays.equals(act, tmp.toByteArray()));
  70. }
  71. @Test
  72. public void testLargeObjectLoader() throws MissingObjectException,
  73. IOException {
  74. final byte[] act = getRng().nextBytes(512);
  75. final ObjectLoader ldr = new ObjectLoader() {
  76. @Override
  77. public byte[] getCachedBytes() throws LargeObjectException {
  78. throw new LargeObjectException();
  79. }
  80. @Override
  81. public long getSize() {
  82. return act.length;
  83. }
  84. @Override
  85. public int getType() {
  86. return OBJ_BLOB;
  87. }
  88. @Override
  89. public ObjectStream openStream() throws MissingObjectException,
  90. IOException {
  91. return new ObjectStream.Filter(getType(), act.length,
  92. new ByteArrayInputStream(act));
  93. }
  94. };
  95. assertEquals(OBJ_BLOB, ldr.getType());
  96. assertEquals(act.length, ldr.getSize());
  97. assertTrue("is large", ldr.isLarge());
  98. try {
  99. ldr.getCachedBytes();
  100. fail("did not throw on getCachedBytes()");
  101. } catch (LargeObjectException tooBig) {
  102. // expected
  103. }
  104. try {
  105. ldr.getBytes();
  106. fail("did not throw on getBytes()");
  107. } catch (LargeObjectException tooBig) {
  108. // expected
  109. }
  110. try {
  111. ldr.getCachedBytes(64);
  112. fail("did not throw on getCachedBytes(64)");
  113. } catch (LargeObjectException tooBig) {
  114. // expected
  115. }
  116. byte[] copy = ldr.getCachedBytes(1024);
  117. assertNotSame(act, copy);
  118. assertTrue("same content", Arrays.equals(act, copy));
  119. ObjectStream in = ldr.openStream();
  120. assertNotNull("has stream", in);
  121. assertEquals(OBJ_BLOB, in.getType());
  122. assertEquals(act.length, in.getSize());
  123. assertEquals(act.length, in.available());
  124. assertTrue("mark supported", in.markSupported());
  125. copy = new byte[act.length];
  126. assertEquals(act.length, in.read(copy));
  127. assertEquals(0, in.available());
  128. assertEquals(-1, in.read());
  129. assertTrue("same content", Arrays.equals(act, copy));
  130. ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  131. ldr.copyTo(tmp);
  132. assertTrue("same content", Arrays.equals(act, tmp.toByteArray()));
  133. }
  134. @Test
  135. public void testLimitedGetCachedBytes() throws LargeObjectException,
  136. MissingObjectException, IOException {
  137. byte[] act = getRng().nextBytes(512);
  138. ObjectLoader ldr = new ObjectLoader.SmallObject(OBJ_BLOB, act) {
  139. @Override
  140. public boolean isLarge() {
  141. return true;
  142. }
  143. };
  144. assertTrue("is large", ldr.isLarge());
  145. try {
  146. ldr.getCachedBytes(10);
  147. fail("Did not throw LargeObjectException");
  148. } catch (LargeObjectException tooBig) {
  149. // Expected result.
  150. }
  151. byte[] copy = ldr.getCachedBytes(512);
  152. assertNotSame(act, copy);
  153. assertTrue("same content", Arrays.equals(act, copy));
  154. copy = ldr.getCachedBytes(1024);
  155. assertNotSame(act, copy);
  156. assertTrue("same content", Arrays.equals(act, copy));
  157. }
  158. @Test
  159. public void testLimitedGetCachedBytesExceedsJavaLimits()
  160. throws LargeObjectException, MissingObjectException, IOException {
  161. ObjectLoader ldr = new ObjectLoader() {
  162. @Override
  163. public boolean isLarge() {
  164. return true;
  165. }
  166. @Override
  167. public byte[] getCachedBytes() throws LargeObjectException {
  168. throw new LargeObjectException();
  169. }
  170. @Override
  171. public long getSize() {
  172. return Long.MAX_VALUE;
  173. }
  174. @Override
  175. public int getType() {
  176. return OBJ_BLOB;
  177. }
  178. @Override
  179. public ObjectStream openStream() throws MissingObjectException,
  180. IOException {
  181. return new ObjectStream() {
  182. @Override
  183. public long getSize() {
  184. return Long.MAX_VALUE;
  185. }
  186. @Override
  187. public int getType() {
  188. return OBJ_BLOB;
  189. }
  190. @Override
  191. public int read() throws IOException {
  192. fail("never should have reached read");
  193. return -1;
  194. }
  195. @Override
  196. public int read(byte b[], int off, int len) {
  197. fail("never should have reached read");
  198. return -1;
  199. }
  200. };
  201. }
  202. };
  203. assertTrue("is large", ldr.isLarge());
  204. try {
  205. ldr.getCachedBytes(10);
  206. fail("Did not throw LargeObjectException");
  207. } catch (LargeObjectException tooBig) {
  208. // Expected result.
  209. }
  210. try {
  211. ldr.getCachedBytes(Integer.MAX_VALUE);
  212. fail("Did not throw LargeObjectException");
  213. } catch (LargeObjectException tooBig) {
  214. // Expected result.
  215. }
  216. }
  217. }