-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelfSignedKeystoreTest.java
69 lines (50 loc) · 2.42 KB
/
SelfSignedKeystoreTest.java
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
/*
* Copyright (c) 2018 Nuvolect LLC.
* This software is offered for free under conditions of the GPLv3 open source software license.
* Contact Nuvolect LLC for a less restrictive commercial license if you would like to use the software
* without the GPLv3 restrictions.
*/
package com.nuvolect.securesuite.util;
import android.content.Context;
import com.nuvolect.securesuite.webserver.SSLUtil;
import com.nuvolect.securesuite.webserver.SelfSignedCertificate;
import org.junit.Test;
import java.io.File;
import javax.net.ssl.SSLServerSocketFactory;
import static com.nuvolect.securesuite.main.App.getContext;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* Test lifecycle of a selfsigned certificate and password
* NOTE THIS IS A DESTRUCTIVE TEST. It will delete the existing certificate and password.
*/
public class SelfSignedKeystoreTest {
@Test
public void createCertTest(){
LogUtil.log( KeystoreUtilTest.class, CrypUtilTest.class.getCanonicalName()+" test starting");
Context ctx = getContext();
// Create a self signed certificate and put it in a BKS keystore
String keystoreFilename = "SelfSignedKeystoreTest.bks";
File file = new File( ctx.getFilesDir(), keystoreFilename);
file.delete();
assertThat( file.exists(), is( false));
Persist.deleteKey(ctx, Persist.SELFSIGNED_KS_KEY);
assertThat( Persist.keyExists(ctx, Persist.SELFSIGNED_KS_KEY), is( false));
String absolutePath = file.getAbsolutePath();
SelfSignedCertificate.makeKeystore( ctx, absolutePath, false);
assertThat( file.exists(), is( true));
assertThat( Persist.keyExists(ctx, Persist.SELFSIGNED_KS_KEY), is( true));
try {
SSLServerSocketFactory sslServerSocketFactory = SSLUtil.configureSSL(ctx, absolutePath);
String[] suites = sslServerSocketFactory.getSupportedCipherSuites();
assertThat( suites.length > 0, is( true));
} catch (Exception e) {
LogUtil.logException(KeystoreUtilTest.class, e);
}
file.delete();
assertThat( file.exists(), is( false));
Persist.deleteKey(ctx, Persist.SELFSIGNED_KS_KEY);
assertThat( Persist.keyExists(ctx, Persist.SELFSIGNED_KS_KEY), is( false));
LogUtil.log( KeystoreUtilTest.class, CrypUtilTest.class.getCanonicalName()+" test ending, certificate deleted");
}
}