package io.sniffy.tls;

import io.sniffy.*;
import io.sniffy.configuration.SniffyConfiguration;
import io.sniffy.socket.AddressMatchers;
import io.sniffy.socket.NetworkPacket;
import io.sniffy.socket.SocketMetaData;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider;
import org.junit.BeforeClass;
import org.junit.Test;

import java.nio.charset.Charset;
import java.security.Security;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import static org.junit.Assert.*;

public class DecryptGoogleTrafficTest {

    @BeforeClass
    public static void loadTlsModuleAndSetupBouncyCastle() {
        SniffyConfiguration.INSTANCE.setDecryptTls(true);
        SniffyConfiguration.INSTANCE.setMonitorSocket(true);
        SniffyConfiguration.INSTANCE.setMonitorNio(true);
        Sniffy.initialize();

        Security.insertProviderAt(new BouncyCastleProvider(), 1);
        Security.insertProviderAt(new BouncyCastleJsseProvider(), 1);
    }

    @SuppressWarnings("CharsetObjectCanBeUsed")
    @Test
    public void testGoogleTraffic() throws Exception {

        try (Spy<?> spy = Sniffy.spy(SpyConfiguration.builder().captureNetworkTraffic(true).captureStackTraces(true).build())) {

            try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) {
                httpclient.start();
                HttpGet request = new HttpGet("https://www.google.com");
                Future<HttpResponse> future = httpclient.execute(request, null);
                HttpResponse response = future.get();
                assertEquals(200, response.getStatusLine().getStatusCode());
            }

            Map<SocketMetaData, List<NetworkPacket>> decryptedNetworkTraffic = spy.getDecryptedNetworkTraffic(
                    Threads.ANY,
                    AddressMatchers.exactAddressMatcher("www.google.com:443"),
                    GroupingOptions.builder().
                            groupByConnection(false).
                            groupByStackTrace(false).
                            groupByThread(false).
                            build()
            );

            assertEquals(1, decryptedNetworkTraffic.size());

            Map.Entry<SocketMetaData, List<NetworkPacket>> entry = decryptedNetworkTraffic.entrySet().iterator().next();

            assertNotNull(entry);
            assertNotNull(entry.getKey());
            assertNotNull(entry.getValue());

            assertEquals(2, entry.getValue().size());

            NetworkPacket request = entry.getValue().get(0);
            NetworkPacket response = entry.getValue().get(1);

            //noinspection SimplifiableAssertion
            assertEquals(true, request.isSent());
            //noinspection SimplifiableAssertion
            assertEquals(false, response.isSent());

            assertTrue(new String(request.getBytes(), Charset.forName("US-ASCII")).contains("Host: www.google.com"));
            assertTrue(new String(response.getBytes(), Charset.forName("US-ASCII")).contains("200"));

        }

    }

}

@@ -1364,21 +1364,16 @@
         
-try (
 Spy%3C?%3E s
@@ -1480,24 +1480,19 @@
 d())
-) %7B%0A%0A   
+;%0A%0A
         
  try
@@ -1491,14 +1491,8 @@
     
- try (
 Clos
@@ -1561,22 +1561,18 @@
 lt()
-) %7B%0A  
+;%0A
         
     
@@ -1567,22 +1567,16 @@
         
-      
 httpclie
@@ -1591,32 +1591,24 @@
 ();%0A        
-      
-  
 HttpGet requ
@@ -1652,24 +1652,16 @@
 .com%22);%0A
-        
         
@@ -1733,24 +1733,16 @@
         
-  
-      
 HttpResp
@@ -1771,24 +1771,16 @@
 .get();%0A
-        
         
@@ -1848,27 +1848,37 @@
         
-    %7D%0A%0A    
+httpclient.close();%0A%0A
         
@@ -1992,20 +1992,16 @@
         
-    
 Threads.
@@ -2021,20 +2021,16 @@
         
- 
-   
 AddressM
@@ -2096,20 +2096,16 @@
         
-   
- 
 Grouping
@@ -2143,28 +2143,24 @@
             
-    
 groupByConne
@@ -2193,28 +2193,24 @@
             
- 
-   
 groupByStack
@@ -2215,36 +2215,32 @@
 ckTrace(false).%0A
-    
                 
@@ -2293,20 +2293,16 @@
         
-    
 build()%0A
@@ -2313,20 +2313,12 @@
     
- 
-   
 );%0A%0A
-    
     
@@ -2363,36 +2363,32 @@
 affic.size());%0A%0A
-    
         Map.Entr
@@ -2489,36 +2489,32 @@
 ext();%0A%0A        
- 
-   
 assertNotNull(en
@@ -2519,20 +2519,16 @@
 entry);%0A
-    
         
@@ -2554,28 +2554,24 @@
 .getKey());%0A
-    
         asse
@@ -2600,36 +2600,32 @@
 ue());%0A%0A        
-    
 assertEquals(2, 
@@ -2647,28 +2647,24 @@
 ).size());%0A%0A
-    
         Netw
@@ -2708,20 +2708,16 @@
 get(0);%0A
-    
         
@@ -2767,36 +2767,32 @@
 et(1);%0A%0A        
-    
 //noinspection S
@@ -2812,36 +2812,32 @@
 sertion%0A        
-  
-  
 assertEquals(tru
@@ -2854,28 +2854,24 @@
 .isSent());%0A
-    
         //no
@@ -2907,28 +2907,24 @@
 ion%0A        
-    
 assertEquals
@@ -2948,28 +2948,24 @@
 isSent());%0A%0A
-    
         asse
@@ -3062,28 +3062,24 @@
 gle.com%22));%0A
-    
         asse
@@ -3169,19 +3169,8 @@
 );%0A%0A
-        %7D%0A%0A
     

Test SSL traffic capture with NIO and BouncyCastle in compatibility tests