// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/renderer/safe_browsing/malware_dom_details.h" #include "chrome/common/render_messages_params.h" #include "base/stringprintf.h" #include "chrome/test/render_view_test.h" #include "net/base/escape.h" typedef RenderViewTest MalwareDOMDetailsTest; TEST_F(MalwareDOMDetailsTest, Everything) { safe_browsing::MalwareDOMDetails details(view_); // Lower kMaxNodes for the test. Loading 500 subframes in a // debug build takes a while. details.kMaxNodes = 50; const char* urlprefix = "data:text/html;charset=utf-8,"; { // An page with an internal script std::string html = ""; LoadHTML(html.c_str()); ViewHostMsg_MalwareDOMDetails_Params params; details.ExtractResources(¶ms); ASSERT_EQ(1u, params.nodes.size()); EXPECT_EQ(GURL(urlprefix + html), params.nodes[0].url); } { // A page with 2 external scripts. // Note: This part of the test causes 2 leaks: LEAK: 5 WebCoreNode // LEAK: 2 CachedResource. GURL script1_url("data:text/javascript;charset=utf-8,var a=1;"); GURL script2_url("data:text/javascript;charset=utf-8,var b=2;"); std::string html = ""; GURL url(urlprefix + html); LoadHTML(html.c_str()); ViewHostMsg_MalwareDOMDetails_Params params; details.ExtractResources(¶ms); ASSERT_EQ(3u, params.nodes.size()); EXPECT_EQ(script1_url, params.nodes[0].url); EXPECT_EQ("SCRIPT", params.nodes[0].tag_name); EXPECT_EQ(script2_url, params.nodes[1].url); EXPECT_EQ("SCRIPT", params.nodes[0].tag_name); EXPECT_EQ(url, params.nodes[2].url); } { // A page with an iframe which in turn contains an iframe. // html // \ iframe1 // \ iframe2 std::string iframe2_html = "iframe2"; GURL iframe2_url(urlprefix + iframe2_html); std::string iframe1_html = ""; GURL iframe1_url(urlprefix + iframe1_html); std::string html = ""; GURL url(urlprefix + html); LoadHTML(html.c_str()); ViewHostMsg_MalwareDOMDetails_Params params; details.ExtractResources(¶ms); ASSERT_EQ(5u, params.nodes.size()); EXPECT_EQ(iframe1_url, params.nodes[0].url); EXPECT_EQ(url, params.nodes[0].parent); EXPECT_EQ(0u, params.nodes[0].children.size()); EXPECT_EQ("IFRAME", params.nodes[0].tag_name); EXPECT_EQ(url, params.nodes[1].url); EXPECT_EQ(GURL(), params.nodes[1].parent); EXPECT_EQ(1u, params.nodes[1].children.size()); EXPECT_EQ(iframe1_url, params.nodes[1].children[0]); EXPECT_EQ(iframe2_url, params.nodes[2].url); EXPECT_EQ(iframe1_url, params.nodes[2].parent); EXPECT_EQ(0u, params.nodes[2].children.size()); EXPECT_EQ("IFRAME", params.nodes[2].tag_name); // The frames are added twice, once with the correct parent and tagname // and once with the correct children. The caller in the browser // is responsible for merging. EXPECT_EQ(iframe1_url, params.nodes[3].url); EXPECT_EQ(GURL(), params.nodes[3].parent); EXPECT_EQ(1u, params.nodes[3].children.size()); EXPECT_EQ(iframe2_url, params.nodes[3].children[0]); EXPECT_EQ(iframe2_url, params.nodes[4].url); EXPECT_EQ(GURL(), params.nodes[4].parent); EXPECT_EQ(0u, params.nodes[4].children.size()); } { // >50 subframes, to verify kMaxNodes. std::string html = ""; for (int i = 0; i < 55; ++i) { // The iframe contents is just a number. GURL iframe_url(StringPrintf("%s%d", urlprefix, i)); html += ""; } GURL url(urlprefix + html); LoadHTML(html.c_str()); ViewHostMsg_MalwareDOMDetails_Params params; details.ExtractResources(¶ms); ASSERT_EQ(51u, params.nodes.size()); } { // A page with >50 scripts, to verify kMaxNodes. std::string html = ""; for (int i = 0; i < 55; ++i) { // The iframe contents is just a number. GURL script_url(StringPrintf("%s%d", urlprefix, i)); html += ""; } GURL url(urlprefix + html); LoadHTML(html.c_str()); ViewHostMsg_MalwareDOMDetails_Params params; details.ExtractResources(¶ms); ASSERT_EQ(51u, params.nodes.size()); } }