Chromium Code Reviews| 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 { NOT_CALLED, SUCCESS, ERROR }; | |
| 62 | |
| 63 FakeWebCallbacks() : result_(NOT_CALLED) {} | |
| 64 | |
| 65 void OnSuccess() override { | |
| 66 result_ = SUCCESS; | |
| 67 run_loop_.Quit(); | |
| 68 } | |
| 69 | |
| 70 void OnError(const blink::WebString& message) override { | |
| 71 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() | |
| 86 : safe_browsing_(new FakeSafeBrowsing()), mojo_binding_(safe_browsing_) { | |
| 87 mojom::SafeBrowsingPtr safe_browsing_ptr; | |
| 88 mojo_binding_.Bind(mojo::MakeRequest(&safe_browsing_ptr)); | |
| 89 throttle_ = base::MakeUnique<WebSocketSBHandshakeThrottle>( | |
| 90 std::move(safe_browsing_ptr)); | |
| 91 } | |
| 92 | |
| 93 base::test::ScopedTaskEnvironment message_loop_; | |
| 94 FakeSafeBrowsing* safe_browsing_; | |
|
yzshen1
2017/06/20 19:52:19
There seems no need to use a raw pointer and leak.
Adam Rice
2017/06/21 06:08:36
Thanks! I was confused about the ownership semanti
| |
| 95 mojo::Binding<mojom::SafeBrowsing> mojo_binding_; | |
| 96 std::unique_ptr<WebSocketSBHandshakeThrottle> throttle_; | |
| 97 FakeWebCallbacks fake_callbacks_; | |
| 98 }; | |
| 99 | |
| 100 TEST_F(WebSocketSBHandshakeThrottleTest, Construction) {} | |
| 101 | |
| 102 TEST_F(WebSocketSBHandshakeThrottleTest, CheckArguments) { | |
| 103 throttle_->ThrottleHandshake(GURL(kTestUrl), nullptr, &fake_callbacks_); | |
| 104 safe_browsing_->RunUntilCalled(); | |
| 105 // TODO(ricea): Find a way to create a WebLocalFrame in a unit test so that | |
| 106 // the code that looks up the render_frame_id can be tested. | |
| 107 EXPECT_EQ(MSG_ROUTING_NONE, safe_browsing_->render_frame_id_); | |
| 108 EXPECT_EQ(GURL(kTestUrl), safe_browsing_->url_); | |
| 109 EXPECT_EQ(0, safe_browsing_->load_flags_); | |
| 110 EXPECT_EQ(content::RESOURCE_TYPE_SUB_RESOURCE, | |
| 111 safe_browsing_->resource_type_); | |
| 112 EXPECT_TRUE(safe_browsing_->callback_); | |
| 113 } | |
| 114 | |
| 115 TEST_F(WebSocketSBHandshakeThrottleTest, Safe) { | |
| 116 throttle_->ThrottleHandshake(GURL(kTestUrl), nullptr, &fake_callbacks_); | |
| 117 safe_browsing_->RunUntilCalled(); | |
| 118 std::move(safe_browsing_->callback_).Run(true); | |
| 119 fake_callbacks_.RunUntilCalled(); | |
| 120 EXPECT_EQ(FakeWebCallbacks::SUCCESS, fake_callbacks_.result_); | |
| 121 } | |
| 122 | |
| 123 TEST_F(WebSocketSBHandshakeThrottleTest, Unsafe) { | |
| 124 throttle_->ThrottleHandshake(GURL(kTestUrl), nullptr, &fake_callbacks_); | |
| 125 safe_browsing_->RunUntilCalled(); | |
| 126 std::move(safe_browsing_->callback_).Run(false); | |
| 127 fake_callbacks_.RunUntilCalled(); | |
| 128 EXPECT_EQ(FakeWebCallbacks::ERROR, fake_callbacks_.result_); | |
| 129 EXPECT_EQ( | |
| 130 blink::WebString( | |
| 131 "WebSocket connection to wss://test/ failed safe browsing check"), | |
| 132 fake_callbacks_.message_); | |
| 133 } | |
| 134 | |
| 135 } // namespace | |
| 136 | |
| 137 } // namespace safe_browsing | |
| OLD | NEW |