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.

ProtocolErrorTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2010, 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.http.test;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertSame;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import java.net.HttpURLConnection;
  51. import java.net.URL;
  52. import javax.servlet.http.HttpServletRequest;
  53. import org.eclipse.jetty.servlet.ServletContextHandler;
  54. import org.eclipse.jetty.servlet.ServletHolder;
  55. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  56. import org.eclipse.jgit.http.server.GitServlet;
  57. import org.eclipse.jgit.http.server.GitSmartHttpTools;
  58. import org.eclipse.jgit.internal.JGitText;
  59. import org.eclipse.jgit.junit.TestRepository;
  60. import org.eclipse.jgit.junit.http.HttpTestCase;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.lib.StoredConfig;
  65. import org.eclipse.jgit.revwalk.RevBlob;
  66. import org.eclipse.jgit.transport.PacketLineIn;
  67. import org.eclipse.jgit.transport.PacketLineOut;
  68. import org.eclipse.jgit.transport.URIish;
  69. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  70. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  71. import org.eclipse.jgit.util.NB;
  72. import org.junit.Before;
  73. import org.junit.Test;
  74. public class ProtocolErrorTest extends HttpTestCase {
  75. private Repository remoteRepository;
  76. private URIish remoteURI;
  77. private RevBlob a_blob;
  78. @Override
  79. @Before
  80. public void setUp() throws Exception {
  81. super.setUp();
  82. final TestRepository<Repository> src = createTestRepository();
  83. final String srcName = src.getRepository().getDirectory().getName();
  84. ServletContextHandler app = server.addContext("/git");
  85. GitServlet gs = new GitServlet();
  86. gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
  87. @Override
  88. public Repository open(HttpServletRequest req, String name)
  89. throws RepositoryNotFoundException,
  90. ServiceNotEnabledException {
  91. if (!name.equals(srcName))
  92. throw new RepositoryNotFoundException(name);
  93. final Repository db = src.getRepository();
  94. db.incrementOpen();
  95. return db;
  96. }
  97. });
  98. app.addServlet(new ServletHolder(gs), "/*");
  99. server.setUp();
  100. remoteRepository = src.getRepository();
  101. remoteURI = toURIish(app, srcName);
  102. StoredConfig cfg = remoteRepository.getConfig();
  103. cfg.setBoolean("http", null, "receivepack", true);
  104. cfg.save();
  105. a_blob = src.blob("a");
  106. }
  107. @Test
  108. public void testPush_UnpackError_TruncatedPack() throws Exception {
  109. StringBuilder sb = new StringBuilder();
  110. sb.append(ObjectId.zeroId().name());
  111. sb.append(' ');
  112. sb.append(a_blob.name());
  113. sb.append(' ');
  114. sb.append("refs/objects/A");
  115. sb.append('\0');
  116. sb.append("report-status");
  117. ByteArrayOutputStream reqbuf = new ByteArrayOutputStream();
  118. PacketLineOut reqpck = new PacketLineOut(reqbuf);
  119. reqpck.writeString(sb.toString());
  120. reqpck.end();
  121. packHeader(reqbuf, 1);
  122. byte[] reqbin = reqbuf.toByteArray();
  123. URL u = new URL(remoteURI.toString() + "/git-receive-pack");
  124. HttpURLConnection c = (HttpURLConnection) u.openConnection();
  125. try {
  126. c.setRequestMethod("POST");
  127. c.setDoOutput(true);
  128. c.setRequestProperty("Content-Type",
  129. GitSmartHttpTools.RECEIVE_PACK_REQUEST_TYPE);
  130. c.setFixedLengthStreamingMode(reqbin.length);
  131. OutputStream out = c.getOutputStream();
  132. try {
  133. out.write(reqbin);
  134. } finally {
  135. out.close();
  136. }
  137. assertEquals(200, c.getResponseCode());
  138. assertEquals(GitSmartHttpTools.RECEIVE_PACK_RESULT_TYPE,
  139. c.getContentType());
  140. InputStream rawin = c.getInputStream();
  141. try {
  142. PacketLineIn pckin = new PacketLineIn(rawin);
  143. assertEquals("unpack error "
  144. + JGitText.get().packfileIsTruncatedNoParam,
  145. pckin.readString());
  146. assertEquals("ng refs/objects/A n/a (unpacker error)",
  147. pckin.readString());
  148. assertSame(PacketLineIn.END, pckin.readString());
  149. } finally {
  150. rawin.close();
  151. }
  152. } finally {
  153. c.disconnect();
  154. }
  155. }
  156. private static void packHeader(ByteArrayOutputStream tinyPack, int cnt)
  157. throws IOException {
  158. final byte[] hdr = new byte[8];
  159. NB.encodeInt32(hdr, 0, 2);
  160. NB.encodeInt32(hdr, 4, cnt);
  161. tinyPack.write(Constants.PACK_SIGNATURE);
  162. tinyPack.write(hdr, 0, 8);
  163. }
  164. }