Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import static org.chromium.base.CollectionUtil.newHashSet; | 7 import static org.chromium.base.CollectionUtil.newHashSet; |
| 8 | 8 |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.ContextWrapper; | 10 import android.content.ContextWrapper; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 public class CronetUrlRequestContextTest extends CronetTestBase { | 38 public class CronetUrlRequestContextTest extends CronetTestBase { |
| 39 // URLs used for tests. | 39 // URLs used for tests. |
| 40 private static final String MOCK_CRONET_TEST_FAILED_URL = | 40 private static final String MOCK_CRONET_TEST_FAILED_URL = |
| 41 "http://mock.failed.request/-2"; | 41 "http://mock.failed.request/-2"; |
| 42 private static final String MOCK_CRONET_TEST_SUCCESS_URL = | 42 private static final String MOCK_CRONET_TEST_SUCCESS_URL = |
| 43 "http://mock.http/success.txt"; | 43 "http://mock.http/success.txt"; |
| 44 | 44 |
| 45 private EmbeddedTestServer mTestServer; | 45 private EmbeddedTestServer mTestServer; |
| 46 private String mUrl; | 46 private String mUrl; |
| 47 private String mUrl404; | 47 private String mUrl404; |
| 48 private String mUrl500; | |
| 48 CronetTestFramework mTestFramework; | 49 CronetTestFramework mTestFramework; |
| 49 | 50 |
| 50 @Override | 51 @Override |
| 51 protected void setUp() throws Exception { | 52 protected void setUp() throws Exception { |
| 52 super.setUp(); | 53 super.setUp(); |
| 53 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext( )); | 54 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext( )); |
| 54 mUrl = mTestServer.getURL("/echo?status=200"); | 55 mUrl = mTestServer.getURL("/echo?status=200"); |
| 55 mUrl404 = mTestServer.getURL("/echo?status=404"); | 56 mUrl404 = mTestServer.getURL("/echo?status=404"); |
| 57 mUrl500 = mTestServer.getURL("/echo?status=500"); | |
| 56 } | 58 } |
| 57 | 59 |
| 58 @Override | 60 @Override |
| 59 protected void tearDown() throws Exception { | 61 protected void tearDown() throws Exception { |
| 60 mTestServer.stopAndDestroyServer(); | 62 mTestServer.stopAndDestroyServer(); |
| 61 super.tearDown(); | 63 super.tearDown(); |
| 62 } | 64 } |
| 63 | 65 |
| 64 static class RequestThread extends Thread { | 66 static class RequestThread extends Thread { |
| 65 public TestUrlRequestCallback mCallback; | 67 public TestUrlRequestCallback mCallback; |
| (...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 cronetEngine.stopNetLog(); | 615 cronetEngine.stopNetLog(); |
| 614 assertTrue(file.exists()); | 616 assertTrue(file.exists()); |
| 615 assertTrue(file.length() != 0); | 617 assertTrue(file.length() != 0); |
| 616 assertFalse(hasBytesInNetLog(file)); | 618 assertFalse(hasBytesInNetLog(file)); |
| 617 assertTrue(file.delete()); | 619 assertTrue(file.delete()); |
| 618 assertTrue(!file.exists()); | 620 assertTrue(!file.exists()); |
| 619 } | 621 } |
| 620 | 622 |
| 621 @SmallTest | 623 @SmallTest |
| 622 @Feature({"Cronet"}) | 624 @Feature({"Cronet"}) |
| 625 @OnlyRunNativeCronet | |
| 626 // Tests that NetLog contains events emitted by all live CronetEngines. | |
| 627 public void testNetLogContainEventsFromAllLiveEngines() throws Exception { | |
| 628 Context context = getContext(); | |
| 629 File directory = new File(PathUtils.getDataDirectory(context)); | |
| 630 File file1 = File.createTempFile("cronet1", "json", directory); | |
| 631 File file2 = File.createTempFile("cronet2", "json", directory); | |
| 632 CronetEngine cronetEngine1 = new CronetUrlRequestContext( | |
| 633 new CronetEngine.Builder(context).setLibraryName("cronet_tests") ); | |
| 634 CronetEngine cronetEngine2 = new CronetUrlRequestContext( | |
| 635 new CronetEngine.Builder(context).setLibraryName("cronet_tests") ); | |
| 636 | |
| 637 cronetEngine1.startNetLogToFile(file1.getPath(), false); | |
| 638 cronetEngine2.startNetLogToFile(file2.getPath(), false); | |
| 639 | |
| 640 // Warm CronetEngine and make sure both CronetUrlRequestContexts are | |
| 641 // initialized before testing the logs. | |
| 642 makeRequestAndCheckStatus(cronetEngine1, mUrl, 200); | |
| 643 makeRequestAndCheckStatus(cronetEngine2, mUrl, 200); | |
| 644 | |
| 645 // Use cronetEngine1 to make a request to mUrl404. | |
| 646 makeRequestAndCheckStatus(cronetEngine1, mUrl404, 404); | |
| 647 | |
| 648 // Use cronetEngine2 to make a request to mUrl500. | |
| 649 makeRequestAndCheckStatus(cronetEngine2, mUrl500, 500); | |
| 650 | |
| 651 cronetEngine1.stopNetLog(); | |
| 652 cronetEngine2.stopNetLog(); | |
| 653 assertTrue(file1.exists()); | |
| 654 assertTrue(file2.exists()); | |
| 655 // Make sure both files contain the two requests made separately using | |
| 656 // different engines. | |
| 657 assertTrue(containsStringInNetLog(file1, mUrl404)); | |
| 658 assertTrue(containsStringInNetLog(file1, mUrl500)); | |
| 659 assertTrue(containsStringInNetLog(file2, mUrl404)); | |
| 660 assertTrue(containsStringInNetLog(file2, mUrl500)); | |
| 661 assertTrue(file1.delete()); | |
| 662 assertTrue(file2.delete()); | |
| 663 } | |
| 664 | |
| 665 @SmallTest | |
| 666 @Feature({"Cronet"}) | |
| 667 @OnlyRunNativeCronet | |
| 668 // Tests that if CronetEngine is shut down when reading from disk cache, | |
| 669 // there isn't a crash. See crbug.com/486120. | |
| 670 public void testShutDownEngineWhenReadingFromDiskCache() throws Exception { | |
| 671 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK); | |
| 672 String url = NativeTestServer.getFileURL("/cacheable.txt"); | |
| 673 // Make a request to a cacheable resource. | |
| 674 checkRequestCaching(url, false); | |
| 675 | |
| 676 // Shut down the server. | |
| 677 NativeTestServer.shutdownNativeTestServer(); | |
| 678 class CancelUrlRequestCallback extends TestUrlRequestCallback { | |
| 679 @Override | |
| 680 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) { | |
| 681 super.onResponseStarted(request, info); | |
| 682 request.cancel(); | |
|
mmenke
2016/04/07 19:55:13
Can we shut down the engine here?
Also, if you re
xunjieli
2016/04/07 21:34:01
Done.
| |
| 683 } | |
| 684 | |
| 685 @Override | |
| 686 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | |
| 687 throw new RuntimeException("Unexpected"); | |
|
mmenke
2016/04/07 19:55:13
May want to explain why these aren't racy (Namely,
xunjieli
2016/04/07 21:34:01
Done.
| |
| 688 } | |
| 689 | |
| 690 @Override | |
| 691 public void onFailed( | |
| 692 UrlRequest request, UrlResponseInfo info, UrlRequestExceptio n error) { | |
| 693 throw new RuntimeException("Unexpected"); | |
| 694 } | |
| 695 } | |
| 696 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); | |
| 697 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | |
| 698 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine); | |
| 699 urlRequestBuilder.build().start(); | |
| 700 callback.blockForDone(); | |
| 701 // Shut down CronetEngine immediately after request is destroyed. | |
| 702 mTestFramework.mCronetEngine.shutdown(); | |
| 703 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | |
| 704 assertTrue(callback.mResponseInfo.wasCached()); | |
| 705 assertTrue(callback.mOnCanceledCalled); | |
| 706 } | |
| 707 | |
| 708 @SmallTest | |
| 709 @Feature({"Cronet"}) | |
| 710 @OnlyRunNativeCronet | |
| 623 public void testNetLogAfterShutdown() throws Exception { | 711 public void testNetLogAfterShutdown() throws Exception { |
| 624 mTestFramework = startCronetTestFramework(); | 712 mTestFramework = startCronetTestFramework(); |
| 625 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 713 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 626 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 714 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 627 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); | 715 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); |
| 628 urlRequestBuilder.build().start(); | 716 urlRequestBuilder.build().start(); |
| 629 callback.blockForDone(); | 717 callback.blockForDone(); |
| 630 mTestFramework.mCronetEngine.shutdown(); | 718 mTestFramework.mCronetEngine.shutdown(); |
| 631 | 719 |
| 632 File directory = new File(PathUtils.getDataDirectory(getContext())); | 720 File directory = new File(PathUtils.getDataDirectory(getContext())); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 712 callback.blockForDone(); | 800 callback.blockForDone(); |
| 713 cronetEngine.stopNetLog(); | 801 cronetEngine.stopNetLog(); |
| 714 assertTrue(file.exists()); | 802 assertTrue(file.exists()); |
| 715 assertTrue(file.length() != 0); | 803 assertTrue(file.length() != 0); |
| 716 assertTrue(hasBytesInNetLog(file)); | 804 assertTrue(hasBytesInNetLog(file)); |
| 717 assertTrue(file.delete()); | 805 assertTrue(file.delete()); |
| 718 assertTrue(!file.exists()); | 806 assertTrue(!file.exists()); |
| 719 } | 807 } |
| 720 | 808 |
| 721 private boolean hasBytesInNetLog(File logFile) throws Exception { | 809 private boolean hasBytesInNetLog(File logFile) throws Exception { |
| 810 return containsStringInNetLog(logFile, "\"hex_encoded_bytes\""); | |
| 811 } | |
| 812 | |
| 813 private boolean containsStringInNetLog(File logFile, String content) throws Exception { | |
| 722 BufferedReader logReader = new BufferedReader(new FileReader(logFile)); | 814 BufferedReader logReader = new BufferedReader(new FileReader(logFile)); |
| 723 try { | 815 try { |
| 724 String logLine; | 816 String logLine; |
| 725 while ((logLine = logReader.readLine()) != null) { | 817 while ((logLine = logReader.readLine()) != null) { |
| 726 if (logLine.contains("\"hex_encoded_bytes\"")) { | 818 if (logLine.contains(content)) { |
| 727 return true; | 819 return true; |
| 728 } | 820 } |
| 729 } | 821 } |
| 730 return false; | 822 return false; |
| 731 } finally { | 823 } finally { |
| 732 logReader.close(); | 824 logReader.close(); |
| 733 } | 825 } |
| 734 } | 826 } |
| 735 | 827 |
| 828 /** | |
| 829 * Helper method to make a request to {@code url}, wait for it to | |
| 830 * complete, and check that the status code is the same as {@code expectedSt atusCode}. | |
| 831 */ | |
| 832 private void makeRequestAndCheckStatus( | |
| 833 CronetEngine engine, String url, int expectedStatusCode) { | |
| 834 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | |
| 835 UrlRequest request = | |
| 836 new UrlRequest.Builder(url, callback, callback.getExecutor(), en gine).build(); | |
| 837 request.start(); | |
| 838 callback.blockForDone(); | |
| 839 assertEquals(expectedStatusCode, callback.mResponseInfo.getHttpStatusCod e()); | |
| 840 } | |
| 841 | |
| 736 private void enableCache(int cacheType) throws Exception { | 842 private void enableCache(int cacheType) throws Exception { |
| 737 String cacheTypeString = ""; | 843 String cacheTypeString = ""; |
| 738 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { | 844 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { |
| 739 cacheTypeString = CronetTestFramework.CACHE_DISK; | 845 cacheTypeString = CronetTestFramework.CACHE_DISK; |
| 740 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { | 846 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { |
| 741 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP; | 847 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP; |
| 742 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) { | 848 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) { |
| 743 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY; | 849 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY; |
| 744 } | 850 } |
| 745 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri ng}; | 851 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri ng}; |
| 746 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandLineArgs); | 852 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandLineArgs); |
| 747 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 853 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 748 } | 854 } |
| 749 | 855 |
| 750 private void checkRequestCaching(String url, boolean expectCached) { | 856 private void checkRequestCaching(String url, boolean expectCached) { |
| 751 checkRequestCaching(url, expectCached, false); | 857 checkRequestCaching(url, expectCached, false); |
| 752 } | 858 } |
| 753 | 859 |
| 754 private void checkRequestCaching(String url, boolean expectCached, | 860 private void checkRequestCaching(String url, boolean expectCached, |
| 755 boolean disableCache) { | 861 boolean disableCache) { |
| 756 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 862 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 757 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 863 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 758 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine); | 864 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine); |
| 759 if (disableCache) { | 865 if (disableCache) { |
| 760 urlRequestBuilder.disableCache(); | 866 urlRequestBuilder.disableCache(); |
| 761 } | 867 } |
| 762 urlRequestBuilder.build().start(); | 868 urlRequestBuilder.build().start(); |
| 763 callback.blockForDone(); | 869 callback.blockForDone(); |
| 764 assertEquals(expectCached, callback.mResponseInfo.wasCached()); | 870 assertEquals(expectCached, callback.mResponseInfo.wasCached()); |
| 871 assertEquals("this is a cacheable file\n", callback.mResponseAsString); | |
| 765 } | 872 } |
| 766 | 873 |
| 767 @SmallTest | 874 @SmallTest |
| 768 @Feature({"Cronet"}) | 875 @Feature({"Cronet"}) |
| 769 @OnlyRunNativeCronet | 876 @OnlyRunNativeCronet |
| 770 public void testEnableHttpCacheDisabled() throws Exception { | 877 public void testEnableHttpCacheDisabled() throws Exception { |
| 771 enableCache(CronetEngine.Builder.HTTP_CACHE_DISABLED); | 878 enableCache(CronetEngine.Builder.HTTP_CACHE_DISABLED); |
| 772 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 879 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 773 checkRequestCaching(url, false); | 880 checkRequestCaching(url, false); |
| 774 checkRequestCaching(url, false); | 881 checkRequestCaching(url, false); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1013 try { | 1120 try { |
| 1014 // ensureInitialized() calls native code to check the version right after library load | 1121 // ensureInitialized() calls native code to check the version right after library load |
| 1015 // and will error with the message below if library loading was skip ped | 1122 // and will error with the message below if library loading was skip ped |
| 1016 CronetLibraryLoader.ensureInitialized(getContext(), builder); | 1123 CronetLibraryLoader.ensureInitialized(getContext(), builder); |
| 1017 fail("Native library should not be loaded"); | 1124 fail("Native library should not be loaded"); |
| 1018 } catch (UnsatisfiedLinkError e) { | 1125 } catch (UnsatisfiedLinkError e) { |
| 1019 assertTrue(loader.wasCalled()); | 1126 assertTrue(loader.wasCalled()); |
| 1020 } | 1127 } |
| 1021 } | 1128 } |
| 1022 } | 1129 } |
| OLD | NEW |