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

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