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  public class KnownHostsProviderTestCase
29      extends PlexusTestCase
30  {
31      protected KnownHostsProvider okHostsProvider;
32  
33      protected KnownHostsProvider failHostsProvider;
34  
35      protected KnownHostsProvider changedHostsProvider;
36  
37      private SshWagon wagon;
38  
39      private Repository source;
40  
41      private static final String CORRECT_KEY = TestData.getHostKey();
42  
43      private static final String CHANGED_KEY =
44          "AAAAB3NzaC1yc2EAAAABIwAAAQEA8VLKkfHl2CNqW+m0603z07dyweWzzdVGQlMPUX4z1264E7M/h+6lPKiOo+u49CL7eQVA+FtW"
45          + "TZoJ3oBAMABcKnHx41TnSpQUkbdR6rzyC6IG1lXiVtEjG2w7DUnxpCtVo5PaQuJobwoXv5NNL3vx03THPgcDJquLPWvGnDWhnXoEh"
46          + "3/6c7rprwT+PrjZ6LIT35ZCUGajoehhF151oNbFMQHllfR6EAiZIP0z0nIVI+Jiv6g+XZapumVPVYjdOfxvLKQope1H9HJamT3bDI"
47          + "m8mkebUB10DzQJYxFt4/0wiNH3L4jsIFn+CiW1/IQm5yyff1CUO87OqVbtp9BlaXZNmw==";
48  
49      /**
50       * tests what happens if the remote host has a different key than the one
51       * we expect
52       *
53       * @throws Exception on error
54       */
55      public void testIncorrectKey()
56          throws Exception
57      {
58          wagon.setKnownHostsProvider( failHostsProvider );
59  
60          try
61          {
62              wagon.connect( source );
63  
64              fail( "Should not have successfully connected - host is not known" );
65          }
66          catch ( UnknownHostException e )
67          {
68              // ok
69          }
70      }
71  
72      /**
73       * tests what happens if the remote host has changed since being recorded.
74       *
75       * @throws Exception on error
76       */
77      public void testChangedKey()
78          throws Exception
79      {
80          wagon.setKnownHostsProvider( changedHostsProvider );
81  
82          try
83          {
84              wagon.connect( source );
85  
86              fail( "Should not have successfully connected - host is changed" );
87          }
88          catch ( KnownHostChangedException e )
89          {
90              // ok
91          }
92      }
93  
94      /**
95       * tests what happens if the remote host has the expected key
96       *
97       * @throws Exception on error
98       */
99      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 }