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

Side by Side Diff: chrome/browser/nacl_host/pnacl_translation_cache_unittest.cc

Issue 21075004: Clean up PnaclTranslationCache interface (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review Created 7 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/nacl_host/pnacl_translation_cache.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/nacl_host/pnacl_translation_cache.h" 5 #include "chrome/browser/nacl_host/pnacl_translation_cache.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "components/nacl/common/pnacl_types.h"
11 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
12 #include "content/public/test/test_browser_thread_bundle.h" 13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/base/io_buffer.h"
13 #include "net/base/test_completion_callback.h" 15 #include "net/base/test_completion_callback.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 using content::BrowserThread; 18 using content::BrowserThread;
17 using base::FilePath; 19 using base::FilePath;
18 20
19 namespace pnacl { 21 namespace pnacl {
20 22
21 class PnaclTranslationCacheTest : public testing::Test { 23 class PnaclTranslationCacheTest : public testing::Test {
22 protected: 24 protected:
23 PnaclTranslationCacheTest() 25 PnaclTranslationCacheTest()
24 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} 26 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
25 virtual ~PnaclTranslationCacheTest() {} 27 virtual ~PnaclTranslationCacheTest() {}
26 virtual void SetUp() { cache_ = new PnaclTranslationCache(); } 28 virtual void SetUp() { cache_ = new PnaclTranslationCache(); }
27 virtual void TearDown() { 29 virtual void TearDown() {
28 // The destructor of PnaclTranslationCacheWriteEntry posts a task to the IO 30 // The destructor of PnaclTranslationCacheWriteEntry posts a task to the IO
29 // thread to close the backend cache entry. We want to make sure the entries 31 // thread to close the backend cache entry. We want to make sure the entries
30 // are closed before we delete the backend (and in particular the destructor 32 // are closed before we delete the backend (and in particular the destructor
31 // for the memory backend has a DCHECK to verify this), so we run the loop 33 // for the memory backend has a DCHECK to verify this), so we run the loop
32 // here to ensure the task gets processed. 34 // here to ensure the task gets processed.
33 base::RunLoop().RunUntilIdle(); 35 base::RunLoop().RunUntilIdle();
34 delete cache_; 36 delete cache_;
35 } 37 }
36 38
37 void InitBackend(bool in_mem); 39 void InitBackend(bool in_mem);
38 void StoreNexe(const std::string& key, const std::string& nexe); 40 void StoreNexe(const std::string& key, const std::string& nexe);
39 std::string GetNexe(const std::string& key); 41 std::string GetNexe(const std::string& key);
40 42
41 protected:
42 PnaclTranslationCache* cache_; 43 PnaclTranslationCache* cache_;
43 content::TestBrowserThreadBundle thread_bundle_; 44 content::TestBrowserThreadBundle thread_bundle_;
44 base::ScopedTempDir temp_dir_; 45 base::ScopedTempDir temp_dir_;
45 }; 46 };
46 47
47 void PnaclTranslationCacheTest::InitBackend(bool in_mem) { 48 void PnaclTranslationCacheTest::InitBackend(bool in_mem) {
48 net::TestCompletionCallback init_cb; 49 net::TestCompletionCallback init_cb;
49 if (!in_mem) { 50 if (!in_mem) {
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
51 } 52 }
52 int rv = cache_->InitCache(temp_dir_.path(), in_mem, init_cb.callback()); 53 int rv = cache_->InitCache(temp_dir_.path(), in_mem, init_cb.callback());
53 if (in_mem) 54 if (in_mem)
54 ASSERT_EQ(net::OK, rv); 55 ASSERT_EQ(net::OK, rv);
55 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); 56 ASSERT_EQ(net::OK, init_cb.GetResult(rv));
56 ASSERT_EQ(0, cache_->Size()); 57 ASSERT_EQ(0, cache_->Size());
57 } 58 }
58 59
59 void PnaclTranslationCacheTest::StoreNexe(const std::string& key, 60 void PnaclTranslationCacheTest::StoreNexe(const std::string& key,
60 const std::string& nexe) { 61 const std::string& nexe) {
61 net::TestCompletionCallback store_cb; 62 net::TestCompletionCallback store_cb;
62 cache_->StoreNexe(key, nexe, store_cb.callback()); 63 scoped_refptr<net::DrainableIOBuffer> nexe_buf(
64 new net::DrainableIOBuffer(new net::StringIOBuffer(nexe), nexe.size()));
65 cache_->StoreNexe(key, nexe_buf, store_cb.callback());
63 // Using ERR_IO_PENDING here causes the callback to wait for the result 66 // Using ERR_IO_PENDING here causes the callback to wait for the result
64 // which should be harmless even if it returns OK immediately. This is because 67 // which should be harmless even if it returns OK immediately. This is because
65 // we don't plumb the intermediate writing stages all the way out. 68 // we don't plumb the intermediate writing stages all the way out.
66 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); 69 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING));
67 } 70 }
68 71
72 // Inspired by net::TestCompletionCallback. Instantiate a TestNexeCallback and
73 // pass the GetNexeCallback returned by the callback() method to GetNexe.
74 // Then call GetResult, which will pump the message loop until it gets a result,
75 // return the resulting IOBuffer and fill in the return value
76 class TestNexeCallback {
77 public:
78 TestNexeCallback()
79 : have_result_(false),
80 result_(-1),
81 cb_(base::Bind(&TestNexeCallback::SetResult, base::Unretained(this))) {}
82 GetNexeCallback callback() { return cb_; }
83 net::DrainableIOBuffer* GetResult(int* result) {
84 while (!have_result_)
85 base::RunLoop().RunUntilIdle();
86 have_result_ = false;
87 *result = result_;
88 return buf_.get();
89 }
90
91 private:
92 void SetResult(int rv, scoped_refptr<net::DrainableIOBuffer> buf) {
93 have_result_ = true;
94 result_ = rv;
95 buf_ = buf;
96 }
97 bool have_result_;
98 int result_;
99 scoped_refptr<net::DrainableIOBuffer> buf_;
100 const GetNexeCallback cb_;
101 };
102
69 std::string PnaclTranslationCacheTest::GetNexe(const std::string& key) { 103 std::string PnaclTranslationCacheTest::GetNexe(const std::string& key) {
70 net::TestCompletionCallback load_cb; 104 TestNexeCallback load_cb;
71 std::string nexe; 105 cache_->GetNexe(key, load_cb.callback());
72 cache_->GetNexe(key, &nexe, load_cb.callback()); 106 int rv;
73 EXPECT_EQ(net::OK, load_cb.GetResult(net::ERR_IO_PENDING)); 107 scoped_refptr<net::DrainableIOBuffer> buf(load_cb.GetResult(&rv));
108 EXPECT_EQ(net::OK, rv);
109 std::string nexe(buf->data(), buf->size());
74 return nexe; 110 return nexe;
75 } 111 }
76 112
77 static const std::string test_key("1"); 113 static const std::string test_key("1");
78 static const std::string test_store_val("testnexe"); 114 static const std::string test_store_val("testnexe");
79 static const int kLargeNexeSize = 16 * 1024 *1024; 115 static const int kLargeNexeSize = 16 * 1024 * 1024;
116
117 TEST(PnaclTranslationCacheKeyTest, CacheKeyTest) {
118 nacl::PnaclCacheInfo info;
119 info.pexe_url = GURL("http://www.google.com");
120 info.abi_version = 0;
121 info.opt_level = 0;
122 // Basic check for URL and time components
123 EXPECT_EQ("ABI:0;opt:0;URL:http://www.google.com/;"
124 "modified:1601:1:1:0:0:0:0:UTC;etag:",
125 PnaclTranslationCache::GetKey(info));
126 // Check that query portion of URL is not stripped
127 info.pexe_url = GURL("http://www.google.com/?foo=bar");
128 EXPECT_EQ("ABI:0;opt:0;URL:http://www.google.com/?foo=bar;"
129 "modified:1601:1:1:0:0:0:0:UTC;etag:",
130 PnaclTranslationCache::GetKey(info));
131 // Check that username, password, and normal port are stripped
132 info.pexe_url = GURL("https://user:host@www.google.com:443/");
133 EXPECT_EQ("ABI:0;opt:0;URL:https://www.google.com/;"
134 "modified:1601:1:1:0:0:0:0:UTC;etag:",
135 PnaclTranslationCache::GetKey(info));
136 // Check that unusual port is not stripped but ref is stripped
137 info.pexe_url = GURL("https://www.google.com:444/#foo");
138 EXPECT_EQ("ABI:0;opt:0;URL:https://www.google.com:444/;"
139 "modified:1601:1:1:0:0:0:0:UTC;etag:",
140 PnaclTranslationCache::GetKey(info));
jvoung (off chromium) 2013/07/31 00:58:38 Could also check that other schemes like chrome-ex
Derek Schuff 2013/07/31 05:38:34 Done.
141 // Check that ABI version, opt level, and etag are in the key
142 info.pexe_url = GURL("http://www.google.com/");
143 info.abi_version = 2;
144 EXPECT_EQ("ABI:2;opt:0;URL:http://www.google.com/;"
145 "modified:1601:1:1:0:0:0:0:UTC;etag:",
146 PnaclTranslationCache::GetKey(info));
147 info.opt_level = 2;
148 EXPECT_EQ("ABI:2;opt:2;URL:http://www.google.com/;"
149 "modified:1601:1:1:0:0:0:0:UTC;etag:",
150 PnaclTranslationCache::GetKey(info));
151 info.etag = std::string("etag");
152 EXPECT_EQ("ABI:2;opt:2;URL:http://www.google.com/;"
153 "modified:1601:1:1:0:0:0:0:UTC;etag:etag",
154 PnaclTranslationCache::GetKey(info));
155
156 // Check for all the time components
157 std::string some_time("Wed, 15 Nov 1995 06:25:24 GMT");
158 base::Time::FromString(some_time.c_str(), &info.last_modified);
159 EXPECT_EQ("ABI:2;opt:2;URL:http://www.google.com/;"
160 "modified:1995:11:15:6:25:24:0:UTC;etag:etag",
161 PnaclTranslationCache::GetKey(info));
162 some_time.assign("Fri, 29 Feb 2008 13:04:12 GMT");
163 base::Time::FromString(some_time.c_str(), &info.last_modified);
164 EXPECT_EQ("ABI:2;opt:2;URL:http://www.google.com/;"
165 "modified:2008:2:29:13:4:12:0:UTC;etag:etag",
166 PnaclTranslationCache::GetKey(info));
167 }
80 168
81 TEST_F(PnaclTranslationCacheTest, StoreSmallInMem) { 169 TEST_F(PnaclTranslationCacheTest, StoreSmallInMem) {
82 // Test that a single store puts something in the mem backend 170 // Test that a single store puts something in the mem backend
83 InitBackend(true); 171 InitBackend(true);
84 StoreNexe(test_key, test_store_val); 172 StoreNexe(test_key, test_store_val);
85 EXPECT_EQ(1, cache_->Size()); 173 EXPECT_EQ(1, cache_->Size());
86 } 174 }
87 175
88 TEST_F(PnaclTranslationCacheTest, StoreSmallOnDisk) { 176 TEST_F(PnaclTranslationCacheTest, StoreSmallOnDisk) {
89 // Test that a single store puts something in the disk backend 177 // Test that a single store puts something in the disk backend
90 InitBackend(false); 178 InitBackend(false);
91 StoreNexe(test_key, test_store_val); 179 StoreNexe(test_key, test_store_val);
92 EXPECT_EQ(1, cache_->Size()); 180 EXPECT_EQ(1, cache_->Size());
93 } 181 }
94 182
95 TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) { 183 TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) {
96 // Test a value too large(?) for a single I/O operation 184 // Test a value too large(?) for a single I/O operation
97 // TODO(dschuff): we only seem to ever have one operation go through into the 185 // TODO(dschuff): we only seem to ever have one operation go through into the
98 // backend. Find out what the 'offset' field means, and if it can ever require 186 // backend. Find out what the 'offset' field means, and if it can ever require
jvoung (off chromium) 2013/07/31 00:58:38 Is this TODO still relevant w/ change to Drainable
Derek Schuff 2013/07/31 05:38:34 Yes, the part that interacts with the disk_cache i
99 // multiple writes. 187 // multiple writes.
100 InitBackend(false); 188 InitBackend(false);
101 const std::string large_buffer(kLargeNexeSize, 'a'); 189 const std::string large_buffer(kLargeNexeSize, 'a');
102 StoreNexe(test_key, large_buffer); 190 StoreNexe(test_key, large_buffer);
103 EXPECT_EQ(1, cache_->Size()); 191 EXPECT_EQ(1, cache_->Size());
104 } 192 }
105 193
106 TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) { 194 TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) {
107 InitBackend(true); 195 InitBackend(true);
108 const std::string large_buffer(kMaxMemCacheSize + 1, 'a'); 196 scoped_refptr<net::DrainableIOBuffer> large_buffer(new net::DrainableIOBuffer(
197 new net::StringIOBuffer(std::string(kMaxMemCacheSize + 1, 'a')),
198 kMaxMemCacheSize + 1));
109 net::TestCompletionCallback store_cb; 199 net::TestCompletionCallback store_cb;
110 cache_->StoreNexe(test_key, large_buffer, store_cb.callback()); 200 cache_->StoreNexe(test_key, large_buffer, store_cb.callback());
111 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING)); 201 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING));
112 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed. 202 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed.
113 EXPECT_EQ(0, cache_->Size()); 203 EXPECT_EQ(0, cache_->Size());
114 } 204 }
115 205
116 TEST_F(PnaclTranslationCacheTest, GetOneInMem) { 206 TEST_F(PnaclTranslationCacheTest, GetOneInMem) {
117 InitBackend(true); 207 InitBackend(true);
118 StoreNexe(test_key, test_store_val); 208 StoreNexe(test_key, test_store_val);
119 EXPECT_EQ(1, cache_->Size()); 209 EXPECT_EQ(1, cache_->Size());
120 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); 210 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
121 } 211 }
122 212
213 TEST_F(PnaclTranslationCacheTest, GetOneOnDisk) {
214 InitBackend(false);
215 StoreNexe(test_key, test_store_val);
216 EXPECT_EQ(1, cache_->Size());
217 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
218 }
219
123 TEST_F(PnaclTranslationCacheTest, GetLargeOnDisk) { 220 TEST_F(PnaclTranslationCacheTest, GetLargeOnDisk) {
124 InitBackend(false); 221 InitBackend(false);
125 const std::string large_buffer(kLargeNexeSize, 'a'); 222 const std::string large_buffer(kLargeNexeSize, 'a');
126 StoreNexe(test_key, large_buffer); 223 StoreNexe(test_key, large_buffer);
127 EXPECT_EQ(1, cache_->Size()); 224 EXPECT_EQ(1, cache_->Size());
128 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer)); 225 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer));
129 } 226 }
130 227
131 TEST_F(PnaclTranslationCacheTest, StoreTwice) { 228 TEST_F(PnaclTranslationCacheTest, StoreTwice) {
132 // Test that storing twice with the same key overwrites 229 // Test that storing twice with the same key overwrites
133 InitBackend(true); 230 InitBackend(true);
134 StoreNexe(test_key, test_store_val); 231 StoreNexe(test_key, test_store_val);
135 StoreNexe(test_key, test_store_val + "aaa"); 232 StoreNexe(test_key, test_store_val + "aaa");
136 EXPECT_EQ(1, cache_->Size()); 233 EXPECT_EQ(1, cache_->Size());
137 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa")); 234 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa"));
138 } 235 }
139 236
140 TEST_F(PnaclTranslationCacheTest, StoreTwo) { 237 TEST_F(PnaclTranslationCacheTest, StoreTwo) {
141 InitBackend(true); 238 InitBackend(true);
142 StoreNexe(test_key, test_store_val); 239 StoreNexe(test_key, test_store_val);
143 StoreNexe(test_key + "a", test_store_val + "aaa"); 240 StoreNexe(test_key + "a", test_store_val + "aaa");
144 EXPECT_EQ(2, cache_->Size()); 241 EXPECT_EQ(2, cache_->Size());
145 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); 242 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
146 EXPECT_EQ(0, GetNexe(test_key + "a").compare(test_store_val + "aaa")); 243 EXPECT_EQ(0, GetNexe(test_key + "a").compare(test_store_val + "aaa"));
147 } 244 }
148 245
149 TEST_F(PnaclTranslationCacheTest, GetMiss) { 246 TEST_F(PnaclTranslationCacheTest, GetMiss) {
150 InitBackend(true); 247 InitBackend(true);
151 StoreNexe(test_key, test_store_val); 248 StoreNexe(test_key, test_store_val);
152 net::TestCompletionCallback load_cb; 249 TestNexeCallback load_cb;
153 std::string nexe; 250 std::string nexe;
154 cache_->GetNexe(test_key + "a", &nexe, load_cb.callback()); 251 cache_->GetNexe(test_key + "a", load_cb.callback());
155 EXPECT_EQ(net::ERR_FAILED, load_cb.GetResult(net::ERR_IO_PENDING)); 252 int rv;
253 scoped_refptr<net::DrainableIOBuffer> buf(load_cb.GetResult(&rv));
254 EXPECT_EQ(net::ERR_FAILED, rv);
156 } 255 }
157 256
158 } // namespace pnacl 257 } // namespace pnacl
OLDNEW
« no previous file with comments | « chrome/browser/nacl_host/pnacl_translation_cache.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698