| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "net/url_request/url_request_http_job.h" | 5 #include "net/url_request/url_request_http_job.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <cstddef> | 9 #include <cstddef> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include "net/url_request/url_request.h" | 33 #include "net/url_request/url_request.h" |
| 34 #include "net/url_request/url_request_job_factory_impl.h" | 34 #include "net/url_request/url_request_job_factory_impl.h" |
| 35 #include "net/url_request/url_request_status.h" | 35 #include "net/url_request/url_request_status.h" |
| 36 #include "net/url_request/url_request_test_util.h" | 36 #include "net/url_request/url_request_test_util.h" |
| 37 #include "net/websockets/websocket_handshake_stream_base.h" | 37 #include "net/websockets/websocket_handshake_stream_base.h" |
| 38 #include "testing/gmock/include/gmock/gmock.h" | 38 #include "testing/gmock/include/gmock/gmock.h" |
| 39 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 40 #include "url/gurl.h" | 40 #include "url/gurl.h" |
| 41 #include "url/url_constants.h" | 41 #include "url/url_constants.h" |
| 42 | 42 |
| 43 #if defined(OS_ANDROID) |
| 44 #include "base/android/build_info.h" |
| 45 #include "base/android/jni_android.h" |
| 46 #include "jni/AndroidNetworkLibraryTestUtil_jni.h" |
| 47 #endif |
| 48 |
| 43 using net::test::IsError; | 49 using net::test::IsError; |
| 44 using net::test::IsOk; | 50 using net::test::IsOk; |
| 45 | 51 |
| 46 namespace net { | 52 namespace net { |
| 47 | 53 |
| 48 namespace { | 54 namespace { |
| 49 | 55 |
| 50 using ::testing::Return; | 56 using ::testing::Return; |
| 51 | 57 |
| 52 const char kSimpleGetMockWrite[] = | 58 const char kSimpleGetMockWrite[] = |
| (...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 base::RunLoop().RunUntilIdle(); | 937 base::RunLoop().RunUntilIdle(); |
| 932 | 938 |
| 933 EXPECT_THAT(delegate.request_status(), IsOk()); | 939 EXPECT_THAT(delegate.request_status(), IsOk()); |
| 934 EXPECT_EQ(12, request->received_response_content_length()); | 940 EXPECT_EQ(12, request->received_response_content_length()); |
| 935 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), | 941 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 936 request->GetTotalSentBytes()); | 942 request->GetTotalSentBytes()); |
| 937 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), | 943 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), |
| 938 request->GetTotalReceivedBytes()); | 944 request->GetTotalReceivedBytes()); |
| 939 } | 945 } |
| 940 | 946 |
| 947 #if defined(OS_ANDROID) |
| 948 TEST_F(URLRequestHttpJobTest, AndroidCleartextPermittedTest) { |
| 949 context_.set_check_cleartext_permitted(true); |
| 950 |
| 951 struct TestCase { |
| 952 const char* url; |
| 953 bool cleartext_permitted; |
| 954 bool should_block; |
| 955 } cases[] = { |
| 956 {"http://blocked.test/", true, false}, |
| 957 {"https://blocked.test/", true, false}, |
| 958 {"http://blocked.test/", false, true}, |
| 959 {"https://blocked.test/", false, false}, |
| 960 }; |
| 961 |
| 962 for (const TestCase& test : cases) { |
| 963 JNIEnv* env = base::android::AttachCurrentThread(); |
| 964 Java_AndroidNetworkLibraryTestUtil_setUpSecurityPolicyForTesting( |
| 965 env, test.cleartext_permitted); |
| 966 |
| 967 TestDelegate delegate; |
| 968 std::unique_ptr<URLRequest> request = |
| 969 context_.CreateRequest(GURL(test.url), DEFAULT_PRIORITY, &delegate); |
| 970 request->Start(); |
| 971 base::RunLoop().Run(); |
| 972 |
| 973 int sdk_int = base::android::BuildInfo::GetInstance()->sdk_int(); |
| 974 bool expect_blocked = (sdk_int >= base::android::SDK_VERSION_MARSHMALLOW && |
| 975 test.should_block); |
| 976 if (expect_blocked) { |
| 977 EXPECT_THAT(delegate.request_status(), |
| 978 IsError(ERR_CLEARTEXT_NOT_PERMITTED)); |
| 979 } else { |
| 980 // Should fail since there's no test server running |
| 981 EXPECT_THAT(delegate.request_status(), IsError(ERR_FAILED)); |
| 982 } |
| 983 } |
| 984 } |
| 985 #endif |
| 986 |
| 941 // This base class just serves to set up some things before the TestURLRequest | 987 // This base class just serves to set up some things before the TestURLRequest |
| 942 // constructor is called. | 988 // constructor is called. |
| 943 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test { | 989 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test { |
| 944 protected: | 990 protected: |
| 945 URLRequestHttpJobWebSocketTestBase() : socket_data_(nullptr, 0, nullptr, 0), | 991 URLRequestHttpJobWebSocketTestBase() : socket_data_(nullptr, 0, nullptr, 0), |
| 946 context_(true) { | 992 context_(true) { |
| 947 // A Network Delegate is required for the WebSocketHandshakeStreamBase | 993 // A Network Delegate is required for the WebSocketHandshakeStreamBase |
| 948 // object to be passed on to the HttpNetworkTransaction. | 994 // object to be passed on to the HttpNetworkTransaction. |
| 949 context_.set_network_delegate(&network_delegate_); | 995 context_.set_network_delegate(&network_delegate_); |
| 950 | 996 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1096 base::RunLoop().RunUntilIdle(); | 1142 base::RunLoop().RunUntilIdle(); |
| 1097 EXPECT_THAT(delegate_.request_status(), IsError(ERR_IO_PENDING)); | 1143 EXPECT_THAT(delegate_.request_status(), IsError(ERR_IO_PENDING)); |
| 1098 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 1144 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
| 1099 } | 1145 } |
| 1100 | 1146 |
| 1101 #endif // BUILDFLAG(ENABLE_WEBSOCKETS) | 1147 #endif // BUILDFLAG(ENABLE_WEBSOCKETS) |
| 1102 | 1148 |
| 1103 } // namespace | 1149 } // namespace |
| 1104 | 1150 |
| 1105 } // namespace net | 1151 } // namespace net |
| OLD | NEW |