Commit 37bccc22 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

[iOS] Ignore invalid frameIds

Additionally, update tests to use correctly formatted frame ids.

Fixed: 1098606
Change-Id: I0d0b9cfa473d20433c228863af3a434ffd50b5f1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2264468
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Auto-Submit: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#782598}
parent 0d2e44c0
...@@ -257,7 +257,7 @@ TEST_F(FormSuggestionControllerTest, ...@@ -257,7 +257,7 @@ TEST_F(FormSuggestionControllerTest,
SetUpController(@[ [TestSuggestionProvider providerWithSuggestions] ]); SetUpController(@[ [TestSuggestionProvider providerWithSuggestions] ]);
GURL url("http://foo.com"); GURL url("http://foo.com");
test_web_state_.SetCurrentURL(url); test_web_state_.SetCurrentURL(url);
web::FakeWebFrame main_frame("main_frame", /*is_main_frame=*/true, url); web::FakeMainWebFrame main_frame(url);
// Trigger form activity, which should set up the suggestions view. // Trigger form activity, which should set up the suggestions view.
autofill::FormActivityParams params; autofill::FormActivityParams params;
...@@ -280,7 +280,7 @@ TEST_F(FormSuggestionControllerTest, FormActivityBlurShouldBeIgnored) { ...@@ -280,7 +280,7 @@ TEST_F(FormSuggestionControllerTest, FormActivityBlurShouldBeIgnored) {
SetUpController(@[ [TestSuggestionProvider providerWithSuggestions] ]); SetUpController(@[ [TestSuggestionProvider providerWithSuggestions] ]);
GURL url("http://foo.com"); GURL url("http://foo.com");
test_web_state_.SetCurrentURL(url); test_web_state_.SetCurrentURL(url);
web::FakeWebFrame main_frame("main_frame", true, url); web::FakeMainWebFrame main_frame(url);
autofill::FormActivityParams params; autofill::FormActivityParams params;
params.form_name = "form"; params.form_name = "form";
...@@ -300,7 +300,7 @@ TEST_F(FormSuggestionControllerTest, ...@@ -300,7 +300,7 @@ TEST_F(FormSuggestionControllerTest,
SetUpController(@[]); SetUpController(@[]);
GURL url("http://foo.com"); GURL url("http://foo.com");
test_web_state_.SetCurrentURL(url); test_web_state_.SetCurrentURL(url);
web::FakeWebFrame main_frame("main_frame", true, url); web::FakeMainWebFrame main_frame(url);
autofill::FormActivityParams params; autofill::FormActivityParams params;
params.form_name = "form"; params.form_name = "form";
...@@ -329,7 +329,7 @@ TEST_F(FormSuggestionControllerTest, ...@@ -329,7 +329,7 @@ TEST_F(FormSuggestionControllerTest,
SetUpController(@[ provider1, provider2 ]); SetUpController(@[ provider1, provider2 ]);
GURL url("http://foo.com"); GURL url("http://foo.com");
test_web_state_.SetCurrentURL(url); test_web_state_.SetCurrentURL(url);
web::FakeWebFrame main_frame("main_frame", true, url); web::FakeMainWebFrame main_frame(url);
autofill::FormActivityParams params; autofill::FormActivityParams params;
params.form_name = "form"; params.form_name = "form";
...@@ -378,7 +378,7 @@ TEST_F(FormSuggestionControllerTest, ...@@ -378,7 +378,7 @@ TEST_F(FormSuggestionControllerTest,
SetUpController(@[ provider1, provider2 ]); SetUpController(@[ provider1, provider2 ]);
GURL url("http://foo.com"); GURL url("http://foo.com");
test_web_state_.SetCurrentURL(url); test_web_state_.SetCurrentURL(url);
web::FakeWebFrame main_frame("main_frame", true, url); web::FakeMainWebFrame main_frame(url);
autofill::FormActivityParams params; autofill::FormActivityParams params;
params.form_name = "form"; params.form_name = "form";
...@@ -419,7 +419,7 @@ TEST_F(FormSuggestionControllerTest, SelectingSuggestionShouldNotifyDelegate) { ...@@ -419,7 +419,7 @@ TEST_F(FormSuggestionControllerTest, SelectingSuggestionShouldNotifyDelegate) {
SetUpController(@[ provider ]); SetUpController(@[ provider ]);
GURL url("http://foo.com"); GURL url("http://foo.com");
test_web_state_.SetCurrentURL(url); test_web_state_.SetCurrentURL(url);
web::FakeWebFrame main_frame("main_frame", true, url); web::FakeMainWebFrame main_frame(url);
autofill::FormActivityParams params; autofill::FormActivityParams params;
params.form_name = "form"; params.form_name = "form";
......
...@@ -77,8 +77,7 @@ class PaymentRequestFullCardRequesterTest : public PlatformTest { ...@@ -77,8 +77,7 @@ class PaymentRequestFullCardRequesterTest : public PlatformTest {
web_state()->SetJSInjectionReceiver(injectionReceiver); web_state()->SetJSInjectionReceiver(injectionReceiver);
auto frames_manager = std::make_unique<web::FakeWebFramesManager>(); auto frames_manager = std::make_unique<web::FakeWebFramesManager>();
auto main_frame = std::make_unique<web::FakeWebFrame>( auto main_frame = std::make_unique<web::FakeMainWebFrame>(
/*frame_id=*/"main", /*is_main_frame=*/true,
/*security_origin=*/GURL()); /*security_origin=*/GURL());
frames_manager->AddWebFrame(std::move(main_frame)); frames_manager->AddWebFrame(std::move(main_frame));
web_state()->SetWebFramesManager(std::move(frames_manager)); web_state()->SetWebFramesManager(std::move(frames_manager));
......
...@@ -51,7 +51,7 @@ class FontSizeTabHelperTest : public PlatformTest { ...@@ -51,7 +51,7 @@ class FontSizeTabHelperTest : public PlatformTest {
GURL url("https://example.com"); GURL url("https://example.com");
web_state_.SetCurrentURL(url); web_state_.SetCurrentURL(url);
auto main_frame = std::make_unique<web::FakeWebFrame>("frameID", true, url); auto main_frame = std::make_unique<web::FakeMainWebFrame>(url);
fake_main_frame_ = main_frame.get(); fake_main_frame_ = main_frame.get();
AddWebFrame(std::move(main_frame)); AddWebFrame(std::move(main_frame));
...@@ -194,8 +194,7 @@ TEST_F(FontSizeTabHelperTest, ZoomInAllFrames) { ...@@ -194,8 +194,7 @@ TEST_F(FontSizeTabHelperTest, ZoomInAllFrames) {
preferred_content_size_category_ = UIContentSizeCategoryExtraLarge; preferred_content_size_category_ = UIContentSizeCategoryExtraLarge;
std::unique_ptr<web::FakeWebFrame> other_frame = std::unique_ptr<web::FakeWebFrame> other_frame =
std::make_unique<web::FakeWebFrame>("frameID2", false, std::make_unique<web::FakeChildWebFrame>(GURL("https://example.com"));
GURL("https://example.com"));
web::FakeWebFrame* fake_other_frame = other_frame.get(); web::FakeWebFrame* fake_other_frame = other_frame.get();
AddWebFrame(std::move(other_frame)); AddWebFrame(std::move(other_frame));
......
...@@ -12,32 +12,19 @@ ...@@ -12,32 +12,19 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace {
const char kOneMatchFrameId[] = "frame_with_one_match";
const char kTwoMatchesFrameId[] = "frame_with_two_matches";
} // namespace
namespace web { namespace web {
class FindInPageRequestTest : public WebTest { class FindInPageRequestTest : public WebTest {
protected: protected:
// Returns a FakeWebFrame with id |frame_id|.
std::unique_ptr<FakeWebFrame> CreateWebFrame(const std::string& frame_id,
bool is_main_frame) {
return std::make_unique<FakeWebFrame>(frame_id, is_main_frame,
GURL::EmptyGURL());
}
FindInPageRequestTest() { FindInPageRequestTest() {
auto main_frame = CreateWebFrame(kOneMatchFrameId, auto main_frame = std::make_unique<FakeMainWebFrame>(GURL::EmptyGURL());
/*is_main_frame=*/true);
request_.AddFrame(main_frame.get()); request_.AddFrame(main_frame.get());
auto frame_with_two_matches = auto frame_with_two_matches =
CreateWebFrame(kTwoMatchesFrameId, /*is_main_frame=*/false); std::make_unique<FakeChildWebFrame>(GURL::EmptyGURL());
request_.AddFrame(frame_with_two_matches.get()); request_.AddFrame(frame_with_two_matches.get());
request_.Reset(@"foo", 2); request_.Reset(@"foo", 2);
request_.SetMatchCountForFrame(1, kOneMatchFrameId); request_.SetMatchCountForFrame(1, kMainFakeFrameId);
request_.SetMatchCountForFrame(2, kTwoMatchesFrameId); request_.SetMatchCountForFrame(2, kChildFakeFrameId);
} }
FindInPageRequest request_; FindInPageRequest request_;
}; };
...@@ -144,7 +131,7 @@ TEST_F(FindInPageRequestTest, RemoveFrame) { ...@@ -144,7 +131,7 @@ TEST_F(FindInPageRequestTest, RemoveFrame) {
EXPECT_EQ(3, request_.GetTotalMatchCount()); EXPECT_EQ(3, request_.GetTotalMatchCount());
EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame()); EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame());
request_.RemoveFrame(kOneMatchFrameId); request_.RemoveFrame(kMainFakeFrameId);
EXPECT_EQ(2, request_.GetTotalMatchCount()); EXPECT_EQ(2, request_.GetTotalMatchCount());
...@@ -186,7 +173,7 @@ TEST_F(FindInPageRequestTest, SetMatchCountForFrame) { ...@@ -186,7 +173,7 @@ TEST_F(FindInPageRequestTest, SetMatchCountForFrame) {
EXPECT_EQ(3, request_.GetTotalMatchCount()); EXPECT_EQ(3, request_.GetTotalMatchCount());
EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame()); EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame());
request_.SetMatchCountForFrame(5, kTwoMatchesFrameId); request_.SetMatchCountForFrame(5, kChildFakeFrameId);
EXPECT_EQ(6, request_.GetTotalMatchCount()); EXPECT_EQ(6, request_.GetTotalMatchCount());
EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame()); EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame());
......
...@@ -34,14 +34,12 @@ class WebFrameUtilTest : public PlatformTest { ...@@ -34,14 +34,12 @@ class WebFrameUtilTest : public PlatformTest {
TEST_F(WebFrameUtilTest, GetMainFrame) { TEST_F(WebFrameUtilTest, GetMainFrame) {
// Still no main frame. // Still no main frame.
EXPECT_EQ(nullptr, GetMainFrame(&test_web_state_)); EXPECT_EQ(nullptr, GetMainFrame(&test_web_state_));
auto iframe = auto iframe = std::make_unique<FakeChildWebFrame>(GURL::EmptyGURL());
std::make_unique<FakeWebFrame>("iframe", false, GURL::EmptyGURL());
fake_web_frames_manager_->AddWebFrame(std::move(iframe)); fake_web_frames_manager_->AddWebFrame(std::move(iframe));
// Still no main frame. // Still no main frame.
EXPECT_EQ(nullptr, GetMainFrame(&test_web_state_)); EXPECT_EQ(nullptr, GetMainFrame(&test_web_state_));
auto main_frame = auto main_frame = std::make_unique<FakeMainWebFrame>(GURL::EmptyGURL());
std::make_unique<FakeWebFrame>("main_frame", true, GURL::EmptyGURL());
FakeWebFrame* main_frame_ptr = main_frame.get(); FakeWebFrame* main_frame_ptr = main_frame.get();
fake_web_frames_manager_->AddWebFrame(std::move(main_frame)); fake_web_frames_manager_->AddWebFrame(std::move(main_frame));
// Now there is a main frame. // Now there is a main frame.
...@@ -56,18 +54,16 @@ TEST_F(WebFrameUtilTest, GetMainFrame) { ...@@ -56,18 +54,16 @@ TEST_F(WebFrameUtilTest, GetMainFrame) {
TEST_F(WebFrameUtilTest, GetMainWebFrameId) { TEST_F(WebFrameUtilTest, GetMainWebFrameId) {
// Still no main frame. // Still no main frame.
EXPECT_TRUE(GetMainWebFrameId(&test_web_state_).empty()); EXPECT_TRUE(GetMainWebFrameId(&test_web_state_).empty());
auto iframe = auto iframe = std::make_unique<FakeChildWebFrame>(GURL::EmptyGURL());
std::make_unique<FakeWebFrame>("iframe", false, GURL::EmptyGURL());
fake_web_frames_manager_->AddWebFrame(std::move(iframe)); fake_web_frames_manager_->AddWebFrame(std::move(iframe));
// Still no main frame. // Still no main frame.
EXPECT_TRUE(GetMainWebFrameId(&test_web_state_).empty()); EXPECT_TRUE(GetMainWebFrameId(&test_web_state_).empty());
auto main_frame = auto main_frame = std::make_unique<FakeMainWebFrame>(GURL::EmptyGURL());
std::make_unique<FakeWebFrame>("main_frame", true, GURL::EmptyGURL());
FakeWebFrame* main_frame_ptr = main_frame.get(); FakeWebFrame* main_frame_ptr = main_frame.get();
fake_web_frames_manager_->AddWebFrame(std::move(main_frame)); fake_web_frames_manager_->AddWebFrame(std::move(main_frame));
// Now there is a main frame. // Now there is a main frame.
EXPECT_EQ("main_frame", GetMainWebFrameId(&test_web_state_)); EXPECT_EQ(kMainFakeFrameId, GetMainWebFrameId(&test_web_state_));
fake_web_frames_manager_->RemoveWebFrame(main_frame_ptr->GetFrameId()); fake_web_frames_manager_->RemoveWebFrame(main_frame_ptr->GetFrameId());
// Now there is no main frame. // Now there is no main frame.
...@@ -77,37 +73,36 @@ TEST_F(WebFrameUtilTest, GetMainWebFrameId) { ...@@ -77,37 +73,36 @@ TEST_F(WebFrameUtilTest, GetMainWebFrameId) {
// Tests the GetWebFrameWithId function. // Tests the GetWebFrameWithId function.
TEST_F(WebFrameUtilTest, GetWebFrameWithId) { TEST_F(WebFrameUtilTest, GetWebFrameWithId) {
// Still no main frame. // Still no main frame.
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "iframe")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, kChildFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "main_frame")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, kMainFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused"));
auto iframe = auto iframe = std::make_unique<FakeChildWebFrame>(GURL::EmptyGURL());
std::make_unique<FakeWebFrame>("iframe", false, GURL::EmptyGURL());
FakeWebFrame* iframe_ptr = iframe.get(); FakeWebFrame* iframe_ptr = iframe.get();
fake_web_frames_manager_->AddWebFrame(std::move(iframe)); fake_web_frames_manager_->AddWebFrame(std::move(iframe));
// There is an iframe. // There is an iframe.
EXPECT_EQ(iframe_ptr, GetWebFrameWithId(&test_web_state_, "iframe")); EXPECT_EQ(iframe_ptr, GetWebFrameWithId(&test_web_state_, kChildFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "main_frame")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, kMainFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused"));
auto main_frame = auto main_frame = std::make_unique<FakeMainWebFrame>(GURL::EmptyGURL());
std::make_unique<FakeWebFrame>("main_frame", true, GURL::EmptyGURL());
FakeWebFrame* main_frame_ptr = main_frame.get(); FakeWebFrame* main_frame_ptr = main_frame.get();
fake_web_frames_manager_->AddWebFrame(std::move(main_frame)); fake_web_frames_manager_->AddWebFrame(std::move(main_frame));
// Now there is a main frame. // Now there is a main frame.
EXPECT_EQ(iframe_ptr, GetWebFrameWithId(&test_web_state_, "iframe")); EXPECT_EQ(iframe_ptr, GetWebFrameWithId(&test_web_state_, kChildFakeFrameId));
EXPECT_EQ(main_frame_ptr, GetWebFrameWithId(&test_web_state_, "main_frame")); EXPECT_EQ(main_frame_ptr,
GetWebFrameWithId(&test_web_state_, kMainFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused"));
fake_web_frames_manager_->RemoveWebFrame(main_frame_ptr->GetFrameId()); fake_web_frames_manager_->RemoveWebFrame(main_frame_ptr->GetFrameId());
// Now there is only an iframe. // Now there is only an iframe.
EXPECT_EQ(iframe_ptr, GetWebFrameWithId(&test_web_state_, "iframe")); EXPECT_EQ(iframe_ptr, GetWebFrameWithId(&test_web_state_, kChildFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "main_frame")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, kMainFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused"));
// Now there nothing left. // Now there nothing left.
fake_web_frames_manager_->RemoveWebFrame(iframe_ptr->GetFrameId()); fake_web_frames_manager_->RemoveWebFrame(iframe_ptr->GetFrameId());
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "iframe")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, kChildFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "main_frame")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, kMainFakeFrameId));
EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused")); EXPECT_EQ(nullptr, GetWebFrameWithId(&test_web_state_, "unused"));
// Test that GetWebFrameWithId returns nullptr for the empty string. // Test that GetWebFrameWithId returns nullptr for the empty string.
...@@ -117,20 +112,18 @@ TEST_F(WebFrameUtilTest, GetWebFrameWithId) { ...@@ -117,20 +112,18 @@ TEST_F(WebFrameUtilTest, GetWebFrameWithId) {
// Tests the GetWebFrameId GetWebFrameId function. // Tests the GetWebFrameId GetWebFrameId function.
TEST_F(WebFrameUtilTest, GetWebFrameId) { TEST_F(WebFrameUtilTest, GetWebFrameId) {
EXPECT_EQ(std::string(), GetWebFrameId(nullptr)); EXPECT_EQ(std::string(), GetWebFrameId(nullptr));
FakeWebFrame frame("frame", true, GURL::EmptyGURL()); FakeMainWebFrame frame(GURL::EmptyGURL());
EXPECT_EQ("frame", GetWebFrameId(&frame)); EXPECT_EQ(kMainFakeFrameId, GetWebFrameId(&frame));
} }
// Tests the GetAllWebFrames function. // Tests the GetAllWebFrames function.
TEST_F(WebFrameUtilTest, GetAllWebFrames) { TEST_F(WebFrameUtilTest, GetAllWebFrames) {
EXPECT_EQ(0U, EXPECT_EQ(0U,
test_web_state_.GetWebFramesManager()->GetAllWebFrames().size()); test_web_state_.GetWebFramesManager()->GetAllWebFrames().size());
auto main_frame = auto main_frame = std::make_unique<FakeMainWebFrame>(GURL::EmptyGURL());
std::make_unique<FakeWebFrame>("main_frame", true, GURL::EmptyGURL());
FakeWebFrame* main_frame_ptr = main_frame.get(); FakeWebFrame* main_frame_ptr = main_frame.get();
fake_web_frames_manager_->AddWebFrame(std::move(main_frame)); fake_web_frames_manager_->AddWebFrame(std::move(main_frame));
auto iframe = auto iframe = std::make_unique<FakeChildWebFrame>(GURL::EmptyGURL());
std::make_unique<FakeWebFrame>("iframe", false, GURL::EmptyGURL());
FakeWebFrame* iframe_ptr = iframe.get(); FakeWebFrame* iframe_ptr = iframe.get();
fake_web_frames_manager_->AddWebFrame(std::move(iframe)); fake_web_frames_manager_->AddWebFrame(std::move(iframe));
std::set<WebFrame*> all_frames = std::set<WebFrame*> all_frames =
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "ios/web/js_messaging/web_frames_manager_impl.h" #include "ios/web/js_messaging/web_frames_manager_impl.h"
#include "base/base64.h" #include "base/base64.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "crypto/symmetric_key.h" #include "crypto/symmetric_key.h"
...@@ -161,6 +162,14 @@ void WebFramesManagerImpl::OnFrameBecameAvailable(WKScriptMessage* message) { ...@@ -161,6 +162,14 @@ void WebFramesManagerImpl::OnFrameBecameAvailable(WKScriptMessage* message) {
std::string frame_id = base::SysNSStringToUTF8(message.body[@"crwFrameId"]); std::string frame_id = base::SysNSStringToUTF8(message.body[@"crwFrameId"]);
if (!GetFrameWithId(frame_id)) { if (!GetFrameWithId(frame_id)) {
// Validate |frame_id| is a proper hex string.
for (const char& c : frame_id) {
if (!base::IsHexDigit(c)) {
// Ignore frame if |frame_id| is malformed.
return;
}
}
GURL message_frame_origin = GURL message_frame_origin =
web::GURLOriginWithWKSecurityOrigin(message.frameInfo.securityOrigin); web::GURLOriginWithWKSecurityOrigin(message.frameInfo.securityOrigin);
......
...@@ -20,11 +20,6 @@ ...@@ -20,11 +20,6 @@
namespace { namespace {
// Frame ids are base16 string of 128 bit numbers.
const char kMainFrameId[] = "1effd8f52a067c8d3a01762d3c41dfd1";
const char kFrame1Id[] = "1effd8f52a067c8d3a01762d3c41dfd2";
const char kFrame2Id[] = "1effd8f52a067c8d3a01762d3c41dfd3";
// A base64 encoded sample key. // A base64 encoded sample key.
const char kFrameKey[] = "R7lsXtR74c6R9A9k691gUQ8JAd0be+w//Lntgcbjwrc="; const char kFrameKey[] = "R7lsXtR74c6R9A9k691gUQ8JAd0be+w//Lntgcbjwrc=";
...@@ -46,13 +41,13 @@ class WebFramesManagerImplTest : public PlatformTest, ...@@ -46,13 +41,13 @@ class WebFramesManagerImplTest : public PlatformTest,
router_([[CRWWKScriptMessageRouter alloc] router_([[CRWWKScriptMessageRouter alloc]
initWithUserContentController:user_content_controller_]), initWithUserContentController:user_content_controller_]),
web_view_(OCMClassMock([WKWebView class])), web_view_(OCMClassMock([WKWebView class])),
main_frame_(kMainFrameId, main_frame_(kMainFakeFrameId,
/*is_main_frame=*/true, /*is_main_frame=*/true,
GURL("https://www.main.test")), GURL("https://www.main.test")),
frame_1_(kFrame1Id, frame_1_(kChildFakeFrameId,
/*is_main_frame=*/false, /*is_main_frame=*/false,
GURL("https://www.frame1.test")), GURL("https://www.frame1.test")),
frame_2_(kFrame2Id, frame_2_(kChildFakeFrameId2,
/*is_main_frame=*/false, /*is_main_frame=*/false,
GURL("https://www.frame2.test")) { GURL("https://www.frame2.test")) {
// Notify |frames_manager_| that its associated WKWebView has changed from // Notify |frames_manager_| that its associated WKWebView has changed from
...@@ -163,6 +158,16 @@ TEST_F(WebFramesManagerImplTest, MainWebFrame) { ...@@ -163,6 +158,16 @@ TEST_F(WebFramesManagerImplTest, MainWebFrame) {
EXPECT_FALSE(frames_manager_.GetFrameWithId(main_frame_.GetFrameId())); EXPECT_FALSE(frames_manager_.GetFrameWithId(main_frame_.GetFrameId()));
} }
// Tests that a WebFrame can not be registered with a malformed frame id.
TEST_F(WebFramesManagerImplTest, WebFrameWithInvalidId) {
FakeWebFrame frame_with_invalid_id(kInvalidFrameId,
/*is_main_frame=*/true,
GURL("https://www.main.test"));
SendFrameBecameAvailableMessage(frame_with_invalid_id);
EXPECT_EQ(0ul, frames_manager_.GetAllWebFrames().size());
}
// Tests multiple web frames construction/destruction. // Tests multiple web frames construction/destruction.
TEST_F(WebFramesManagerImplTest, MultipleWebFrame) { TEST_F(WebFramesManagerImplTest, MultipleWebFrame) {
// Add main frame. // Add main frame.
......
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
namespace web { namespace web {
// Frame ids are base16 string of 128 bit numbers.
const char kMainFakeFrameId[] = "1effd8f52a067c8d3a01762d3c41dfd1";
const char kInvalidFrameId[] = "1effd8f52a067c8d3a01762d3c4;dfd1";
const char kChildFakeFrameId[] = "1effd8f52a067c8d3a01762d3c41dfd2";
const char kChildFakeFrameId2[] = "1effd8f52a067c8d3a01762d3c41dfd3";
FakeWebFrame::FakeWebFrame(const std::string& frame_id, FakeWebFrame::FakeWebFrame(const std::string& frame_id,
bool is_main_frame, bool is_main_frame,
GURL security_origin) GURL security_origin)
...@@ -81,4 +87,18 @@ void FakeWebFrame::AddJsResultForFunctionCall( ...@@ -81,4 +87,18 @@ void FakeWebFrame::AddJsResultForFunctionCall(
result_map_[function_name] = std::move(js_result); result_map_[function_name] = std::move(js_result);
} }
// FakeMainWebFrame
FakeMainWebFrame::FakeMainWebFrame(GURL security_origin)
: FakeWebFrame(kMainFakeFrameId, /*is_main_frame=*/true, security_origin) {}
FakeMainWebFrame::~FakeMainWebFrame() {}
// FakeChildWebFrame
FakeChildWebFrame::FakeChildWebFrame(GURL security_origin)
: FakeWebFrame(kChildFakeFrameId,
/*is_main_frame=*/false,
security_origin) {}
FakeChildWebFrame::~FakeChildWebFrame() {}
} // namespace web } // namespace web
...@@ -13,8 +13,18 @@ ...@@ -13,8 +13,18 @@
namespace web { namespace web {
// Frame id constants which can be used to initialize FakeWebFrame.
// Frame ids are base16 string of 128 bit numbers.
extern const char kMainFakeFrameId[];
extern const char kInvalidFrameId[];
extern const char kChildFakeFrameId[];
extern const char kChildFakeFrameId2[];
// A fake web frame to use for testing.
class FakeWebFrame : public WebFrame { class FakeWebFrame : public WebFrame {
public: public:
// Creates a web frame. |frame_id| must be a string representing a valid
// hexadecimal number.
FakeWebFrame(const std::string& frame_id, FakeWebFrame(const std::string& frame_id,
bool is_main_frame, bool is_main_frame,
GURL security_origin); GURL security_origin);
...@@ -77,6 +87,22 @@ class FakeWebFrame : public WebFrame { ...@@ -77,6 +87,22 @@ class FakeWebFrame : public WebFrame {
bool can_call_function_ = true; bool can_call_function_ = true;
}; };
// A fake web frame representing the main frame with a |frame_id_| of
// |kMainFakeFrameId|.
class FakeMainWebFrame : public FakeWebFrame {
public:
explicit FakeMainWebFrame(GURL security_origin);
~FakeMainWebFrame() override;
};
// A fake web frame representing a child frame with a |frame_id_| of
// |kChildFakeFrameId|.
class FakeChildWebFrame : public FakeWebFrame {
public:
explicit FakeChildWebFrame(GURL security_origin);
~FakeChildWebFrame() override;
};
} // namespace web } // namespace web
#endif // IOS_WEB_PUBLIC_TEST_FAKES_FAKE_WEB_FRAME_H_ #endif // IOS_WEB_PUBLIC_TEST_FAKES_FAKE_WEB_FRAME_H_
...@@ -312,7 +312,7 @@ TEST_F(WebStateImplTest, ObserverTest) { ...@@ -312,7 +312,7 @@ TEST_F(WebStateImplTest, ObserverTest) {
// Test that WebFrameDidBecomeAvailable() is called. // Test that WebFrameDidBecomeAvailable() is called.
ASSERT_FALSE(observer->web_frame_available_info()); ASSERT_FALSE(observer->web_frame_available_info());
web::FakeWebFrame main_frame("main", true, GURL()); web::FakeMainWebFrame main_frame(GURL::EmptyGURL());
web_state_->OnWebFrameAvailable(&main_frame); web_state_->OnWebFrameAvailable(&main_frame);
ASSERT_TRUE(observer->web_frame_available_info()); ASSERT_TRUE(observer->web_frame_available_info());
EXPECT_EQ(web_state_.get(), observer->web_frame_available_info()->web_state); EXPECT_EQ(web_state_.get(), observer->web_frame_available_info()->web_state);
...@@ -815,7 +815,7 @@ TEST_F(WebStateImplTest, ScriptCommand) { ...@@ -815,7 +815,7 @@ TEST_F(WebStateImplTest, ScriptCommand) {
value_1.SetString("a", "b"); value_1.SetString("a", "b");
const GURL kUrl1("http://foo"); const GURL kUrl1("http://foo");
bool is_called_1 = false; bool is_called_1 = false;
web::FakeWebFrame main_frame("main", true, GURL()); web::FakeMainWebFrame main_frame(GURL::EmptyGURL());
auto subscription_1 = web_state_->AddScriptCommandCallback( auto subscription_1 = web_state_->AddScriptCommandCallback(
base::BindRepeating(&HandleScriptCommand, &is_called_1, &value_1, kUrl1, base::BindRepeating(&HandleScriptCommand, &is_called_1, &value_1, kUrl1,
/*expected_user_is_interacting*/ false, &main_frame), /*expected_user_is_interacting*/ false, &main_frame),
...@@ -838,7 +838,7 @@ TEST_F(WebStateImplTest, ScriptCommand) { ...@@ -838,7 +838,7 @@ TEST_F(WebStateImplTest, ScriptCommand) {
value_3.SetString("e", "f"); value_3.SetString("e", "f");
const GURL kUrl3("http://iframe"); const GURL kUrl3("http://iframe");
bool is_called_3 = false; bool is_called_3 = false;
web::FakeWebFrame subframe("subframe", false, GURL()); web::FakeChildWebFrame subframe(GURL::EmptyGURL());
auto subscription_3 = web_state_->AddScriptCommandCallback( auto subscription_3 = web_state_->AddScriptCommandCallback(
base::BindRepeating(&HandleScriptCommand, &is_called_3, &value_3, kUrl3, base::BindRepeating(&HandleScriptCommand, &is_called_3, &value_3, kUrl3,
/*expected_user_is_interacting*/ false, &subframe), /*expected_user_is_interacting*/ false, &subframe),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment