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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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.ObjectId;
  57. import org.eclipse.jgit.lib.ObjectLoader;
  58. import org.eclipse.jgit.lib.Ref;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.eclipse.jgit.revwalk.RevBlob;
  61. import org.eclipse.jgit.revwalk.RevCommit;
  62. import org.eclipse.jgit.revwalk.RevTree;
  63. import org.eclipse.jgit.storage.file.ObjectDirectory;
  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, 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. @Override
  102. protected void tearDown() throws Exception {
  103. if (src != null)
  104. src.close();
  105. if (dst != null)
  106. dst.close();
  107. super.tearDown();
  108. }
  109. public void testFilterHidesPrivate() throws Exception {
  110. Map<String, Ref> refs;
  111. TransportLocal t = new TransportLocal(src, uriOf(dst)) {
  112. @Override
  113. ReceivePack createReceivePack(final Repository db) {
  114. db.close();
  115. dst.incrementOpen();
  116. final ReceivePack rp = super.createReceivePack(dst);
  117. rp.setRefFilter(new HidePrivateFilter());
  118. return rp;
  119. }
  120. };
  121. try {
  122. PushConnection c = t.openPush();
  123. try {
  124. refs = c.getRefsMap();
  125. } finally {
  126. c.close();
  127. }
  128. } finally {
  129. t.close();
  130. }
  131. assertNotNull(refs);
  132. assertNull("no private", refs.get(R_PRIVATE));
  133. assertNull("no HEAD", refs.get(Constants.HEAD));
  134. assertEquals(1, refs.size());
  135. Ref master = refs.get(R_MASTER);
  136. assertNotNull("has master", master);
  137. assertEquals(B, master.getObjectId());
  138. }
  139. public void testSuccess() throws Exception {
  140. // Manually force a delta of an object so we reuse it later.
  141. //
  142. TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  143. packHeader(pack, 2);
  144. pack.write((Constants.OBJ_BLOB) << 4 | 1);
  145. deflate(pack, new byte[] { 'a' });
  146. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  147. a.copyRawTo(pack);
  148. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  149. digest(pack);
  150. openPack(pack);
  151. // Verify the only storage of b is our packed delta above.
  152. //
  153. ObjectDirectory od = (ObjectDirectory) src.getObjectDatabase();
  154. assertTrue("has b", src.hasObject(b));
  155. assertFalse("b not loose", od.fileFor(b).exists());
  156. // Now use b but in a different commit than what is hidden.
  157. //
  158. TestRepository s = new TestRepository(src);
  159. RevCommit N = s.commit().parent(B).add("q", b).create();
  160. s.update(R_MASTER, N);
  161. // Push this new content to the remote, doing strict validation.
  162. //
  163. TransportLocal t = new TransportLocal(src, uriOf(dst)) {
  164. @Override
  165. ReceivePack createReceivePack(final Repository db) {
  166. db.close();
  167. dst.incrementOpen();
  168. final ReceivePack rp = super.createReceivePack(dst);
  169. rp.setCheckReceivedObjects(true);
  170. rp.setCheckReferencedObjectsAreReachable(true);
  171. rp.setRefFilter(new HidePrivateFilter());
  172. return rp;
  173. }
  174. };
  175. RemoteRefUpdate u = new RemoteRefUpdate( //
  176. src, //
  177. R_MASTER, // src name
  178. R_MASTER, // dst name
  179. false, // do not force update
  180. null, // local tracking branch
  181. null // expected id
  182. );
  183. PushResult r;
  184. try {
  185. t.setPushThin(true);
  186. r = t.push(PM, Collections.singleton(u));
  187. } finally {
  188. t.close();
  189. }
  190. assertNotNull("have result", r);
  191. assertNull("private not advertised", r.getAdvertisedRef(R_PRIVATE));
  192. assertSame("master updated", RemoteRefUpdate.Status.OK, u.getStatus());
  193. assertEquals(N, dst.resolve(R_MASTER));
  194. }
  195. public void testCreateBranchAtHiddenCommitFails() throws Exception {
  196. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64);
  197. packHeader(pack, 0);
  198. digest(pack);
  199. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256);
  200. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  201. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + P.name() + ' '
  202. + "refs/heads/s" + '\0'
  203. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  204. inPckLine.end();
  205. pack.writeTo(inBuf, PM);
  206. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  207. final ReceivePack rp = new ReceivePack(dst);
  208. rp.setCheckReceivedObjects(true);
  209. rp.setCheckReferencedObjectsAreReachable(true);
  210. rp.setRefFilter(new HidePrivateFilter());
  211. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  212. final PacketLineIn r = asPacketLineIn(outBuf);
  213. String master = r.readString();
  214. int nul = master.indexOf('\0');
  215. assertTrue("has capability list", nul > 0);
  216. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  217. assertSame(PacketLineIn.END, r.readString());
  218. assertEquals("unpack error Missing commit " + P.name(), r.readString());
  219. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  220. assertSame(PacketLineIn.END, r.readString());
  221. }
  222. public void testUsingHiddenDeltaBaseFails() throws Exception {
  223. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  224. packHeader(pack, 1);
  225. pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
  226. b.copyRawTo(pack);
  227. deflate(pack, new byte[] { 0x1, 0x1, 0x1, 'b' });
  228. digest(pack);
  229. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256);
  230. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  231. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + P.name() + ' '
  232. + "refs/heads/s" + '\0'
  233. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  234. inPckLine.end();
  235. pack.writeTo(inBuf, PM);
  236. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  237. final ReceivePack rp = new ReceivePack(dst);
  238. rp.setCheckReceivedObjects(true);
  239. rp.setCheckReferencedObjectsAreReachable(true);
  240. rp.setRefFilter(new HidePrivateFilter());
  241. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  242. final PacketLineIn r = asPacketLineIn(outBuf);
  243. String master = r.readString();
  244. int nul = master.indexOf('\0');
  245. assertTrue("has capability list", nul > 0);
  246. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  247. assertSame(PacketLineIn.END, r.readString());
  248. assertEquals("unpack error Missing blob " + b.name(), r.readString());
  249. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  250. assertSame(PacketLineIn.END, r.readString());
  251. }
  252. public void testUsingHiddenCommonBlobFails() throws Exception {
  253. // Try to use the 'b' blob that is hidden.
  254. //
  255. TestRepository<Repository> s = new TestRepository<Repository>(src);
  256. RevCommit N = s.commit().parent(B).add("q", s.blob("b")).create();
  257. // But don't include it in the pack.
  258. //
  259. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  260. packHeader(pack, 2);
  261. copy(pack, src.open(N));
  262. copy(pack,src.open(s.parseBody(N).getTree()));
  263. digest(pack);
  264. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024);
  265. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  266. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' '
  267. + "refs/heads/s" + '\0'
  268. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  269. inPckLine.end();
  270. pack.writeTo(inBuf, PM);
  271. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  272. final ReceivePack rp = new ReceivePack(dst);
  273. rp.setCheckReceivedObjects(true);
  274. rp.setCheckReferencedObjectsAreReachable(true);
  275. rp.setRefFilter(new HidePrivateFilter());
  276. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  277. final PacketLineIn r = asPacketLineIn(outBuf);
  278. String master = r.readString();
  279. int nul = master.indexOf('\0');
  280. assertTrue("has capability list", nul > 0);
  281. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  282. assertSame(PacketLineIn.END, r.readString());
  283. assertEquals("unpack error Missing blob " + b.name(), r.readString());
  284. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  285. assertSame(PacketLineIn.END, r.readString());
  286. }
  287. public void testUsingUnknownBlobFails() throws Exception {
  288. // Try to use the 'n' blob that is not on the server.
  289. //
  290. TestRepository<Repository> s = new TestRepository<Repository>(src);
  291. RevBlob n = s.blob("n");
  292. RevCommit N = s.commit().parent(B).add("q", n).create();
  293. // But don't include it in the pack.
  294. //
  295. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  296. packHeader(pack, 2);
  297. copy(pack, src.open(N));
  298. copy(pack,src.open(s.parseBody(N).getTree()));
  299. digest(pack);
  300. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024);
  301. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  302. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' '
  303. + "refs/heads/s" + '\0'
  304. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  305. inPckLine.end();
  306. pack.writeTo(inBuf, PM);
  307. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  308. final ReceivePack rp = new ReceivePack(dst);
  309. rp.setCheckReceivedObjects(true);
  310. rp.setCheckReferencedObjectsAreReachable(true);
  311. rp.setRefFilter(new HidePrivateFilter());
  312. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  313. final PacketLineIn r = asPacketLineIn(outBuf);
  314. String master = r.readString();
  315. int nul = master.indexOf('\0');
  316. assertTrue("has capability list", nul > 0);
  317. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  318. assertSame(PacketLineIn.END, r.readString());
  319. assertEquals("unpack error Missing blob " + n.name(), r.readString());
  320. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  321. assertSame(PacketLineIn.END, r.readString());
  322. }
  323. public void testUsingUnknownTreeFails() throws Exception {
  324. TestRepository<Repository> s = new TestRepository<Repository>(src);
  325. RevCommit N = s.commit().parent(B).add("q", s.blob("a")).create();
  326. RevTree t = s.parseBody(N).getTree();
  327. // Don't include the tree in the pack.
  328. //
  329. final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
  330. packHeader(pack, 1);
  331. copy(pack, src.open(N));
  332. digest(pack);
  333. final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024);
  334. final PacketLineOut inPckLine = new PacketLineOut(inBuf);
  335. inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' '
  336. + "refs/heads/s" + '\0'
  337. + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
  338. inPckLine.end();
  339. pack.writeTo(inBuf, PM);
  340. final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
  341. final ReceivePack rp = new ReceivePack(dst);
  342. rp.setCheckReceivedObjects(true);
  343. rp.setCheckReferencedObjectsAreReachable(true);
  344. rp.setRefFilter(new HidePrivateFilter());
  345. rp.receive(new ByteArrayInputStream(inBuf.toByteArray()), outBuf, null);
  346. final PacketLineIn r = asPacketLineIn(outBuf);
  347. String master = r.readString();
  348. int nul = master.indexOf('\0');
  349. assertTrue("has capability list", nul > 0);
  350. assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
  351. assertSame(PacketLineIn.END, r.readString());
  352. assertEquals("unpack error Missing tree " + t.name(), r.readString());
  353. assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
  354. assertSame(PacketLineIn.END, r.readString());
  355. }
  356. private void packHeader(TemporaryBuffer.Heap tinyPack, int cnt)
  357. throws IOException {
  358. final byte[] hdr = new byte[8];
  359. NB.encodeInt32(hdr, 0, 2);
  360. NB.encodeInt32(hdr, 4, cnt);
  361. tinyPack.write(Constants.PACK_SIGNATURE);
  362. tinyPack.write(hdr, 0, 8);
  363. }
  364. private void copy(TemporaryBuffer.Heap tinyPack, ObjectLoader ldr)
  365. throws IOException {
  366. final byte[] buf = new byte[64];
  367. final byte[] content = ldr.getCachedBytes();
  368. int dataLength = content.length;
  369. int nextLength = dataLength >>> 4;
  370. int size = 0;
  371. buf[size++] = (byte) ((nextLength > 0 ? 0x80 : 0x00)
  372. | (ldr.getType() << 4) | (dataLength & 0x0F));
  373. dataLength = nextLength;
  374. while (dataLength > 0) {
  375. nextLength >>>= 7;
  376. buf[size++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (dataLength & 0x7F));
  377. dataLength = nextLength;
  378. }
  379. tinyPack.write(buf, 0, size);
  380. deflate(tinyPack, content);
  381. }
  382. private void deflate(TemporaryBuffer.Heap tinyPack, final byte[] content)
  383. throws IOException {
  384. final Deflater deflater = new Deflater();
  385. final byte[] buf = new byte[128];
  386. deflater.setInput(content, 0, content.length);
  387. deflater.finish();
  388. do {
  389. final int n = deflater.deflate(buf, 0, buf.length);
  390. if (n > 0)
  391. tinyPack.write(buf, 0, n);
  392. } while (!deflater.finished());
  393. }
  394. private void digest(TemporaryBuffer.Heap buf) throws IOException {
  395. MessageDigest md = Constants.newMessageDigest();
  396. md.update(buf.toByteArray());
  397. buf.write(md.digest());
  398. }
  399. private void openPack(TemporaryBuffer.Heap buf) throws IOException {
  400. final byte[] raw = buf.toByteArray();
  401. IndexPack ip = IndexPack.create(src, new ByteArrayInputStream(raw));
  402. ip.setFixThin(true);
  403. ip.index(PM);
  404. ip.renameAndOpenPack();
  405. }
  406. private static PacketLineIn asPacketLineIn(TemporaryBuffer.Heap buf)
  407. throws IOException {
  408. return new PacketLineIn(new ByteArrayInputStream(buf.toByteArray()));
  409. }
  410. private static final class HidePrivateFilter implements RefFilter {
  411. public Map<String, Ref> filter(Map<String, Ref> refs) {
  412. Map<String, Ref> r = new HashMap<String, Ref>(refs);
  413. assertNotNull(r.remove(R_PRIVATE));
  414. return r;
  415. }
  416. }
  417. private static URIish uriOf(Repository r) throws URISyntaxException {
  418. return new URIish(r.getDirectory().getAbsolutePath());
  419. }
  420. }