11package com .pusher .client .channel .impl ;
22
3- import static org .junit .Assert .assertEquals ;
4- import static org .mockito .Matchers .any ;
5- import static org .mockito .Matchers .anyString ;
6- import static org .mockito .Matchers .eq ;
7- import static org .mockito .Mockito .mock ;
8- import static org .mockito .Mockito .times ;
9- import static org .mockito .Mockito .verify ;
10- import static org .mockito .Mockito .when ;
11-
123import com .pusher .client .AuthorizationFailureException ;
134import com .pusher .client .Authorizer ;
145import com .pusher .client .channel .ChannelEventListener ;
2112import org .junit .Before ;
2213import org .junit .Test ;
2314import org .junit .runner .RunWith ;
24- import org .mockito .ArgumentCaptor ;
25- import org .mockito .Captor ;
2615import org .mockito .Matchers ;
2716import org .mockito .Mock ;
2817import org .mockito .runners .MockitoJUnitRunner ;
2918
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .mockito .Matchers .any ;
21+ import static org .mockito .Matchers .anyString ;
22+ import static org .mockito .Matchers .eq ;
23+ import static org .mockito .Mockito .mock ;
24+ import static org .mockito .Mockito .times ;
25+ import static org .mockito .Mockito .verify ;
26+ import static org .mockito .Mockito .when ;
27+
3028@ RunWith (MockitoJUnitRunner .class )
3129public class PrivateEncryptedChannelImplTest extends ChannelImplTest {
3230
@@ -45,9 +43,6 @@ public class PrivateEncryptedChannelImplTest extends ChannelImplTest {
4543 @ Mock
4644 SecretBoxOpenerFactory mockSecretBoxOpenerFactory ;
4745
48- @ Captor
49- ArgumentCaptor <Exception > exceptionArgumentCaptor ;
50-
5146 @ Override
5247 @ Before
5348 public void setUp () {
@@ -255,7 +250,7 @@ public void onMessageRaisesExceptionWhenFailingToDecryptTwice() {
255250 "\\ \" ciphertext\\ \" : \\ \" /GMESnFGlbNn01BuBjp31XYa3i9vZsGKR8fgR9EDhXKx3lzGiUD501A=\\ \" " +
256251 "}\" }" );
257252
258- verify (mockListener1 ).onDecryptionFailure (exceptionArgumentCaptor . capture ());
253+ verify (mockListener1 ).onDecryptionFailure (anyString (), anyString ());
259254 }
260255
261256 @ Test
@@ -288,4 +283,43 @@ public void onMessageRetriesDecryptionOnce() {
288283 assertEquals ("{\" message\" :\" hello world\" }" , argCaptor .getValue ().getData ());
289284 }
290285
286+ @ Test
287+ public void twoEventsReceivedWithIncorrectSharedSecret () {
288+
289+ PrivateEncryptedChannelImpl channel = new PrivateEncryptedChannelImpl (
290+ mockInternalConnection ,
291+ getChannelName (),
292+ mockAuthorizer ,
293+ factory ,
294+ mockSecretBoxOpenerFactory );
295+
296+ when (mockAuthorizer .authorize (Matchers .anyString (), Matchers .anyString ()))
297+ .thenReturn (AUTH_RESPONSE_INCORRECT_SHARED_SECRET )
298+ .thenReturn (AUTH_RESPONSE_INCORRECT_SHARED_SECRET )
299+ .thenReturn (AUTH_RESPONSE_INCORRECT_SHARED_SECRET );
300+ when (mockSecretBoxOpenerFactory .create (any ()))
301+ .thenReturn (new SecretBoxOpener (Base64 .decode (SHARED_SECRET_INCORRECT )))
302+ .thenReturn (new SecretBoxOpener (Base64 .decode (SHARED_SECRET_INCORRECT )))
303+ .thenReturn (new SecretBoxOpener (Base64 .decode (SHARED_SECRET_INCORRECT )));
304+
305+ channel .toSubscribeMessage ();
306+
307+ PrivateEncryptedChannelEventListener mockListener1 = mock (PrivateEncryptedChannelEventListener .class );
308+ channel .bind ("my-event" , mockListener1 );
309+ channel .onMessage ("my-event" , "{\" event\" :\" event1\" ,\" data\" :\" {" +
310+ "\\ \" nonce\\ \" : \\ \" 4sVYwy4j/8dCcjyxtPCWyk19GaaViaW9\\ \" ," +
311+ "\\ \" ciphertext\\ \" : \\ \" /GMESnFGlbNn01BuBjp31XYa3i9vZsGKR8fgR9EDhXKx3lzGiUD501A=\\ \" " +
312+ "}\" }" );
313+
314+ verify (mockListener1 ).onDecryptionFailure ("my-event" , "Failed to decrypt message." );
315+
316+ // send a second message
317+ channel .onMessage ("my-event" , "{\" event\" :\" event1\" ,\" data\" :\" {" +
318+ "\\ \" nonce\\ \" : \\ \" 4sVYwy4j/8dCcjyxtPCWyk19GaaViaW9\\ \" ," +
319+ "\\ \" ciphertext\\ \" : \\ \" /GMESnFGlbNn01BuBjp31XYa3i9vZsGKR8fgR9EDhXKx3lzGiUD501A=\\ \" " +
320+ "}\" }" );
321+
322+ verify (mockListener1 ).onDecryptionFailure ("my-event" , "Too many failed attempts to decrypt a previous message." );
323+ }
324+
291325}
0 commit comments