@SmallTest
 @Feature({"Cronet"})
 // Tests that creating a ByteBufferUploadProvider using a byte array with an
 // offset gives a ByteBuffer with position 0. crbug.com/603124.
 public void testCreateByteBufferUploadWithArrayOffset() throws Exception {
   TestUrlRequestCallback callback = new TestUrlRequestCallback();
   // This URL will trigger a rewind().
   UrlRequest.Builder builder =
       new UrlRequest.Builder(
           NativeTestServer.getRedirectToEchoBody(),
           callback,
           callback.getExecutor(),
           mTestFramework.mCronetEngine);
   builder.addHeader("Content-Type", "useless/string");
   byte[] uploadData = LOREM.getBytes("UTF-8");
   int offset = 5;
   byte[] uploadDataWithPadding = new byte[uploadData.length + offset];
   System.arraycopy(uploadData, 0, uploadDataWithPadding, offset, uploadData.length);
   UploadDataProvider dataProvider =
       UploadDataProviders.create(uploadDataWithPadding, offset, uploadData.length);
   assertEquals(uploadData.length, dataProvider.getLength());
   builder.setUploadDataProvider(dataProvider, callback.getExecutor());
   UrlRequest urlRequest = builder.build();
   urlRequest.start();
   callback.blockForDone();
   assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
   assertEquals(LOREM, callback.mResponseAsString);
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testDisableCache() throws Exception {
    enableCache(CronetEngine.Builder.HTTP_CACHE_DISK);
    String url = NativeTestServer.getFileURL("/cacheable.txt");

    // When cache is disabled, making a request does not write to the cache.
    checkRequestCaching(
        url, false, true
        /** disable cache */
        );
    checkRequestCaching(url, false);

    // When cache is enabled, the second request is cached.
    checkRequestCaching(
        url, false, true
        /** disable cache */
        );
    checkRequestCaching(url, true);

    // Shut down the server, next request should have a cached response.
    NativeTestServer.shutdownNativeTestServer();
    checkRequestCaching(url, true);

    // Cache is disabled after server is shut down, request should fail.
    TestUrlRequestListener listener = new TestUrlRequestListener();
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(url, listener, listener.getExecutor(), mActivity.mCronetEngine);
    urlRequestBuilder.disableCache();
    urlRequestBuilder.build().start();
    listener.blockForDone();
    assertNotNull(listener.mError);
    assertEquals(
        "Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED", listener.mError.getMessage());
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testNetLogStopMultipleTimes() throws Exception {
   mActivity = launchCronetTestApp();
   File directory = new File(PathUtils.getDataDirectory(getInstrumentation().getTargetContext()));
   File file = File.createTempFile("cronet", "json", directory);
   mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
   // Start a request.
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
   urlRequestBuilder.build().start();
   listener.blockForDone();
   // Stop NetLog multiple times.
   mActivity.mCronetEngine.stopNetLog();
   mActivity.mCronetEngine.stopNetLog();
   mActivity.mCronetEngine.stopNetLog();
   mActivity.mCronetEngine.stopNetLog();
   mActivity.mCronetEngine.stopNetLog();
   assertTrue(file.exists());
   assertTrue(file.length() != 0);
   assertFalse(hasBytesInNetLog(file));
   assertTrue(file.delete());
   assertTrue(!file.exists());
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testNetLog() throws Exception {
    Context context = getInstrumentation().getTargetContext();
    File directory = new File(PathUtils.getDataDirectory(context));
    File file = File.createTempFile("cronet", "json", directory);
    CronetEngine cronetEngine =
        new CronetUrlRequestContext(
            new CronetEngine.Builder(context).setLibraryName("cronet_tests"));
    // Start NetLog immediately after the request context is created to make
    // sure that the call won't crash the app even when the native request
    // context is not fully initialized. See crbug.com/470196.
    cronetEngine.startNetLogToFile(file.getPath(), false);

    // Start a request.
    TestUrlRequestListener listener = new TestUrlRequestListener();
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), cronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();
    cronetEngine.stopNetLog();
    assertTrue(file.exists());
    assertTrue(file.length() != 0);
    assertFalse(hasBytesInNetLog(file));
    assertTrue(file.delete());
    assertTrue(!file.exists());
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testSimpleGet() throws Exception {
    String url = NativeTestServer.getEchoMethodURL();
    TestUrlRequestCallback callback = new TestUrlRequestCallback();
    callback.setAutoAdvance(false);
    UrlRequest.Builder builder =
        new UrlRequest.Builder(url, callback, callback.getExecutor(), mTestFramework.mCronetEngine);
    UrlRequest urlRequest = builder.build();
    // Calling before request is started should give Status.INVALID,
    // since the native adapter is not created.
    TestStatusListener statusListener0 = new TestStatusListener();
    urlRequest.getStatus(statusListener0);
    statusListener0.waitUntilOnStatusCalled();
    assertTrue(statusListener0.mOnStatusCalled);
    assertEquals(Status.INVALID, statusListener0.mStatus);

    urlRequest.start();

    // Should receive a valid status.
    TestStatusListener statusListener1 = new TestStatusListener();
    urlRequest.getStatus(statusListener1);
    statusListener1.waitUntilOnStatusCalled();
    assertTrue(statusListener1.mOnStatusCalled);
    assertTrue("Status is :" + statusListener1.mStatus, statusListener1.mStatus >= Status.IDLE);
    assertTrue(
        "Status is :" + statusListener1.mStatus,
        statusListener1.mStatus <= Status.READING_RESPONSE);

    callback.waitForNextStep();
    assertEquals(ResponseStep.ON_RESPONSE_STARTED, callback.mResponseStep);
    callback.startNextRead(urlRequest);

    // Should receive a valid status.
    TestStatusListener statusListener2 = new TestStatusListener();
    urlRequest.getStatus(statusListener2);
    statusListener2.waitUntilOnStatusCalled();
    assertTrue(statusListener2.mOnStatusCalled);
    assertTrue(statusListener1.mStatus >= Status.IDLE);
    assertTrue(statusListener1.mStatus <= Status.READING_RESPONSE);

    callback.waitForNextStep();
    assertEquals(ResponseStep.ON_READ_COMPLETED, callback.mResponseStep);

    callback.startNextRead(urlRequest);
    callback.blockForDone();

    // Calling after request done should give Status.INVALID, since
    // the native adapter is destroyed.
    TestStatusListener statusListener3 = new TestStatusListener();
    urlRequest.getStatus(statusListener3);
    statusListener3.waitUntilOnStatusCalled();
    assertTrue(statusListener3.mOnStatusCalled);
    assertEquals(Status.INVALID, statusListener3.mStatus);

    assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
    assertEquals("GET", callback.mResponseAsString);
  }
 @Override
 public void run() {
   mRunBlocker.block();
   CronetEngine cronetEngine = mActivity.initCronetEngine();
   mListener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(mUrl, mListener, mListener.getExecutor(), cronetEngine);
   urlRequestBuilder.build().start();
   mListener.blockForDone();
 }
 private void checkRequestCaching(String url, boolean expectCached, boolean disableCache) {
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(url, listener, listener.getExecutor(), mActivity.mCronetEngine);
   if (disableCache) {
     urlRequestBuilder.disableCache();
   }
   urlRequestBuilder.build().start();
   listener.blockForDone();
   assertEquals(expectCached, listener.mResponseInfo.wasCached());
 }
 @SmallTest
 @Feature({"Cronet"})
 public void testShutdownAfterError() throws Exception {
   mActivity = launchCronetTestApp();
   TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(
           MOCK_CRONET_TEST_FAILED_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
   urlRequestBuilder.build().start();
   listener.blockForDone();
   assertTrue(listener.mOnErrorCalled);
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testDataReductionProxyEnabled() throws Exception {
    mActivity = launchCronetTestAppAndSkipFactoryInit();

    // Ensure native code is loaded before trying to start test server.
    new CronetEngine.Builder(getInstrumentation().getTargetContext())
        .setLibraryName("cronet_tests")
        .build()
        .shutdown();

    assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().getTargetContext()));
    if (!NativeTestServer.isDataReductionProxySupported()) {
      return;
    }
    String serverHostPort = NativeTestServer.getHostPort();

    // Enable the Data Reduction Proxy and configure it to use the test
    // server as its primary proxy, and to check successfully that this
    // proxy is OK to use.
    CronetEngine.Builder cronetEngineBuilder =
        new CronetEngine.Builder(getInstrumentation().getTargetContext());
    cronetEngineBuilder.enableDataReductionProxy("test-key");
    cronetEngineBuilder.setDataReductionProxyOptions(
        serverHostPort,
        "unused.net:9999",
        NativeTestServer.getFileURL("/secureproxychecksuccess.txt"));
    cronetEngineBuilder.setLibraryName("cronet_tests");
    mActivity.mCronetEngine = cronetEngineBuilder.build();
    TestUrlRequestListener listener = new TestUrlRequestListener();

    // Construct and start a request that can only be returned by the test
    // server. This request will fail if the configuration logic for the
    // Data Reduction Proxy is not used.
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(
            "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
            listener,
            listener.getExecutor(),
            mActivity.mCronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();

    // Verify that the request is successful and that the Data Reduction
    // Proxy logic configured to use the test server as its proxy.
    assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
    assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer());
    assertEquals(
        "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
        listener.mResponseInfo.getUrl());
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testInitEngineAndStartRequest() {
    CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit();

    // Immediately make a request after initializing the engine.
    CronetEngine cronetEngine = activity.initCronetEngine();
    TestUrlRequestListener listener = new TestUrlRequestListener();
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), cronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();
    assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testFileProvider() throws Exception {
   TestUrlRequestCallback callback = new TestUrlRequestCallback();
   UrlRequest.Builder builder =
       new UrlRequest.Builder(
           NativeTestServer.getRedirectToEchoBody(),
           callback,
           callback.getExecutor(),
           mTestFramework.mCronetEngine);
   UploadDataProvider dataProvider = UploadDataProviders.create(mFile);
   builder.setUploadDataProvider(dataProvider, callback.getExecutor());
   builder.addHeader("Content-Type", "useless/string");
   builder.build().start();
   callback.blockForDone();
   assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
   assertEquals(LOREM, callback.mResponseAsString);
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testShutdown() throws Exception {
    mActivity = launchCronetTestApp();
    TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
    // Block listener when response starts to verify that shutdown fails
    // if there are active requests.
    listener.setAutoAdvance(false);
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
    UrlRequest urlRequest = urlRequestBuilder.build();
    urlRequest.start();
    try {
      mActivity.mCronetEngine.shutdown();
      fail("Should throw an exception");
    } catch (Exception e) {
      assertEquals("Cannot shutdown with active requests.", e.getMessage());
    }

    listener.waitForNextStep();
    assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep);
    try {
      mActivity.mCronetEngine.shutdown();
      fail("Should throw an exception");
    } catch (Exception e) {
      assertEquals("Cannot shutdown with active requests.", e.getMessage());
    }
    listener.startNextRead(urlRequest);

    listener.waitForNextStep();
    assertEquals(ResponseStep.ON_READ_COMPLETED, listener.mResponseStep);
    try {
      mActivity.mCronetEngine.shutdown();
      fail("Should throw an exception");
    } catch (Exception e) {
      assertEquals("Cannot shutdown with active requests.", e.getMessage());
    }

    // May not have read all the data, in theory. Just enable auto-advance
    // and finish the request.
    listener.setAutoAdvance(true);
    listener.startNextRead(urlRequest);
    listener.blockForDone();
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testInitEngineStartTwoRequests() throws Exception {
    CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit();

    // Make two requests after initializing the context.
    CronetEngine cronetEngine = activity.initCronetEngine();
    int[] statusCodes = {0, 0};
    String[] urls = {TEST_URL, URL_404};
    for (int i = 0; i < 2; i++) {
      TestUrlRequestListener listener = new TestUrlRequestListener();
      UrlRequest.Builder urlRequestBuilder =
          new UrlRequest.Builder(urls[i], listener, listener.getExecutor(), cronetEngine);
      urlRequestBuilder.build().start();
      listener.blockForDone();
      statusCodes[i] = listener.mResponseInfo.getHttpStatusCode();
    }
    assertEquals(200, statusCodes[0]);
    assertEquals(404, statusCodes[1]);
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testConfigUserAgent() throws Exception {
   String userAgentName = "User-Agent";
   String userAgentValue = "User-Agent-Value";
   CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(mActivity);
   cronetEngineBuilder.setUserAgent(userAgentValue);
   cronetEngineBuilder.setLibraryName("cronet_tests");
   String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, cronetEngineBuilder.toString()};
   mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL, commandLineArgs);
   assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().getTargetContext()));
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(
           NativeTestServer.getEchoHeaderURL(userAgentName),
           listener,
           listener.getExecutor(),
           mActivity.mCronetEngine);
   urlRequestBuilder.build().start();
   listener.blockForDone();
   assertEquals(userAgentValue, listener.mResponseAsString);
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testNoErrorWhenExceptionDuringStart() throws Exception {
    TestUrlRequestCallback callback = new TestUrlRequestCallback();
    UrlRequest.Builder builder =
        new UrlRequest.Builder(
            NativeTestServer.getEchoBodyURL(),
            callback,
            callback.getExecutor(),
            mTestFramework.mCronetEngine);
    final ConditionVariable first = new ConditionVariable();
    final String exceptionMessage = "Bad Length";
    builder.addHeader("Content-Type", "useless/string");
    builder.setUploadDataProvider(
        new UploadDataProvider() {
          @Override
          public long getLength() throws IOException {
            first.open();
            throw new IOException(exceptionMessage);
          }

          @Override
          public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer)
              throws IOException {}

          @Override
          public void rewind(UploadDataSink uploadDataSink) throws IOException {}
        },
        callback.getExecutor());
    UrlRequest urlRequest = builder.build();
    urlRequest.start();
    first.block();
    callback.blockForDone();
    assertFalse(callback.mOnCanceledCalled);
    assertEquals(UrlRequestError.LISTENER_EXCEPTION_THROWN, callback.mError.getErrorCode());
    assertEquals("Exception received from UploadDataProvider", callback.mError.getMessage());
    assertEquals(exceptionMessage, callback.mError.getCause().getMessage());
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testNoErrorWhenCanceledDuringStart() throws Exception {
    TestUrlRequestCallback callback = new TestUrlRequestCallback();
    UrlRequest.Builder builder =
        new UrlRequest.Builder(
            NativeTestServer.getEchoBodyURL(),
            callback,
            callback.getExecutor(),
            mTestFramework.mCronetEngine);
    final ConditionVariable first = new ConditionVariable();
    final ConditionVariable second = new ConditionVariable();
    builder.addHeader("Content-Type", "useless/string");
    builder.setUploadDataProvider(
        new UploadDataProvider() {
          @Override
          public long getLength() throws IOException {
            first.open();
            second.block();
            return 0;
          }

          @Override
          public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer)
              throws IOException {}

          @Override
          public void rewind(UploadDataSink uploadDataSink) throws IOException {}
        },
        callback.getExecutor());
    UrlRequest urlRequest = builder.build();
    urlRequest.start();
    first.block();
    urlRequest.cancel();
    second.open();
    callback.blockForDone();
    assertTrue(callback.mOnCanceledCalled);
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testNetLogAfterShutdown() throws Exception {
    mActivity = launchCronetTestApp();
    TestUrlRequestListener listener = new TestUrlRequestListener();
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();
    mActivity.mCronetEngine.shutdown();

    File directory = new File(PathUtils.getDataDirectory(getInstrumentation().getTargetContext()));
    File file = File.createTempFile("cronet", "json", directory);
    try {
      mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
      fail("Should throw an exception.");
    } catch (Exception e) {
      assertEquals("Engine is shut down.", e.getMessage());
    }
    assertFalse(hasBytesInNetLog(file));
    assertTrue(file.delete());
    assertTrue(!file.exists());
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testShutdownAfterCancel() throws Exception {
   mActivity = launchCronetTestApp();
   TestUrlRequestListener listener = new TestUrlRequestListener();
   // Block listener when response starts to verify that shutdown fails
   // if there are active requests.
   listener.setAutoAdvance(false);
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
   UrlRequest urlRequest = urlRequestBuilder.build();
   urlRequest.start();
   try {
     mActivity.mCronetEngine.shutdown();
     fail("Should throw an exception");
   } catch (Exception e) {
     assertEquals("Cannot shutdown with active requests.", e.getMessage());
   }
   listener.waitForNextStep();
   assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep);
   urlRequest.cancel();
   mActivity.mCronetEngine.shutdown();
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testBadFileDescriptorProvider() throws Exception {
    TestUrlRequestCallback callback = new TestUrlRequestCallback();
    UrlRequest.Builder builder =
        new UrlRequest.Builder(
            NativeTestServer.getRedirectToEchoBody(),
            callback,
            callback.getExecutor(),
            mTestFramework.mCronetEngine);
    ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
    try {
      UploadDataProvider dataProvider = UploadDataProviders.create(pipe[0]);
      builder.setUploadDataProvider(dataProvider, callback.getExecutor());
      builder.addHeader("Content-Type", "useless/string");
      builder.build().start();
      callback.blockForDone();

      assertTrue(callback.mError.getCause() instanceof IllegalArgumentException);
    } finally {
      pipe[1].close();
    }
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testNetLogWithBytes() throws Exception {
   Context context = getInstrumentation().getTargetContext();
   File directory = new File(PathUtils.getDataDirectory(context));
   File file = File.createTempFile("cronet", "json", directory);
   CronetEngine cronetEngine =
       new CronetUrlRequestContext(
           new CronetEngine.Builder(context).setLibraryName("cronet_tests"));
   // Start NetLog with logAll as true.
   cronetEngine.startNetLogToFile(file.getPath(), true);
   // Start a request.
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), cronetEngine);
   urlRequestBuilder.build().start();
   listener.blockForDone();
   cronetEngine.stopNetLog();
   assertTrue(file.exists());
   assertTrue(file.length() != 0);
   assertTrue(hasBytesInNetLog(file));
   assertTrue(file.delete());
   assertTrue(!file.exists());
 }