View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: SSHAgentTest.java 4220 2015-05-07 09:43:46Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/util/ssh/SSHAgentTest.java $
7    * %%
8    * Copyright (C) 2009 - 2015 Ifremer, Code Lutin, Chatellier Eric
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/gpl-3.0.html>.
23   * #L%
24   */
25  
26  package fr.ifremer.isisfish.util.ssh;
27  
28  import java.io.File;
29  import java.net.URISyntaxException;
30  import java.net.URL;
31  
32  import org.junit.Assert;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  
36  import com.jcraft.jsch.JSch;
37  import com.jcraft.jsch.JSchException;
38  import com.jcraft.jsch.KeyPair;
39  
40  /**
41   * Class de test créé pour tester et centraliser la gestion
42   * des clés SSH et leur passphrase dans Isis.
43   *
44   * @author chatellier
45   * @version $Revision: 4220 $
46   * 
47   * Last update : $Date: 2015-05-07 11:43:46 +0200 (Thu, 07 May 2015) $
48   * By : $Author: echatellier $
49   */
50  public class SSHAgentTest {
51  
52      protected static File keyFile;
53      
54      /**
55       * Find ssh key file.
56       * 
57       * @throws URISyntaxException 
58       */
59      @BeforeClass
60      public static void init() throws URISyntaxException {
61          URL keyURL = SSHAgentTest.class.getResource("isistestkey");
62          keyFile = new File(keyURL.getPath());
63      }
64      
65      /**
66       * Test que la clé existe.
67       */
68      @Test
69      public void testKeyExist() {
70          Assert.assertNotNull(keyFile);
71          Assert.assertTrue(keyFile.isFile());
72      }
73      
74      /**
75       * Test si une passphrase est valide pour une clé SSH.
76       * 
77       * @throws JSchException 
78       */
79      @Test
80      public void testIsValidPassphrase() throws JSchException {
81          char[] passphrase = "isispassphrase".toCharArray();
82          
83          JSch jsch = new JSch();
84          KeyPair kpair = KeyPair.load(jsch, keyFile.getAbsolutePath());
85  
86          Assert.assertTrue(kpair.isEncrypted()); // cle protegee
87          Assert.assertTrue(kpair.decrypt(SSHAgent.toBytes(passphrase))); // decodage fonctionne
88          
89      }
90      
91      /**
92       * Test qu'une passphrase n'est pas valide pour une clé.
93       * @throws JSchException 
94       */
95      @Test
96      public void testIsNotValidPassphrase() throws JSchException {
97          char[] passphrase = "passphare not good".toCharArray();
98          
99          JSch jsch = new JSch();
100         KeyPair kpair = KeyPair.load(jsch, keyFile.getAbsolutePath());
101 
102         Assert.assertTrue(kpair.isEncrypted()); // cle protegee
103         Assert.assertFalse(kpair.decrypt(SSHAgent.toBytes(passphrase))); // decodage fonctionne
104     }
105 }