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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 java.io.ByteArrayInputStream;
  51. import java.io.IOException;
  52. import java.io.InputStreamReader;
  53. import java.util.ArrayList;
  54. import java.util.Arrays;
  55. import java.util.List;
  56. import java.util.concurrent.atomic.AtomicInteger;
  57. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  58. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.PersonIdent;
  62. import org.eclipse.jgit.revwalk.RevCommit;
  63. import org.eclipse.jgit.revwalk.RevWalk;
  64. import org.junit.Before;
  65. import org.junit.Test;
  66. public class PushCertificateStoreTest {
  67. private static final ObjectId ID1 =
  68. ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
  69. private static final ObjectId ID2 =
  70. ObjectId.fromString("badc0ffebadc0ffebadc0ffebadc0ffebadc0ffe");
  71. private static PushCertificate newCert(String... updateLines) {
  72. StringBuilder cert = new StringBuilder(
  73. "certificate version 0.1\n"
  74. + "pusher Dave Borowitz <dborowitz@google.com> 1433954361 -0700\n"
  75. + "pushee git://localhost/repo.git\n"
  76. + "nonce 1433954361-bde756572d665bba81d8\n"
  77. + "\n");
  78. for (String updateLine : updateLines) {
  79. cert.append(updateLine).append('\n');
  80. }
  81. cert.append(
  82. "-----BEGIN PGP SIGNATURE-----\n"
  83. + "DUMMY/SIGNATURE\n"
  84. + "-----END PGP SIGNATURE-----\n");
  85. try {
  86. return PushCertificateParser.fromReader(new InputStreamReader(
  87. new ByteArrayInputStream(Constants.encode(cert.toString()))));
  88. } catch (IOException e) {
  89. throw new IllegalArgumentException(e);
  90. }
  91. }
  92. private static String command(ObjectId oldId, ObjectId newId, String ref) {
  93. return oldId.name() + " " + newId.name() + " " + ref;
  94. }
  95. private AtomicInteger ts = new AtomicInteger(1433954361);
  96. private InMemoryRepository repo;
  97. private PushCertificateStore store;
  98. @Before
  99. public void setUp() throws Exception {
  100. repo = new InMemoryRepository(new DfsRepositoryDescription("repo"));
  101. store = newStore();
  102. }
  103. @Test
  104. public void missingRef() throws Exception {
  105. assertCerts("refs/heads/master");
  106. }
  107. @Test
  108. public void saveNoChange() throws Exception {
  109. assertEquals(NO_CHANGE, store.save());
  110. }
  111. @Test
  112. public void saveOneCertOnOneRef() throws Exception {
  113. PersonIdent ident = newIdent();
  114. PushCertificate addMaster = newCert(
  115. command(zeroId(), ID1, "refs/heads/master"));
  116. store.put(addMaster, ident);
  117. assertEquals(NEW, store.save());
  118. assertCerts("refs/heads/master", addMaster);
  119. assertCerts("refs/heads/branch");
  120. try (RevWalk rw = new RevWalk(repo)) {
  121. RevCommit c = rw.parseCommit(repo.resolve(PushCertificateStore.REF_NAME));
  122. rw.parseBody(c);
  123. assertEquals("Store push certificate for refs/heads/master\n",
  124. c.getFullMessage());
  125. assertEquals(ident, c.getAuthorIdent());
  126. assertEquals(ident, c.getCommitterIdent());
  127. }
  128. }
  129. @Test
  130. public void saveTwoCertsOnSameRefInTwoUpdates() throws Exception {
  131. PushCertificate addMaster = newCert(
  132. command(zeroId(), ID1, "refs/heads/master"));
  133. store.put(addMaster, newIdent());
  134. assertEquals(NEW, store.save());
  135. PushCertificate updateMaster = newCert(
  136. command(ID1, ID2, "refs/heads/master"));
  137. store.put(updateMaster, newIdent());
  138. assertEquals(FAST_FORWARD, store.save());
  139. assertCerts("refs/heads/master", updateMaster, addMaster);
  140. }
  141. @Test
  142. public void saveTwoCertsOnSameRefInOneUpdate() throws Exception {
  143. PersonIdent ident1 = newIdent();
  144. PersonIdent ident2 = newIdent();
  145. PushCertificate updateMaster = newCert(
  146. command(ID1, ID2, "refs/heads/master"));
  147. store.put(updateMaster, ident2);
  148. PushCertificate addMaster = newCert(
  149. command(zeroId(), ID1, "refs/heads/master"));
  150. store.put(addMaster, ident1);
  151. assertEquals(NEW, store.save());
  152. assertCerts("refs/heads/master", updateMaster, addMaster);
  153. }
  154. @Test
  155. public void saveTwoCertsOnDifferentRefsInOneUpdate() throws Exception {
  156. PersonIdent ident1 = newIdent();
  157. PersonIdent ident3 = newIdent();
  158. PushCertificate addBranch = newCert(
  159. command(zeroId(), ID1, "refs/heads/branch"));
  160. store.put(addBranch, ident3);
  161. PushCertificate addMaster = newCert(
  162. command(zeroId(), ID1, "refs/heads/master"));
  163. store.put(addMaster, ident1);
  164. assertEquals(NEW, store.save());
  165. assertCerts("refs/heads/master", addMaster);
  166. assertCerts("refs/heads/branch", addBranch);
  167. }
  168. @Test
  169. public void saveTwoCertsOnDifferentRefsInTwoUpdates() throws Exception {
  170. PushCertificate addMaster = newCert(
  171. command(zeroId(), ID1, "refs/heads/master"));
  172. store.put(addMaster, newIdent());
  173. assertEquals(NEW, store.save());
  174. PushCertificate addBranch = newCert(
  175. command(zeroId(), ID1, "refs/heads/branch"));
  176. store.put(addBranch, newIdent());
  177. assertEquals(FAST_FORWARD, store.save());
  178. assertCerts("refs/heads/master", addMaster);
  179. assertCerts("refs/heads/branch", addBranch);
  180. }
  181. @Test
  182. public void saveOneCertOnMultipleRefs() throws Exception {
  183. PersonIdent ident = newIdent();
  184. PushCertificate addMasterAndBranch = newCert(
  185. command(zeroId(), ID1, "refs/heads/branch"),
  186. command(zeroId(), ID2, "refs/heads/master"));
  187. store.put(addMasterAndBranch, ident);
  188. assertEquals(NEW, store.save());
  189. assertCerts("refs/heads/master", addMasterAndBranch);
  190. assertCerts("refs/heads/branch", addMasterAndBranch);
  191. try (RevWalk rw = new RevWalk(repo)) {
  192. RevCommit c = rw.parseCommit(repo.resolve(PushCertificateStore.REF_NAME));
  193. rw.parseBody(c);
  194. assertEquals("Store push certificate for 2 refs\n", c.getFullMessage());
  195. assertEquals(ident, c.getAuthorIdent());
  196. assertEquals(ident, c.getCommitterIdent());
  197. }
  198. }
  199. @Test
  200. public void changeRefFileToDirectory() throws Exception {
  201. PushCertificate deleteRefsHeads = newCert(
  202. command(ID1, zeroId(), "refs/heads"));
  203. store.put(deleteRefsHeads, newIdent());
  204. PushCertificate addMaster = newCert(
  205. command(zeroId(), ID1, "refs/heads/master"));
  206. store.put(addMaster, newIdent());
  207. assertEquals(NEW, store.save());
  208. assertCerts("refs/heads", deleteRefsHeads);
  209. assertCerts("refs/heads/master", addMaster);
  210. }
  211. @Test
  212. public void getBeforeSaveDoesNotIncludePending() throws Exception {
  213. PushCertificate addMaster = newCert(
  214. command(zeroId(), ID1, "refs/heads/master"));
  215. store.put(addMaster, newIdent());
  216. assertEquals(NEW, store.save());
  217. PushCertificate updateMaster = newCert(
  218. command(ID1, ID2, "refs/heads/master"));
  219. store.put(updateMaster, newIdent());
  220. assertCerts("refs/heads/master", addMaster);
  221. assertEquals(FAST_FORWARD, store.save());
  222. assertCerts("refs/heads/master", updateMaster, addMaster);
  223. }
  224. @Test
  225. public void lockFailure() throws Exception {
  226. PushCertificateStore store1 = store;
  227. PushCertificateStore store2 = newStore();
  228. store2.get("refs/heads/master");
  229. PushCertificate addMaster = newCert(
  230. command(zeroId(), ID1, "refs/heads/master"));
  231. store1.put(addMaster, newIdent());
  232. assertEquals(NEW, store1.save());
  233. PushCertificate addBranch = newCert(
  234. command(zeroId(), ID2, "refs/heads/branch"));
  235. store2.put(addBranch, newIdent());
  236. assertEquals(LOCK_FAILURE, store2.save());
  237. // Reread ref after lock failure.
  238. assertCerts(store2, "refs/heads/master", addMaster);
  239. assertCerts(store2, "refs/heads/branch");
  240. assertEquals(FAST_FORWARD, store2.save());
  241. assertCerts(store2, "refs/heads/master", addMaster);
  242. assertCerts(store2, "refs/heads/branch", addBranch);
  243. }
  244. private PersonIdent newIdent() {
  245. return new PersonIdent(
  246. "A U. Thor", "author@example.com", ts.getAndIncrement(), 0);
  247. }
  248. private PushCertificateStore newStore() {
  249. return new PushCertificateStore(repo);
  250. }
  251. private void assertCerts(String refName, PushCertificate... expected)
  252. throws Exception {
  253. assertCerts(store, refName, expected);
  254. assertCerts(newStore(), refName, expected);
  255. }
  256. private static void assertCerts(PushCertificateStore store, String refName,
  257. PushCertificate... expected) throws Exception {
  258. List<PushCertificate> ex = Arrays.asList(expected);
  259. PushCertificate first = !ex.isEmpty() ? ex.get(0) : null;
  260. assertEquals(first, store.get(refName));
  261. assertEquals(ex, toList(store.getAll(refName)));
  262. }
  263. private static <T> List<T> toList(Iterable<T> it) {
  264. List<T> list = new ArrayList<>();
  265. for (T t : it) {
  266. list.add(t);
  267. }
  268. return list;
  269. }
  270. }