Home | History | Annotate | Download | only in activity_log
      1 // Copyright 2013 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/time/time.h"
      6 #include "chrome/browser/extensions/activity_log/activity_action_constants.h"
      7 #include "chrome/browser/extensions/activity_log/uma_policy.h"
      8 #include "chrome/test/base/testing_profile.h"
      9 #include "extensions/common/dom_action_types.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace extensions {
     13 
     14 class UmaPolicyTest : public testing::Test {
     15  public:
     16   UmaPolicyTest() {
     17     profile_.reset(new TestingProfile());
     18   }
     19 
     20  protected:
     21   scoped_ptr<TestingProfile> profile_;
     22 };
     23 
     24 TEST_F(UmaPolicyTest, Construct) {
     25   ActivityLogPolicy* policy = new UmaPolicy(profile_.get());
     26   policy->Close();
     27 }
     28 
     29 TEST_F(UmaPolicyTest, MatchActionToStatusTest) {
     30   UmaPolicy* policy = new UmaPolicy(profile_.get());
     31 
     32   scoped_refptr<Action> action = new Action(
     33       "id", base::Time::Now(), Action::ACTION_API_CALL, "extension.connect");
     34   ASSERT_EQ(UmaPolicy::NONE, policy->MatchActionToStatus(action));
     35 
     36   action = new Action(
     37       "id", base::Time::Now(), Action::ACTION_API_CALL, "tabs.executeScript");
     38   ASSERT_EQ(
     39       (1 << UmaPolicy::CONTENT_SCRIPT), policy->MatchActionToStatus(action));
     40 
     41   action = new Action(
     42       "id", base::Time::Now(), Action::ACTION_CONTENT_SCRIPT, "");
     43   ASSERT_TRUE(
     44       (1 << UmaPolicy::CONTENT_SCRIPT) & policy->MatchActionToStatus(action));
     45   ASSERT_EQ(
     46       (1 << UmaPolicy::CONTENT_SCRIPT), policy->MatchActionToStatus(action));
     47 
     48   action = new Action(
     49       "id", base::Time::Now(), Action::ACTION_DOM_ACCESS, "Document.location");
     50   action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
     51                                       DomActionType::GETTER);
     52   ASSERT_TRUE((1 << UmaPolicy::READ_DOM) & policy->MatchActionToStatus(action));
     53   ASSERT_EQ((1 << UmaPolicy::READ_DOM), policy->MatchActionToStatus(action));
     54 
     55   action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
     56                                       DomActionType::SETTER);
     57   ASSERT_TRUE(
     58       (1 << UmaPolicy::MODIFIED_DOM) & policy->MatchActionToStatus(action));
     59   ASSERT_EQ(
     60       (1 << UmaPolicy::MODIFIED_DOM), policy->MatchActionToStatus(action));
     61 
     62   action = new Action(
     63       "id", base::Time::Now(), Action::ACTION_DOM_ACCESS, "HTMLDocument.write");
     64   action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
     65                                       DomActionType::METHOD);
     66   ASSERT_TRUE(
     67       (1 << UmaPolicy::DOCUMENT_WRITE) & policy->MatchActionToStatus(action));
     68   ASSERT_TRUE(
     69       (1 << UmaPolicy::DOM_METHOD) & policy->MatchActionToStatus(action));
     70 
     71   action = new Action("id",
     72                       base::Time::Now(),
     73                       Action::ACTION_DOM_ACCESS,
     74                       "Document.createElement");
     75   scoped_ptr<base::ListValue> args(new base::ListValue());
     76   args->Set(0, new base::StringValue("script"));
     77   action->set_args(args.Pass());
     78   ASSERT_EQ(UmaPolicy::NONE, policy->MatchActionToStatus(action));
     79   action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
     80                                       DomActionType::METHOD);
     81   ASSERT_TRUE(
     82       (1 << UmaPolicy::CREATED_SCRIPT) & policy->MatchActionToStatus(action));
     83   ASSERT_TRUE(
     84       (1 << UmaPolicy::DOM_METHOD) & policy->MatchActionToStatus(action));
     85 
     86   policy->Close();
     87 }
     88 
     89 TEST_F(UmaPolicyTest, SiteUrlTest) {
     90   UmaPolicy* policy = new UmaPolicy(profile_.get());
     91 
     92   const std::string site0 = "http://www.zzz.com/";
     93   const std::string site1 = "http://www.foo.com/";
     94   const std::string site2 = "http://www.google.com#a";
     95   const std::string site3 = "http://www.google.com#bb";
     96 
     97   // Record some opened sites.
     98   policy->SetupOpenedPage(site1);
     99   policy->SetupOpenedPage(site2);
    100   policy->SetupOpenedPage(site2);
    101   policy->SetupOpenedPage(site3);
    102   policy->SetupOpenedPage(site3);
    103   policy->SetupOpenedPage(site3);
    104 
    105   // Check that site1, site2, and site3 were recorded.
    106   ASSERT_EQ(3u, policy->url_status().size());
    107   ASSERT_EQ(1, policy->url_status()[site1][UmaPolicy::kNumberOfTabs]);
    108   ASSERT_EQ(2, policy->url_status()[site2][UmaPolicy::kNumberOfTabs]);
    109   ASSERT_EQ(3, policy->url_status()[site3][UmaPolicy::kNumberOfTabs]);
    110 
    111   // Remove some sites.
    112   policy->CleanupClosedPage(site0, NULL);
    113   policy->CleanupClosedPage(site2, NULL);
    114   policy->CleanupClosedPage(site2, NULL);
    115   policy->CleanupClosedPage(site3, NULL);
    116 
    117   // Check that the removal worked.
    118   ASSERT_EQ(2u, policy->url_status().size());
    119   ASSERT_EQ(1, policy->url_status()[site1][UmaPolicy::kNumberOfTabs]);
    120   ASSERT_EQ(2, policy->url_status()[site3][UmaPolicy::kNumberOfTabs]);
    121 
    122   policy->Close();
    123 }
    124 
    125 TEST_F(UmaPolicyTest, ProcessActionTest) {
    126   const std::string site_a = "http://www.zzz.com/";
    127   const std::string site_b = "http://www.foo.com/";
    128   const std::string ext_a = "a";
    129   const std::string ext_b = "b";
    130   UmaPolicy* policy = new UmaPolicy(profile_.get());
    131 
    132   // Populate with a few different pages.
    133   policy->SetupOpenedPage(site_a);
    134   policy->SetupOpenedPage(site_b);
    135 
    136   // Process a few actions for site_a.
    137   scoped_refptr<Action> action1 = new Action(
    138       ext_a, base::Time::Now(), Action::ACTION_CONTENT_SCRIPT, "");
    139   action1->set_page_url(GURL(site_a));
    140   policy->ProcessAction(action1);
    141 
    142   scoped_refptr<Action> action2 = new Action(
    143       ext_a, base::Time::Now(), Action::ACTION_CONTENT_SCRIPT, "");
    144   action2->set_page_url(GURL(site_a));
    145   policy->ProcessAction(action2);
    146 
    147   scoped_refptr<Action> action3 = new Action(
    148       ext_b, base::Time::Now(), Action::ACTION_DOM_ACCESS, "Document.location");
    149   action3->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
    150                                        DomActionType::GETTER);
    151   action3->set_page_url(GURL(site_a));
    152   policy->ProcessAction(action3);
    153 
    154   // Process an action for site_b.
    155   scoped_refptr<Action> action4 = new Action(
    156       ext_a, base::Time::Now(), Action::ACTION_DOM_ACCESS, "Document.location");
    157   action4->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
    158                                        DomActionType::SETTER);
    159   action4->set_page_url(GURL(site_b));
    160   policy->ProcessAction(action4);
    161 
    162   scoped_refptr<Action> action5 = new Action(
    163       ext_b, base::Time::Now(), Action::ACTION_DOM_ACCESS, "Document.location");
    164   action5->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
    165                                        DomActionType::SETTER);
    166   action5->set_page_url(GURL(site_b));
    167   policy->ProcessAction(action5);
    168 
    169   scoped_refptr<Action> action6 = new Action(
    170       ext_b, base::Time::Now(), Action::ACTION_API_CALL, "tabs.executeScript");
    171   action6->set_arg_url(GURL(site_b));
    172   policy->ProcessAction(action6);
    173 
    174   // Now check what's been recorded.
    175   ASSERT_EQ(2u, policy->url_status().size());
    176 
    177   ASSERT_EQ(3u, policy->url_status()[site_a].size());
    178   ASSERT_TRUE(
    179       (1 << UmaPolicy::CONTENT_SCRIPT) & policy->url_status()[site_a][ext_a]);
    180   ASSERT_FALSE(
    181       (1 << UmaPolicy::CONTENT_SCRIPT) & policy->url_status()[site_a][ext_b]);
    182   ASSERT_TRUE((1 << UmaPolicy::READ_DOM) & policy->url_status()[site_a][ext_b]);
    183   ASSERT_FALSE(
    184       (1 << UmaPolicy::READ_DOM) & policy->url_status()[site_a][ext_a]);
    185 
    186   ASSERT_EQ(3u, policy->url_status()[site_b].size());
    187   ASSERT_TRUE(
    188       (1 << UmaPolicy::MODIFIED_DOM) & policy->url_status()[site_b][ext_a]);
    189   ASSERT_TRUE(
    190       (1 << UmaPolicy::MODIFIED_DOM) & policy->url_status()[site_b][ext_b]);
    191   ASSERT_TRUE(
    192       (1 << UmaPolicy::CONTENT_SCRIPT) & policy->url_status()[site_b][ext_b]);
    193 
    194   policy->Close();
    195 }
    196 
    197 TEST_F(UmaPolicyTest, CleanURLTest) {
    198   ASSERT_EQ("http://www.google.com/",
    199             UmaPolicy::CleanURL(GURL("http://www.google.com/")));
    200   ASSERT_EQ("http://www.google.com/",
    201             UmaPolicy::CleanURL(GURL("http://www.google.com")));
    202   ASSERT_EQ("http://www.google.com:8080/a.html",
    203             UmaPolicy::CleanURL(GURL("http://www.google.com:8080/a.html")));
    204   ASSERT_EQ("http://www.google.com/",
    205             UmaPolicy::CleanURL(GURL("http://www.google.com/#a")));
    206   ASSERT_EQ("http://www.google.com/",
    207             UmaPolicy::CleanURL(GURL("http://www.google.com/#aaaa")));
    208   ASSERT_EQ("http://www.google.com/?q=a",
    209             UmaPolicy::CleanURL(GURL("http://www.google.com/?q=a")));
    210 
    211   ASSERT_EQ("http://www.cnn.com/",
    212             UmaPolicy::CleanURL(GURL("http://www.cnn.com/")));
    213   ASSERT_EQ("http://www.cnn.com:8080/a.html",
    214             UmaPolicy::CleanURL(GURL("http://www.cnn.com:8080/a.html")));
    215   ASSERT_EQ("http://www.cnn.com/",
    216             UmaPolicy::CleanURL(GURL("http://www.cnn.com")));
    217   ASSERT_EQ("http://www.cnn.com/",
    218             UmaPolicy::CleanURL(GURL("http://www.cnn.com/#a")));
    219   ASSERT_EQ("http://www.cnn.com/",
    220             UmaPolicy::CleanURL(GURL("http://www.cnn.com/#aaaa")));
    221   ASSERT_EQ("http://www.cnn.com/?q=a",
    222             UmaPolicy::CleanURL(GURL("http://www.cnn.com/?q=a")));
    223 }
    224 
    225 }  // namespace extensions
    226