OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/safe_browsing/renderer/websocket_sb_handshake_throttle.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/callback.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/test/scoped_task_environment.h" |
| 13 #include "components/safe_browsing/common/safe_browsing.mojom.h" |
| 14 #include "content/public/common/resource_type.h" |
| 15 #include "ipc/ipc_message.h" |
| 16 #include "mojo/public/cpp/bindings/binding.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "third_party/WebKit/public/platform/WebString.h" |
| 20 #include "third_party/WebKit/public/platform/WebURL.h" |
| 21 |
| 22 namespace safe_browsing { |
| 23 |
| 24 namespace { |
| 25 |
| 26 constexpr char kTestUrl[] = "wss://test/"; |
| 27 |
| 28 class FakeSafeBrowsing : public mojom::SafeBrowsing { |
| 29 public: |
| 30 FakeSafeBrowsing() : render_frame_id_(), load_flags_(-1), resource_type_() {} |
| 31 |
| 32 void CreateCheckerAndCheck(int32_t render_frame_id, |
| 33 mojom::SafeBrowsingUrlCheckerRequest request, |
| 34 const GURL& url, |
| 35 int32_t load_flags, |
| 36 content::ResourceType resource_type, |
| 37 CreateCheckerAndCheckCallback callback) override { |
| 38 render_frame_id_ = render_frame_id; |
| 39 request_ = std::move(request); |
| 40 url_ = url; |
| 41 load_flags_ = load_flags; |
| 42 resource_type_ = resource_type; |
| 43 callback_ = std::move(callback); |
| 44 run_loop_.Quit(); |
| 45 } |
| 46 |
| 47 void RunUntilCalled() { run_loop_.Run(); } |
| 48 |
| 49 int32_t render_frame_id_; |
| 50 mojom::SafeBrowsingUrlCheckerRequest request_; |
| 51 GURL url_; |
| 52 int32_t load_flags_; |
| 53 content::ResourceType resource_type_; |
| 54 CreateCheckerAndCheckCallback callback_; |
| 55 base::RunLoop run_loop_; |
| 56 }; |
| 57 |
| 58 class FakeWebCallbacks |
| 59 : public blink::WebCallbacks<void, const blink::WebString&> { |
| 60 public: |
| 61 enum Result { RESULT_NOT_CALLED, RESULT_SUCCESS, RESULT_ERROR }; |
| 62 |
| 63 FakeWebCallbacks() : result_(RESULT_NOT_CALLED) {} |
| 64 |
| 65 void OnSuccess() override { |
| 66 result_ = RESULT_SUCCESS; |
| 67 run_loop_.Quit(); |
| 68 } |
| 69 |
| 70 void OnError(const blink::WebString& message) override { |
| 71 result_ = RESULT_ERROR; |
| 72 message_ = message; |
| 73 run_loop_.Quit(); |
| 74 } |
| 75 |
| 76 void RunUntilCalled() { run_loop_.Run(); } |
| 77 |
| 78 Result result_; |
| 79 blink::WebString message_; |
| 80 base::RunLoop run_loop_; |
| 81 }; |
| 82 |
| 83 class WebSocketSBHandshakeThrottleTest : public ::testing::Test { |
| 84 protected: |
| 85 WebSocketSBHandshakeThrottleTest() : mojo_binding_(&safe_browsing_) { |
| 86 mojo_binding_.Bind(mojo::MakeRequest(&safe_browsing_ptr_)); |
| 87 throttle_ = base::MakeUnique<WebSocketSBHandshakeThrottle>( |
| 88 safe_browsing_ptr_.get()); |
| 89 } |
| 90 |
| 91 base::test::ScopedTaskEnvironment message_loop_; |
| 92 FakeSafeBrowsing safe_browsing_; |
| 93 mojo::Binding<mojom::SafeBrowsing> mojo_binding_; |
| 94 mojom::SafeBrowsingPtr safe_browsing_ptr_; |
| 95 std::unique_ptr<WebSocketSBHandshakeThrottle> throttle_; |
| 96 FakeWebCallbacks fake_callbacks_; |
| 97 }; |
| 98 |
| 99 TEST_F(WebSocketSBHandshakeThrottleTest, Construction) {} |
| 100 |
| 101 TEST_F(WebSocketSBHandshakeThrottleTest, CheckArguments) { |
| 102 throttle_->ThrottleHandshake(GURL(kTestUrl), nullptr, &fake_callbacks_); |
| 103 safe_browsing_.RunUntilCalled(); |
| 104 // TODO(ricea): Find a way to create a WebLocalFrame in a unit test so that |
| 105 // the code that looks up the render_frame_id can be tested. |
| 106 EXPECT_EQ(MSG_ROUTING_NONE, safe_browsing_.render_frame_id_); |
| 107 EXPECT_EQ(GURL(kTestUrl), safe_browsing_.url_); |
| 108 EXPECT_EQ(0, safe_browsing_.load_flags_); |
| 109 EXPECT_EQ(content::RESOURCE_TYPE_SUB_RESOURCE, safe_browsing_.resource_type_); |
| 110 EXPECT_TRUE(safe_browsing_.callback_); |
| 111 } |
| 112 |
| 113 TEST_F(WebSocketSBHandshakeThrottleTest, Safe) { |
| 114 throttle_->ThrottleHandshake(GURL(kTestUrl), nullptr, &fake_callbacks_); |
| 115 safe_browsing_.RunUntilCalled(); |
| 116 std::move(safe_browsing_.callback_).Run(true); |
| 117 fake_callbacks_.RunUntilCalled(); |
| 118 EXPECT_EQ(FakeWebCallbacks::RESULT_SUCCESS, fake_callbacks_.result_); |
| 119 } |
| 120 |
| 121 TEST_F(WebSocketSBHandshakeThrottleTest, Unsafe) { |
| 122 throttle_->ThrottleHandshake(GURL(kTestUrl), nullptr, &fake_callbacks_); |
| 123 safe_browsing_.RunUntilCalled(); |
| 124 std::move(safe_browsing_.callback_).Run(false); |
| 125 fake_callbacks_.RunUntilCalled(); |
| 126 EXPECT_EQ(FakeWebCallbacks::RESULT_ERROR, fake_callbacks_.result_); |
| 127 EXPECT_EQ( |
| 128 blink::WebString( |
| 129 "WebSocket connection to wss://test/ failed safe browsing check"), |
| 130 fake_callbacks_.message_); |
| 131 } |
| 132 |
| 133 } // namespace |
| 134 |
| 135 } // namespace safe_browsing |
OLD | NEW |