001package org.apache.maven.wagon.providers.ssh.knownhost; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.wagon.Wagon; 023import org.apache.maven.wagon.providers.ssh.SshWagon; 024import org.apache.maven.wagon.providers.ssh.TestData; 025import org.apache.maven.wagon.repository.Repository; 026import org.codehaus.plexus.PlexusTestCase; 027 028/** 029 * 030 */ 031public class KnownHostsProviderTestCase 032 extends PlexusTestCase 033{ 034 protected KnownHostsProvider okHostsProvider; 035 036 protected KnownHostsProvider failHostsProvider; 037 038 protected KnownHostsProvider changedHostsProvider; 039 040 private SshWagon wagon; 041 042 private Repository source; 043 044 private static final String CORRECT_KEY = TestData.getHostKey(); 045 046 private static final String CHANGED_KEY = 047 "AAAAB3NzaC1yc2EAAAABIwAAAQEA8VLKkfHl2CNqW+m0603z07dyweWzzdVGQlMPUX4z1264E7M/h+6lPKiOo+u49CL7eQVA+FtW" 048 + "TZoJ3oBAMABcKnHx41TnSpQUkbdR6rzyC6IG1lXiVtEjG2w7DUnxpCtVo5PaQuJobwoXv5NNL3vx03THPgcDJquLPWvGnDWhnXoEh" 049 + "3/6c7rprwT+PrjZ6LIT35ZCUGajoehhF151oNbFMQHllfR6EAiZIP0z0nIVI+Jiv6g+XZapumVPVYjdOfxvLKQope1H9HJamT3bDI" 050 + "m8mkebUB10DzQJYxFt4/0wiNH3L4jsIFn+CiW1/IQm5yyff1CUO87OqVbtp9BlaXZNmw=="; 051 052 /** 053 * tests what happens if the remote host has a different key than the one 054 * we expect 055 * 056 * @throws Exception on error 057 */ 058 public void testIncorrectKey() 059 throws Exception 060 { 061 wagon.setKnownHostsProvider( failHostsProvider ); 062 063 try 064 { 065 wagon.connect( source ); 066 067 fail( "Should not have successfully connected - host is not known" ); 068 } 069 catch ( UnknownHostException e ) 070 { 071 // ok 072 } 073 } 074 075 /** 076 * tests what happens if the remote host has changed since being recorded. 077 * 078 * @throws Exception on error 079 */ 080 public void testChangedKey() 081 throws Exception 082 { 083 wagon.setKnownHostsProvider( changedHostsProvider ); 084 085 try 086 { 087 wagon.connect( source ); 088 089 fail( "Should not have successfully connected - host is changed" ); 090 } 091 catch ( KnownHostChangedException e ) 092 { 093 // ok 094 } 095 } 096 097 /** 098 * tests what happens if the remote host has the expected key 099 * 100 * @throws Exception on error 101 */ 102 public void testCorrectKey() 103 throws Exception 104 { 105 wagon.setKnownHostsProvider( okHostsProvider ); 106 107 wagon.connect( source ); 108 109 assertTrue( true ); 110 } 111 112 protected void setUp() 113 throws Exception 114 { 115 super.setUp(); 116 source = 117 new Repository( "test", "scp://" + TestData.getUserName() + "@" + TestData.getHostname() + "/tmp/foo" ); 118 119 wagon = (SshWagon) lookup( Wagon.ROLE, "scp" ); 120 wagon.setInteractive( false ); 121 122 this.okHostsProvider = new SingleKnownHostProvider( TestData.getHostname(), CORRECT_KEY ); 123 this.failHostsProvider = new SingleKnownHostProvider( "beaver.codehaus.org", CORRECT_KEY ); 124 this.changedHostsProvider = new SingleKnownHostProvider( TestData.getHostname(), CHANGED_KEY ); 125 } 126}