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

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: rebase 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
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()));
jvoung (off chromium) 2013/07/30 22:51:39 Does DrainableIOBuffer take ownership of the thing
Derek Schuff 2013/07/31 00:04:16 It keeps a scoped_refptr to what it wraps, so when
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 NexeCallback 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 NexeCallback 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 NexeCallback 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("http://www.google.com/;0;0;1601:1:1:0:0:0:0;",
124 PnaclTranslationCache::GetKey(info));
125 // Check that query portion of URL is not stripped
126 info.pexe_url = GURL("http://www.google.com/?foo=bar");
127 EXPECT_EQ("http://www.google.com/?foo=bar;0;0;1601:1:1:0:0:0:0;",
128 PnaclTranslationCache::GetKey(info));
129 // Check that username, password, and normal port are stripped
130 info.pexe_url = GURL("https://user:host@www.google.com:443/");
131 EXPECT_EQ("https://www.google.com/;0;0;1601:1:1:0:0:0:0;",
132 PnaclTranslationCache::GetKey(info));
133 // Check that unusual port is not stripped but ref is stripped
134 info.pexe_url = GURL("https://www.google.com:444/#foo");
135 EXPECT_EQ("https://www.google.com:444/;0;0;1601:1:1:0:0:0:0;",
136 PnaclTranslationCache::GetKey(info));
137 // Check that ABI version, opt level, and etag are in the key
138 info.pexe_url = GURL("http://www.google.com/");
139 info.abi_version = 2;
140 EXPECT_EQ("http://www.google.com/;2;0;1601:1:1:0:0:0:0;",
141 PnaclTranslationCache::GetKey(info));
142 info.opt_level = 2;
143 EXPECT_EQ("http://www.google.com/;2;2;1601:1:1:0:0:0:0;",
144 PnaclTranslationCache::GetKey(info));
145 info.etag = std::string("etag");
146 EXPECT_EQ("http://www.google.com/;2;2;1601:1:1:0:0:0:0;etag",
147 PnaclTranslationCache::GetKey(info));
148
149 // Check for all the time components
150 std::string some_time("Wed, 15 Nov 1995 06:25:24 GMT");
151 base::Time::FromString(some_time.c_str(), &info.last_modified);
152 EXPECT_EQ("http://www.google.com/;2;2;1995:11:15:6:25:24:0;etag",
153 PnaclTranslationCache::GetKey(info));
154 some_time.assign("Fri, 29 Feb 2008 13:04:12 GMT");
155 base::Time::FromString(some_time.c_str(), &info.last_modified);
156 EXPECT_EQ("http://www.google.com/;2;2;2008:2:29:13:4:12:0;etag",
157 PnaclTranslationCache::GetKey(info));
158 }
80 159
81 TEST_F(PnaclTranslationCacheTest, StoreSmallInMem) { 160 TEST_F(PnaclTranslationCacheTest, StoreSmallInMem) {
82 // Test that a single store puts something in the mem backend 161 // Test that a single store puts something in the mem backend
83 InitBackend(true); 162 InitBackend(true);
84 StoreNexe(test_key, test_store_val); 163 StoreNexe(test_key, test_store_val);
85 EXPECT_EQ(1, cache_->Size()); 164 EXPECT_EQ(1, cache_->Size());
86 } 165 }
87 166
88 TEST_F(PnaclTranslationCacheTest, StoreSmallOnDisk) { 167 TEST_F(PnaclTranslationCacheTest, StoreSmallOnDisk) {
89 // Test that a single store puts something in the disk backend 168 // Test that a single store puts something in the disk backend
90 InitBackend(false); 169 InitBackend(false);
91 StoreNexe(test_key, test_store_val); 170 StoreNexe(test_key, test_store_val);
92 EXPECT_EQ(1, cache_->Size()); 171 EXPECT_EQ(1, cache_->Size());
93 } 172 }
94 173
95 TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) { 174 TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) {
96 // Test a value too large(?) for a single I/O operation 175 // 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 176 // 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 177 // backend. Find out what the 'offset' field means, and if it can ever require
99 // multiple writes. 178 // multiple writes.
100 InitBackend(false); 179 InitBackend(false);
101 const std::string large_buffer(kLargeNexeSize, 'a'); 180 const std::string large_buffer(kLargeNexeSize, 'a');
102 StoreNexe(test_key, large_buffer); 181 StoreNexe(test_key, large_buffer);
103 EXPECT_EQ(1, cache_->Size()); 182 EXPECT_EQ(1, cache_->Size());
104 } 183 }
105 184
106 TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) { 185 TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) {
107 InitBackend(true); 186 InitBackend(true);
108 const std::string large_buffer(kMaxMemCacheSize + 1, 'a'); 187 scoped_refptr<net::DrainableIOBuffer> large_buffer(new net::DrainableIOBuffer(
188 new net::StringIOBuffer(std::string(kMaxMemCacheSize + 1, 'a')),
189 kMaxMemCacheSize + 1));
109 net::TestCompletionCallback store_cb; 190 net::TestCompletionCallback store_cb;
110 cache_->StoreNexe(test_key, large_buffer, store_cb.callback()); 191 cache_->StoreNexe(test_key, large_buffer, store_cb.callback());
111 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING)); 192 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING));
112 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed. 193 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed.
113 EXPECT_EQ(0, cache_->Size()); 194 EXPECT_EQ(0, cache_->Size());
114 } 195 }
115 196
116 TEST_F(PnaclTranslationCacheTest, GetOneInMem) { 197 TEST_F(PnaclTranslationCacheTest, GetOneInMem) {
117 InitBackend(true); 198 InitBackend(true);
118 StoreNexe(test_key, test_store_val); 199 StoreNexe(test_key, test_store_val);
119 EXPECT_EQ(1, cache_->Size()); 200 EXPECT_EQ(1, cache_->Size());
120 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); 201 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
121 } 202 }
122 203
204 TEST_F(PnaclTranslationCacheTest, GetOneOnDisk) {
205 InitBackend(false);
206 StoreNexe(test_key, test_store_val);
207 EXPECT_EQ(1, cache_->Size());
208 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
209 }
210
123 TEST_F(PnaclTranslationCacheTest, GetLargeOnDisk) { 211 TEST_F(PnaclTranslationCacheTest, GetLargeOnDisk) {
124 InitBackend(false); 212 InitBackend(false);
125 const std::string large_buffer(kLargeNexeSize, 'a'); 213 const std::string large_buffer(kLargeNexeSize, 'a');
126 StoreNexe(test_key, large_buffer); 214 StoreNexe(test_key, large_buffer);
127 EXPECT_EQ(1, cache_->Size()); 215 EXPECT_EQ(1, cache_->Size());
128 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer)); 216 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer));
129 } 217 }
130 218
131 TEST_F(PnaclTranslationCacheTest, StoreTwice) { 219 TEST_F(PnaclTranslationCacheTest, StoreTwice) {
132 // Test that storing twice with the same key overwrites 220 // Test that storing twice with the same key overwrites
133 InitBackend(true); 221 InitBackend(true);
134 StoreNexe(test_key, test_store_val); 222 StoreNexe(test_key, test_store_val);
135 StoreNexe(test_key, test_store_val + "aaa"); 223 StoreNexe(test_key, test_store_val + "aaa");
136 EXPECT_EQ(1, cache_->Size()); 224 EXPECT_EQ(1, cache_->Size());
137 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa")); 225 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa"));
138 } 226 }
139 227
140 TEST_F(PnaclTranslationCacheTest, StoreTwo) { 228 TEST_F(PnaclTranslationCacheTest, StoreTwo) {
141 InitBackend(true); 229 InitBackend(true);
142 StoreNexe(test_key, test_store_val); 230 StoreNexe(test_key, test_store_val);
143 StoreNexe(test_key + "a", test_store_val + "aaa"); 231 StoreNexe(test_key + "a", test_store_val + "aaa");
144 EXPECT_EQ(2, cache_->Size()); 232 EXPECT_EQ(2, cache_->Size());
145 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); 233 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
146 EXPECT_EQ(0, GetNexe(test_key + "a").compare(test_store_val + "aaa")); 234 EXPECT_EQ(0, GetNexe(test_key + "a").compare(test_store_val + "aaa"));
147 } 235 }
148 236
149 TEST_F(PnaclTranslationCacheTest, GetMiss) { 237 TEST_F(PnaclTranslationCacheTest, GetMiss) {
150 InitBackend(true); 238 InitBackend(true);
151 StoreNexe(test_key, test_store_val); 239 StoreNexe(test_key, test_store_val);
152 net::TestCompletionCallback load_cb; 240 TestNexeCallback load_cb;
153 std::string nexe; 241 std::string nexe;
154 cache_->GetNexe(test_key + "a", &nexe, load_cb.callback()); 242 cache_->GetNexe(test_key + "a", load_cb.callback());
155 EXPECT_EQ(net::ERR_FAILED, load_cb.GetResult(net::ERR_IO_PENDING)); 243 int rv;
244 scoped_refptr<net::DrainableIOBuffer> buf(load_cb.GetResult(&rv));
245 EXPECT_EQ(net::ERR_FAILED, rv);
156 } 246 }
157 247
158 } // namespace pnacl 248 } // namespace pnacl
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698