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
028public class KnownHostsProviderTestCase
029    extends PlexusTestCase
030{
031    protected KnownHostsProvider okHostsProvider;
032
033    protected KnownHostsProvider failHostsProvider;
034
035    protected KnownHostsProvider changedHostsProvider;
036
037    private SshWagon wagon;
038
039    private Repository source;
040
041    private static final String CORRECT_KEY = TestData.getHostKey();
042
043    private static final String CHANGED_KEY =
044        "AAAAB3NzaC1yc2EAAAABIwAAAQEA8VLKkfHl2CNqW+m0603z07dyweWzzdVGQlMPUX4z1264E7M/h+6lPKiOo+u49CL7eQVA+FtW"
045        + "TZoJ3oBAMABcKnHx41TnSpQUkbdR6rzyC6IG1lXiVtEjG2w7DUnxpCtVo5PaQuJobwoXv5NNL3vx03THPgcDJquLPWvGnDWhnXoEh"
046        + "3/6c7rprwT+PrjZ6LIT35ZCUGajoehhF151oNbFMQHllfR6EAiZIP0z0nIVI+Jiv6g+XZapumVPVYjdOfxvLKQope1H9HJamT3bDI"
047        + "m8mkebUB10DzQJYxFt4/0wiNH3L4jsIFn+CiW1/IQm5yyff1CUO87OqVbtp9BlaXZNmw==";
048
049    /**
050     * tests what happens if the remote host has a different key than the one
051     * we expect
052     *
053     * @throws Exception on error
054     */
055    public void testIncorrectKey()
056        throws Exception
057    {
058        wagon.setKnownHostsProvider( failHostsProvider );
059
060        try
061        {
062            wagon.connect( source );
063
064            fail( "Should not have successfully connected - host is not known" );
065        }
066        catch ( UnknownHostException e )
067        {
068            // ok
069        }
070    }
071
072    /**
073     * tests what happens if the remote host has changed since being recorded.
074     *
075     * @throws Exception on error
076     */
077    public void testChangedKey()
078        throws Exception
079    {
080        wagon.setKnownHostsProvider( changedHostsProvider );
081
082        try
083        {
084            wagon.connect( source );
085
086            fail( "Should not have successfully connected - host is changed" );
087        }
088        catch ( KnownHostChangedException e )
089        {
090            // ok
091        }
092    }
093
094    /**
095     * tests what happens if the remote host has the expected key
096     *
097     * @throws Exception on error
098     */
099    public void testCorrectKey()
100        throws Exception
101    {
102        wagon.setKnownHostsProvider( okHostsProvider );
103
104        wagon.connect( source );
105
106        assertTrue( true );
107    }
108
109    protected void setUp()
110        throws Exception
111    {
112        super.setUp();
113        source =
114            new Repository( "test", "scp://" + TestData.getUserName() + "@" + TestData.getHostname() + "/tmp/foo" );
115
116        wagon = (SshWagon) lookup( Wagon.ROLE, "scp" );
117        wagon.setInteractive( false );
118
119        this.okHostsProvider = new SingleKnownHostProvider( TestData.getHostname(), CORRECT_KEY );
120        this.failHostsProvider = new SingleKnownHostProvider( "beaver.codehaus.org", CORRECT_KEY );
121        this.changedHostsProvider = new SingleKnownHostProvider( TestData.getHostname(), CHANGED_KEY );
122    }
123}