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 java.io.File;
23  
24  import org.codehaus.plexus.util.FileUtils;
25  
26  import junit.framework.TestCase;
27  
28  public class FileKnownHostsProviderTest
29      extends TestCase
30  {
31      private File basedir = new File( System.getProperty( "basedir", "." ) );
32      
33      private File testKnownHostsFile;
34      
35      
36      private FileKnownHostsProvider provider;
37      
38      public void setUp()
39          throws Exception
40      {
41          File readonlyKnownHostFile = new File( basedir, "src/test/resources/known_hosts" );
42          testKnownHostsFile = new File( basedir, "target/known_hosts" );
43          testKnownHostsFile.delete();
44          FileUtils.copyFile( readonlyKnownHostFile, testKnownHostsFile );
45          testKnownHostsFile.setLastModified( testKnownHostsFile.lastModified() - 60 * 1000 );
46  
47          provider = new FileKnownHostsProvider( testKnownHostsFile );
48         
49      }
50      
51      public void testStoreKnownHostsNoChange()
52          throws Exception
53      {
54          long timestamp = this.testKnownHostsFile.lastModified();
55          //file with the same contents, but with entries swapped
56          File sameKnownHostFile = new File( basedir, "src/test/resources/known_hosts_same" );
57          String contents = FileUtils.fileRead( sameKnownHostFile );
58          
59          provider.storeKnownHosts( contents );
60          assertEquals( "known_hosts file is rewritten", timestamp, testKnownHostsFile.lastModified() );
61      }
62      
63      public void testStoreKnownHostsWithChange()
64          throws Exception
65      {
66          long timestamp = this.testKnownHostsFile.lastModified();
67          File sameKnownHostFile = new File( basedir, "src/test/resources/known_hosts_same" );
68          String contents = FileUtils.fileRead( sameKnownHostFile );
69          contents += "1 2 3";
70          
71          provider.storeKnownHosts( contents );
72          assertTrue( "known_hosts file is not rewritten", timestamp != testKnownHostsFile.lastModified() );
73      }
74      
75  }