From 5438e6c708143fb8055862aa1f07a5e9c29aa984 Mon Sep 17 00:00:00 2001 From: Marek Urbaniak <8502071+marekoid@users.noreply.github.com> Date: Mon, 30 Mar 2020 16:21:22 +0100 Subject: [PATCH] Add a test for crypto authenticity failure --- .../client/crypto/nacl/SecretBoxOpenerTest.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/pusher/client/crypto/nacl/SecretBoxOpenerTest.java b/src/test/java/com/pusher/client/crypto/nacl/SecretBoxOpenerTest.java index e234ed6a..5b02c3b7 100644 --- a/src/test/java/com/pusher/client/crypto/nacl/SecretBoxOpenerTest.java +++ b/src/test/java/com/pusher/client/crypto/nacl/SecretBoxOpenerTest.java @@ -1,6 +1,7 @@ package com.pusher.client.crypto.nacl; import static com.google.common.truth.Truth.assertThat; +import static java.util.Arrays.copyOf; import com.pusher.client.util.internal.Base64; import org.junit.Before; @@ -9,6 +10,10 @@ public class SecretBoxOpenerTest { byte[] key = Base64.decode("6071zp2l/GPnDPDXNWTJDHyIZ8pZMvQrYsa4xuTKK2c="); + + byte[] nonce = Base64.decode("xsbOS0KylAV2ziTDHrP/7rSFqpCOah3p"); + byte[] cipher = Base64.decode("tvttPE2PRQp0bWDmaPyiEU8YJGztmTvTN77OoPwftTNTdDgJXwxHQPE="); + SecretBoxOpener subject; @Before @@ -18,11 +23,16 @@ public void setUp() { @Test public void open() { - byte[] cipher = Base64.decode("tvttPE2PRQp0bWDmaPyiEU8YJGztmTvTN77OoPwftTNTdDgJXwxHQPE="); - byte[] nonce = Base64.decode("xsbOS0KylAV2ziTDHrP/7rSFqpCOah3p"); - byte[] clearText = subject.open(cipher, nonce); assertThat(new String(clearText)).isEqualTo("{\"message\":\"hello world\"}"); } + + @Test(expected = AuthenticityException.class) + public void openFailsForTamperedCipher() { + byte[] tamperedCipher = copyOf(cipher, cipher.length); + tamperedCipher[0] ^= tamperedCipher[0]; + + subject.open(tamperedCipher, nonce); + } } \ No newline at end of file