Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(918)

Side by Side Diff: content/browser/browsing_data/clear_site_data_throttle_unittest.cc

Issue 2025683003: First experimental implementation of the Clear-Site-Data header (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Many changes, most importantly synchronous deletion. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "content/browser/browsing_data/clear_site_data_throttle.h"
6
7 #include <memory>
8
9 #include "base/command_line.h"
10 #include "content/public/common/content_switches.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace content {
15
16 class ClearSiteDataThrottleTest : public testing::Test {
17 public:
18 void SetUp() override {
19 base::CommandLine::ForCurrentProcess()->AppendSwitch(
20 switches::kEnableExperimentalWebPlatformFeatures);
21 throttle_ = ClearSiteDataThrottle::CreateThrottleForNavigation(nullptr);
22 }
23
24 ClearSiteDataThrottle* GetThrottle() {
25 return static_cast<ClearSiteDataThrottle*>(throttle_.get());
26 }
27
28 private:
29 std::unique_ptr<NavigationThrottle> throttle_;
30 };
31
32 TEST_F(ClearSiteDataThrottleTest, ParseHeader) {
33 struct TestCase {
34 const char* header;
35 bool cookies;
36 bool storage;
37 bool cache;
38 } test_cases[] = {
39 // One data type.
40 {"{ \"types\": [\"cookies\"] }", true, false, false},
41 {"{ \"types\": [\"storage\"] }", false, true, false},
42 {"{ \"types\": [\"cache\"] }", false, false, true},
43
44 // Two data types.
45 {"{ \"types\": [\"cookies\", \"storage\"] }", true, true, false},
46 {"{ \"types\": [\"cookies\", \"cache\"] }", true, false, true},
47 {"{ \"types\": [\"storage\", \"cache\"] }", false, true, true},
48
49 // Three data types.
50 {"{ \"types\": [\"storage\", \"cache\", \"cookies\"] }", true, true,
51 true},
52 {"{ \"types\": [\"cache\", \"cookies\", \"storage\"] }", true, true,
53 true},
54 {"{ \"types\": [\"cookies\", \"storage\", \"cache\"] }", true, true,
55 true},
56
57 // Different formatting.
58 {" { \"types\": [\"cookies\" ]}", true, false, false},
59
60 // Duplicates.
61 {"{ \"types\": [\"cookies\", \"cookies\"] }", true, false, false},
62
63 // Other entries in the dictionary.
64 {"{ \"types\": [\"storage\"], \"other_params\": {} }", false, true,
nasko 2016/08/11 20:07:22 Shouldn't we be failing the parsing in this case?
msramek 2016/08/12 15:06:28 See the comment about compatibility below. If the
65 false},
66
67 // Unknown types are ignored, but we still proceed with the deletion for
68 // those that we recognize.
69 {"{ \"types\": [\"cache\", \"foo\"] }", false, false, true},
nasko 2016/08/11 20:07:22 Typos are easy to occur, so I think it is best for
msramek 2016/08/12 15:06:28 We do handle that :) See the "Invalid type:" error
nasko 2016/08/12 21:44:53 Acknowledged.
70 };
71
72 for (const TestCase& test_case : test_cases) {
73 SCOPED_TRACE(test_case.header);
74
75 bool actual_cookies;
76 bool actual_storage;
77 bool actual_cache;
78
79 std::vector<ClearSiteDataThrottle::ConsoleMessage> messages;
80
81 EXPECT_TRUE(GetThrottle()->ParseHeader(test_case.header, &actual_cookies,
82 &actual_storage, &actual_cache,
83 &messages));
84
85 EXPECT_EQ(test_case.cookies, actual_cookies);
86 EXPECT_EQ(test_case.storage, actual_storage);
87 EXPECT_EQ(test_case.cache, actual_cache);
88 }
89 }
90
91 TEST_F(ClearSiteDataThrottleTest, InvalidHeader) {
nasko 2016/08/11 20:07:22 I would also suggest looking at libFuzzer and writ
msramek 2016/08/12 15:06:28 Yep, that sounds like something we might want :) A
92 struct TestCase {
93 const char* header;
94 const char* console_message;
95 } test_cases[] = {
96 {"", "Not a valid JSON or non-ASCII characters present.\n"},
97 {"\"unclosed quote",
98 "Not a valid JSON or non-ASCII characters present.\n"},
99 {"\"some text\"", "Expecting a JSON dictionary with a 'types' field.\n"},
100 {"{ \"field\" : {} }",
101 "Expecting a JSON dictionary with a 'types' field.\n"},
102 {"{ \"types\" : [ \"passwords\" ] }",
103 "Invalid type: \"passwords\".\n"
104 "No valid types specified in the 'types' field.\n"},
105 {"{ \"types\" : [ [ \"list in a list\" ] ] }",
106 "Invalid type: [\"list in a list\"].\n"
107 "No valid types specified in the 'types' field.\n"},
108 {"{ \"types\" : [ \"кукис\", \"сторидж\", \"кэш\" ]",
109 "Not a valid JSON or non-ASCII characters present.\n"}};
110
111 for (const TestCase& test_case : test_cases) {
112 SCOPED_TRACE(test_case.header);
113
114 bool actual_cookies;
115 bool actual_storage;
116 bool actual_cache;
117
118 std::vector<ClearSiteDataThrottle::ConsoleMessage> messages;
119
120 EXPECT_FALSE(GetThrottle()->ParseHeader(test_case.header, &actual_cookies,
121 &actual_storage, &actual_cache,
122 &messages));
123
124 std::string multiline_message;
125 for (const auto& message : messages) {
126 EXPECT_EQ(CONSOLE_MESSAGE_LEVEL_ERROR, message.level);
127 multiline_message += message.text + "\n";
128 }
129
130 EXPECT_EQ(test_case.console_message, multiline_message);
131 }
132 }
133
134 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698