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.

TransportHttpTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2018, Konrad Windszus <konrad_w@gmx.de> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import static org.hamcrest.MatcherAssert.assertThat;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.net.HttpCookie;
  15. import java.time.Instant;
  16. import java.util.Arrays;
  17. import java.util.Collections;
  18. import java.util.Date;
  19. import java.util.LinkedHashMap;
  20. import java.util.LinkedHashSet;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import org.eclipse.jgit.internal.transport.http.NetscapeCookieFile;
  24. import org.eclipse.jgit.lib.Config;
  25. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  26. import org.eclipse.jgit.transport.http.HttpConnection;
  27. import org.eclipse.jgit.util.http.HttpCookiesMatcher;
  28. import org.junit.Assert;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. import org.mockito.ArgumentMatchers;
  32. import org.mockito.Mockito;
  33. public class TransportHttpTest extends SampleDataRepositoryTestCase {
  34. private URIish uri;
  35. private File cookieFile;
  36. @Override
  37. @Before
  38. public void setUp() throws Exception {
  39. super.setUp();
  40. uri = new URIish("https://everyones.loves.git/u/2");
  41. final Config config = db.getConfig();
  42. config.setBoolean("http", null, "saveCookies", true);
  43. cookieFile = createTempFile();
  44. config.setString("http", null, "cookieFile",
  45. cookieFile.getAbsolutePath());
  46. }
  47. @Test
  48. public void testMatchesCookieDomain() {
  49. Assert.assertTrue(TransportHttp.matchesCookieDomain("example.com",
  50. "example.com"));
  51. Assert.assertTrue(TransportHttp.matchesCookieDomain("Example.Com",
  52. "example.cOM"));
  53. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  54. "some.subdomain.example.com", "example.com"));
  55. Assert.assertFalse(TransportHttp
  56. .matchesCookieDomain("someotherexample.com", "example.com"));
  57. Assert.assertFalse(TransportHttp.matchesCookieDomain("example.com",
  58. "example1.com"));
  59. Assert.assertFalse(TransportHttp
  60. .matchesCookieDomain("sub.sub.example.com", ".example.com"));
  61. Assert.assertTrue(TransportHttp.matchesCookieDomain("host.example.com",
  62. "example.com"));
  63. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  64. "something.example.com", "something.example.com"));
  65. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  66. "host.something.example.com", "something.example.com"));
  67. }
  68. @Test
  69. public void testMatchesCookiePath() {
  70. Assert.assertTrue(
  71. TransportHttp.matchesCookiePath("/some/path", "/some/path"));
  72. Assert.assertTrue(TransportHttp.matchesCookiePath("/some/path/child",
  73. "/some/path"));
  74. Assert.assertTrue(TransportHttp.matchesCookiePath("/some/path/child",
  75. "/some/path/"));
  76. Assert.assertFalse(TransportHttp.matchesCookiePath("/some/pathother",
  77. "/some/path"));
  78. Assert.assertFalse(
  79. TransportHttp.matchesCookiePath("otherpath", "/some/path"));
  80. }
  81. @Test
  82. public void testProcessResponseCookies() throws IOException {
  83. HttpConnection connection = Mockito.mock(HttpConnection.class);
  84. Mockito.when(
  85. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie")))
  86. .thenReturn(Arrays.asList(
  87. "id=a3fWa; Expires=Fri, 01 Jan 2100 11:00:00 GMT; Secure; HttpOnly",
  88. "sessionid=38afes7a8; HttpOnly; Path=/"));
  89. Mockito.when(
  90. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie2")))
  91. .thenReturn(Collections
  92. .singletonList("cookie2=some value; Max-Age=1234; Path=/"));
  93. try (TransportHttp transportHttp = new TransportHttp(db, uri)) {
  94. Date creationDate = new Date();
  95. transportHttp.processResponseCookies(connection);
  96. // evaluate written cookie file
  97. Set<HttpCookie> expectedCookies = new LinkedHashSet<>();
  98. HttpCookie cookie = new HttpCookie("id", "a3fWa");
  99. cookie.setDomain("everyones.loves.git");
  100. cookie.setPath("/u/2/");
  101. cookie.setMaxAge(
  102. (Instant.parse("2100-01-01T11:00:00.000Z").toEpochMilli()
  103. - creationDate.getTime()) / 1000);
  104. cookie.setSecure(true);
  105. cookie.setHttpOnly(true);
  106. expectedCookies.add(cookie);
  107. cookie = new HttpCookie("cookie2", "some value");
  108. cookie.setDomain("everyones.loves.git");
  109. cookie.setPath("/");
  110. cookie.setMaxAge(1234);
  111. expectedCookies.add(cookie);
  112. assertThat(
  113. new NetscapeCookieFile(cookieFile.toPath())
  114. .getCookies(true),
  115. HttpCookiesMatcher.containsInOrder(expectedCookies, 5));
  116. }
  117. }
  118. @Test
  119. public void testProcessResponseCookiesNotPersistingWithSaveCookiesFalse()
  120. throws IOException {
  121. HttpConnection connection = Mockito.mock(HttpConnection.class);
  122. Mockito.when(
  123. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie")))
  124. .thenReturn(Arrays.asList(
  125. "id=a3fWa; Expires=Thu, 21 Oct 2100 11:00:00 GMT; Secure; HttpOnly",
  126. "sessionid=38afes7a8; HttpOnly; Path=/"));
  127. Mockito.when(
  128. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie2")))
  129. .thenReturn(Collections.singletonList(
  130. "cookie2=some value; Max-Age=1234; Path=/"));
  131. // tweak config
  132. final Config config = db.getConfig();
  133. config.setBoolean("http", null, "saveCookies", false);
  134. try (TransportHttp transportHttp = new TransportHttp(db, uri)) {
  135. transportHttp.processResponseCookies(connection);
  136. // evaluate written cookie file
  137. Assert.assertFalse("Cookie file was not supposed to be written!",
  138. cookieFile.exists());
  139. }
  140. }
  141. private void assertHeaders(String expected, String... headersToAdd) {
  142. HttpConnection fake = Mockito.mock(HttpConnection.class);
  143. Map<String, String> headers = new LinkedHashMap<>();
  144. Mockito.doAnswer(invocation -> {
  145. Object[] args = invocation.getArguments();
  146. headers.put(args[0].toString(), args[1].toString());
  147. return null;
  148. }).when(fake).setRequestProperty(ArgumentMatchers.anyString(),
  149. ArgumentMatchers.anyString());
  150. TransportHttp.addHeaders(fake, Arrays.asList(headersToAdd));
  151. Assert.assertEquals(expected, headers.toString());
  152. }
  153. @Test
  154. public void testAddHeaders() {
  155. assertHeaders("{a=b, c=d}", "a: b", "c :d");
  156. }
  157. @Test
  158. public void testAddHeaderEmptyValue() {
  159. assertHeaders("{a-x=b, c=, d=e}", "a-x: b", "c:", "d:e");
  160. }
  161. @Test
  162. public void testSkipHeaderWithEmptyKey() {
  163. assertHeaders("{a=b, c=d}", "a: b", " : x", "c :d");
  164. assertHeaders("{a=b, c=d}", "a: b", ": x", "c :d");
  165. }
  166. @Test
  167. public void testSkipHeaderWithoutKey() {
  168. assertHeaders("{a=b, c=d}", "a: b", "x", "c :d");
  169. }
  170. @Test
  171. public void testSkipHeaderWithInvalidKey() {
  172. assertHeaders("{a=b, c=d}", "a: b", "q/p: x", "c :d");
  173. assertHeaders("{a=b, c=d}", "a: b", "ä: x", "c :d");
  174. }
  175. @Test
  176. public void testSkipHeaderWithNonAsciiValue() {
  177. assertHeaders("{a=b, c=d}", "a: b", "q/p: x", "c :d");
  178. assertHeaders("{a=b, c=d}", "a: b", "x: ä", "c :d");
  179. }
  180. }