Skip to content

Commit

Permalink
TKSS-440: The temp test files should be in build directories
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshajiang committed Oct 8, 2023
1 parent 8b0f48f commit 025f3c6
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
public class KeyStoreToolTest {

private static final Path BASE_DIR = Paths.get("KeyStoreToolTest");
private static final Path BASE_DIR = Paths.get("build", "KeyStoreToolTest");

private static final Path TRUST_STORE = path("truststore.ks");
private static final Path KEY_STORE = path("keystore.ks");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public class KeyToolTest {

private static final Path BASE_DIR = Paths.get("KeyStoreToolTest");
private static final Path BASE_DIR = Paths.get("build", "KeyToolTest");

private static final Path KEYSTORE = path("keystore.ks");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyStore;
Expand Down Expand Up @@ -255,8 +256,10 @@ public class TLCPWithHttpClientDemo {
"Iml9tJuJhh6aZQl9LN/7uEHq/8Fz0PZg4Pe9cy6mvsa1xdUTAR/+Kiim";

private static final String PASSWORD = "password";
private static final String TRUSTSTORE = "truststore";
private static final String KEYSTORE = "keystore";

private static final Path BASE_DIR = Paths.get("build");
private static final Path TRUSTSTORE = BASE_DIR.resolve("truststore");
private static final Path KEYSTORE = BASE_DIR.resolve("keystore");

@BeforeAll
public static void setup() throws Exception {
Expand Down Expand Up @@ -285,8 +288,7 @@ private static void createTrustStoreFile(String caStr, String caId)
KeyStore trustStore = PKIXInsts.getKeyStore("PKCS12");
trustStore.load(null, null);
trustStore.setCertificateEntry("tlcp-trust-demo", loadCert(caStr, caId));
try (FileOutputStream out = new FileOutputStream(
Paths.get(TRUSTSTORE).toFile())) {
try (FileOutputStream out = new FileOutputStream(TRUSTSTORE.toFile())) {
trustStore.store(out, PASSWORD.toCharArray());
}
}
Expand All @@ -307,8 +309,7 @@ private static void createKeyStoreFile(
PASSWORD.toCharArray(),
new Certificate[] { loadCert(encEeStr, encEeId) } );

try (FileOutputStream out = new FileOutputStream(
Paths.get(KEYSTORE).toFile())) {
try (FileOutputStream out = new FileOutputStream(KEYSTORE.toFile())) {
keyStore.store(out, PASSWORD.toCharArray());
}
}
Expand Down Expand Up @@ -341,8 +342,8 @@ public static void clean() throws IOException {
}

private static void deleteStoreFiles() throws IOException {
Files.deleteIfExists(Paths.get(TRUSTSTORE));
Files.deleteIfExists(Paths.get(KEYSTORE));
Files.deleteIfExists(TRUSTSTORE);
Files.deleteIfExists(KEYSTORE);
}

@Test
Expand Down Expand Up @@ -404,7 +405,8 @@ private static CloseableHttpClient createClient() throws Exception {
private static SSLContext createContext() throws Exception {
// Load trust store
KeyStore trustStore = PKIXInsts.getKeyStore("PKCS12");
try (FileInputStream keyStoreIn = new FileInputStream(TRUSTSTORE)) {
try (FileInputStream keyStoreIn = new FileInputStream(
TRUSTSTORE.toFile())) {
trustStore.load(keyStoreIn, PASSWORD.toCharArray());
}

Expand All @@ -413,7 +415,8 @@ private static SSLContext createContext() throws Exception {

// Load key store
KeyStore keyStore = PKIXInsts.getKeyStore("PKCS12");
try (FileInputStream keyStoreIn = new FileInputStream(KEYSTORE)) {
try (FileInputStream keyStoreIn = new FileInputStream(
KEYSTORE.toFile())) {
keyStore.load(keyStoreIn, PASSWORD.toCharArray());
}
KeyManagerFactory kmf = SSLInsts.getKeyManagerFactory("NewSunX509");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyStore;
Expand Down Expand Up @@ -252,8 +253,10 @@ public class TLCPWithoutCertValidationDemo {
"aaiEOTrMdFQ5OFFtufGi5VPem396J75x04aWvRsB22OQaWLDD9YUvtZA";

private static final String PASSWORD = "password";
private static final String TRUSTSTORE = "truststore";
private static final String KEYSTORE = "keystore";

private static final Path BASE_DIR = Paths.get("build");
private static final Path TRUSTSTORE = BASE_DIR.resolve("truststore");
private static final Path KEYSTORE = BASE_DIR.resolve("keystore");

@BeforeAll
public static void setup() throws Exception {
Expand Down Expand Up @@ -283,7 +286,7 @@ private static void createTrustStoreFile(String caStr, String caId)
trustStore.load(null, null);
trustStore.setCertificateEntry("tlcp-trust-demo", loadCert(caStr, caId));
try (FileOutputStream out = new FileOutputStream(
Paths.get(TRUSTSTORE).toFile())) {
TRUSTSTORE.toFile())) {
trustStore.store(out, PASSWORD.toCharArray());
}
}
Expand All @@ -305,7 +308,7 @@ private static void createKeyStoreFile(
new Certificate[] { loadCert(encEeStr, encEeId) } );

try (FileOutputStream out = new FileOutputStream(
Paths.get(KEYSTORE).toFile())) {
KEYSTORE.toFile())) {
keyStore.store(out, PASSWORD.toCharArray());
}
}
Expand Down Expand Up @@ -338,8 +341,8 @@ public static void clean() throws IOException {
}

private static void deleteStoreFiles() throws IOException {
Files.deleteIfExists(Paths.get(TRUSTSTORE));
Files.deleteIfExists(Paths.get(KEYSTORE));
Files.deleteIfExists(TRUSTSTORE);
Files.deleteIfExists(KEYSTORE);
}

@Test
Expand Down Expand Up @@ -402,7 +405,8 @@ private static CloseableHttpClient createClient() throws Exception {
private static SSLContext createContext() throws Exception {
// Load trust store
KeyStore trustStore = PKIXInsts.getKeyStore("PKCS12");
try (FileInputStream keyStoreIn = new FileInputStream(TRUSTSTORE)) {
try (FileInputStream keyStoreIn = new FileInputStream(
TRUSTSTORE.toFile())) {
trustStore.load(keyStoreIn, PASSWORD.toCharArray());
}

Expand All @@ -421,7 +425,8 @@ private static SSLContext createContext() throws Exception {

// Load key store
KeyStore keyStore = PKIXInsts.getKeyStore("PKCS12");
try (FileInputStream keyStoreIn = new FileInputStream(KEYSTORE)) {
try (FileInputStream keyStoreIn = new FileInputStream(
KEYSTORE.toFile())) {
keyStore.load(keyStoreIn, PASSWORD.toCharArray());
}
KeyManagerFactory kmf = SSLInsts.getKeyManagerFactory("NewSunX509");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutorService;
Expand All @@ -59,16 +60,17 @@
*/
public class BabaSSLServerJdkClientTest {

private static final String PAGE_FILE_NAME = "tlcp-page";
private static final Path PAGE_FILE = Paths.get("build", "tlcp-page");

@BeforeAll
public static void setup() throws IOException {
TestUtils.addProviders();
deleteWebPage();
createWebPage();
}

private static void createWebPage() throws IOException {
Files.write(Paths.get(PAGE_FILE_NAME),
Files.write(PAGE_FILE,
"BabaSSL server".getBytes(Utilities.CHARSET),
StandardOpenOption.CREATE);
}
Expand All @@ -79,7 +81,7 @@ public static void clean() throws IOException {
}

private static void deleteWebPage() throws IOException {
Files.deleteIfExists(Paths.get(PAGE_FILE_NAME));
Files.deleteIfExists(PAGE_FILE);
}

@Test
Expand Down Expand Up @@ -332,7 +334,7 @@ private JdkClient.Builder createClientBuilder(
clientBuilder.setCipherSuites(cipherSuite);
clientBuilder.setMessage(
// An HTTP request asks to access the page.
String.format("GET /%s HTTP/1.1\r\n", PAGE_FILE_NAME));
String.format("GET /%s HTTP/1.1\r\n", PAGE_FILE));
clientBuilder.setReadResponse(true);
clientBuilder.setContext(context);
return clientBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -49,11 +50,12 @@
*/
public class JdkServerBabaSSLClient {

private static final String SESS_FILE_NAME = "babassl.sess";
private static final Path SESS_FILE = Paths.get("build", "openssl.sess");

@BeforeAll
public static void setup() throws IOException {
TestUtils.addProviders();
deleteSessFile();
}

@AfterAll
Expand All @@ -62,7 +64,7 @@ public static void clean() throws IOException {
}

private static void deleteSessFile() throws IOException {
Files.deleteIfExists(Paths.get(SESS_FILE_NAME));
Files.deleteIfExists(SESS_FILE);
}

@Test
Expand Down Expand Up @@ -289,15 +291,15 @@ private void resumeSession(
try (BabaSSLClient client = createClientBuilder(
clientCertTuple, clientCipherSuite,
useSessionTicket,
SESS_FILE_NAME, true).build()) {
SESS_FILE, true).build()) {
client.connect("127.0.0.1", server.getPort());
firstCreationTime = server.getSession().getCreationTime();
}

try (BabaSSLClient client = createClientBuilder(
clientCertTuple, clientCipherSuite,
useSessionTicket,
SESS_FILE_NAME, false).build()) {
SESS_FILE, false).build()) {
client.connect("127.0.0.1", server.getPort());

long secondCreationTime = server.getSession().getCreationTime();
Expand All @@ -309,8 +311,8 @@ private void resumeSession(
}

private BabaSSLClient.Builder createClientBuilder(CertTuple certTuple,
CipherSuite cipherSuite, boolean useSessionTicket,
String sessFile, boolean saveSess) {
CipherSuite cipherSuite, boolean useSessionTicket,
Path sessFile, boolean saveSess) {
BabaSSLClient.Builder builder = new BabaSSLClient.Builder();
builder.setCertTuple(certTuple);
builder.setProtocols(Protocol.TLCPV1_1);
Expand All @@ -320,10 +322,11 @@ private BabaSSLClient.Builder createClientBuilder(CertTuple certTuple,
builder.setUseSessTicket(useSessionTicket);

if (sessFile != null) {
String sessFilePath = sessFile.toAbsolutePath().toString();
if (saveSess) {
builder.sessOut(sessFile);
builder.sessOut(sessFilePath);
} else {
builder.sessIn(sessFile);
builder.sessIn(sessFilePath);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -73,16 +74,17 @@ public class BabaSSLServerJdkClientTest {
"ee-sm2sm2-sm2sm2-sm2sm2.crt",
"ee-sm2sm2-sm2sm2-sm2sm2.key");

private static final String PAGE_FILE_NAME = "page";
private static final Path PAGE_FILE = Paths.get("build", "page");

@BeforeAll
public static void setup() throws IOException {
TestUtils.addProviders();
deleteWebPage();
createWebPage();
}

private static void createWebPage() throws IOException {
Files.write(Paths.get(PAGE_FILE_NAME),
Files.write(PAGE_FILE,
"OpenSSL server".getBytes(Utilities.CHARSET),
StandardOpenOption.CREATE);
}
Expand All @@ -93,7 +95,7 @@ public static void clean() throws IOException {
}

private static void deleteWebPage() throws IOException {
Files.deleteIfExists(Paths.get(PAGE_FILE_NAME));
Files.deleteIfExists(PAGE_FILE);
}

@Test
Expand Down Expand Up @@ -280,7 +282,7 @@ private JdkProcClient createProcClient(
builder.setSignatureSchemes(signatureScheme);
builder.setMessage(
// An HTTP request asks to access the page.
String.format("GET /%s HTTP/1.1\r\n", PAGE_FILE_NAME));
String.format("GET /%s HTTP/1.1\r\n", PAGE_FILE));
builder.setReadResponse(true);
return builder.build();
}
Expand Down Expand Up @@ -442,7 +444,7 @@ private JdkClient createClient(
// builder.setSignatureSchemes(signatureScheme);
builder.setMessage(
// An HTTP request asks to access the page.
String.format("GET /%s HTTP/1.1\r\n", PAGE_FILE_NAME));
String.format("GET /%s HTTP/1.1\r\n", PAGE_FILE));
builder.setReadResponse(true);
builder.setContext(context);
return builder.build();
Expand Down
Loading

0 comments on commit 025f3c6

Please sign in to comment.