1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
42
43
44
45
46
47
48
49
50 public class SSHAgentTest {
51
52 protected static File keyFile;
53
54
55
56
57
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
67
68 @Test
69 public void testKeyExist() {
70 Assert.assertNotNull(keyFile);
71 Assert.assertTrue(keyFile.isFile());
72 }
73
74
75
76
77
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());
87 Assert.assertTrue(kpair.decrypt(SSHAgent.toBytes(passphrase)));
88
89 }
90
91
92
93
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());
103 Assert.assertFalse(kpair.decrypt(SSHAgent.toBytes(passphrase)));
104 }
105 }