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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.LinkedHashSet;
  20. import java.util.Set;
  21. import org.eclipse.jgit.internal.transport.http.NetscapeCookieFile;
  22. import org.eclipse.jgit.lib.Config;
  23. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  24. import org.eclipse.jgit.transport.http.HttpConnection;
  25. import org.eclipse.jgit.util.http.HttpCookiesMatcher;
  26. import org.junit.Assert;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.mockito.ArgumentMatchers;
  30. import org.mockito.Mockito;
  31. public class TransportHttpTest extends SampleDataRepositoryTestCase {
  32. private URIish uri;
  33. private File cookieFile;
  34. @Override
  35. @Before
  36. public void setUp() throws Exception {
  37. super.setUp();
  38. uri = new URIish("https://everyones.loves.git/u/2");
  39. final Config config = db.getConfig();
  40. config.setBoolean("http", null, "saveCookies", true);
  41. cookieFile = createTempFile();
  42. config.setString("http", null, "cookieFile",
  43. cookieFile.getAbsolutePath());
  44. }
  45. @Test
  46. public void testMatchesCookieDomain() {
  47. Assert.assertTrue(TransportHttp.matchesCookieDomain("example.com",
  48. "example.com"));
  49. Assert.assertTrue(TransportHttp.matchesCookieDomain("Example.Com",
  50. "example.cOM"));
  51. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  52. "some.subdomain.example.com", "example.com"));
  53. Assert.assertFalse(TransportHttp
  54. .matchesCookieDomain("someotherexample.com", "example.com"));
  55. Assert.assertFalse(TransportHttp.matchesCookieDomain("example.com",
  56. "example1.com"));
  57. Assert.assertFalse(TransportHttp
  58. .matchesCookieDomain("sub.sub.example.com", ".example.com"));
  59. Assert.assertTrue(TransportHttp.matchesCookieDomain("host.example.com",
  60. "example.com"));
  61. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  62. "something.example.com", "something.example.com"));
  63. Assert.assertTrue(TransportHttp.matchesCookieDomain(
  64. "host.something.example.com", "something.example.com"));
  65. }
  66. @Test
  67. public void testMatchesCookiePath() {
  68. Assert.assertTrue(
  69. TransportHttp.matchesCookiePath("/some/path", "/some/path"));
  70. Assert.assertTrue(TransportHttp.matchesCookiePath("/some/path/child",
  71. "/some/path"));
  72. Assert.assertTrue(TransportHttp.matchesCookiePath("/some/path/child",
  73. "/some/path/"));
  74. Assert.assertFalse(TransportHttp.matchesCookiePath("/some/pathother",
  75. "/some/path"));
  76. Assert.assertFalse(
  77. TransportHttp.matchesCookiePath("otherpath", "/some/path"));
  78. }
  79. @Test
  80. public void testProcessResponseCookies() throws IOException {
  81. HttpConnection connection = Mockito.mock(HttpConnection.class);
  82. Mockito.when(
  83. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie")))
  84. .thenReturn(Arrays.asList(
  85. "id=a3fWa; Expires=Fri, 01 Jan 2100 11:00:00 GMT; Secure; HttpOnly",
  86. "sessionid=38afes7a8; HttpOnly; Path=/"));
  87. Mockito.when(
  88. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie2")))
  89. .thenReturn(Collections
  90. .singletonList("cookie2=some value; Max-Age=1234; Path=/"));
  91. try (TransportHttp transportHttp = new TransportHttp(db, uri)) {
  92. Date creationDate = new Date();
  93. transportHttp.processResponseCookies(connection);
  94. // evaluate written cookie file
  95. Set<HttpCookie> expectedCookies = new LinkedHashSet<>();
  96. HttpCookie cookie = new HttpCookie("id", "a3fWa");
  97. cookie.setDomain("everyones.loves.git");
  98. cookie.setPath("/u/2/");
  99. cookie.setMaxAge(
  100. (Instant.parse("2100-01-01T11:00:00.000Z").toEpochMilli()
  101. - creationDate.getTime()) / 1000);
  102. cookie.setSecure(true);
  103. cookie.setHttpOnly(true);
  104. expectedCookies.add(cookie);
  105. cookie = new HttpCookie("cookie2", "some value");
  106. cookie.setDomain("everyones.loves.git");
  107. cookie.setPath("/");
  108. cookie.setMaxAge(1234);
  109. expectedCookies.add(cookie);
  110. assertThat(
  111. new NetscapeCookieFile(cookieFile.toPath())
  112. .getCookies(true),
  113. HttpCookiesMatcher.containsInOrder(expectedCookies, 5));
  114. }
  115. }
  116. @Test
  117. public void testProcessResponseCookiesNotPersistingWithSaveCookiesFalse()
  118. throws IOException {
  119. HttpConnection connection = Mockito.mock(HttpConnection.class);
  120. Mockito.when(
  121. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie")))
  122. .thenReturn(Arrays.asList(
  123. "id=a3fWa; Expires=Thu, 21 Oct 2100 11:00:00 GMT; Secure; HttpOnly",
  124. "sessionid=38afes7a8; HttpOnly; Path=/"));
  125. Mockito.when(
  126. connection.getHeaderFields(ArgumentMatchers.eq("Set-Cookie2")))
  127. .thenReturn(Collections.singletonList(
  128. "cookie2=some value; Max-Age=1234; Path=/"));
  129. // tweak config
  130. final Config config = db.getConfig();
  131. config.setBoolean("http", null, "saveCookies", false);
  132. try (TransportHttp transportHttp = new TransportHttp(db, uri)) {
  133. transportHttp.processResponseCookies(connection);
  134. // evaluate written cookie file
  135. Assert.assertFalse("Cookie file was not supposed to be written!",
  136. cookieFile.exists());
  137. }
  138. }
  139. }