Skip to content

Commit a880c55

Browse files
authored
Merge pull request #239 from pusher/decrypt-testFail
Add a test for crypto authenticity failure
2 parents 5c00b4a + 5438e6c commit a880c55

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pusher.client.crypto.nacl;
22

33
import static com.google.common.truth.Truth.assertThat;
4+
import static java.util.Arrays.copyOf;
45

56
import com.pusher.client.util.internal.Base64;
67
import org.junit.Before;
@@ -9,6 +10,10 @@
910
public class SecretBoxOpenerTest {
1011

1112
byte[] key = Base64.decode("6071zp2l/GPnDPDXNWTJDHyIZ8pZMvQrYsa4xuTKK2c=");
13+
14+
byte[] nonce = Base64.decode("xsbOS0KylAV2ziTDHrP/7rSFqpCOah3p");
15+
byte[] cipher = Base64.decode("tvttPE2PRQp0bWDmaPyiEU8YJGztmTvTN77OoPwftTNTdDgJXwxHQPE=");
16+
1217
SecretBoxOpener subject;
1318

1419
@Before
@@ -18,11 +23,16 @@ public void setUp() {
1823

1924
@Test
2025
public void open() {
21-
byte[] cipher = Base64.decode("tvttPE2PRQp0bWDmaPyiEU8YJGztmTvTN77OoPwftTNTdDgJXwxHQPE=");
22-
byte[] nonce = Base64.decode("xsbOS0KylAV2ziTDHrP/7rSFqpCOah3p");
23-
2426
byte[] clearText = subject.open(cipher, nonce);
2527

2628
assertThat(new String(clearText)).isEqualTo("{\"message\":\"hello world\"}");
2729
}
30+
31+
@Test(expected = AuthenticityException.class)
32+
public void openFailsForTamperedCipher() {
33+
byte[] tamperedCipher = copyOf(cipher, cipher.length);
34+
tamperedCipher[0] ^= tamperedCipher[0];
35+
36+
subject.open(tamperedCipher, nonce);
37+
}
2838
}

0 commit comments

Comments
 (0)