1// Copyright 2014 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 "base/prefs/testing_pref_service.h" 6#include "base/time/time.h" 7#include "chrome/browser/extensions/activity_log/activity_actions.h" 8#include "components/rappor/byte_vector_utils.h" 9#include "components/rappor/proto/rappor_metric.pb.h" 10#include "components/rappor/rappor_service.h" 11#include "extensions/common/value_builder.h" 12#include "testing/gtest/include/gtest/gtest.h" 13 14namespace extensions { 15 16namespace { 17 18scoped_refptr<Action> CreateAction(const std::string& api_name, 19 const std::string& element, 20 const std::string& attr) { 21 scoped_refptr<Action> action = new Action("id", 22 base::Time::Now(), 23 Action::ACTION_DOM_ACCESS, 24 api_name); 25 action->set_args(ListBuilder() 26 .Append(element) 27 .Append(attr) 28 .Append("http://www.google.co.uk") 29 .Append("http://www.google.co.uk") 30 .Build()); 31 return action; 32} 33 34} // namespace 35 36class TestRapporService : public rappor::RapporService { 37 public: 38 TestRapporService(); 39 virtual ~TestRapporService(); 40 41 // Returns the active reports. This also clears the internal map of metrics 42 // as a biproduct, so if comparing numbers of reports, the comparison should 43 // be from the last time GetReports() was called (not from the beginning of 44 // the test). 45 rappor::RapporReports GetReports(); 46 47 protected: 48 TestingPrefServiceSimple prefs_; 49}; 50 51TestRapporService::TestRapporService() 52 : rappor::RapporService(&prefs_) { 53 // Initialize the RapporService for testing. 54 SetCohortForTesting(0); 55 SetSecretForTesting(rappor::HmacByteVectorGenerator::GenerateEntropyInput()); 56} 57 58TestRapporService::~TestRapporService() {} 59 60rappor::RapporReports TestRapporService::GetReports() { 61 rappor::RapporReports result; 62 rappor::RapporService::ExportMetrics(&result); 63 return result; 64} 65 66// Test that the actions properly upload the URLs to the RAPPOR service if 67// the action may have injected the ad. 68TEST(AdInjectionUnittest, CheckActionForAdInjectionTest) { 69 TestRapporService rappor_service; 70 rappor::RapporReports reports = rappor_service.GetReports(); 71 EXPECT_EQ(0, reports.report_size()); 72 73 scoped_refptr<Action> modify_iframe_src = 74 CreateAction("blinkSetAttribute", "iframe", "src"); 75 modify_iframe_src->DidInjectAd(&rappor_service); 76 reports = rappor_service.GetReports(); 77 EXPECT_EQ(1, reports.report_size()); 78 79 scoped_refptr<Action> modify_anchor_href = 80 CreateAction("blinkSetAttribute", "a", "href"); 81 modify_anchor_href->DidInjectAd(&rappor_service); 82 reports = rappor_service.GetReports(); 83 EXPECT_EQ(1, reports.report_size()); 84 85 scoped_refptr<Action> harmless_action = 86 CreateAction("Location.replace", "", ""); 87 harmless_action->DidInjectAd(&rappor_service); 88 reports = rappor_service.GetReports(); 89 EXPECT_EQ(0, reports.report_size()); 90} 91 92} // namespace extensions 93