View Javadoc
1   package org.apache.maven.wagon.providers.ssh.knownhost;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.wagon.Wagon;
23  import org.apache.maven.wagon.providers.ssh.SshWagon;
24  import org.apache.maven.wagon.providers.ssh.TestData;
25  import org.apache.maven.wagon.repository.Repository;
26  import org.codehaus.plexus.PlexusTestCase;
27  
28  /**
29   * 
30   */
31  public class KnownHostsProviderTestCase
32      extends PlexusTestCase
33  {
34      protected KnownHostsProvider okHostsProvider;
35  
36      protected KnownHostsProvider failHostsProvider;
37  
38      protected KnownHostsProvider changedHostsProvider;
39  
40      private SshWagon wagon;
41  
42      private Repository source;
43  
44      private static final String CORRECT_KEY = TestData.getHostKey();
45  
46      private static final String CHANGED_KEY =
47          "AAAAB3NzaC1yc2EAAAABIwAAAQEA8VLKkfHl2CNqW+m0603z07dyweWzzdVGQlMPUX4z1264E7M/h+6lPKiOo+u49CL7eQVA+FtW"
48          + "TZoJ3oBAMABcKnHx41TnSpQUkbdR6rzyC6IG1lXiVtEjG2w7DUnxpCtVo5PaQuJobwoXv5NNL3vx03THPgcDJquLPWvGnDWhnXoEh"
49          + "3/6c7rprwT+PrjZ6LIT35ZCUGajoehhF151oNbFMQHllfR6EAiZIP0z0nIVI+Jiv6g+XZapumVPVYjdOfxvLKQope1H9HJamT3bDI"
50          + "m8mkebUB10DzQJYxFt4/0wiNH3L4jsIFn+CiW1/IQm5yyff1CUO87OqVbtp9BlaXZNmw==";
51  
52      /**
53       * tests what happens if the remote host has a different key than the one
54       * we expect
55       *
56       * @throws Exception on error
57       */
58      public void testIncorrectKey()
59          throws Exception
60      {
61          wagon.setKnownHostsProvider( failHostsProvider );
62  
63          try
64          {
65              wagon.connect( source );
66  
67              fail( "Should not have successfully connected - host is not known" );
68          }
69          catch ( UnknownHostException e )
70          {
71              // ok
72          }
73      }
74  
75      /**
76       * tests what happens if the remote host has changed since being recorded.
77       *
78       * @throws Exception on error
79       */
80      public void testChangedKey()
81          throws Exception
82      {
83          wagon.setKnownHostsProvider( changedHostsProvider );
84  
85          try
86          {
87              wagon.connect( source );
88  
89              fail( "Should not have successfully connected - host is changed" );
90          }
91          catch ( KnownHostChangedException e )
92          {
93              // ok
94          }
95      }
96  
97      /**
98       * tests what happens if the remote host has the expected key
99       *
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 }