### Question:
PropertyBasedSshSessionFactory extends JschConfigSessionFactory { @Override protected Session createSession(Host hc, String user, String host, int port, FS fs) throws JSchException { if (this.sshKeysByHostname.containsKey(host)) { JGitEnvironmentProperties sshUriProperties = this.sshKeysByHostname.get(host); this.jSch.addIdentity(host, sshUriProperties.getPrivateKey().getBytes(), null, null); if (sshUriProperties.getKnownHostsFile() != null) { this.jSch.setKnownHosts(sshUriProperties.getKnownHostsFile()); } if (sshUriProperties.getHostKey() != null) { HostKey hostkey = new HostKey(host, Base64.decode(sshUriProperties.getHostKey())); this.jSch.getHostKeyRepository().add(hostkey, null); } return this.jSch.getSession(user, host, port); } throw new JSchException("no keys configured for hostname " + host); } PropertyBasedSshSessionFactory(Map<String, JGitEnvironmentProperties> sshKeysByHostname, JSch jSch);   }### Answer:
@Test public void privateKeyIsUsed() throws Exception { JGitEnvironmentProperties sshKey = new JGitEnvironmentProperties(); sshKey.setUri("git@gitlab.example.local:someorg/somerepo.git"); sshKey.setPrivateKey(PRIVATE_KEY); setupSessionFactory(sshKey); this.factory.createSession(this.hc, null, SshUriPropertyProcessor.getHostname(sshKey.getUri()), 22, null); verify(this.jSch).addIdentity("gitlab.example.local", PRIVATE_KEY.getBytes(), null, null); }
@Test public void hostKeyIsUsed() throws Exception { JGitEnvironmentProperties sshKey = new JGitEnvironmentProperties(); sshKey.setUri("git@gitlab.example.local:someorg/somerepo.git"); sshKey.setHostKey(HOST_KEY); sshKey.setPrivateKey(PRIVATE_KEY); setupSessionFactory(sshKey); this.factory.createSession(this.hc, null, SshUriPropertyProcessor.getHostname(sshKey.getUri()), 22, null); ArgumentCaptor<HostKey> captor = ArgumentCaptor.forClass(HostKey.class); verify(this.hostKeyRepository).add(captor.capture(), isNull()); HostKey hostKey = captor.getValue(); assertThat(hostKey.getHost()).isEqualTo("gitlab.example.local"); assertThat(hostKey.getKey()).isEqualTo(HOST_KEY); }
@Test public void customKnownHostsFileIsUsed() throws Exception { JGitEnvironmentProperties sshKey = new JGitEnvironmentProperties(); sshKey.setUri("git@gitlab.example.local:someorg/somerepo.git"); sshKey.setPrivateKey(PRIVATE_KEY); sshKey.setKnownHostsFile("/ssh/known_hosts"); setupSessionFactory(sshKey); this.factory.createSession(this.hc, null, SshUriPropertyProcessor.getHostname(sshKey.getUri()), 22, null); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(this.jSch).setKnownHosts(captor.capture()); assertThat(captor.getValue()).isEqualTo("/ssh/known_hosts"); }