1// Copyright (c) 2011 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/content_settings/core/browser/content_settings_origin_identifier_value_map.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "base/values.h"
9#include "chrome/browser/content_settings/content_settings_utils.h"
10#include "components/content_settings/core/browser/content_settings_rule.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "url/gurl.h"
13
14TEST(OriginIdentifierValueMapTest, SetGetValue) {
15  content_settings::OriginIdentifierValueMap map;
16
17  EXPECT_EQ(NULL,
18            map.GetValue(GURL("http://www.google.com"),
19                         GURL("http://www.google.com"),
20                         CONTENT_SETTINGS_TYPE_COOKIES,
21                         std::string()));
22  map.SetValue(ContentSettingsPattern::FromString("[*.]google.com"),
23               ContentSettingsPattern::FromString("[*.]google.com"),
24               CONTENT_SETTINGS_TYPE_COOKIES,
25               std::string(),
26               new base::FundamentalValue(1));
27
28  scoped_ptr<base::Value> expected_value(new base::FundamentalValue(1));
29  EXPECT_TRUE(expected_value->Equals(map.GetValue(GURL("http://www.google.com"),
30                                                  GURL("http://www.google.com"),
31                                                  CONTENT_SETTINGS_TYPE_COOKIES,
32                                                  std::string())));
33
34  EXPECT_EQ(NULL,
35            map.GetValue(GURL("http://www.google.com"),
36                         GURL("http://www.youtube.com"),
37                         CONTENT_SETTINGS_TYPE_COOKIES,
38                         std::string()));
39
40  EXPECT_EQ(NULL,
41            map.GetValue(GURL("http://www.youtube.com"),
42                         GURL("http://www.google.com"),
43                         CONTENT_SETTINGS_TYPE_COOKIES,
44                         std::string()));
45
46  EXPECT_EQ(NULL,
47            map.GetValue(GURL("http://www.google.com"),
48                         GURL("http://www.google.com"),
49                         CONTENT_SETTINGS_TYPE_POPUPS,
50                         std::string()));
51
52  EXPECT_EQ(NULL, map.GetValue(GURL("http://www.google.com"),
53                               GURL("http://www.google.com"),
54                               CONTENT_SETTINGS_TYPE_COOKIES,
55                               "resource_id"));
56}
57
58TEST(OriginIdentifierValueMapTest, SetDeleteValue) {
59  content_settings::OriginIdentifierValueMap map;
60
61  EXPECT_EQ(NULL, map.GetValue(GURL("http://www.google.com"),
62                               GURL("http://www.google.com"),
63                               CONTENT_SETTINGS_TYPE_PLUGINS,
64                               "java-plugin"));
65
66  // Set sample values.
67  map.SetValue(ContentSettingsPattern::FromString("[*.]google.com"),
68               ContentSettingsPattern::FromString("[*.]google.com"),
69               CONTENT_SETTINGS_TYPE_PLUGINS,
70               "java-plugin",
71               new base::FundamentalValue(1));
72
73  int actual_value;
74  EXPECT_TRUE(map.GetValue(GURL("http://www.google.com"),
75                           GURL("http://www.google.com"),
76                           CONTENT_SETTINGS_TYPE_PLUGINS,
77                           "java-plugin")->GetAsInteger(&actual_value));
78  EXPECT_EQ(1, actual_value);
79  EXPECT_EQ(NULL, map.GetValue(GURL("http://www.google.com"),
80                               GURL("http://www.google.com"),
81                               CONTENT_SETTINGS_TYPE_PLUGINS,
82                               "flash-plugin"));
83  // Delete non-existing value.
84  map.DeleteValue(
85      ContentSettingsPattern::FromString("[*.]google.com"),
86      ContentSettingsPattern::FromString("[*.]google.com"),
87      CONTENT_SETTINGS_TYPE_PLUGINS,
88      "flash-plugin");
89  EXPECT_EQ(NULL, map.GetValue(GURL("http://www.google.com"),
90                               GURL("http://www.google.com"),
91                               CONTENT_SETTINGS_TYPE_PLUGINS,
92                               "flash-plugin"));
93  EXPECT_TRUE(map.GetValue(GURL("http://www.google.com"),
94                           GURL("http://www.google.com"),
95                           CONTENT_SETTINGS_TYPE_PLUGINS,
96                           "java-plugin")->GetAsInteger(&actual_value));
97  EXPECT_EQ(1, actual_value);
98
99  // Delete existing value.
100  map.DeleteValue(
101      ContentSettingsPattern::FromString("[*.]google.com"),
102      ContentSettingsPattern::FromString("[*.]google.com"),
103      CONTENT_SETTINGS_TYPE_PLUGINS,
104      "java-plugin");
105
106  EXPECT_EQ(NULL, map.GetValue(GURL("http://www.google.com"),
107                               GURL("http://www.google.com"),
108                               CONTENT_SETTINGS_TYPE_PLUGINS,
109                               "java-plugin"));
110}
111
112TEST(OriginIdentifierValueMapTest, Clear) {
113  content_settings::OriginIdentifierValueMap map;
114  EXPECT_TRUE(map.empty());
115
116  // Set two values.
117  map.SetValue(ContentSettingsPattern::FromString("[*.]google.com"),
118               ContentSettingsPattern::FromString("[*.]google.com"),
119               CONTENT_SETTINGS_TYPE_PLUGINS,
120               "java-plugin",
121               new base::FundamentalValue(1));
122  map.SetValue(ContentSettingsPattern::FromString("[*.]google.com"),
123               ContentSettingsPattern::FromString("[*.]google.com"),
124               CONTENT_SETTINGS_TYPE_COOKIES,
125               std::string(),
126               new base::FundamentalValue(1));
127  EXPECT_FALSE(map.empty());
128  int actual_value;
129  EXPECT_TRUE(map.GetValue(GURL("http://www.google.com"),
130                           GURL("http://www.google.com"),
131                           CONTENT_SETTINGS_TYPE_PLUGINS,
132                           "java-plugin")->GetAsInteger(&actual_value));
133  EXPECT_EQ(1, actual_value);
134
135  // Clear the map.
136  map.clear();
137  EXPECT_TRUE(map.empty());
138  EXPECT_EQ(NULL, map.GetValue(GURL("http://www.google.com"),
139                               GURL("http://www.google.com"),
140                               CONTENT_SETTINGS_TYPE_PLUGINS,
141                               "java-plugin"));
142}
143
144TEST(OriginIdentifierValueMapTest, ListEntryPrecedences) {
145  content_settings::OriginIdentifierValueMap map;
146
147  map.SetValue(ContentSettingsPattern::FromString("[*.]google.com"),
148               ContentSettingsPattern::FromString("[*.]google.com"),
149               CONTENT_SETTINGS_TYPE_COOKIES,
150               std::string(),
151               new base::FundamentalValue(1));
152
153  map.SetValue(ContentSettingsPattern::FromString("www.google.com"),
154               ContentSettingsPattern::FromString("[*.]google.com"),
155               CONTENT_SETTINGS_TYPE_COOKIES,
156               std::string(),
157               new base::FundamentalValue(2));
158
159  int actual_value;
160  EXPECT_TRUE(map.GetValue(GURL("http://mail.google.com"),
161                           GURL("http://www.google.com"),
162                           CONTENT_SETTINGS_TYPE_COOKIES,
163                           std::string())->GetAsInteger(&actual_value));
164  EXPECT_EQ(1, actual_value);
165
166  EXPECT_TRUE(map.GetValue(GURL("http://www.google.com"),
167                           GURL("http://www.google.com"),
168                           CONTENT_SETTINGS_TYPE_COOKIES,
169                           std::string())->GetAsInteger(&actual_value));
170  EXPECT_EQ(2, actual_value);
171}
172
173TEST(OriginIdentifierValueMapTest, IterateEmpty) {
174  content_settings::OriginIdentifierValueMap map;
175  scoped_ptr<content_settings::RuleIterator> rule_iterator(
176      map.GetRuleIterator(CONTENT_SETTINGS_TYPE_COOKIES, std::string(), NULL));
177  EXPECT_FALSE(rule_iterator->HasNext());
178}
179
180TEST(OriginIdentifierValueMapTest, IterateNonempty) {
181  // Verify the precedence order.
182  content_settings::OriginIdentifierValueMap map;
183  ContentSettingsPattern pattern =
184      ContentSettingsPattern::FromString("[*.]google.com");
185  ContentSettingsPattern sub_pattern =
186      ContentSettingsPattern::FromString("sub.google.com");
187  map.SetValue(pattern,
188               ContentSettingsPattern::Wildcard(),
189               CONTENT_SETTINGS_TYPE_COOKIES,
190               std::string(),
191               new base::FundamentalValue(1));
192  map.SetValue(sub_pattern,
193               ContentSettingsPattern::Wildcard(),
194               CONTENT_SETTINGS_TYPE_COOKIES,
195               std::string(),
196               new base::FundamentalValue(2));
197
198  scoped_ptr<content_settings::RuleIterator> rule_iterator(
199      map.GetRuleIterator(CONTENT_SETTINGS_TYPE_COOKIES, std::string(), NULL));
200  ASSERT_TRUE(rule_iterator->HasNext());
201  content_settings::Rule rule = rule_iterator->Next();
202  EXPECT_EQ(sub_pattern, rule.primary_pattern);
203  EXPECT_EQ(2, content_settings::ValueToContentSetting(rule.value.get()));
204
205  ASSERT_TRUE(rule_iterator->HasNext());
206  rule = rule_iterator->Next();
207  EXPECT_EQ(pattern, rule.primary_pattern);
208  EXPECT_EQ(1, content_settings::ValueToContentSetting(rule.value.get()));
209}
210