From 336cc956c810b4a63412934523d28d0fe13cd403 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Fri, 10 May 2024 18:21:10 +0200 Subject: [PATCH] Explicit error message when no storetype is specified and the keystore file doesn't exist --- .../main/java/net/jsign/KeyStoreBuilder.java | 6 +++- .../java/net/jsign/KeyStoreBuilderTest.java | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/jsign-core/src/main/java/net/jsign/KeyStoreBuilder.java b/jsign-core/src/main/java/net/jsign/KeyStoreBuilder.java index 4ab4bc5e..cede7b38 100644 --- a/jsign-core/src/main/java/net/jsign/KeyStoreBuilder.java +++ b/jsign-core/src/main/java/net/jsign/KeyStoreBuilder.java @@ -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"); } diff --git a/jsign-core/src/test/java/net/jsign/KeyStoreBuilderTest.java b/jsign-core/src/test/java/net/jsign/KeyStoreBuilderTest.java index 25cf85a8..715ab881 100644 --- a/jsign-core/src/test/java/net/jsign/KeyStoreBuilderTest.java +++ b/jsign-core/src/test/java/net/jsign/KeyStoreBuilderTest.java @@ -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);