KeystoreCreator { static void updateSystemPropertiesFile(KeystoreHelper keystoreHelper, File keyStoreFile, String systemPropertiesPath) { File systemPropertiesFile = new File(systemPropertiesPath); try (FileWriter fileWriter = new FileWriter(systemPropertiesFile, true); BufferedWriter br = new BufferedWriter(fileWriter) ) { br.newLine(); br.write("# Properties to configure a file based Keystore"); br.newLine(); br.write("com.l7tech.common.security.jceProviderEngineName=generic"); br.newLine(); br.write("com.l7tech.keystore.type=" + keystoreHelper.getKeyStoreType()); br.newLine(); br.write("com.l7tech.keystore.path=" + PathUtils.unixPath(keyStoreFile.getAbsolutePath())); br.newLine(); br.write("com.l7tech.keystore.savePath=EMPTY"); br.newLine(); br.write("com.l7tech.keystore.password=" + new String(keystoreHelper.getKeystorePassword())); br.newLine(); br.write("com.l7tech.common.security.jceProviderEngine=com.l7tech.security.prov.generic.GenericJceProviderEngine"); } catch (IOException e) { throw new KeyStoreCreationException("Unexpected error adding to system properties", e); } } private  KeystoreCreator();   }
@Test void createKeystore_checkPropertiesFile() throws IOException { final KeystoreHelper keystoreHelper = new KeystoreHelper(); KeystoreCreator.updateSystemPropertiesFile(keystoreHelper, keystoreFile, systemProperties.getPath()); assertTrue(systemProperties.exists()); try (InputStream stream = Files.newInputStream(systemProperties.toPath())){ Properties properties = new Properties(); properties.load(stream); assertFalse(properties.isEmpty()); assertEquals(6, properties.size()); assertNotNull(properties.getProperty("com.l7tech.common.security.jceProviderEngineName")); assertEquals("generic", properties.getProperty("com.l7tech.common.security.jceProviderEngineName")); assertNotNull(properties.getProperty("com.l7tech.keystore.type")); assertEquals(keystoreHelper.getKeyStoreType(), properties.getProperty("com.l7tech.keystore.type")); assertNotNull(properties.getProperty("com.l7tech.keystore.path")); assertEquals(FilenameUtils.separatorsToUnix(keystoreFile.getPath()), properties.getProperty("com.l7tech.keystore.path")); assertNotNull(properties.getProperty("com.l7tech.keystore.savePath")); assertEquals("EMPTY", properties.getProperty("com.l7tech.keystore.savePath")); assertNotNull(properties.getProperty("com.l7tech.keystore.password")); assertEquals(new String(keystoreHelper.getKeystorePassword()), properties.getProperty("com.l7tech.keystore.password")); assertNotNull(properties.getProperty("com.l7tech.common.security.jceProviderEngine")); assertEquals("com.l7tech.security.prov.generic.GenericJceProviderEngine", properties.getProperty("com.l7tech.common.security.jceProviderEngine")); } Files.delete(systemProperties.toPath()); }
@Test void createKeystore_errorPropertiesFile() throws IOException { final KeystoreHelper keystoreHelper = new KeystoreHelper(); if (FileUtils.POSIX_ENABLED) { Files.setPosixFilePermissions(systemProperties.toPath(), PosixFilePermissions.fromString("---------")); } else { systemProperties.setWritable(false); } assertThrows(KeyStoreCreationException.class, () -> KeystoreCreator.updateSystemPropertiesFile(keystoreHelper, keystoreFile, systemProperties.getPath())); if (FileUtils.POSIX_ENABLED) { Files.setPosixFilePermissions(systemProperties.toPath(), PosixFilePermissions.fromString("rwxrwxrwx")); } else { systemProperties.setWritable(true); } Files.delete(systemProperties.toPath()); }