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.

ApacheSshTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> 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.sshd;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.UncheckedIOException;
  17. import java.nio.file.Files;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.stream.Collectors;
  21. import org.apache.sshd.client.config.hosts.KnownHostEntry;
  22. import org.eclipse.jgit.api.errors.TransportException;
  23. import org.eclipse.jgit.junit.ssh.SshTestBase;
  24. import org.eclipse.jgit.lib.Constants;
  25. import org.eclipse.jgit.transport.SshSessionFactory;
  26. import org.eclipse.jgit.util.FS;
  27. import org.junit.Test;
  28. import org.junit.experimental.theories.Theories;
  29. import org.junit.runner.RunWith;
  30. @RunWith(Theories.class)
  31. public class ApacheSshTest extends SshTestBase {
  32. @Override
  33. protected SshSessionFactory createSessionFactory() {
  34. SshdSessionFactory result = new SshdSessionFactory(new JGitKeyCache(),
  35. null);
  36. // The home directory is mocked at this point!
  37. result.setHomeDirectory(FS.DETECTED.userHome());
  38. result.setSshDirectory(sshDir);
  39. return result;
  40. }
  41. @Override
  42. protected void installConfig(String... config) {
  43. File configFile = new File(sshDir, Constants.CONFIG);
  44. if (config != null) {
  45. try {
  46. Files.write(configFile.toPath(), Arrays.asList(config));
  47. } catch (IOException e) {
  48. throw new UncheckedIOException(e);
  49. }
  50. }
  51. }
  52. @Test
  53. public void testEd25519HostKey() throws Exception {
  54. // Using ed25519 user identities is tested in the super class in
  55. // testSshKeys().
  56. File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
  57. copyTestResource("id_ed25519", newHostKey);
  58. server.addHostKey(newHostKey.toPath(), true);
  59. File newHostKeyPub = new File(getTemporaryDirectory(),
  60. "newhostkey.pub");
  61. copyTestResource("id_ed25519.pub", newHostKeyPub);
  62. createKnownHostsFile(knownHosts, "localhost", testPort, newHostKeyPub);
  63. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  64. "Host git", //
  65. "HostName localhost", //
  66. "Port " + testPort, //
  67. "User " + TEST_USER, //
  68. "IdentityFile " + privateKey1.getAbsolutePath());
  69. }
  70. @Test
  71. public void testHashedKnownHosts() throws Exception {
  72. assertTrue("Failed to delete known_hosts", knownHosts.delete());
  73. // The provider will answer "yes" to all questions, so we should be able
  74. // to connect and end up with a new known_hosts file with the host key.
  75. TestCredentialsProvider provider = new TestCredentialsProvider();
  76. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  77. "HashKnownHosts yes", //
  78. "Host localhost", //
  79. "HostName localhost", //
  80. "Port " + testPort, //
  81. "User " + TEST_USER, //
  82. "IdentityFile " + privateKey1.getAbsolutePath());
  83. List<LogEntry> messages = provider.getLog();
  84. assertFalse("Expected user interaction", messages.isEmpty());
  85. assertEquals(
  86. "Expected to be asked about the key, and the file creation", 2,
  87. messages.size());
  88. assertTrue("~/.ssh/known_hosts should exist now", knownHosts.exists());
  89. // Let's clone again without provider. If it works, the server host key
  90. // was written correctly.
  91. File clonedAgain = new File(getTemporaryDirectory(), "cloned2");
  92. cloneWith("ssh://localhost/doesntmatter", clonedAgain, null, //
  93. "Host localhost", //
  94. "HostName localhost", //
  95. "Port " + testPort, //
  96. "User " + TEST_USER, //
  97. "IdentityFile " + privateKey1.getAbsolutePath());
  98. // Check that the first line contains neither "localhost" nor
  99. // "127.0.0.1", but does contain the expected hash.
  100. List<String> lines = Files.readAllLines(knownHosts.toPath()).stream()
  101. .filter(s -> s != null && s.length() >= 1 && s.charAt(0) != '#'
  102. && !s.trim().isEmpty())
  103. .collect(Collectors.toList());
  104. assertEquals("Unexpected number of known_hosts lines", 1, lines.size());
  105. String line = lines.get(0);
  106. assertFalse("Found host in line", line.contains("localhost"));
  107. assertFalse("Found IP in line", line.contains("127.0.0.1"));
  108. assertTrue("Hash not found", line.contains("|"));
  109. KnownHostEntry entry = KnownHostEntry.parseKnownHostEntry(line);
  110. assertTrue("Hash doesn't match localhost",
  111. entry.isHostMatch("localhost", testPort)
  112. || entry.isHostMatch("127.0.0.1", testPort));
  113. }
  114. @Test
  115. public void testPreamble() throws Exception {
  116. // Test that the client can deal with strange lines being sent before
  117. // the server identification string.
  118. StringBuilder b = new StringBuilder();
  119. for (int i = 0; i < 257; i++) {
  120. b.append('a');
  121. }
  122. server.setPreamble("A line with a \000 NUL",
  123. "A long line: " + b.toString());
  124. cloneWith(
  125. "ssh://" + TEST_USER + "@localhost:" + testPort
  126. + "/doesntmatter",
  127. defaultCloneDir, null,
  128. "IdentityFile " + privateKey1.getAbsolutePath());
  129. }
  130. @Test
  131. public void testLongPreamble() throws Exception {
  132. // Test that the client can deal with a long (about 60k) preamble.
  133. StringBuilder b = new StringBuilder();
  134. for (int i = 0; i < 1024; i++) {
  135. b.append('a');
  136. }
  137. String line = b.toString();
  138. String[] lines = new String[60];
  139. for (int i = 0; i < lines.length; i++) {
  140. lines[i] = line;
  141. }
  142. server.setPreamble(lines);
  143. cloneWith(
  144. "ssh://" + TEST_USER + "@localhost:" + testPort
  145. + "/doesntmatter",
  146. defaultCloneDir, null,
  147. "IdentityFile " + privateKey1.getAbsolutePath());
  148. }
  149. @Test (expected = TransportException.class)
  150. public void testHugePreamble() throws Exception {
  151. // Test that the connection fails when the preamble is longer than 64k.
  152. StringBuilder b = new StringBuilder();
  153. for (int i = 0; i < 1024; i++) {
  154. b.append('a');
  155. }
  156. String line = b.toString();
  157. String[] lines = new String[70];
  158. for (int i = 0; i < lines.length; i++) {
  159. lines[i] = line;
  160. }
  161. server.setPreamble(lines);
  162. cloneWith(
  163. "ssh://" + TEST_USER + "@localhost:" + testPort
  164. + "/doesntmatter",
  165. defaultCloneDir, null,
  166. "IdentityFile " + privateKey1.getAbsolutePath());
  167. }
  168. }