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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.HashMap;
  52. import java.util.Map;
  53. import org.eclipse.jgit.errors.TransportException;
  54. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  55. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.NullProgressMonitor;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.ObjectInserter;
  60. import org.eclipse.jgit.lib.RefUpdate;
  61. import org.eclipse.jgit.lib.Repository;
  62. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  63. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  64. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  65. import org.junit.After;
  66. import org.junit.Before;
  67. import org.junit.Test;
  68. public class PushConnectionTest {
  69. private URIish uri;
  70. private TestProtocol<Object> testProtocol;
  71. private Object ctx = new Object();
  72. private InMemoryRepository server;
  73. private InMemoryRepository client;
  74. private ObjectId obj1;
  75. private ObjectId obj2;
  76. private ObjectId obj3;
  77. private String refName = "refs/tags/blob";
  78. @Before
  79. public void setUp() throws Exception {
  80. server = newRepo("server");
  81. client = newRepo("client");
  82. testProtocol = new TestProtocol<>(
  83. null,
  84. new ReceivePackFactory<Object>() {
  85. @Override
  86. public ReceivePack create(Object req, Repository db)
  87. throws ServiceNotEnabledException,
  88. ServiceNotAuthorizedException {
  89. return new ReceivePack(db);
  90. }
  91. });
  92. uri = testProtocol.register(ctx, server);
  93. try (ObjectInserter ins = server.newObjectInserter()) {
  94. obj1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("test"));
  95. obj3 = ins.insert(Constants.OBJ_BLOB, Constants.encode("not"));
  96. ins.flush();
  97. RefUpdate u = server.updateRef(refName);
  98. u.setNewObjectId(obj1);
  99. assertEquals(RefUpdate.Result.NEW, u.update());
  100. }
  101. try (ObjectInserter ins = client.newObjectInserter()) {
  102. obj2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("file"));
  103. ins.flush();
  104. }
  105. }
  106. @After
  107. public void tearDown() {
  108. Transport.unregister(testProtocol);
  109. }
  110. private static InMemoryRepository newRepo(String name) {
  111. return new InMemoryRepository(new DfsRepositoryDescription(name));
  112. }
  113. @Test
  114. public void testWrongOldIdDoesNotReplace() throws IOException {
  115. RemoteRefUpdate rru = new RemoteRefUpdate(null, null, obj2, refName,
  116. false, null, obj3);
  117. Map<String, RemoteRefUpdate> updates = new HashMap<>();
  118. updates.put(rru.getRemoteName(), rru);
  119. try (Transport tn = testProtocol.open(uri, client, "server");
  120. PushConnection connection = tn.openPush()) {
  121. connection.push(NullProgressMonitor.INSTANCE, updates);
  122. }
  123. assertEquals(REJECTED_OTHER_REASON, rru.getStatus());
  124. assertEquals("invalid old id sent", rru.getMessage());
  125. }
  126. @Test
  127. public void invalidCommand() throws IOException {
  128. try (Transport tn = testProtocol.open(uri, client, "server");
  129. InternalPushConnection c = (InternalPushConnection) tn.openPush()) {
  130. StringWriter msgs = new StringWriter();
  131. PacketLineOut pckOut = c.pckOut;
  132. @SuppressWarnings("resource")
  133. SideBandInputStream in = new SideBandInputStream(c.in,
  134. NullProgressMonitor.INSTANCE, msgs, null);
  135. // Explicitly invalid command, but sane enough capabilities.
  136. StringBuilder buf = new StringBuilder();
  137. buf.append("42");
  138. buf.append(' ');
  139. buf.append(obj2.name());
  140. buf.append(' ');
  141. buf.append("refs/heads/A" + obj2.name());
  142. buf.append('\0').append(CAPABILITY_SIDE_BAND_64K);
  143. buf.append(' ').append(CAPABILITY_REPORT_STATUS);
  144. buf.append('\n');
  145. pckOut.writeString(buf.toString());
  146. pckOut.end();
  147. try {
  148. in.read();
  149. fail("expected TransportException");
  150. } catch (TransportException e) {
  151. assertEquals(
  152. "remote: error: invalid protocol: wanted 'old new ref'",
  153. e.getMessage());
  154. }
  155. }
  156. }
  157. @Test
  158. public void limitCommandBytes() throws IOException {
  159. Map<String, RemoteRefUpdate> updates = new HashMap<>();
  160. for (int i = 0; i < 4; i++) {
  161. RemoteRefUpdate rru = new RemoteRefUpdate(
  162. null, null, obj2, "refs/test/T" + i,
  163. false, null, ObjectId.zeroId());
  164. updates.put(rru.getRemoteName(), rru);
  165. }
  166. server.getConfig().setInt("receive", null, "maxCommandBytes", 190);
  167. try (Transport tn = testProtocol.open(uri, client, "server");
  168. PushConnection connection = tn.openPush()) {
  169. try {
  170. connection.push(NullProgressMonitor.INSTANCE, updates);
  171. fail("server did not abort");
  172. } catch (TransportException e) {
  173. String msg = e.getMessage();
  174. assertEquals("remote: Too many commands", msg);
  175. }
  176. }
  177. }
  178. }