PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/bamboo/plugins/git/UriUtilsTest.java

https://bitbucket.org/atlassian/bamboo-git-plugin
Java | 244 lines | 205 code | 39 blank | 0 comment | 0 complexity | 26a9f147db51148ca1f7f5bd7f9d3d19 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.bamboo.plugins.git;
  2. import com.atlassian.bamboo.plugins.ssh.ProxyRegistrationInfoImpl;
  3. import com.google.common.base.Function;
  4. import com.google.common.base.Predicate;
  5. import com.google.common.collect.ImmutableList;
  6. import com.google.common.collect.Iterables;
  7. import org.eclipse.jgit.transport.URIish;
  8. import org.hamcrest.Matcher;
  9. import org.jetbrains.annotations.NotNull;
  10. import org.junit.Assert;
  11. import org.junit.BeforeClass;
  12. import org.junit.Test;
  13. import java.net.URI;
  14. import java.net.URISyntaxException;
  15. import static org.hamcrest.CoreMatchers.equalTo;
  16. import static org.junit.Assert.assertThat;
  17. import static org.junit.Assert.assertTrue;
  18. public class UriUtilsTest
  19. {
  20. static GitRepositoryAccessData proxyAccessData = new GitRepositoryAccessData();
  21. private final static ImmutableList<String> FILE_URLS = ImmutableList.of(
  22. "host", "host/path", "user@host/path", "user@host",
  23. "/host", "/host/path", "/user@host/path", "/user@host",
  24. "host/", "host/path/", "user@host/path/", "user@host/",
  25. "/host/", "/host/path/", "/user@host/path/", "/user@host/",
  26. "file://host", "file://host/path", "file://login@host");
  27. private final static ImmutableList<String> SSH_URLS = ImmutableList.of(
  28. "ssh://user:password@host/path",
  29. "ssh://user:password@host:22/path",
  30. "ssh://host", "ssh://host/path", "ssh://login@host");
  31. private final static ImmutableList<String> GIT_URLS = ImmutableList.copyOf(Iterables.transform(SSH_URLS, new Function<String, String>()
  32. {
  33. @Override
  34. public String apply(String input)
  35. {
  36. return input.replace("ssh://", "git://");
  37. }
  38. }));
  39. private final static ImmutableList<String> SCP_URLS = ImmutableList.of(
  40. "host:", "host:/", "host:22", "host:22/path", "host:/path", "user@host:22/path",
  41. "host:path", "host:path/path", "user@host:path", "user:password@host/path", "user:password@host:22/path"
  42. );
  43. private final static ImmutableList<String> UNKNOWN_URLS = ImmutableList.of(
  44. "/ssh://host", "/ssh://host/path", "/ssh://login@host"
  45. );
  46. @BeforeClass
  47. public static void setup()
  48. {
  49. proxyAccessData.setProxyRegistrationInfo(new ProxyRegistrationInfoImpl("proxyHost", 22, null, "proxyUserName"));
  50. }
  51. @Test
  52. public void uriCreation() throws URISyntaxException
  53. {
  54. String uri = "ssh://[user@]host.xz[:port]/path/to/repo.git/";
  55. createUris(uri, false);
  56. uri = "git://host.xz[:port]/path/to/repo.git/";
  57. createUris(uri, false);
  58. uri = "http[s]://host.xz[:port]/path/to/repo.git/";
  59. createUris(uri, false);
  60. uri = "ftp[s]://host.xz[:port]/path/to/repo.git/";
  61. createUris(uri, false);
  62. uri = "rsync://host.xz/path/to/repo.git/";
  63. createUris(uri, false);
  64. uri = "ssh://[user@]host.xz[:port]/~[user]/path/to/repo.git/";
  65. createUris(uri, false);
  66. uri = "git://host.xz[:port]/~[user]/path/to/repo.git/";
  67. createUris(uri, false);
  68. uri = "/path/to/repo.git/";
  69. createUris(uri, false);
  70. uri = "file:///path/to/repo.git/";
  71. createUris(uri, false);
  72. uri = "[user@]host.xz:path/to/repo.git/";
  73. createUris(uri, true);
  74. uri = "[user@]host.xz:/~[user]/path/to/repo.git/";
  75. createUris(uri, false);
  76. }
  77. private void createUris(String uriStr, boolean isRelative) throws URISyntaxException
  78. {
  79. String minimalUriStr = uriStr.replaceAll("\\[[:\\w@]*\\]", "");
  80. testUri(minimalUriStr, isRelative);
  81. String fullUriStr = uriStr.replaceAll("[\\[\\]]", "");
  82. testUri(fullUriStr, isRelative);
  83. }
  84. private void testUri(String minimalUriStr, boolean isRelative) throws URISyntaxException
  85. {
  86. ScpAwareUri uri = ScpAwareUri.create(minimalUriStr);
  87. Assert.assertTrue(uri.getRawPath() + " should contain path", uri.getRawPath().contains("path"));
  88. Assert.assertEquals(uri.isRelativePath(), isRelative);
  89. URI minimalUriViaProxy = UriUtils.getUriViaProxy(proxyAccessData, uri);
  90. }
  91. @Test
  92. public void recognisesScpLikeUris()
  93. {
  94. assertTrue(none(FILE_URLS, hasScpSyntax()));
  95. assertTrue(none(SSH_URLS, hasScpSyntax()));
  96. assertTrue(Iterables.all(SCP_URLS, hasScpSyntax()));
  97. }
  98. private <T> boolean none(@NotNull Iterable<T> iterable, @NotNull Predicate<? super T> predicate)
  99. {
  100. return !Iterables.any(iterable, predicate);
  101. }
  102. private Predicate<? super String> hasScpSyntax()
  103. {
  104. return new Predicate<String>()
  105. {
  106. @Override
  107. public boolean apply(String url)
  108. {
  109. return UriUtils.hasScpSyntax(url);
  110. }
  111. };
  112. }
  113. @Test
  114. public void recognisesSshTransport()
  115. {
  116. assertTrue(none(FILE_URLS, requiresSshTransport()));
  117. assertTrue(none(UNKNOWN_URLS, requiresSshTransport()));
  118. assertTrue(Iterables.all(SSH_URLS, requiresSshTransport()));
  119. assertTrue(Iterables.all(SCP_URLS, requiresSshTransport()));
  120. assertTrue(none(GIT_URLS, requiresSshTransport()));
  121. }
  122. @Test
  123. public void normalisesUrlProperly() throws URISyntaxException
  124. {
  125. URIish repo = new URIish("http://wrong1:wrong2@host/path");
  126. URIish normalised = UriUtils.normaliseRepositoryLocation("user", "password", repo, true);
  127. assertThatUrl(normalised, equalTo("http://user:password@host/path"));
  128. repo = new URIish("http://wrong1@host/path");
  129. normalised = UriUtils.normaliseRepositoryLocation("user", null, repo, true);
  130. assertThatUrl(normalised, equalTo("http://user:" + UriUtils.FAKE_PASSWORD + "@host/path"));
  131. repo = new URIish("http://host/path");
  132. normalised = UriUtils.normaliseRepositoryLocation("user", null, repo, true);
  133. assertThatUrl(normalised, equalTo("http://user:" + UriUtils.FAKE_PASSWORD + "@host/path"));
  134. repo = new URIish("ssh://okish@host/path");
  135. normalised = UriUtils.normaliseRepositoryLocation(null, "password", repo, true);
  136. assertThatUrl(normalised, equalTo("ssh://okish@host/path"));
  137. repo = new URIish("ssh://host/path");
  138. normalised = UriUtils.normaliseRepositoryLocation(null, "password", repo, true);
  139. assertThatUrl(normalised, equalTo("ssh://host/path"));
  140. }
  141. @Test
  142. public void properlyHandlesSshWithPasssword() throws URISyntaxException
  143. {
  144. final String password = "this_password_would_be_ignored_by_ssh_transport";
  145. URIish repo = new URIish("ssh://wrong1:" + password + "@host/path");
  146. URIish normalised = UriUtils.normaliseRepositoryLocation("user", "password", repo, true);
  147. assertThatUrl(normalised, equalTo("ssh://user@host/path"));
  148. repo = new URIish("ssh://okish:" + password + "@host/path");
  149. normalised = UriUtils.normaliseRepositoryLocation(null, "password", repo, true);
  150. assertThatUrl(normalised, equalTo("ssh://okish@host/path"));
  151. }
  152. @Test
  153. public void properlyNormalisesUserName() throws URISyntaxException
  154. {
  155. URIish repo = new URIish("ssh://okish@host/path");
  156. URIish normalised = UriUtils.normaliseRepositoryLocation(null, "password", repo, true);
  157. assertThatUrl(normalised, equalTo("ssh://okish@host/path"));
  158. repo = new URIish("ssh://okish@host/path");
  159. normalised = UriUtils.normaliseRepositoryLocation(null, null, repo, true);
  160. assertThatUrl(normalised, equalTo("ssh://okish@host/path"));
  161. repo = new URIish("http://okish@host/path");
  162. normalised = UriUtils.normaliseRepositoryLocation(null, null, repo, true);
  163. assertThatUrl(normalised, equalTo("http://okish:" + UriUtils.FAKE_PASSWORD + "@host/path"));
  164. repo = new URIish("http://host/path");
  165. normalised = UriUtils.normaliseRepositoryLocation(null, null, repo, true);
  166. assertThatUrl(normalised, equalTo("http://" + UriUtils.FAKE_USER + ":" + UriUtils.FAKE_PASSWORD + "@host/path"));
  167. }
  168. @Test
  169. public void properlyUrlEncodesUsernameAndPassword() throws URISyntaxException
  170. {
  171. URIish repo = new URIish("ssh://host/path");
  172. URIish normalised = UriUtils.normaliseRepositoryLocation("@", "@", repo, true);
  173. assertThatUrl(normalised, equalTo("ssh://%40@host/path"));
  174. repo = new URIish("http://host/path");
  175. normalised = UriUtils.normaliseRepositoryLocation("@", "@", repo, true);
  176. assertThatUrl(normalised, equalTo("http://%40:%40@host/path"));
  177. }
  178. @Test
  179. public void handlesLocalUrls() throws URISyntaxException
  180. {
  181. final String localRepo1 = "/okish@host/path";
  182. URIish repo = new URIish(localRepo1);
  183. URIish normalised = UriUtils.normaliseRepositoryLocation("user", "password", repo, true);
  184. assertThatUrl(normalised, equalTo(localRepo1));
  185. final String localRepo2 = "file://okish@host/path";
  186. repo = new URIish(localRepo2);
  187. normalised = UriUtils.normaliseRepositoryLocation("user", "password", repo, true);
  188. assertThatUrl(normalised, equalTo(localRepo2));
  189. }
  190. private void assertThatUrl(final URIish normalised, final Matcher<String> matcher)
  191. {
  192. assertThat(normalised.toPrivateString(), matcher);
  193. }
  194. private Predicate<? super String> requiresSshTransport()
  195. {
  196. return new Predicate<String>()
  197. {
  198. @Override
  199. public boolean apply(String url)
  200. {
  201. return UriUtils.requiresSshTransport(ScpAwareUri.create(url));
  202. }
  203. };
  204. }
  205. }