Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PushCertificateParserTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (C) 2015, Google Inc.
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  30. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  35. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  40. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.transport;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertFalse;
  45. import static org.junit.Assert.assertNotEquals;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.ByteArrayInputStream;
  51. import java.io.EOFException;
  52. import java.io.IOException;
  53. import java.io.InputStreamReader;
  54. import java.io.Reader;
  55. import java.io.StringReader;
  56. import org.eclipse.jgit.errors.PackProtocolException;
  57. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  58. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  59. import org.eclipse.jgit.lib.Config;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.ObjectId;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.transport.PushCertificate.NonceStatus;
  64. import org.junit.Before;
  65. import org.junit.Test;
  66. /** Test for push certificate parsing. */
  67. public class PushCertificateParserTest {
  68. // Example push certificate generated by C git 2.2.0.
  69. private static final String INPUT = "001ccertificate version 0.1\n"
  70. + "0041pusher Dave Borowitz <dborowitz@google.com> 1433954361 -0700\n"
  71. + "0024pushee git://localhost/repo.git\n"
  72. + "002anonce 1433954361-bde756572d665bba81d8\n"
  73. + "0005\n"
  74. + "00680000000000000000000000000000000000000000"
  75. + " 6c2b981a177396fb47345b7df3e4d3f854c6bea7"
  76. + " refs/heads/master\n"
  77. + "0022-----BEGIN PGP SIGNATURE-----\n"
  78. + "0016Version: GnuPG v1\n"
  79. + "0005\n"
  80. + "0045iQEcBAABAgAGBQJVeGg5AAoJEPfTicJkUdPkUggH/RKAeI9/i/LduuiqrL/SSdIa\n"
  81. + "00459tYaSqJKLbXz63M/AW4Sp+4u+dVCQvnAt/a35CVEnpZz6hN4Kn/tiswOWVJf4CO7\n"
  82. + "0045htNubGs5ZMwvD6sLYqKAnrM3WxV/2TbbjzjZW6Jkidz3jz/WRT4SmjGYiEO7aA+V\n"
  83. + "00454ZdIS9f7sW5VsHHYlNThCA7vH8Uu48bUovFXyQlPTX0pToSgrWV3JnTxDNxfn3iG\n"
  84. + "0045IL0zTY/qwVCdXgFownLcs6J050xrrBWIKqfcWr3u4D2aCLyR0v+S/KArr7ulZygY\n"
  85. + "0045+SOklImn8TAZiNxhWtA6ens66IiammUkZYFv7SSzoPLFZT4dC84SmGPWgf94NoQ=\n"
  86. + "000a=XFeC\n"
  87. + "0020-----END PGP SIGNATURE-----\n"
  88. + "0012push-cert-end\n";
  89. // Same push certificate, with all trailing newlines stripped.
  90. // (Note that the canonical signed payload is the same, so the same signature
  91. // is still valid.)
  92. private static final String INPUT_NO_NEWLINES = "001bcertificate version 0.1"
  93. + "0040pusher Dave Borowitz <dborowitz@google.com> 1433954361 -0700"
  94. + "0023pushee git://localhost/repo.git"
  95. + "0029nonce 1433954361-bde756572d665bba81d8"
  96. + "0004"
  97. + "00670000000000000000000000000000000000000000"
  98. + " 6c2b981a177396fb47345b7df3e4d3f854c6bea7"
  99. + " refs/heads/master"
  100. + "0021-----BEGIN PGP SIGNATURE-----"
  101. + "0015Version: GnuPG v1"
  102. + "0004"
  103. + "0044iQEcBAABAgAGBQJVeGg5AAoJEPfTicJkUdPkUggH/RKAeI9/i/LduuiqrL/SSdIa"
  104. + "00449tYaSqJKLbXz63M/AW4Sp+4u+dVCQvnAt/a35CVEnpZz6hN4Kn/tiswOWVJf4CO7"
  105. + "0044htNubGs5ZMwvD6sLYqKAnrM3WxV/2TbbjzjZW6Jkidz3jz/WRT4SmjGYiEO7aA+V"
  106. + "00444ZdIS9f7sW5VsHHYlNThCA7vH8Uu48bUovFXyQlPTX0pToSgrWV3JnTxDNxfn3iG"
  107. + "0044IL0zTY/qwVCdXgFownLcs6J050xrrBWIKqfcWr3u4D2aCLyR0v+S/KArr7ulZygY"
  108. + "0044+SOklImn8TAZiNxhWtA6ens66IiammUkZYFv7SSzoPLFZT4dC84SmGPWgf94NoQ="
  109. + "0009=XFeC"
  110. + "001f-----END PGP SIGNATURE-----"
  111. + "0011push-cert-end";
  112. private Repository db;
  113. @Before
  114. public void setUp() {
  115. db = new InMemoryRepository(new DfsRepositoryDescription("repo"));
  116. }
  117. private static SignedPushConfig newEnabledConfig() {
  118. Config cfg = new Config();
  119. cfg.setString("receive", null, "certnonceseed", "sekret");
  120. return SignedPushConfig.KEY.parse(cfg);
  121. }
  122. private static SignedPushConfig newDisabledConfig() {
  123. return SignedPushConfig.KEY.parse(new Config());
  124. }
  125. @Test
  126. public void noCert() throws Exception {
  127. PushCertificateParser parser =
  128. new PushCertificateParser(db, newEnabledConfig());
  129. assertTrue(parser.enabled());
  130. assertNull(parser.build());
  131. ObjectId oldId = ObjectId.zeroId();
  132. ObjectId newId =
  133. ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
  134. String line = oldId.name() + " " + newId.name() + " refs/heads/master";
  135. ReceiveCommand cmd = BaseReceivePack.parseCommand(line);
  136. parser.addCommand(cmd);
  137. parser.addCommand(line);
  138. assertNull(parser.build());
  139. }
  140. @Test
  141. public void disabled() throws Exception {
  142. PacketLineIn pckIn = newPacketLineIn(INPUT);
  143. PushCertificateParser parser =
  144. new PushCertificateParser(db, newDisabledConfig());
  145. assertFalse(parser.enabled());
  146. assertNull(parser.build());
  147. parser.receiveHeader(pckIn, false);
  148. parser.addCommand(pckIn.readString());
  149. assertEquals(PushCertificateParser.BEGIN_SIGNATURE, pckIn.readString());
  150. parser.receiveSignature(pckIn);
  151. assertNull(parser.build());
  152. }
  153. @Test
  154. public void disabledParserStillRequiresCorrectSyntax() throws Exception {
  155. PacketLineIn pckIn = newPacketLineIn("001ccertificate version XYZ\n");
  156. PushCertificateParser parser =
  157. new PushCertificateParser(db, newDisabledConfig());
  158. assertFalse(parser.enabled());
  159. try {
  160. parser.receiveHeader(pckIn, false);
  161. fail("Expected PackProtocolException");
  162. } catch (PackProtocolException e) {
  163. assertEquals(
  164. "Push certificate has missing or invalid value for certificate"
  165. + " version: XYZ",
  166. e.getMessage());
  167. }
  168. assertNull(parser.build());
  169. }
  170. @Test
  171. public void parseCertFromPktLine() throws Exception {
  172. PacketLineIn pckIn = newPacketLineIn(INPUT);
  173. PushCertificateParser parser =
  174. new PushCertificateParser(db, newEnabledConfig());
  175. parser.receiveHeader(pckIn, false);
  176. parser.addCommand(pckIn.readString());
  177. assertEquals(PushCertificateParser.BEGIN_SIGNATURE, pckIn.readString());
  178. parser.receiveSignature(pckIn);
  179. PushCertificate cert = parser.build();
  180. assertEquals("0.1", cert.getVersion());
  181. assertEquals("Dave Borowitz", cert.getPusherIdent().getName());
  182. assertEquals("dborowitz@google.com",
  183. cert.getPusherIdent().getEmailAddress());
  184. assertEquals(1433954361000L, cert.getPusherIdent().getWhen().getTime());
  185. assertEquals(-7 * 60, cert.getPusherIdent().getTimeZoneOffset());
  186. assertEquals("git://localhost/repo.git", cert.getPushee());
  187. assertEquals("1433954361-bde756572d665bba81d8", cert.getNonce());
  188. assertNotEquals(cert.getNonce(), parser.getAdvertiseNonce());
  189. assertEquals(PushCertificate.NonceStatus.BAD, cert.getNonceStatus());
  190. assertEquals(1, cert.getCommands().size());
  191. ReceiveCommand cmd = cert.getCommands().get(0);
  192. assertEquals("refs/heads/master", cmd.getRefName());
  193. assertEquals(ObjectId.zeroId(), cmd.getOldId());
  194. assertEquals("6c2b981a177396fb47345b7df3e4d3f854c6bea7",
  195. cmd.getNewId().name());
  196. assertEquals(concatPacketLines(INPUT, 0, 6), cert.toText());
  197. assertEquals(concatPacketLines(INPUT, 0, 17), cert.toTextWithSignature());
  198. String signature = concatPacketLines(INPUT, 6, 17);
  199. assertTrue(signature.startsWith(PushCertificateParser.BEGIN_SIGNATURE));
  200. assertTrue(signature.endsWith(PushCertificateParser.END_SIGNATURE + "\n"));
  201. assertEquals(signature, cert.getSignature());
  202. }
  203. @Test
  204. public void parseCertFromPktLineNoNewlines() throws Exception {
  205. PacketLineIn pckIn = newPacketLineIn(INPUT_NO_NEWLINES);
  206. PushCertificateParser parser =
  207. new PushCertificateParser(db, newEnabledConfig());
  208. parser.receiveHeader(pckIn, false);
  209. parser.addCommand(pckIn.readString());
  210. assertEquals(PushCertificateParser.BEGIN_SIGNATURE, pckIn.readString());
  211. parser.receiveSignature(pckIn);
  212. PushCertificate cert = parser.build();
  213. assertEquals("0.1", cert.getVersion());
  214. assertEquals("Dave Borowitz", cert.getPusherIdent().getName());
  215. assertEquals("dborowitz@google.com",
  216. cert.getPusherIdent().getEmailAddress());
  217. assertEquals(1433954361000L, cert.getPusherIdent().getWhen().getTime());
  218. assertEquals(-7 * 60, cert.getPusherIdent().getTimeZoneOffset());
  219. assertEquals("git://localhost/repo.git", cert.getPushee());
  220. assertEquals("1433954361-bde756572d665bba81d8", cert.getNonce());
  221. assertNotEquals(cert.getNonce(), parser.getAdvertiseNonce());
  222. assertEquals(PushCertificate.NonceStatus.BAD, cert.getNonceStatus());
  223. assertEquals(1, cert.getCommands().size());
  224. ReceiveCommand cmd = cert.getCommands().get(0);
  225. assertEquals("refs/heads/master", cmd.getRefName());
  226. assertEquals(ObjectId.zeroId(), cmd.getOldId());
  227. assertEquals("6c2b981a177396fb47345b7df3e4d3f854c6bea7",
  228. cmd.getNewId().name());
  229. // Canonical signed payload has reinserted newlines.
  230. assertEquals(concatPacketLines(INPUT, 0, 6), cert.toText());
  231. String signature = concatPacketLines(INPUT, 6, 17);
  232. assertTrue(signature.startsWith(PushCertificateParser.BEGIN_SIGNATURE));
  233. assertTrue(signature.endsWith(PushCertificateParser.END_SIGNATURE + "\n"));
  234. assertEquals(signature, cert.getSignature());
  235. }
  236. @Test
  237. public void testConcatPacketLines() throws Exception {
  238. String input = "000bline 1\n000bline 2\n000bline 3\n";
  239. assertEquals("line 1\n", concatPacketLines(input, 0, 1));
  240. assertEquals("line 1\nline 2\n", concatPacketLines(input, 0, 2));
  241. assertEquals("line 2\nline 3\n", concatPacketLines(input, 1, 3));
  242. assertEquals("line 2\nline 3\n", concatPacketLines(input, 1, 4));
  243. }
  244. @Test
  245. public void testConcatPacketLinesInsertsNewlines() throws Exception {
  246. String input = "000bline 1\n000aline 2000bline 3\n";
  247. assertEquals("line 1\n", concatPacketLines(input, 0, 1));
  248. assertEquals("line 1\nline 2\n", concatPacketLines(input, 0, 2));
  249. assertEquals("line 2\nline 3\n", concatPacketLines(input, 1, 3));
  250. assertEquals("line 2\nline 3\n", concatPacketLines(input, 1, 4));
  251. }
  252. @Test
  253. public void testParseReader() throws Exception {
  254. Reader reader = new StringReader(concatPacketLines(INPUT, 0, 18));
  255. PushCertificate streamCert = PushCertificateParser.fromReader(reader);
  256. PacketLineIn pckIn = newPacketLineIn(INPUT);
  257. PushCertificateParser pckParser =
  258. new PushCertificateParser(db, newEnabledConfig());
  259. pckParser.receiveHeader(pckIn, false);
  260. pckParser.addCommand(pckIn.readString());
  261. assertEquals(PushCertificateParser.BEGIN_SIGNATURE, pckIn.readString());
  262. pckParser.receiveSignature(pckIn);
  263. PushCertificate pckCert = pckParser.build();
  264. // Nonce status is unsolicited since this was not parsed in the context of
  265. // the wire protocol; as a result, certs are not actually equal.
  266. assertEquals(NonceStatus.UNSOLICITED, streamCert.getNonceStatus());
  267. assertEquals(pckCert.getVersion(), streamCert.getVersion());
  268. assertEquals(pckCert.getPusherIdent().getName(),
  269. streamCert.getPusherIdent().getName());
  270. assertEquals(pckCert.getPusherIdent().getEmailAddress(),
  271. streamCert.getPusherIdent().getEmailAddress());
  272. assertEquals(pckCert.getPusherIdent().getWhen().getTime(),
  273. streamCert.getPusherIdent().getWhen().getTime());
  274. assertEquals(pckCert.getPusherIdent().getTimeZoneOffset(),
  275. streamCert.getPusherIdent().getTimeZoneOffset());
  276. assertEquals(pckCert.getPushee(), streamCert.getPushee());
  277. assertEquals(pckCert.getNonce(), streamCert.getNonce());
  278. assertEquals(pckCert.getSignature(), streamCert.getSignature());
  279. assertEquals(pckCert.toText(), streamCert.toText());
  280. assertEquals(pckCert.getCommands().size(), streamCert.getCommands().size());
  281. ReceiveCommand pckCmd = pckCert.getCommands().get(0);
  282. ReceiveCommand streamCmd = streamCert.getCommands().get(0);
  283. assertEquals(pckCmd.getRefName(), streamCmd.getRefName());
  284. assertEquals(pckCmd.getOldId(), streamCmd.getOldId());
  285. assertEquals(pckCmd.getNewId().name(), streamCmd.getNewId().name());
  286. }
  287. @Test
  288. public void testParseString() throws Exception {
  289. String str = concatPacketLines(INPUT, 0, 18);
  290. assertEquals(
  291. PushCertificateParser.fromReader(new StringReader(str)),
  292. PushCertificateParser.fromString(str));
  293. }
  294. @Test
  295. public void testParseMultipleFromStream() throws Exception {
  296. String input = concatPacketLines(INPUT, 0, 17);
  297. assertFalse(input.contains(PushCertificateParser.END_CERT));
  298. input += input;
  299. Reader reader = new InputStreamReader(
  300. new ByteArrayInputStream(Constants.encode(input)));
  301. assertNotNull(PushCertificateParser.fromReader(reader));
  302. assertNotNull(PushCertificateParser.fromReader(reader));
  303. assertEquals(-1, reader.read());
  304. assertNull(PushCertificateParser.fromReader(reader));
  305. }
  306. @Test
  307. public void testMissingPusheeField() throws Exception {
  308. // Omit pushee line from existing cert. (This means the signature would not
  309. // match, but we're not verifying it here.)
  310. String input = INPUT.replace("0024pushee git://localhost/repo.git\n", "");
  311. assertFalse(input.contains(PushCertificateParser.PUSHEE));
  312. PacketLineIn pckIn = newPacketLineIn(input);
  313. PushCertificateParser parser =
  314. new PushCertificateParser(db, newEnabledConfig());
  315. parser.receiveHeader(pckIn, false);
  316. parser.addCommand(pckIn.readString());
  317. assertEquals(PushCertificateParser.BEGIN_SIGNATURE, pckIn.readString());
  318. parser.receiveSignature(pckIn);
  319. PushCertificate cert = parser.build();
  320. assertEquals("0.1", cert.getVersion());
  321. assertNull(cert.getPushee());
  322. assertFalse(cert.toText().contains(PushCertificateParser.PUSHEE));
  323. }
  324. private static String concatPacketLines(String input, int begin, int end)
  325. throws IOException {
  326. StringBuilder result = new StringBuilder();
  327. int i = 0;
  328. PacketLineIn pckIn = newPacketLineIn(input);
  329. while (i < end) {
  330. String line;
  331. try {
  332. line = pckIn.readString();
  333. } catch (EOFException e) {
  334. break;
  335. }
  336. if (++i > begin) {
  337. result.append(line).append('\n');
  338. }
  339. }
  340. return result.toString();
  341. }
  342. private static PacketLineIn newPacketLineIn(String input) {
  343. return new PacketLineIn(new ByteArrayInputStream(Constants.encode(input)));
  344. }
  345. }