Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TransportHttpTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2018, Konrad Windszus <konrad_w@gmx.de>
  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 java.io.File;
  45. import java.io.IOException;
  46. import java.net.HttpCookie;
  47. import java.time.Instant;
  48. import java.util.Arrays;
  49. import java.util.Collections;
  50. import java.util.Date;
  51. import java.util.LinkedHashSet;
  52. import java.util.Set;
  53. import org.eclipse.jgit.internal.transport.http.NetscapeCookieFile;
  54. import org.eclipse.jgit.lib.Config;
  55. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  56. import org.eclipse.jgit.transport.http.HttpConnection;
  57. import org.eclipse.jgit.util.http.HttpCookiesMatcher;
  58. import org.junit.Assert;
  59. import org.junit.Before;
  60. import org.junit.Test;
  61. import org.mockito.ArgumentMatchers;
  62. import org.mockito.Mockito;
  63. public class TransportHttpTest extends SampleDataRepositoryTestCase {
  64. private URIish uri;
  65. private File cookieFile;
  66. @Override
  67. @Before
  68. public void setUp() throws Exception {
  69. super.setUp();
  70. uri = new URIish("https://everyones.loves.git/u/2");
  71. final Config config = db.getConfig();
  72. config.setBoolean("http", null, "saveCookies", true);
  73. cookieFile = createTempFile();
  74. config.setString("http", null, "cookieFile",
  75. cookieFile.getAbsolutePath());
  76. }
  77. @Test
  78. public void testMatchesCookieDomain() {
  79. Assert.assertTrue(TransportHttp.matchesCookieDomain("example.com",
  80. "example.com"));
  81. Assert.assertTrue(TransportHttp.matchesCookieDomain("Example.Com",
  82. "example.cOM"));
  83. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  84. "some.subdomain.example.com", "example.com"));
  85. Assert.assertFalse(TransportHttp
  86. .matchesCookieDomain("someotherexample.com", "example.com"));
  87. Assert.assertFalse(TransportHttp.matchesCookieDomain("example.com",
  88. "example1.com"));
  89. Assert.assertFalse(TransportHttp
  90. .matchesCookieDomain("sub.sub.example.com", ".example.com"));
  91. Assert.assertTrue(TransportHttp.matchesCookieDomain("host.example.com",
  92. "example.com"));
  93. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  94. "something.example.com", "something.example.com"));
  95. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  96. "host.something.example.com", "something.example.com"));
  97. }
  98. @Test
  99. public void testMatchesCookiePath() {
  100. Assert.assertTrue(
  101. TransportHttp.matchesCookiePath("/some/path", "/some/path"));
  102. Assert.assertTrue(TransportHttp.matchesCookiePath("/some/path/child",
  103. "/some/path"));
  104. Assert.assertTrue(TransportHttp.matchesCookiePath("/some/path/child",
  105. "/some/path/"));
  106. Assert.assertFalse(TransportHttp.matchesCookiePath("/some/pathother",
  107. "/some/path"));
  108. Assert.assertFalse(
  109. TransportHttp.matchesCookiePath("otherpath", "/some/path"));
  110. }
  111. @Test
  112. public void testProcessResponseCookies() throws IOException {
  113. HttpConnection connection = Mockito.mock(HttpConnection.class);
  114. Mockito.when(
  115. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie")))
  116. .thenReturn(Arrays.asList(
  117. "id=a3fWa; Expires=Fri, 01 Jan 2100 11:00:00 GMT; Secure; HttpOnly",
  118. "sessionid=38afes7a8; HttpOnly; Path=/"));
  119. Mockito.when(
  120. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie2")))
  121. .thenReturn(Collections
  122. .singletonList("cookie2=some value; Max-Age=1234; Path=/"));
  123. try (TransportHttp transportHttp = new TransportHttp(db, uri)) {
  124. Date creationDate = new Date();
  125. transportHttp.processResponseCookies(connection);
  126. // evaluate written cookie file
  127. Set<HttpCookie> expectedCookies = new LinkedHashSet<>();
  128. HttpCookie cookie = new HttpCookie("id", "a3fWa");
  129. cookie.setDomain("everyones.loves.git");
  130. cookie.setPath("/u/2/");
  131. cookie.setMaxAge(
  132. (Instant.parse("2100-01-01T11:00:00.000Z").toEpochMilli()
  133. - creationDate.getTime()) / 1000);
  134. cookie.setSecure(true);
  135. cookie.setHttpOnly(true);
  136. expectedCookies.add(cookie);
  137. cookie = new HttpCookie("cookie2", "some value");
  138. cookie.setDomain("everyones.loves.git");
  139. cookie.setPath("/");
  140. cookie.setMaxAge(1234);
  141. expectedCookies.add(cookie);
  142. Assert.assertThat(
  143. new NetscapeCookieFile(cookieFile.toPath())
  144. .getCookies(true),
  145. HttpCookiesMatcher.containsInOrder(expectedCookies, 5));
  146. }
  147. }
  148. @Test
  149. public void testProcessResponseCookiesNotPersistingWithSaveCookiesFalse()
  150. throws IOException {
  151. HttpConnection connection = Mockito.mock(HttpConnection.class);
  152. Mockito.when(
  153. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie")))
  154. .thenReturn(Arrays.asList(
  155. "id=a3fWa; Expires=Thu, 21 Oct 2100 11:00:00 GMT; Secure; HttpOnly",
  156. "sessionid=38afes7a8; HttpOnly; Path=/"));
  157. Mockito.when(
  158. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie2")))
  159. .thenReturn(Collections.singletonList(
  160. "cookie2=some value; Max-Age=1234; Path=/"));
  161. // tweak config
  162. final Config config = db.getConfig();
  163. config.setBoolean("http", null, "saveCookies", false);
  164. try (TransportHttp transportHttp = new TransportHttp(db, uri)) {
  165. transportHttp.processResponseCookies(connection);
  166. // evaluate written cookie file
  167. Assert.assertFalse("Cookie file was not supposed to be written!",
  168. cookieFile.exists());
  169. }
  170. }
  171. }