Newer
Older
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (C) 2012 Damien Raude-Morvan <drazzib@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.debian.security;
import java.io.File;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Emmanuel Bourg
* @version $Revision$, $Date$
*/
public class KeyStoreHandlerTest {
private String ksFilename = "./target/test-classes/tests-cacerts";
private char[] ksPassword = "changeit".toCharArray();
/**
* Test a simple open then write without any modification.
*/
@Test
public void testNoop() throws Exception {
KeyStoreHandler keystore = new KeyStoreHandler(ksFilename, ksPassword);
keystore.save();
}
/**
* Test a to open a keystore and write without any modification
* and then try to open it again with wrong password : will throw a
* InvalidKeystorePassword
*/
@Test
public void testWriteThenOpenWrongPwd() throws Exception {
try {
KeyStoreHandler keystore = new KeyStoreHandler(ksFilename, ksPassword);
keystore.save();
} catch (InvalidKeystorePasswordException e) {
fail();
}
try {
KeyStoreHandler keystore = new KeyStoreHandler(ksFilename, "wrongpassword".toCharArray());
fail();
keystore.save();
} catch (InvalidKeystorePasswordException e) {
assertEquals("Cannot open Java keystore. Is the password correct?", e.getMessage());
}
}
/**
* Test a to open a keystore then remove its backing File (and replace it
* with a directory with the same name) and try to write in to disk :
* will throw an UnableToSaveKeystore
*/
@Test
public void testDeleteThenWrite() throws Exception {
try {
KeyStoreHandler keystore = new KeyStoreHandler(ksFilename, ksPassword);
// Replace actual file by a directory !
File file = new File(ksFilename);
file.delete();
file.mkdir();
// Will fail with some IOException
keystore.save();
fail();
} catch (UnableToSaveKeystoreException e) {
assertEquals("There was a problem saving the new Java keystore.", e.getMessage());
}
}
}