OLD | NEW |
| (Empty) |
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 "base/file_path.h" | |
6 #include "base/file_util.h" | |
7 #include "content/browser/browser_thread_impl.h" | |
8 #include "content/browser/in_process_webkit/dom_storage_context_impl.h" | |
9 #include "content/test/test_browser_context.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "webkit/dom_storage/dom_storage_types.h" | |
12 #include "webkit/quota/mock_special_storage_policy.h" | |
13 | |
14 #ifdef ENABLE_NEW_DOM_STORAGE_BACKEND | |
15 // No longer applicable. | |
16 #else | |
17 | |
18 using content::BrowserContext; | |
19 using content::BrowserThread; | |
20 using content::BrowserThreadImpl; | |
21 | |
22 class DOMStorageTest : public testing::Test { | |
23 public: | |
24 DOMStorageTest() | |
25 : message_loop_(MessageLoop::TYPE_IO), | |
26 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), | |
27 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), | |
28 io_thread_(BrowserThread::IO, &message_loop_) { | |
29 } | |
30 | |
31 protected: | |
32 MessageLoop message_loop_; | |
33 | |
34 private: | |
35 BrowserThreadImpl webkit_thread_; | |
36 BrowserThreadImpl file_thread_; | |
37 BrowserThreadImpl io_thread_; | |
38 }; | |
39 | |
40 TEST_F(DOMStorageTest, SessionOnly) { | |
41 GURL session_only_origin("http://www.sessiononly.com"); | |
42 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | |
43 new quota::MockSpecialStoragePolicy; | |
44 special_storage_policy->AddSessionOnly(session_only_origin); | |
45 | |
46 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext); | |
47 | |
48 // Create databases for permanent and session-only origins. | |
49 FilePath domstorage_dir = browser_context->GetPath().Append( | |
50 DOMStorageContextImpl::kLocalStorageDirectory); | |
51 FilePath::StringType session_only_database( | |
52 FILE_PATH_LITERAL("http_www.sessiononly.com_0")); | |
53 FilePath::StringType permanent_database( | |
54 FILE_PATH_LITERAL("http_www.permanent.com_0")); | |
55 session_only_database.append(DOMStorageContextImpl::kLocalStorageExtension); | |
56 permanent_database.append(DOMStorageContextImpl::kLocalStorageExtension); | |
57 FilePath session_only_database_path = | |
58 domstorage_dir.Append(session_only_database); | |
59 FilePath permanent_database_path = | |
60 domstorage_dir.Append(permanent_database); | |
61 | |
62 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); | |
63 | |
64 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1)); | |
65 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1)); | |
66 | |
67 // Inject MockSpecialStoragePolicy into DOMStorageContext. | |
68 DOMStorageContextImpl* dom_storage_context = | |
69 static_cast<DOMStorageContextImpl*>( | |
70 BrowserContext::GetDOMStorageContext(browser_context.get())); | |
71 dom_storage_context->special_storage_policy_ = special_storage_policy; | |
72 | |
73 // Delete the TestBrowserContext but own the temp dir. This way the | |
74 // temporary data directory stays alive long enough to conduct the test. | |
75 ScopedTempDir temp_dir; | |
76 ignore_result(temp_dir.Set(browser_context->TakePath())); | |
77 message_loop_.RunAllPending(); | |
78 browser_context.reset(); | |
79 // Run the message loop to ensure that DOMStorageContext gets destroyed. | |
80 message_loop_.RunAllPending(); | |
81 | |
82 // Expected result: the database file for the permanent storage remains but | |
83 // the database for the session only storage was deleted. | |
84 EXPECT_FALSE(file_util::PathExists(session_only_database_path)); | |
85 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); | |
86 } | |
87 | |
88 TEST_F(DOMStorageTest, SaveSessionState) { | |
89 GURL session_only_origin("http://www.sessiononly.com"); | |
90 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | |
91 new quota::MockSpecialStoragePolicy; | |
92 special_storage_policy->AddSessionOnly(session_only_origin); | |
93 | |
94 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext); | |
95 | |
96 // Create databases for permanent and session-only origins. | |
97 FilePath domstorage_dir = browser_context->GetPath().Append( | |
98 DOMStorageContextImpl::kLocalStorageDirectory); | |
99 FilePath::StringType session_only_database( | |
100 FILE_PATH_LITERAL("http_www.sessiononly.com_0")); | |
101 FilePath::StringType permanent_database( | |
102 FILE_PATH_LITERAL("http_www.permanent.com_0")); | |
103 session_only_database.append(DOMStorageContextImpl::kLocalStorageExtension); | |
104 permanent_database.append(DOMStorageContextImpl::kLocalStorageExtension); | |
105 FilePath session_only_database_path = | |
106 domstorage_dir.Append(session_only_database); | |
107 FilePath permanent_database_path = | |
108 domstorage_dir.Append(permanent_database); | |
109 | |
110 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); | |
111 | |
112 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1)); | |
113 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1)); | |
114 | |
115 // Inject MockSpecialStoragePolicy into DOMStorageContext. | |
116 DOMStorageContextImpl* dom_storage_context = | |
117 static_cast<DOMStorageContextImpl*>( | |
118 BrowserContext::GetDOMStorageContext(browser_context.get())); | |
119 dom_storage_context->special_storage_policy_ = special_storage_policy; | |
120 | |
121 dom_storage_context->SetClearLocalState(true); | |
122 | |
123 // Save session state. This should bypass the destruction-time deletion. | |
124 dom_storage_context->SaveSessionState(); | |
125 | |
126 // Delete the TestBrowserContext but own the temp dir. This way the | |
127 // temporary data directory stays alive long enough to conduct the test. | |
128 ScopedTempDir temp_dir; | |
129 ignore_result(temp_dir.Set(browser_context->TakePath())); | |
130 message_loop_.RunAllPending(); | |
131 browser_context.reset(); | |
132 // Run the message loop to ensure that DOMStorageContext gets destroyed. | |
133 message_loop_.RunAllPending(); | |
134 | |
135 // Expected result: no database files were deleted because of | |
136 // SaveSessionState. | |
137 EXPECT_TRUE(file_util::PathExists(session_only_database_path)); | |
138 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); | |
139 } | |
140 | |
141 TEST_F(DOMStorageTest, ClearLocalState) { | |
142 // Create test files. | |
143 ScopedTempDir temp_dir; | |
144 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
145 FilePath domstorage_dir = temp_dir.path().Append( | |
146 DOMStorageContextImpl::kLocalStorageDirectory); | |
147 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); | |
148 | |
149 FilePath::StringType file_name_1(FILE_PATH_LITERAL("http_foo_0")); | |
150 file_name_1.append(DOMStorageContextImpl::kLocalStorageExtension); | |
151 FilePath::StringType file_name_2(FILE_PATH_LITERAL("chrome-devtools_foo_0")); | |
152 file_name_2.append(DOMStorageContextImpl::kLocalStorageExtension); | |
153 FilePath temp_file_path_1 = domstorage_dir.Append(file_name_1); | |
154 FilePath temp_file_path_2 = domstorage_dir.Append(file_name_2); | |
155 | |
156 ASSERT_EQ(1, file_util::WriteFile(temp_file_path_1, ".", 1)); | |
157 ASSERT_EQ(1, file_util::WriteFile(temp_file_path_2, "o", 1)); | |
158 | |
159 // Create the scope which will ensure we run the destructor of the webkit | |
160 // context which should trigger the clean up. | |
161 { | |
162 TestBrowserContext browser_context; | |
163 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | |
164 new quota::MockSpecialStoragePolicy; | |
165 special_storage_policy->AddProtected(GURL("chrome-devtools://")); | |
166 browser_context.SetSpecialStoragePolicy(special_storage_policy); | |
167 DOMStorageContextImpl* dom_storage_context = | |
168 static_cast<DOMStorageContextImpl*>( | |
169 BrowserContext::GetDOMStorageContext(&browser_context)); | |
170 dom_storage_context->set_data_path_for_testing(temp_dir.path()); | |
171 dom_storage_context->SetClearLocalState(true); | |
172 message_loop_.RunAllPending(); | |
173 } | |
174 message_loop_.RunAllPending(); | |
175 | |
176 // Because we specified https for scheme to be skipped the second file | |
177 // should survive and the first go into vanity. | |
178 ASSERT_FALSE(file_util::PathExists(temp_file_path_1)); | |
179 ASSERT_TRUE(file_util::PathExists(temp_file_path_2)); | |
180 } | |
181 | |
182 #endif // ENABLE_NEW_DOM_STORAGE_BACKEND | |
OLD | NEW |