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.

PushConnectionTest.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.transport.BasePackPushConnection.CAPABILITY_REPORT_STATUS;
  45. import static org.eclipse.jgit.transport.BasePackPushConnection.CAPABILITY_SIDE_BAND_64K;
  46. import static org.eclipse.jgit.transport.RemoteRefUpdate.Status.REJECTED_OTHER_REASON;
  47. import static org.junit.Assert.assertEquals;
  48. import static org.junit.Assert.fail;
  49. import java.io.IOException;
  50. import java.io.StringWriter;
  51. import java.util.ArrayList;
  52. import java.util.Collection;
  53. import java.util.HashMap;
  54. import java.util.List;
  55. import java.util.Map;
  56. import org.eclipse.jgit.errors.TransportException;
  57. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  58. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  59. import org.eclipse.jgit.junit.TestRepository;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.NullProgressMonitor;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ObjectInserter;
  64. import org.eclipse.jgit.lib.RefUpdate;
  65. import org.eclipse.jgit.lib.Repository;
  66. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  67. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  68. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  69. import org.junit.After;
  70. import org.junit.Before;
  71. import org.junit.Test;
  72. public class PushConnectionTest {
  73. private URIish uri;
  74. private TestProtocol<Object> testProtocol;
  75. private Object ctx = new Object();
  76. private InMemoryRepository server;
  77. private InMemoryRepository client;
  78. private List<String> processedRefs;
  79. private ObjectId obj1;
  80. private ObjectId obj2;
  81. private ObjectId obj3;
  82. private String refName = "refs/tags/blob";
  83. @Before
  84. public void setUp() throws Exception {
  85. server = newRepo("server");
  86. client = newRepo("client");
  87. processedRefs = new ArrayList<>();
  88. testProtocol = new TestProtocol<>(
  89. null,
  90. new ReceivePackFactory<Object>() {
  91. @Override
  92. public ReceivePack create(Object req, Repository db)
  93. throws ServiceNotEnabledException,
  94. ServiceNotAuthorizedException {
  95. ReceivePack rp = new ReceivePack(db);
  96. rp.setPreReceiveHook(
  97. new PreReceiveHook() {
  98. @Override
  99. public void onPreReceive(ReceivePack receivePack,
  100. Collection<ReceiveCommand> cmds) {
  101. for (ReceiveCommand cmd : cmds) {
  102. processedRefs.add(cmd.getRefName());
  103. }
  104. }
  105. });
  106. return rp;
  107. }
  108. });
  109. uri = testProtocol.register(ctx, server);
  110. try (ObjectInserter ins = server.newObjectInserter()) {
  111. obj1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("test"));
  112. obj3 = ins.insert(Constants.OBJ_BLOB, Constants.encode("not"));
  113. ins.flush();
  114. RefUpdate u = server.updateRef(refName);
  115. u.setNewObjectId(obj1);
  116. assertEquals(RefUpdate.Result.NEW, u.update());
  117. }
  118. try (ObjectInserter ins = client.newObjectInserter()) {
  119. obj2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("file"));
  120. ins.flush();
  121. }
  122. }
  123. @After
  124. public void tearDown() {
  125. Transport.unregister(testProtocol);
  126. }
  127. private static InMemoryRepository newRepo(String name) {
  128. return new InMemoryRepository(new DfsRepositoryDescription(name));
  129. }
  130. @Test
  131. public void testWrongOldIdDoesNotReplace() throws IOException {
  132. RemoteRefUpdate rru = new RemoteRefUpdate(null, null, obj2, refName,
  133. false, null, obj3);
  134. Map<String, RemoteRefUpdate> updates = new HashMap<>();
  135. updates.put(rru.getRemoteName(), rru);
  136. try (Transport tn = testProtocol.open(uri, client, "server");
  137. PushConnection connection = tn.openPush()) {
  138. connection.push(NullProgressMonitor.INSTANCE, updates);
  139. }
  140. assertEquals(REJECTED_OTHER_REASON, rru.getStatus());
  141. assertEquals("invalid old id sent", rru.getMessage());
  142. }
  143. @Test
  144. public void invalidCommand() throws IOException {
  145. try (Transport tn = testProtocol.open(uri, client, "server");
  146. InternalPushConnection c = (InternalPushConnection) tn.openPush()) {
  147. StringWriter msgs = new StringWriter();
  148. PacketLineOut pckOut = c.pckOut;
  149. @SuppressWarnings("resource")
  150. SideBandInputStream in = new SideBandInputStream(c.in,
  151. NullProgressMonitor.INSTANCE, msgs, null);
  152. // Explicitly invalid command, but sane enough capabilities.
  153. StringBuilder buf = new StringBuilder();
  154. buf.append("42");
  155. buf.append(' ');
  156. buf.append(obj2.name());
  157. buf.append(' ');
  158. buf.append("refs/heads/A" + obj2.name());
  159. buf.append('\0').append(CAPABILITY_SIDE_BAND_64K);
  160. buf.append(' ').append(CAPABILITY_REPORT_STATUS);
  161. buf.append('\n');
  162. pckOut.writeString(buf.toString());
  163. pckOut.end();
  164. try {
  165. in.read();
  166. fail("expected TransportException");
  167. } catch (TransportException e) {
  168. assertEquals(
  169. "remote: error: invalid protocol: wanted 'old new ref'",
  170. e.getMessage());
  171. }
  172. }
  173. }
  174. @Test
  175. public void limitCommandBytes() throws IOException {
  176. Map<String, RemoteRefUpdate> updates = new HashMap<>();
  177. for (int i = 0; i < 4; i++) {
  178. RemoteRefUpdate rru = new RemoteRefUpdate(
  179. null, null, obj2, "refs/test/T" + i,
  180. false, null, ObjectId.zeroId());
  181. updates.put(rru.getRemoteName(), rru);
  182. }
  183. server.getConfig().setInt("receive", null, "maxCommandBytes", 195);
  184. try (Transport tn = testProtocol.open(uri, client, "server");
  185. PushConnection connection = tn.openPush()) {
  186. try {
  187. connection.push(NullProgressMonitor.INSTANCE, updates);
  188. fail("server did not abort");
  189. } catch (TransportException e) {
  190. String msg = e.getMessage();
  191. assertEquals("remote: Too many commands", msg);
  192. }
  193. }
  194. }
  195. @Test
  196. public void commandOrder() throws Exception {
  197. TestRepository<?> tr = new TestRepository<>(client);
  198. List<RemoteRefUpdate> updates = new ArrayList<>();
  199. // Arbitrary non-sorted order.
  200. for (int i = 9; i >= 0; i--) {
  201. String name = "refs/heads/b" + i;
  202. tr.branch(name).commit().create();
  203. RemoteRefUpdate rru = new RemoteRefUpdate(client, name, name, false, null,
  204. ObjectId.zeroId());
  205. updates.add(rru);
  206. }
  207. PushResult result;
  208. try (Transport tn = testProtocol.open(uri, client, "server")) {
  209. result = tn.push(NullProgressMonitor.INSTANCE, updates);
  210. }
  211. for (RemoteRefUpdate remoteUpdate : result.getRemoteUpdates()) {
  212. assertEquals(
  213. "update should succeed on " + remoteUpdate.getRemoteName(),
  214. RemoteRefUpdate.Status.OK, remoteUpdate.getStatus());
  215. }
  216. List<String> expected = remoteRefNames(updates);
  217. assertEquals(
  218. "ref names processed by ReceivePack should match input ref names in order",
  219. expected, processedRefs);
  220. assertEquals(
  221. "remote ref names should match input ref names in order",
  222. expected, remoteRefNames(result.getRemoteUpdates()));
  223. }
  224. private static List<String> remoteRefNames(Collection<RemoteRefUpdate> updates) {
  225. List<String> result = new ArrayList<>();
  226. for (RemoteRefUpdate u : updates) {
  227. result.add(u.getRemoteName());
  228. }
  229. return result;
  230. }
  231. }