Skip to content

Commit

Permalink
KMS: fixes the kms failing tests.
Browse files Browse the repository at this point in the history
Fix the tests introduced in GoogleCloudPlatform#816.

Some code was still calling encrypt(...) with a key version.
  • Loading branch information
white0ut committed Aug 20, 2017
1 parent 6661719 commit c2725eb
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions kms/src/test/java/com/example/SnippetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit c2725eb

Please sign in to comment.