Skip to content

Commit

Permalink
Explicit error message when no storetype is specified and the keystor…
Browse files Browse the repository at this point in the history
…e file doesn't exist
  • Loading branch information
ebourg committed May 10, 2024
1 parent 7b66e46 commit 336cc95
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion jsign-core/src/main/java/net/jsign/KeyStoreBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ KeyStoreType storetype() {
storetype = NONE;
} else {
// the keystore type wasn't specified, let's try to guess it
storetype = KeyStoreType.of(createFile(keystore));
File file = createFile(keystore);
if (!file.isFile()) {
throw new IllegalArgumentException("Keystore file '" + keystore + "' not found");
}
storetype = KeyStoreType.of(file);
if (storetype == null) {
throw new IllegalArgumentException("Keystore type of '" + keystore + "' not recognized");
}
Expand Down
28 changes: 28 additions & 0 deletions jsign-core/src/test/java/net/jsign/KeyStoreBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,34 @@ public void testBuildPKCS12() throws Exception {
assertNotNull("keystore", keystore);
}

@Test
public void testBuildWithoutStoreType() throws Exception {
KeyStoreBuilder builder = new KeyStoreBuilder();

try {
builder.build();
fail("Exception not thrown");
} catch (IllegalArgumentException e) {
assertEquals("message", "Either keystore, or keyfile and certfile, or storetype parameters must be set", e.getMessage());
}

builder.keystore("target/test-classes/keystores/keystore.error");

try {
builder.build();
fail("Exception not thrown");
} catch (IllegalArgumentException e) {
assertEquals("message", "Keystore file 'target/test-classes/keystores/keystore.error' not found", e.getMessage());
}

builder.keystore("target/test-classes/keystores/keystore.p12");

assertEquals("storetype", PKCS12, builder.storetype());

KeyStore keystore = builder.build();
assertNotNull("keystore", keystore);
}

@Test
public void testBuildPKCS11() throws Exception {
KeyStoreBuilder builder = new KeyStoreBuilder().storetype(PKCS11);
Expand Down

0 comments on commit 336cc95

Please sign in to comment.