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

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

Issue 21065007: Revert 214755 "Clean up PnaclTranslationCache interface" (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: 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 | « trunk/src/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"
12 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread_bundle.h" 12 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/test_completion_callback.h" 13 #include "net/base/test_completion_callback.h"
16 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
17 15
18 using content::BrowserThread; 16 using content::BrowserThread;
19 using base::FilePath; 17 using base::FilePath;
20 18
21 namespace pnacl { 19 namespace pnacl {
22 20
23 class PnaclTranslationCacheTest : public testing::Test { 21 class PnaclTranslationCacheTest : public testing::Test {
24 protected: 22 protected:
25 PnaclTranslationCacheTest() 23 PnaclTranslationCacheTest()
26 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} 24 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
27 virtual ~PnaclTranslationCacheTest() {} 25 virtual ~PnaclTranslationCacheTest() {}
28 virtual void SetUp() { cache_ = new PnaclTranslationCache(); } 26 virtual void SetUp() { cache_ = new PnaclTranslationCache(); }
29 virtual void TearDown() { 27 virtual void TearDown() {
30 // The destructor of PnaclTranslationCacheWriteEntry posts a task to the IO 28 // The destructor of PnaclTranslationCacheWriteEntry posts a task to the IO
31 // thread to close the backend cache entry. We want to make sure the entries 29 // thread to close the backend cache entry. We want to make sure the entries
32 // are closed before we delete the backend (and in particular the destructor 30 // are closed before we delete the backend (and in particular the destructor
33 // for the memory backend has a DCHECK to verify this), so we run the loop 31 // for the memory backend has a DCHECK to verify this), so we run the loop
34 // here to ensure the task gets processed. 32 // here to ensure the task gets processed.
35 base::RunLoop().RunUntilIdle(); 33 base::RunLoop().RunUntilIdle();
36 delete cache_; 34 delete cache_;
37 } 35 }
38 36
39 void InitBackend(bool in_mem); 37 void InitBackend(bool in_mem);
40 void StoreNexe(const std::string& key, const std::string& nexe); 38 void StoreNexe(const std::string& key, const std::string& nexe);
41 std::string GetNexe(const std::string& key); 39 std::string GetNexe(const std::string& key);
42 40
41 protected:
43 PnaclTranslationCache* cache_; 42 PnaclTranslationCache* cache_;
44 content::TestBrowserThreadBundle thread_bundle_; 43 content::TestBrowserThreadBundle thread_bundle_;
45 base::ScopedTempDir temp_dir_; 44 base::ScopedTempDir temp_dir_;
46 }; 45 };
47 46
48 void PnaclTranslationCacheTest::InitBackend(bool in_mem) { 47 void PnaclTranslationCacheTest::InitBackend(bool in_mem) {
49 net::TestCompletionCallback init_cb; 48 net::TestCompletionCallback init_cb;
50 if (!in_mem) { 49 if (!in_mem) {
51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
52 } 51 }
53 int rv = cache_->InitCache(temp_dir_.path(), in_mem, init_cb.callback()); 52 int rv = cache_->InitCache(temp_dir_.path(), in_mem, init_cb.callback());
54 if (in_mem) 53 if (in_mem)
55 ASSERT_EQ(net::OK, rv); 54 ASSERT_EQ(net::OK, rv);
56 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); 55 ASSERT_EQ(net::OK, init_cb.GetResult(rv));
57 ASSERT_EQ(0, cache_->Size()); 56 ASSERT_EQ(0, cache_->Size());
58 } 57 }
59 58
60 void PnaclTranslationCacheTest::StoreNexe(const std::string& key, 59 void PnaclTranslationCacheTest::StoreNexe(const std::string& key,
61 const std::string& nexe) { 60 const std::string& nexe) {
62 net::TestCompletionCallback store_cb; 61 net::TestCompletionCallback store_cb;
63 scoped_refptr<net::DrainableIOBuffer> nexe_buf( 62 cache_->StoreNexe(key, nexe, store_cb.callback());
64 new net::DrainableIOBuffer(new net::StringIOBuffer(nexe), nexe.size()));
65 cache_->StoreNexe(key, nexe_buf, store_cb.callback());
66 // Using ERR_IO_PENDING here causes the callback to wait for the result 63 // Using ERR_IO_PENDING here causes the callback to wait for the result
67 // which should be harmless even if it returns OK immediately. This is because 64 // which should be harmless even if it returns OK immediately. This is because
68 // we don't plumb the intermediate writing stages all the way out. 65 // we don't plumb the intermediate writing stages all the way out.
69 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); 66 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING));
70 } 67 }
71 68
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
103 std::string PnaclTranslationCacheTest::GetNexe(const std::string& key) { 69 std::string PnaclTranslationCacheTest::GetNexe(const std::string& key) {
104 TestNexeCallback load_cb; 70 net::TestCompletionCallback load_cb;
105 cache_->GetNexe(key, load_cb.callback()); 71 std::string nexe;
106 int rv; 72 cache_->GetNexe(key, &nexe, load_cb.callback());
107 scoped_refptr<net::DrainableIOBuffer> buf(load_cb.GetResult(&rv)); 73 EXPECT_EQ(net::OK, load_cb.GetResult(net::ERR_IO_PENDING));
108 EXPECT_EQ(net::OK, rv);
109 std::string nexe(buf->data(), buf->size());
110 return nexe; 74 return nexe;
111 } 75 }
112 76
113 static const std::string test_key("1"); 77 static const std::string test_key("1");
114 static const std::string test_store_val("testnexe"); 78 static const std::string test_store_val("testnexe");
115 static const int kLargeNexeSize = 16 * 1024 * 1024; 79 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));
141 // Check chrome-extesnsion scheme
142 info.pexe_url = GURL("chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/");
143 EXPECT_EQ("ABI:0;opt:0;"
144 "URL:chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/;"
145 "modified:1601:1:1:0:0:0:0:UTC;etag:",
146 PnaclTranslationCache::GetKey(info));
147 // Check that ABI version, opt level, and etag are in the key
148 info.pexe_url = GURL("http://www.google.com/");
149 info.abi_version = 2;
150 EXPECT_EQ("ABI:2;opt:0;URL:http://www.google.com/;"
151 "modified:1601:1:1:0:0:0:0:UTC;etag:",
152 PnaclTranslationCache::GetKey(info));
153 info.opt_level = 2;
154 EXPECT_EQ("ABI:2;opt:2;URL:http://www.google.com/;"
155 "modified:1601:1:1:0:0:0:0:UTC;etag:",
156 PnaclTranslationCache::GetKey(info));
157 info.etag = std::string("etag");
158 EXPECT_EQ("ABI:2;opt:2;URL:http://www.google.com/;"
159 "modified:1601:1:1:0:0:0:0:UTC;etag:etag",
160 PnaclTranslationCache::GetKey(info));
161
162 // Check for all the time components
163 std::string some_time("Wed, 15 Nov 1995 06:25:24 GMT");
164 base::Time::FromString(some_time.c_str(), &info.last_modified);
165 EXPECT_EQ("ABI:2;opt:2;URL:http://www.google.com/;"
166 "modified:1995:11:15:6:25:24:0:UTC;etag:etag",
167 PnaclTranslationCache::GetKey(info));
168 some_time.assign("Fri, 29 Feb 2008 13:04:12 GMT");
169 base::Time::FromString(some_time.c_str(), &info.last_modified);
170 EXPECT_EQ("ABI:2;opt:2;URL:http://www.google.com/;"
171 "modified:2008:2:29:13:4:12:0:UTC;etag:etag",
172 PnaclTranslationCache::GetKey(info));
173 }
174 80
175 TEST_F(PnaclTranslationCacheTest, StoreSmallInMem) { 81 TEST_F(PnaclTranslationCacheTest, StoreSmallInMem) {
176 // Test that a single store puts something in the mem backend 82 // Test that a single store puts something in the mem backend
177 InitBackend(true); 83 InitBackend(true);
178 StoreNexe(test_key, test_store_val); 84 StoreNexe(test_key, test_store_val);
179 EXPECT_EQ(1, cache_->Size()); 85 EXPECT_EQ(1, cache_->Size());
180 } 86 }
181 87
182 TEST_F(PnaclTranslationCacheTest, StoreSmallOnDisk) { 88 TEST_F(PnaclTranslationCacheTest, StoreSmallOnDisk) {
183 // Test that a single store puts something in the disk backend 89 // Test that a single store puts something in the disk backend
184 InitBackend(false); 90 InitBackend(false);
185 StoreNexe(test_key, test_store_val); 91 StoreNexe(test_key, test_store_val);
186 EXPECT_EQ(1, cache_->Size()); 92 EXPECT_EQ(1, cache_->Size());
187 } 93 }
188 94
189 TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) { 95 TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) {
190 // Test a value too large(?) for a single I/O operation 96 // 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
98 // backend. Find out what the 'offset' field means, and if it can ever require
99 // multiple writes.
191 InitBackend(false); 100 InitBackend(false);
192 const std::string large_buffer(kLargeNexeSize, 'a'); 101 const std::string large_buffer(kLargeNexeSize, 'a');
193 StoreNexe(test_key, large_buffer); 102 StoreNexe(test_key, large_buffer);
194 EXPECT_EQ(1, cache_->Size()); 103 EXPECT_EQ(1, cache_->Size());
195 } 104 }
196 105
197 TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) { 106 TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) {
198 InitBackend(true); 107 InitBackend(true);
199 scoped_refptr<net::DrainableIOBuffer> large_buffer(new net::DrainableIOBuffer( 108 const std::string large_buffer(kMaxMemCacheSize + 1, 'a');
200 new net::StringIOBuffer(std::string(kMaxMemCacheSize + 1, 'a')),
201 kMaxMemCacheSize + 1));
202 net::TestCompletionCallback store_cb; 109 net::TestCompletionCallback store_cb;
203 cache_->StoreNexe(test_key, large_buffer, store_cb.callback()); 110 cache_->StoreNexe(test_key, large_buffer, store_cb.callback());
204 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING)); 111 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING));
205 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed. 112 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed.
206 EXPECT_EQ(0, cache_->Size()); 113 EXPECT_EQ(0, cache_->Size());
207 } 114 }
208 115
209 TEST_F(PnaclTranslationCacheTest, GetOneInMem) { 116 TEST_F(PnaclTranslationCacheTest, GetOneInMem) {
210 InitBackend(true); 117 InitBackend(true);
211 StoreNexe(test_key, test_store_val); 118 StoreNexe(test_key, test_store_val);
212 EXPECT_EQ(1, cache_->Size()); 119 EXPECT_EQ(1, cache_->Size());
213 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); 120 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
214 } 121 }
215 122
216 TEST_F(PnaclTranslationCacheTest, GetOneOnDisk) {
217 InitBackend(false);
218 StoreNexe(test_key, test_store_val);
219 EXPECT_EQ(1, cache_->Size());
220 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
221 }
222
223 TEST_F(PnaclTranslationCacheTest, GetLargeOnDisk) { 123 TEST_F(PnaclTranslationCacheTest, GetLargeOnDisk) {
224 InitBackend(false); 124 InitBackend(false);
225 const std::string large_buffer(kLargeNexeSize, 'a'); 125 const std::string large_buffer(kLargeNexeSize, 'a');
226 StoreNexe(test_key, large_buffer); 126 StoreNexe(test_key, large_buffer);
227 EXPECT_EQ(1, cache_->Size()); 127 EXPECT_EQ(1, cache_->Size());
228 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer)); 128 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer));
229 } 129 }
230 130
231 TEST_F(PnaclTranslationCacheTest, StoreTwice) { 131 TEST_F(PnaclTranslationCacheTest, StoreTwice) {
232 // Test that storing twice with the same key overwrites 132 // Test that storing twice with the same key overwrites
233 InitBackend(true); 133 InitBackend(true);
234 StoreNexe(test_key, test_store_val); 134 StoreNexe(test_key, test_store_val);
235 StoreNexe(test_key, test_store_val + "aaa"); 135 StoreNexe(test_key, test_store_val + "aaa");
236 EXPECT_EQ(1, cache_->Size()); 136 EXPECT_EQ(1, cache_->Size());
237 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa")); 137 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa"));
238 } 138 }
239 139
240 TEST_F(PnaclTranslationCacheTest, StoreTwo) { 140 TEST_F(PnaclTranslationCacheTest, StoreTwo) {
241 InitBackend(true); 141 InitBackend(true);
242 StoreNexe(test_key, test_store_val); 142 StoreNexe(test_key, test_store_val);
243 StoreNexe(test_key + "a", test_store_val + "aaa"); 143 StoreNexe(test_key + "a", test_store_val + "aaa");
244 EXPECT_EQ(2, cache_->Size()); 144 EXPECT_EQ(2, cache_->Size());
245 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); 145 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
246 EXPECT_EQ(0, GetNexe(test_key + "a").compare(test_store_val + "aaa")); 146 EXPECT_EQ(0, GetNexe(test_key + "a").compare(test_store_val + "aaa"));
247 } 147 }
248 148
249 TEST_F(PnaclTranslationCacheTest, GetMiss) { 149 TEST_F(PnaclTranslationCacheTest, GetMiss) {
250 InitBackend(true); 150 InitBackend(true);
251 StoreNexe(test_key, test_store_val); 151 StoreNexe(test_key, test_store_val);
252 TestNexeCallback load_cb; 152 net::TestCompletionCallback load_cb;
253 std::string nexe; 153 std::string nexe;
254 cache_->GetNexe(test_key + "a", load_cb.callback()); 154 cache_->GetNexe(test_key + "a", &nexe, load_cb.callback());
255 int rv; 155 EXPECT_EQ(net::ERR_FAILED, load_cb.GetResult(net::ERR_IO_PENDING));
256 scoped_refptr<net::DrainableIOBuffer> buf(load_cb.GetResult(&rv));
257 EXPECT_EQ(net::ERR_FAILED, rv);
258 } 156 }
259 157
260 } // namespace pnacl 158 } // namespace pnacl
OLDNEW
« no previous file with comments | « trunk/src/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