PrivateKeyStorage { public Tuple<VirgilPrivateKey, Map<String, String>> load(String keyName) throws CryptoException { KeyEntry keyEntry = this.keyStorage.load(keyName); if (keyEntry != null) { VirgilPrivateKey privateKey = this.keyExporter.importPrivateKey(keyEntry.getValue()); Tuple<VirgilPrivateKey, Map<String, String>> pair = new Tuple<>( privateKey, keyEntry.getMeta()); return pair; } return null; } PrivateKeyStorage(VirgilPrivateKeyExporter keyExporter, KeyStorage keyStorage); void delete(String keyName); boolean exists(String keyName); Tuple<VirgilPrivateKey, Map<String, String>> load(String keyName); Set<String> names(); void store(VirgilPrivateKey privateKey, String name, Map<String, String> meta);  }
@Test public void load() throws CryptoException { byte[] privateKeyData = this.crypto.exportPrivateKey(privateKey); when(this.keyExporter.exportPrivateKey(privateKey)).thenReturn(privateKeyData); when(this.keyExporter.importPrivateKey(privateKeyData)) .thenReturn(this.crypto.importPrivateKey(privateKeyData).getPrivateKey()); Map<String, String> meta = new HashMap<>(); meta.put("key1", "value1"); meta.put("key2", "value2"); KeyEntry entry = new JsonKeyEntry(this.keyName, this.keyExporter.exportPrivateKey(this.privateKey)); entry.setMeta(meta); when(this.keyStorage.load(this.keyName)).thenReturn(entry); Tuple<VirgilPrivateKey, Map<String, String>> keyData = storage.load(this.keyName); assertNotNull(keyData); VirgilPrivateKey key = keyData.getLeft(); assertNotNull(key); assertEquals(this.privateKey, key); assertEquals(meta, keyData.getRight()); }
@Test public void load_noMeta() throws CryptoException { byte[] privateKeyData = this.crypto.exportPrivateKey(privateKey); when(this.keyExporter.exportPrivateKey(privateKey)).thenReturn(privateKeyData); when(this.keyExporter.importPrivateKey(privateKeyData)) .thenReturn(this.crypto.importPrivateKey(privateKeyData).getPrivateKey()); KeyEntry entry = new JsonKeyEntry(this.keyName, this.keyExporter.exportPrivateKey(this.privateKey)); when(this.keyStorage.load(this.keyName)).thenReturn(entry); Tuple<VirgilPrivateKey, Map<String, String>> keyData = storage.load(this.keyName); assertNotNull(keyData); VirgilPrivateKey key = keyData.getLeft(); assertNotNull(key); assertEquals(this.privateKey, key); assertTrue(keyData.getRight().isEmpty()); }
@Test public void load_nonExisting() { when(this.keyStorage.load(this.keyName)).thenThrow(KeyEntryNotFoundException.class); assertThrows(KeyEntryNotFoundException.class, () -> { storage.load(this.keyName); }); }
@Test public void load_nullName() { when(this.keyStorage.load(null)).thenThrow(KeyEntryNotFoundException.class); assertThrows(KeyEntryNotFoundException.class, () -> { storage.load(null); }); }