diff --git a/kms/src/test/java/com/example/SnippetsIT.java b/kms/src/test/java/com/example/SnippetsIT.java index fe37acbd70c..a36b0a7033f 100644 --- a/kms/src/test/java/com/example/SnippetsIT.java +++ b/kms/src/test/java/com/example/SnippetsIT.java @@ -259,21 +259,15 @@ public void addAndRemoveMemberToKeyRingPolicy_addsDisplaysAndRemoves() throws Ex @Test public void encryptDecrypt_encryptsAndDecrypts() throws Exception { - // Get an enabled crypto key version, since the primary version is likely disabled - Snippets.listCryptoKeyVersions(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID); - Matcher matcher = Pattern.compile(".*cryptoKeyVersions/(\\d+)\",\"state\":\"ENABLED\".*", - Pattern.DOTALL | Pattern.MULTILINE).matcher(bout.toString().trim()); - assertTrue(matcher.matches()); - String version = matcher.group(1); - - byte[] encrypted = CryptFile.encrypt( - PROJECT_ID, KEY_RING_ID, CRYPTO_KEY_ID, version, ENCRYPT_STRING.getBytes()); + // Encrypt ENCRYPT_STRING with the current primary version. + byte[] ciphertext = CryptFile.encrypt( + PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, ENCRYPT_STRING.getBytes()); - assertThat(new String(encrypted)).isNotEqualTo(ENCRYPT_STRING); + assertThat(new String(ciphertext)).isNotEqualTo(ENCRYPT_STRING); - byte[] decrypted = CryptFile.decrypt( - PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, encrypted); + byte[] plaintext = CryptFile.decrypt( + PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, ciphertext); - assertThat(new String(decrypted)).isEqualTo(ENCRYPT_STRING); + assertThat(new String(plaintext)).isEqualTo(ENCRYPT_STRING); } }