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

Side by Side Diff: content/browser/appcache/chrome_appcache_service_unittest.cc

Issue 7210006: AppCaches which belong to hosted apps are not protected from deletion (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: More cleanup. Created 9 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "chrome/test/appcache_util.h"
9 #include "chrome/test/testing_browser_process.h" 10 #include "chrome/test/testing_browser_process.h"
10 #include "chrome/test/testing_browser_process_test.h" 11 #include "chrome/test/testing_browser_process_test.h"
11 #include "content/browser/appcache/chrome_appcache_service.h" 12 #include "content/browser/appcache/chrome_appcache_service.h"
12 #include "content/browser/browser_thread.h" 13 #include "content/browser/browser_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/appcache/appcache_database.h"
14 #include "webkit/appcache/appcache_storage_impl.h" 16 #include "webkit/appcache/appcache_storage_impl.h"
17 #include "webkit/quota/mock_special_storage_policy.h"
18
19 #include <set>
15 20
16 namespace { 21 namespace {
17 const FilePath::CharType kTestingAppCacheDirname[] = 22 const FilePath::CharType kTestingAppCacheDirname[] =
18 FILE_PATH_LITERAL("Application Cache"); 23 FILE_PATH_LITERAL("Application Cache");
19 } 24
25 // Examples of a protected and an unprotected origin, to be used througout the
26 // test.
27 const GURL kProtectedManifest("http://www.protected.com/cache.manifest");
28 const GURL kNormalManifest("http://www.normal.com/cache.manifest");
michaeln 2011/07/15 19:15:46 Can you make these non-primitives data members of
marja(google) 2011/07/18 09:55:41 Done for this file. But I left the GURLs in browsi
29
30 } // namespace
20 31
21 namespace appcache { 32 namespace appcache {
22 33
23 class ChromeAppCacheServiceTest : public TestingBrowserProcessTest { 34 class ChromeAppCacheServiceTest : public TestingBrowserProcessTest {
24 public: 35 public:
25 ChromeAppCacheServiceTest() 36 ChromeAppCacheServiceTest()
26 : message_loop_(MessageLoop::TYPE_IO), 37 : message_loop_(MessageLoop::TYPE_IO),
27 db_thread_(BrowserThread::DB, &message_loop_), 38 db_thread_(BrowserThread::DB, &message_loop_),
28 file_thread_(BrowserThread::FILE, &message_loop_), 39 file_thread_(BrowserThread::FILE, &message_loop_),
29 cache_thread_(BrowserThread::CACHE, &message_loop_), 40 cache_thread_(BrowserThread::CACHE, &message_loop_),
30 io_thread_(BrowserThread::IO, &message_loop_) { 41 io_thread_(BrowserThread::IO, &message_loop_) {
31 } 42 }
32 43
33 protected: 44 protected:
45 void CreateAppCacheService(
46 scoped_refptr<ChromeAppCacheService>& appache_service,
47 const FilePath& appcache_path,
48 bool init_storage);
49 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service);
50
34 MessageLoop message_loop_; 51 MessageLoop message_loop_;
35 ScopedTempDir temp_dir_; 52 ScopedTempDir temp_dir_;
36 53
37 private: 54 private:
38 BrowserThread db_thread_; 55 BrowserThread db_thread_;
39 BrowserThread file_thread_; 56 BrowserThread file_thread_;
40 BrowserThread cache_thread_; 57 BrowserThread cache_thread_;
41 BrowserThread io_thread_; 58 BrowserThread io_thread_;
42 }; 59 };
43 60
44 TEST_F(ChromeAppCacheServiceTest, KeepOnDestruction) { 61 void ChromeAppCacheServiceTest::CreateAppCacheService(
45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 62 scoped_refptr<ChromeAppCacheService>& appcache_service,
michaeln 2011/07/15 19:15:46 Google style thing... we discourage non-const refe
marja(google) 2011/07/18 09:55:41 The reason for not returning scoped_refptr was to
marja(google) 2011/07/18 15:03:20 Hmm, it seems that ASSSERT_* in a non-test functio
marja(google) 2011/07/18 15:03:20 Done.
46 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); 63 const FilePath& appcache_path,
47 scoped_refptr<ChromeAppCacheService> appcache_service = 64 bool init_storage) {
48 new ChromeAppCacheService(NULL); 65 appcache_service = new ChromeAppCacheService(NULL);
49 const content::ResourceContext* resource_context = NULL; 66 const content::ResourceContext* resource_context = NULL;
67 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
68 new quota::MockSpecialStoragePolicy;
69 mock_policy->AddProtected(kProtectedManifest.GetOrigin());
50 BrowserThread::PostTask( 70 BrowserThread::PostTask(
51 BrowserThread::IO, FROM_HERE, 71 BrowserThread::IO, FROM_HERE,
52 NewRunnableMethod(appcache_service.get(), 72 NewRunnableMethod(appcache_service.get(),
53 &ChromeAppCacheService::InitializeOnIOThread, 73 &ChromeAppCacheService::InitializeOnIOThread,
54 appcache_path, 74 appcache_path,
55 resource_context, 75 resource_context,
56 scoped_refptr<quota::SpecialStoragePolicy>(NULL), 76 mock_policy));
57 false));
58 // Make the steps needed to initialize the storage of AppCache data. 77 // Make the steps needed to initialize the storage of AppCache data.
59 message_loop_.RunAllPending(); 78 message_loop_.RunAllPending();
60 appcache::AppCacheStorageImpl* storage = 79 if (init_storage) {
61 static_cast<appcache::AppCacheStorageImpl*>(appcache_service->storage()); 80 appcache::AppCacheStorageImpl* storage =
62 ASSERT_TRUE(storage->database_->db_connection()); 81 static_cast<appcache::AppCacheStorageImpl*>(
63 ASSERT_EQ(1, storage->NewCacheId()); 82 appcache_service->storage());
64 storage->disk_cache(); 83 ASSERT_TRUE(storage->database_->db_connection());
65 message_loop_.RunAllPending(); 84 ASSERT_EQ(1, storage->NewCacheId());
85 storage->disk_cache();
86 message_loop_.RunAllPending();
87 }
88 }
66 89
90 void ChromeAppCacheServiceTest::InsertDataIntoAppCache(
91 ChromeAppCacheService* appcache_service) {
92 AppCacheInserter appcache_inserter;
93 appcache_inserter.AddGroupAndCache(appcache_service, kNormalManifest);
94 appcache_inserter.AddGroupAndCache(appcache_service, kProtectedManifest);
95
96 // Verify that adding the data succeeded
97 AppCacheReader reader(appcache_service);
98 std::set<GURL>* origins = reader.ReadAppCaches();
99 ASSERT_EQ(2UL, origins->size());
100 ASSERT_TRUE(origins->find(kProtectedManifest.GetOrigin()) != origins->end());
101 ASSERT_TRUE(origins->find(kNormalManifest.GetOrigin()) != origins->end());
102 }
103
104 TEST_F(ChromeAppCacheServiceTest, KeepOnDestruction) {
105 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
106 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname);
107
108 // Create a ChromeAppCacheService and insert data into it
109 scoped_refptr<ChromeAppCacheService> appcache_service;
110 CreateAppCacheService(appcache_service, appcache_path, true);
67 ASSERT_TRUE(file_util::PathExists(appcache_path)); 111 ASSERT_TRUE(file_util::PathExists(appcache_path));
68 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); 112 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index")));
113 InsertDataIntoAppCache(appcache_service);
69 114
115 // Test: delete the ChromeAppCacheService
116 appcache_service->set_clear_local_state_on_exit(false);
70 appcache_service = NULL; 117 appcache_service = NULL;
71 message_loop_.RunAllPending(); 118 message_loop_.RunAllPending();
72 119
120 // Recreate the appcache (for reading the data back)
121 CreateAppCacheService(appcache_service, appcache_path, false);
122
123 // The directory is still there
73 ASSERT_TRUE(file_util::PathExists(appcache_path)); 124 ASSERT_TRUE(file_util::PathExists(appcache_path));
125
126 // The appcache data is also there
127 AppCacheReader reader(appcache_service);
128 std::set<GURL>* origins = reader.ReadAppCaches();
129 EXPECT_EQ(2UL, origins->size());
130 EXPECT_TRUE(origins->find(kProtectedManifest.GetOrigin()) != origins->end());
131 EXPECT_TRUE(origins->find(kNormalManifest.GetOrigin()) != origins->end());
74 } 132 }
75 133
76 TEST_F(ChromeAppCacheServiceTest, RemoveOnDestruction) { 134 TEST_F(ChromeAppCacheServiceTest, RemoveOnDestruction) {
77 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 135 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
78 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); 136 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname);
79 scoped_refptr<ChromeAppCacheService> appcache_service =
80 new ChromeAppCacheService(NULL);
81 const content::ResourceContext* resource_context = NULL;
82 BrowserThread::PostTask(
83 BrowserThread::IO, FROM_HERE,
84 NewRunnableMethod(appcache_service.get(),
85 &ChromeAppCacheService::InitializeOnIOThread,
86 appcache_path,
87 resource_context,
88 scoped_refptr<quota::SpecialStoragePolicy>(NULL),
89 true));
90 // Make the steps needed to initialize the storage of AppCache data.
91 message_loop_.RunAllPending();
92 appcache::AppCacheStorageImpl* storage =
93 static_cast<appcache::AppCacheStorageImpl*>(appcache_service->storage());
94 ASSERT_TRUE(storage->database_->db_connection());
95 ASSERT_EQ(1, storage->NewCacheId());
96 storage->disk_cache();
97 message_loop_.RunAllPending();
98 137
138 // Create a ChromeAppCacheService and insert data into it
139 scoped_refptr<ChromeAppCacheService> appcache_service;
140 CreateAppCacheService(appcache_service, appcache_path, true);
99 ASSERT_TRUE(file_util::PathExists(appcache_path)); 141 ASSERT_TRUE(file_util::PathExists(appcache_path));
100 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); 142 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index")));
143 InsertDataIntoAppCache(appcache_service);
101 144
145 // Test: delete the ChromeAppCacheService
146 appcache_service->set_clear_local_state_on_exit(true);
102 appcache_service = NULL; 147 appcache_service = NULL;
103 message_loop_.RunAllPending(); 148 message_loop_.RunAllPending();
104 149
105 ASSERT_FALSE(file_util::PathExists(appcache_path)); 150 // Recreate the appcache (for reading the data back)
151 CreateAppCacheService(appcache_service, appcache_path, false);
152
153 // The directory is still there
154 ASSERT_TRUE(file_util::PathExists(appcache_path));
155
156 // The appcache data for the protected origin is there, and the data for the
157 // unprotected origin was deleted.
158 AppCacheReader reader(appcache_service);
159 std::set<GURL>* origins = reader.ReadAppCaches();
160 EXPECT_EQ(1UL, origins->size());
161 EXPECT_TRUE(origins->find(kProtectedManifest.GetOrigin()) != origins->end());
162 EXPECT_TRUE(origins->find(kNormalManifest.GetOrigin()) == origins->end());
106 } 163 }
107 164
108 } // namespace appcache 165 } // namespace appcache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698