tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TCPChannelClientTest.java (5448B)


      1 /*
      2 *  Copyright 2016 The WebRTC Project Authors. All rights reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 package org.appspot.apprtc;
     12 
     13 import static org.junit.Assert.fail;
     14 import static org.mockito.Mockito.timeout;
     15 import static org.mockito.Mockito.verify;
     16 import static org.mockito.Mockito.verifyNoMoreInteractions;
     17 
     18 import org.junit.After;
     19 import org.junit.Before;
     20 import org.junit.Ignore;
     21 import org.junit.Test;
     22 import org.junit.runner.RunWith;
     23 import org.mockito.Mock;
     24 import org.mockito.MockitoAnnotations;
     25 import org.robolectric.annotation.Config;
     26 import org.robolectric.shadows.ShadowLog;
     27 import org.robolectric.RobolectricTestRunner;
     28 
     29 import java.util.concurrent.ExecutorService;
     30 import java.util.concurrent.Executors;
     31 import java.util.concurrent.TimeUnit;
     32 
     33 @RunWith(RobolectricTestRunner.class)
     34 @Config(manifest = Config.NONE)
     35 public class TCPChannelClientTest {
     36  private static final int PORT = 8888;
     37  /**
     38   * How long we wait before trying to connect to the server. Note: was
     39   * previously only 10, which was too short (tests were flaky).
     40   */
     41  private static final int SERVER_WAIT = 300;
     42  private static final int CONNECT_TIMEOUT = 1000;
     43  private static final int SEND_TIMEOUT = 1000;
     44  private static final int DISCONNECT_TIMEOUT = 1000;
     45  private static final int TERMINATION_TIMEOUT = 1000;
     46  private static final String TEST_MESSAGE_SERVER = "Hello, Server!";
     47  private static final String TEST_MESSAGE_CLIENT = "Hello, Client!";
     48 
     49  @Mock TCPChannelClient.TCPChannelEvents serverEvents;
     50  @Mock TCPChannelClient.TCPChannelEvents clientEvents;
     51 
     52  private ExecutorService executor;
     53  private TCPChannelClient server;
     54  private TCPChannelClient client;
     55 
     56  @Before
     57  public void setUp() {
     58    ShadowLog.stream = System.out;
     59 
     60    MockitoAnnotations.initMocks(this);
     61 
     62    executor = Executors.newSingleThreadExecutor();
     63  }
     64 
     65  @After
     66  public void tearDown() {
     67    verifyNoMoreEvents();
     68 
     69    executeAndWait(new Runnable() {
     70      @Override
     71      public void run() {
     72        client.disconnect();
     73        server.disconnect();
     74      }
     75    });
     76 
     77    // Stop the executor thread
     78    executor.shutdown();
     79    try {
     80      executor.awaitTermination(TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS);
     81    } catch (InterruptedException e) {
     82      fail(e.getMessage());
     83    }
     84  }
     85 
     86  @Test
     87  public void testConnectIPv4() {
     88    setUpIPv4Server();
     89    try {
     90      Thread.sleep(SERVER_WAIT);
     91    } catch (InterruptedException e) {
     92      fail(e.getMessage());
     93    }
     94    setUpIPv4Client();
     95 
     96    verify(serverEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(true);
     97    verify(clientEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(false);
     98  }
     99 
    100  // TODO:b/389829614 - Below test is failing.
    101  @Test
    102  @Ignore
    103  public void testConnectIPv6() {
    104    setUpIPv6Server();
    105    try {
    106      Thread.sleep(SERVER_WAIT);
    107    } catch (InterruptedException e) {
    108      fail(e.getMessage());
    109    }
    110    setUpIPv6Client();
    111 
    112    verify(serverEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(true);
    113    verify(clientEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(false);
    114  }
    115 
    116  @Test
    117  public void testSendData() {
    118    testConnectIPv4();
    119 
    120    executeAndWait(new Runnable() {
    121      @Override
    122      public void run() {
    123        client.send(TEST_MESSAGE_SERVER);
    124        server.send(TEST_MESSAGE_CLIENT);
    125      }
    126    });
    127 
    128    verify(serverEvents, timeout(SEND_TIMEOUT)).onTCPMessage(TEST_MESSAGE_SERVER);
    129    verify(clientEvents, timeout(SEND_TIMEOUT)).onTCPMessage(TEST_MESSAGE_CLIENT);
    130  }
    131 
    132  @Test
    133  public void testDisconnectServer() {
    134    testConnectIPv4();
    135    executeAndWait(new Runnable() {
    136      @Override
    137      public void run() {
    138        server.disconnect();
    139      }
    140    });
    141 
    142    verify(serverEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose();
    143    verify(clientEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose();
    144  }
    145 
    146  @Test
    147  public void testDisconnectClient() {
    148    testConnectIPv4();
    149    executeAndWait(new Runnable() {
    150      @Override
    151      public void run() {
    152        client.disconnect();
    153      }
    154    });
    155 
    156    verify(serverEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose();
    157    verify(clientEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose();
    158  }
    159 
    160  private void setUpIPv4Server() {
    161    setUpServer("0.0.0.0", PORT);
    162  }
    163 
    164  private void setUpIPv4Client() {
    165    setUpClient("127.0.0.1", PORT);
    166  }
    167 
    168  private void setUpIPv6Server() {
    169    setUpServer("::", PORT);
    170  }
    171 
    172  private void setUpIPv6Client() {
    173    setUpClient("::1", PORT);
    174  }
    175 
    176  private void setUpServer(String ip, int port) {
    177    server = new TCPChannelClient(executor, serverEvents, ip, port);
    178  }
    179 
    180  private void setUpClient(String ip, int port) {
    181    client = new TCPChannelClient(executor, clientEvents, ip, port);
    182  }
    183 
    184  /**
    185   * Verifies no more server or client events have been issued
    186   */
    187  private void verifyNoMoreEvents() {
    188    verifyNoMoreInteractions(serverEvents);
    189    verifyNoMoreInteractions(clientEvents);
    190  }
    191 
    192  /**
    193   * Queues runnable to be run and waits for it to be executed by the executor thread
    194   */
    195  public void executeAndWait(Runnable runnable) {
    196    try {
    197      executor.submit(runnable).get();
    198    } catch (Exception e) {
    199      fail(e.getMessage());
    200    }
    201  }
    202 }