OLD | NEW |
(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, |
| 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}, |
| 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 // TODO(msramek): Also test invalid inputs using libFuzzer. |
| 92 TEST_F(ClearSiteDataThrottleTest, InvalidHeader) { |
| 93 struct TestCase { |
| 94 const char* header; |
| 95 const char* console_message; |
| 96 } test_cases[] = { |
| 97 {"", "Not a valid JSON.\n"}, |
| 98 {"\"unclosed quote", "Not a valid JSON.\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 "Must only contain ASCII characters.\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 |
OLD | NEW |