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.

ReceivePackRefFilterTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.ByteArrayInputStream;
  45. import java.io.IOException;
  46. import java.net.URISyntaxException;
  47. import java.security.MessageDigest;
  48. import java.util.Collections;
  49. import java.util.HashMap;
  50. import java.util.Map;
  51. import java.util.zip.Deflater;
  52. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  53. import org.eclipse.jgit.junit.TestRepository;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.NullProgressMonitor;
  56. import org.eclipse.jgit.lib.ObjectDirectory;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.ObjectLoader;
  59. import org.eclipse.jgit.lib.Ref;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.revwalk.RevBlob;
  62. import org.eclipse.jgit.revwalk.RevCommit;
  63. import org.eclipse.jgit.revwalk.RevTree;
  64. import org.eclipse.jgit.util.NB;
  65. import org.eclipse.jgit.util.TemporaryBuffer;
  66. public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase {
  67. private static final NullProgressMonitor PM = NullProgressMonitor.INSTANCE;
  68. private static final String R_MASTER = Constants.R_HEADS + Constants.MASTER;
  69. private static final String R_PRIVATE = Constants.R_HEADS + "private";
  70. private Repository src;
  71. private Repository dst;
  72. private RevCommit A, B, P;
  73. private RevBlob a, b;
  74. @Override
  75. protected void setUp() throws Exception {
  76. super.setUp();
  77. src = createBareRepository();
  78. dst = createBareRepository();
  79. // Fill dst with a some common history.
  80. //
  81. TestRepository d = new TestRepository(dst);
  82. a = d.blob("a");
  83. A = d.commit(d.tree(d.file("a", a)));
  84. B = d.commit().parent(A).create();
  85. d.update(R_MASTER, B);
  86. // Clone from dst into src
  87. //
  88. Transport t = Transport.open(src, uriOf(dst));
  89. try {
  90. t.fetch(PM, Collections.singleton(new RefSpec("+refs/*:refs/*")));
  91. assertEquals(B.copy(), src.resolve(R_MASTER));
  92. } finally {
  93. t.close();
  94. }
  95. // Now put private stuff into dst.
  96. //
  97. b = d.blob("b");
  98. P = d.commit(d.tree(d.file("b", b)), A);
  99. d.update(R_PRIVATE, P);
  100. }
  101. public void testFilterHidesPrivate() throws Exception {
  102. Map<String, Ref> refs;
  103. TransportLocal t = new TransportLocal(src, uriOf(dst)) {
  104. @Override
  105. ReceivePack createReceivePack(final Repository db) {
  106. db.close();
  107. dst.incrementOpen();
  108. final ReceivePack rp = super.createReceivePack(dst);
  109. rp.setRefFilter(new HidePrivateFilter());
  110. return rp;
  111. }
  112. };
  113. try {
  114. PushConnection c = t.openPush();
  115. try {
  116. refs = c.getRefsMap();
  117. } finally {
  118. c.close();
  119. }
  120. } finally {
  121. t.close();
  122. }
  123. assertNotNull(refs);
  124. assertNull("no private", refs.get(R_PRIVATE));
  125. assertNull("no HEAD", refs.get(Constants.HEAD));
  126. assertEquals(1, refs.size());
  127. Ref master = refs.get(R_MASTER);
  128. assertNotNull("has master", master);
  129. assertEquals(B.copy(), master.getObjectId());
  130. }
  131. public void testSuccess() throws Exception {
  132. // Manually force a delta of an object so we reuse it later.
  133. //
  134. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  135. packHeader(pack, 2);
  136. pack.write((Constants.OBJ_BLOB) << 4 | 1);
  137. deflate(pack, new byte[] { 'a' });
  138. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  139. a.copyRawTo(pack);
  140. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  141. digest(pack);
  142. openPack(pack);
  143. // Verify the only storage of b is our packed delta above.
  144. //
  145. ObjectDirectory od = (ObjectDirectory) src.getObjectDatabase();
  146. assertTrue("has b", od.hasObject(b));
  147. assertFalse("b not loose", od.fileFor(b).exists());
  148. // Now use b but in a different commit than what is hidden.
  149. //
  150. TestRepository s = new TestRepository(src);
  151. RevCommit N = s.commit().parent(B).add("q", b).create();
  152. s.update(R_MASTER, N);
  153. // Push this new content to the remote, doing strict validation.
  154. //
  155. TransportLocal t = new TransportLocal(src, uriOf(dst)) {
  156. @Override
  157. ReceivePack createReceivePack(final Repository db) {
  158. db.close();
  159. dst.incrementOpen();
  160. final ReceivePack rp = super.createReceivePack(dst);
  161. rp.setCheckReceivedObjects(true);
  162. rp.setCheckReferencedObjectsAreReachable(true);
  163. rp.setRefFilter(new HidePrivateFilter());
  164. return rp;
  165. }
  166. };
  167. RemoteRefUpdate u = new RemoteRefUpdate( //
  168. src, //
  169. R_MASTER, // src name
  170. R_MASTER, // dst name
  171. false, // do not force update
  172. null, // local tracking branch
  173. null // expected id
  174. );
  175. PushResult r;
  176. try {
  177. t.setPushThin(true);
  178. r = t.push(PM, Collections.singleton(u));
  179. } finally {
  180. t.close();
  181. }
  182. assertNotNull("have result", r);
  183. assertNull("private not advertised", r.getAdvertisedRef(R_PRIVATE));
  184. assertSame("master updated", RemoteRefUpdate.Status.OK, u.getStatus());
  185. assertEquals(N.copy(), dst.resolve(R_MASTER));
  186. }
  187. public void testCreateBranchAtHiddenCommitFails() throws Exception {
  188. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64);
  189. packHeader(pack, 0);
  190. digest(pack);
  191. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256);
  192. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  193. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + P.name() + ' '
  194. + "refs/heads/s" + '\0'
  195. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  196. inPckLine.end();
  197. pack.writeTo(inBuf, PM);
  198. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  199. final ReceivePack rp = new ReceivePack(dst);
  200. rp.setCheckReceivedObjects(true);
  201. rp.setCheckReferencedObjectsAreReachable(true);
  202. rp.setRefFilter(new HidePrivateFilter());
  203. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  204. final PacketLineIn r = asPacketLineIn(outBuf);
  205. String master = r.readString();
  206. int nul = master.indexOf('\0');
  207. assertTrue("has capability list", nul > 0);
  208. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  209. assertSame(PacketLineIn.END, r.readString());
  210. assertEquals("unpack error Missing commit " + P.name(), r.readString());
  211. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  212. assertSame(PacketLineIn.END, r.readString());
  213. }
  214. public void testUsingHiddenDeltaBaseFails() throws Exception {
  215. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64);
  216. packHeader(pack, 1);
  217. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  218. b.copyRawTo(pack);
  219. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  220. digest(pack);
  221. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256);
  222. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  223. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + P.name() + ' '
  224. + "refs/heads/s" + '\0'
  225. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  226. inPckLine.end();
  227. pack.writeTo(inBuf, PM);
  228. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  229. final ReceivePack rp = new ReceivePack(dst);
  230. rp.setCheckReceivedObjects(true);
  231. rp.setCheckReferencedObjectsAreReachable(true);
  232. rp.setRefFilter(new HidePrivateFilter());
  233. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  234. final PacketLineIn r = asPacketLineIn(outBuf);
  235. String master = r.readString();
  236. int nul = master.indexOf('\0');
  237. assertTrue("has capability list", nul > 0);
  238. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  239. assertSame(PacketLineIn.END, r.readString());
  240. assertEquals("unpack error Missing blob " + b.name(), r.readString());
  241. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  242. assertSame(PacketLineIn.END, r.readString());
  243. }
  244. public void testUsingHiddenCommonBlobFails() throws Exception {
  245. // Try to use the 'b' blob that is hidden.
  246. //
  247. TestRepository s = new TestRepository(src);
  248. RevCommit N = s.commit().parent(B).add("q", s.blob("b")).create();
  249. // But don't include it in the pack.
  250. //
  251. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64);
  252. packHeader(pack, 2);
  253. copy(pack, src.openObject(N));
  254. copy(pack,src.openObject(s.parseBody(N).getTree()));
  255. digest(pack);
  256. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256);
  257. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  258. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' '
  259. + "refs/heads/s" + '\0'
  260. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  261. inPckLine.end();
  262. pack.writeTo(inBuf, PM);
  263. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  264. final ReceivePack rp = new ReceivePack(dst);
  265. rp.setCheckReceivedObjects(true);
  266. rp.setCheckReferencedObjectsAreReachable(true);
  267. rp.setRefFilter(new HidePrivateFilter());
  268. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  269. final PacketLineIn r = asPacketLineIn(outBuf);
  270. String master = r.readString();
  271. int nul = master.indexOf('\0');
  272. assertTrue("has capability list", nul > 0);
  273. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  274. assertSame(PacketLineIn.END, r.readString());
  275. assertEquals("unpack error Missing blob " + b.name(), r.readString());
  276. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  277. assertSame(PacketLineIn.END, r.readString());
  278. }
  279. public void testUsingUnknownBlobFails() throws Exception {
  280. // Try to use the 'n' blob that is not on the server.
  281. //
  282. TestRepository s = new TestRepository(src);
  283. RevBlob n = s.blob("n");
  284. RevCommit N = s.commit().parent(B).add("q", n).create();
  285. // But don't include it in the pack.
  286. //
  287. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64);
  288. packHeader(pack, 2);
  289. copy(pack, src.openObject(N));
  290. copy(pack,src.openObject(s.parseBody(N).getTree()));
  291. digest(pack);
  292. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256);
  293. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  294. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' '
  295. + "refs/heads/s" + '\0'
  296. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  297. inPckLine.end();
  298. pack.writeTo(inBuf, PM);
  299. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  300. final ReceivePack rp = new ReceivePack(dst);
  301. rp.setCheckReceivedObjects(true);
  302. rp.setCheckReferencedObjectsAreReachable(true);
  303. rp.setRefFilter(new HidePrivateFilter());
  304. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  305. final PacketLineIn r = asPacketLineIn(outBuf);
  306. String master = r.readString();
  307. int nul = master.indexOf('\0');
  308. assertTrue("has capability list", nul > 0);
  309. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  310. assertSame(PacketLineIn.END, r.readString());
  311. assertEquals("unpack error Missing blob " + n.name(), r.readString());
  312. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  313. assertSame(PacketLineIn.END, r.readString());
  314. }
  315. public void testUsingUnknownTreeFails() throws Exception {
  316. TestRepository s = new TestRepository(src);
  317. RevCommit N = s.commit().parent(B).add("q", s.blob("a")).create();
  318. RevTree t = s.parseBody(N).getTree();
  319. // Don't include the tree in the pack.
  320. //
  321. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64);
  322. packHeader(pack, 1);
  323. copy(pack, src.openObject(N));
  324. digest(pack);
  325. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256);
  326. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  327. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' '
  328. + "refs/heads/s" + '\0'
  329. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  330. inPckLine.end();
  331. pack.writeTo(inBuf, PM);
  332. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  333. final ReceivePack rp = new ReceivePack(dst);
  334. rp.setCheckReceivedObjects(true);
  335. rp.setCheckReferencedObjectsAreReachable(true);
  336. rp.setRefFilter(new HidePrivateFilter());
  337. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  338. final PacketLineIn r = asPacketLineIn(outBuf);
  339. String master = r.readString();
  340. int nul = master.indexOf('\0');
  341. assertTrue("has capability list", nul > 0);
  342. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  343. assertSame(PacketLineIn.END, r.readString());
  344. assertEquals("unpack error Missing tree " + t.name(), r.readString());
  345. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  346. assertSame(PacketLineIn.END, r.readString());
  347. }
  348. private void packHeader(TemporaryBuffer.Heap tinyPack, int cnt)
  349. throws IOException {
  350. final byte[] hdr = new byte[8];
  351. NB.encodeInt32(hdr, 0, 2);
  352. NB.encodeInt32(hdr, 4, cnt);
  353. tinyPack.write(Constants.PACK_SIGNATURE);
  354. tinyPack.write(hdr, 0, 8);
  355. }
  356. private void copy(TemporaryBuffer.Heap tinyPack, ObjectLoader ldr)
  357. throws IOException {
  358. final byte[] buf = new byte[64];
  359. final byte[] content = ldr.getCachedBytes();
  360. int dataLength = content.length;
  361. int nextLength = dataLength >>> 4;
  362. int size = 0;
  363. buf[size++] = (byte) ((nextLength > 0 ? 0x80 : 0x00)
  364. | (ldr.getType() << 4) | (dataLength & 0x0F));
  365. dataLength = nextLength;
  366. while (dataLength > 0) {
  367. nextLength >>>= 7;
  368. buf[size++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (dataLength & 0x7F));
  369. dataLength = nextLength;
  370. }
  371. tinyPack.write(buf, 0, size);
  372. deflate(tinyPack, content);
  373. }
  374. private void deflate(TemporaryBuffer.Heap tinyPack, final byte[] content)
  375. throws IOException {
  376. final Deflater deflater = new Deflater();
  377. final byte[] buf = new byte[128];
  378. deflater.setInput(content, 0, content.length);
  379. deflater.finish();
  380. do {
  381. final int n = deflater.deflate(buf, 0, buf.length);
  382. if (n > 0)
  383. tinyPack.write(buf, 0, n);
  384. } while (!deflater.finished());
  385. }
  386. private void digest(TemporaryBuffer.Heap buf) throws IOException {
  387. MessageDigest md = Constants.newMessageDigest();
  388. md.update(buf.toByteArray());
  389. buf.write(md.digest());
  390. }
  391. private void openPack(TemporaryBuffer.Heap buf) throws IOException {
  392. final byte[] raw = buf.toByteArray();
  393. IndexPack ip = IndexPack.create(src, new ByteArrayInputStream(raw));
  394. ip.setFixThin(true);
  395. ip.index(PM);
  396. ip.renameAndOpenPack();
  397. }
  398. private static PacketLineIn asPacketLineIn(TemporaryBuffer.Heap buf)
  399. throws IOException {
  400. return new PacketLineIn(new ByteArrayInputStream(buf.toByteArray()));
  401. }
  402. private static final class HidePrivateFilter implements RefFilter {
  403. public Map<String, Ref> filter(Map<String, Ref> refs) {
  404. Map<String, Ref> r = new HashMap<String, Ref>(refs);
  405. assertNotNull(r.remove(R_PRIVATE));
  406. return r;
  407. }
  408. }
  409. private static URIish uriOf(Repository r) throws URISyntaxException {
  410. return new URIish(r.getDirectory().getAbsolutePath());
  411. }
  412. }