### Question:
KeyCodec { public void writeKey(KeyPair keyPair) throws IOException { writeKey(location, keyPair, securityConfig.getPrivateKeyFileName(), securityConfig.getPublicKeyFileName(), false); } KeyCodec(SecurityConfig config, String component); @VisibleForTesting Set<PosixFilePermission> getPermissionSet(); SecurityConfig getSecurityConfig(); @VisibleForTesting void setIsPosixFileSystem(Supplier<Boolean> isPosixFileSystem); void writeKey(KeyPair keyPair); void writePrivateKey(PrivateKey key); void writePublicKey(PublicKey key); void writeKey(KeyPair keyPair, boolean overwrite); void writeKey(Path basePath, KeyPair keyPair, boolean overwrite); PrivateKey readPrivateKey(Path basePath, String privateKeyFileName); PublicKey readPublicKey(); PublicKey readPublicKey(Path basePath, String publicKeyFileName); PrivateKey readPrivateKey(); final static String PRIVATE_KEY; final static String PUBLIC_KEY; final static Charset DEFAULT_CHARSET; }### Answer:
@Test public void testWriteKey() throws NoSuchProviderException, NoSuchAlgorithmException, IOException, InvalidKeySpecException { KeyPair keys = keyGenerator.generateKey(); KeyCodec pemWriter = new KeyCodec(securityConfig, component); pemWriter.writeKey(keys); Path keyLocation = pemWriter.getSecurityConfig().getKeyLocation(component); Assert.assertTrue(keyLocation.toFile().exists()); Assert.assertTrue(keyLocation.toString().startsWith(prefix)); Path privateKeyPath = Paths.get(keyLocation.toString(), pemWriter.getSecurityConfig().getPrivateKeyFileName()); Assert.assertTrue(privateKeyPath.toFile().exists()); Path publicKeyPath = Paths.get(keyLocation.toString(), pemWriter.getSecurityConfig().getPublicKeyFileName()); Assert.assertTrue(publicKeyPath.toFile().exists()); byte[] privateKey = Files.readAllBytes(privateKeyPath); String privateKeydata = new String(privateKey, StandardCharsets.UTF_8); Assert.assertTrue(privateKeydata.contains("PRIVATE KEY")); byte[] publicKey = Files.readAllBytes(publicKeyPath); String publicKeydata = new String(publicKey, StandardCharsets.UTF_8); Assert.assertTrue(publicKeydata.contains("PUBLIC KEY")); KeyFactory kf = KeyFactory.getInstance( pemWriter.getSecurityConfig().getKeyAlgo()); privateKeydata = privateKeydata.replace("-----BEGIN PRIVATE KEY-----\n", ""); privateKeydata = privateKeydata.replace("-----END PRIVATE KEY-----", ""); byte[] keyBytes = Base64.decodeBase64(privateKeydata); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); PrivateKey privateKeyDecoded = kf.generatePrivate(spec); assertNotNull("Private Key should not be null", privateKeyDecoded); publicKeydata = publicKeydata.replace("-----BEGIN PUBLIC KEY-----\n", ""); publicKeydata = publicKeydata.replace("-----END PUBLIC KEY-----", ""); keyBytes = Base64.decodeBase64(publicKeydata); X509EncodedKeySpec pubKeyspec = new X509EncodedKeySpec(keyBytes); PublicKey publicKeyDecoded = kf.generatePublic(pubKeyspec); assertNotNull("Public Key should not be null", publicKeyDecoded); Set<PosixFilePermission> expectedSet = pemWriter.getPermissionSet(); Set<PosixFilePermission> currentSet = Files.getPosixFilePermissions(privateKeyPath); currentSet.removeAll(expectedSet); Assert.assertEquals(0, currentSet.size()); currentSet = Files.getPosixFilePermissions(publicKeyPath); currentSet.removeAll(expectedSet); Assert.assertEquals(0, currentSet.size()); currentSet = Files.getPosixFilePermissions(keyLocation); currentSet.removeAll(expectedSet); Assert.assertEquals(0, currentSet.size()); }