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.

PushCertificateStoreTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (C) 2015, 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 static org.eclipse.jgit.lib.ObjectId.zeroId;
  45. import static org.eclipse.jgit.lib.RefUpdate.Result.FAST_FORWARD;
  46. import static org.eclipse.jgit.lib.RefUpdate.Result.LOCK_FAILURE;
  47. import static org.eclipse.jgit.lib.RefUpdate.Result.NEW;
  48. import static org.eclipse.jgit.lib.RefUpdate.Result.NO_CHANGE;
  49. import static org.junit.Assert.assertEquals;
  50. import static org.junit.Assert.assertFalse;
  51. import static org.junit.Assert.assertTrue;
  52. import java.io.ByteArrayInputStream;
  53. import java.io.IOException;
  54. import java.io.InputStreamReader;
  55. import java.util.ArrayList;
  56. import java.util.Arrays;
  57. import java.util.Collections;
  58. import java.util.List;
  59. import java.util.concurrent.atomic.AtomicInteger;
  60. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  61. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  62. import org.eclipse.jgit.lib.BatchRefUpdate;
  63. import org.eclipse.jgit.lib.Constants;
  64. import org.eclipse.jgit.lib.NullProgressMonitor;
  65. import org.eclipse.jgit.lib.ObjectId;
  66. import org.eclipse.jgit.lib.PersonIdent;
  67. import org.eclipse.jgit.revwalk.RevCommit;
  68. import org.eclipse.jgit.revwalk.RevWalk;
  69. import org.junit.Before;
  70. import org.junit.Test;
  71. public class PushCertificateStoreTest {
  72. private static final ObjectId ID1 =
  73. ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
  74. private static final ObjectId ID2 =
  75. ObjectId.fromString("badc0ffebadc0ffebadc0ffebadc0ffebadc0ffe");
  76. private static PushCertificate newCert(String... updateLines) {
  77. StringBuilder cert = new StringBuilder(
  78. "certificate version 0.1\n"
  79. + "pusher Dave Borowitz <dborowitz@google.com> 1433954361 -0700\n"
  80. + "pushee git://localhost/repo.git\n"
  81. + "nonce 1433954361-bde756572d665bba81d8\n"
  82. + "\n");
  83. for (String updateLine : updateLines) {
  84. cert.append(updateLine).append('\n');
  85. }
  86. cert.append(
  87. "-----BEGIN PGP SIGNATURE-----\n"
  88. + "DUMMY/SIGNATURE\n"
  89. + "-----END PGP SIGNATURE-----\n");
  90. try {
  91. return PushCertificateParser.fromReader(new InputStreamReader(
  92. new ByteArrayInputStream(Constants.encode(cert.toString()))));
  93. } catch (IOException e) {
  94. throw new IllegalArgumentException(e);
  95. }
  96. }
  97. private static String command(ObjectId oldId, ObjectId newId, String ref) {
  98. return oldId.name() + " " + newId.name() + " " + ref;
  99. }
  100. private AtomicInteger ts = new AtomicInteger(1433954361);
  101. private InMemoryRepository repo;
  102. private PushCertificateStore store;
  103. @Before
  104. public void setUp() throws Exception {
  105. repo = new InMemoryRepository(new DfsRepositoryDescription("repo"));
  106. store = newStore();
  107. }
  108. @Test
  109. public void missingRef() throws Exception {
  110. assertCerts("refs/heads/master");
  111. }
  112. @Test
  113. public void saveNoChange() throws Exception {
  114. assertEquals(NO_CHANGE, store.save());
  115. }
  116. @Test
  117. public void saveOneCertOnOneRef() throws Exception {
  118. PersonIdent ident = newIdent();
  119. PushCertificate addMaster = newCert(
  120. command(zeroId(), ID1, "refs/heads/master"));
  121. store.put(addMaster, ident);
  122. assertEquals(NEW, store.save());
  123. assertCerts("refs/heads/master", addMaster);
  124. assertCerts("refs/heads/branch");
  125. try (RevWalk rw = new RevWalk(repo)) {
  126. RevCommit c = rw.parseCommit(repo.resolve(PushCertificateStore.REF_NAME));
  127. rw.parseBody(c);
  128. assertEquals("Store push certificate for refs/heads/master\n",
  129. c.getFullMessage());
  130. assertEquals(ident, c.getAuthorIdent());
  131. assertEquals(ident, c.getCommitterIdent());
  132. }
  133. }
  134. @Test
  135. public void saveTwoCertsOnSameRefInTwoUpdates() throws Exception {
  136. PushCertificate addMaster = newCert(
  137. command(zeroId(), ID1, "refs/heads/master"));
  138. store.put(addMaster, newIdent());
  139. assertEquals(NEW, store.save());
  140. PushCertificate updateMaster = newCert(
  141. command(ID1, ID2, "refs/heads/master"));
  142. store.put(updateMaster, newIdent());
  143. assertEquals(FAST_FORWARD, store.save());
  144. assertCerts("refs/heads/master", updateMaster, addMaster);
  145. }
  146. @Test
  147. public void saveTwoCertsOnSameRefInOneUpdate() throws Exception {
  148. PersonIdent ident1 = newIdent();
  149. PersonIdent ident2 = newIdent();
  150. PushCertificate updateMaster = newCert(
  151. command(ID1, ID2, "refs/heads/master"));
  152. store.put(updateMaster, ident2);
  153. PushCertificate addMaster = newCert(
  154. command(zeroId(), ID1, "refs/heads/master"));
  155. store.put(addMaster, ident1);
  156. assertEquals(NEW, store.save());
  157. assertCerts("refs/heads/master", updateMaster, addMaster);
  158. }
  159. @Test
  160. public void saveTwoCertsOnDifferentRefsInOneUpdate() throws Exception {
  161. PersonIdent ident1 = newIdent();
  162. PersonIdent ident3 = newIdent();
  163. PushCertificate addBranch = newCert(
  164. command(zeroId(), ID1, "refs/heads/branch"));
  165. store.put(addBranch, ident3);
  166. PushCertificate addMaster = newCert(
  167. command(zeroId(), ID1, "refs/heads/master"));
  168. store.put(addMaster, ident1);
  169. assertEquals(NEW, store.save());
  170. assertCerts("refs/heads/master", addMaster);
  171. assertCerts("refs/heads/branch", addBranch);
  172. }
  173. @Test
  174. public void saveTwoCertsOnDifferentRefsInTwoUpdates() throws Exception {
  175. PushCertificate addMaster = newCert(
  176. command(zeroId(), ID1, "refs/heads/master"));
  177. store.put(addMaster, newIdent());
  178. assertEquals(NEW, store.save());
  179. PushCertificate addBranch = newCert(
  180. command(zeroId(), ID1, "refs/heads/branch"));
  181. store.put(addBranch, newIdent());
  182. assertEquals(FAST_FORWARD, store.save());
  183. assertCerts("refs/heads/master", addMaster);
  184. assertCerts("refs/heads/branch", addBranch);
  185. }
  186. @Test
  187. public void saveOneCertOnMultipleRefs() throws Exception {
  188. PersonIdent ident = newIdent();
  189. PushCertificate addMasterAndBranch = newCert(
  190. command(zeroId(), ID1, "refs/heads/branch"),
  191. command(zeroId(), ID2, "refs/heads/master"));
  192. store.put(addMasterAndBranch, ident);
  193. assertEquals(NEW, store.save());
  194. assertCerts("refs/heads/master", addMasterAndBranch);
  195. assertCerts("refs/heads/branch", addMasterAndBranch);
  196. try (RevWalk rw = new RevWalk(repo)) {
  197. RevCommit c = rw.parseCommit(repo.resolve(PushCertificateStore.REF_NAME));
  198. rw.parseBody(c);
  199. assertEquals("Store push certificate for 2 refs\n", c.getFullMessage());
  200. assertEquals(ident, c.getAuthorIdent());
  201. assertEquals(ident, c.getCommitterIdent());
  202. }
  203. }
  204. @Test
  205. public void changeRefFileToDirectory() throws Exception {
  206. PushCertificate deleteRefsHeads = newCert(
  207. command(ID1, zeroId(), "refs/heads"));
  208. store.put(deleteRefsHeads, newIdent());
  209. PushCertificate addMaster = newCert(
  210. command(zeroId(), ID1, "refs/heads/master"));
  211. store.put(addMaster, newIdent());
  212. assertEquals(NEW, store.save());
  213. assertCerts("refs/heads", deleteRefsHeads);
  214. assertCerts("refs/heads/master", addMaster);
  215. }
  216. @Test
  217. public void getBeforeSaveDoesNotIncludePending() throws Exception {
  218. PushCertificate addMaster = newCert(
  219. command(zeroId(), ID1, "refs/heads/master"));
  220. store.put(addMaster, newIdent());
  221. assertEquals(NEW, store.save());
  222. PushCertificate updateMaster = newCert(
  223. command(ID1, ID2, "refs/heads/master"));
  224. store.put(updateMaster, newIdent());
  225. assertCerts("refs/heads/master", addMaster);
  226. assertEquals(FAST_FORWARD, store.save());
  227. assertCerts("refs/heads/master", updateMaster, addMaster);
  228. }
  229. @Test
  230. public void lockFailure() throws Exception {
  231. PushCertificateStore store1 = store;
  232. PushCertificateStore store2 = newStore();
  233. store2.get("refs/heads/master");
  234. PushCertificate addMaster = newCert(
  235. command(zeroId(), ID1, "refs/heads/master"));
  236. store1.put(addMaster, newIdent());
  237. assertEquals(NEW, store1.save());
  238. PushCertificate addBranch = newCert(
  239. command(zeroId(), ID2, "refs/heads/branch"));
  240. store2.put(addBranch, newIdent());
  241. assertEquals(LOCK_FAILURE, store2.save());
  242. // Reread ref after lock failure.
  243. assertCerts(store2, "refs/heads/master", addMaster);
  244. assertCerts(store2, "refs/heads/branch");
  245. assertEquals(FAST_FORWARD, store2.save());
  246. assertCerts(store2, "refs/heads/master", addMaster);
  247. assertCerts(store2, "refs/heads/branch", addBranch);
  248. }
  249. @Test
  250. public void saveInBatch() throws Exception {
  251. BatchRefUpdate batch = repo.getRefDatabase().newBatchUpdate();
  252. assertFalse(store.save(batch));
  253. assertEquals(0, batch.getCommands().size());
  254. PushCertificate addMaster = newCert(
  255. command(zeroId(), ID1, "refs/heads/master"));
  256. store.put(addMaster, newIdent());
  257. assertTrue(store.save(batch));
  258. List<ReceiveCommand> commands = batch.getCommands();
  259. assertEquals(1, commands.size());
  260. ReceiveCommand cmd = commands.get(0);
  261. assertEquals("refs/meta/push-certs", cmd.getRefName());
  262. assertEquals(ReceiveCommand.Result.NOT_ATTEMPTED, cmd.getResult());
  263. try (RevWalk rw = new RevWalk(repo)) {
  264. batch.execute(rw, NullProgressMonitor.INSTANCE);
  265. assertEquals(ReceiveCommand.Result.OK, cmd.getResult());
  266. }
  267. }
  268. @Test
  269. public void putMatchingWithNoMatchingRefs() throws Exception {
  270. PushCertificate addMaster = newCert(
  271. command(zeroId(), ID1, "refs/heads/master"),
  272. command(zeroId(), ID2, "refs/heads/branch"));
  273. store.put(addMaster, newIdent(), Collections.<ReceiveCommand> emptyList());
  274. assertEquals(NO_CHANGE, store.save());
  275. }
  276. @Test
  277. public void putMatchingWithNoMatchingRefsInBatchOnEmptyRef()
  278. throws Exception {
  279. PushCertificate addMaster = newCert(
  280. command(zeroId(), ID1, "refs/heads/master"),
  281. command(zeroId(), ID2, "refs/heads/branch"));
  282. store.put(addMaster, newIdent(), Collections.<ReceiveCommand> emptyList());
  283. BatchRefUpdate batch = repo.getRefDatabase().newBatchUpdate();
  284. assertFalse(store.save(batch));
  285. assertEquals(0, batch.getCommands().size());
  286. }
  287. @Test
  288. public void putMatchingWithNoMatchingRefsInBatchOnNonEmptyRef()
  289. throws Exception {
  290. PushCertificate addMaster = newCert(
  291. command(zeroId(), ID1, "refs/heads/master"));
  292. store.put(addMaster, newIdent());
  293. assertEquals(NEW, store.save());
  294. PushCertificate addBranch = newCert(
  295. command(zeroId(), ID2, "refs/heads/branch"));
  296. store.put(addBranch, newIdent(), Collections.<ReceiveCommand> emptyList());
  297. BatchRefUpdate batch = repo.getRefDatabase().newBatchUpdate();
  298. assertFalse(store.save(batch));
  299. assertEquals(0, batch.getCommands().size());
  300. }
  301. @Test
  302. public void putMatchingWithSomeMatchingRefs() throws Exception {
  303. PushCertificate addMasterAndBranch = newCert(
  304. command(zeroId(), ID1, "refs/heads/master"),
  305. command(zeroId(), ID2, "refs/heads/branch"));
  306. store.put(addMasterAndBranch, newIdent(),
  307. Collections.singleton(addMasterAndBranch.getCommands().get(0)));
  308. assertEquals(NEW, store.save());
  309. assertCerts("refs/heads/master", addMasterAndBranch);
  310. assertCerts("refs/heads/branch");
  311. }
  312. private PersonIdent newIdent() {
  313. return new PersonIdent(
  314. "A U. Thor", "author@example.com", ts.getAndIncrement(), 0);
  315. }
  316. private PushCertificateStore newStore() {
  317. return new PushCertificateStore(repo);
  318. }
  319. private void assertCerts(String refName, PushCertificate... expected)
  320. throws Exception {
  321. assertCerts(store, refName, expected);
  322. assertCerts(newStore(), refName, expected);
  323. }
  324. private static void assertCerts(PushCertificateStore store, String refName,
  325. PushCertificate... expected) throws Exception {
  326. List<PushCertificate> ex = Arrays.asList(expected);
  327. PushCertificate first = !ex.isEmpty() ? ex.get(0) : null;
  328. assertEquals(first, store.get(refName));
  329. assertEquals(ex, toList(store.getAll(refName)));
  330. }
  331. private static <T> List<T> toList(Iterable<T> it) {
  332. List<T> list = new ArrayList<>();
  333. for (T t : it) {
  334. list.add(t);
  335. }
  336. return list;
  337. }
  338. }