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

Unified 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, 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/nacl_host/pnacl_translation_cache_unittest.cc
diff --git a/chrome/browser/nacl_host/pnacl_translation_cache_unittest.cc b/chrome/browser/nacl_host/pnacl_translation_cache_unittest.cc
index b2e80615f7f6fb7c28d2e40d4b869ef86e43e031..9c4436558c7ff19ac6b15124020e3cf5c520f9d4 100644
--- a/chrome/browser/nacl_host/pnacl_translation_cache_unittest.cc
+++ b/chrome/browser/nacl_host/pnacl_translation_cache_unittest.cc
@@ -8,8 +8,10 @@
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
+#include "components/nacl/common/pnacl_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
+#include "net/base/io_buffer.h"
#include "net/base/test_completion_callback.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -38,7 +40,6 @@ class PnaclTranslationCacheTest : public testing::Test {
void StoreNexe(const std::string& key, const std::string& nexe);
std::string GetNexe(const std::string& key);
- protected:
PnaclTranslationCache* cache_;
content::TestBrowserThreadBundle thread_bundle_;
base::ScopedTempDir temp_dir_;
@@ -59,24 +60,102 @@ void PnaclTranslationCacheTest::InitBackend(bool in_mem) {
void PnaclTranslationCacheTest::StoreNexe(const std::string& key,
const std::string& nexe) {
net::TestCompletionCallback store_cb;
- cache_->StoreNexe(key, nexe, store_cb.callback());
+ scoped_refptr<net::DrainableIOBuffer> nexe_buf(
+ 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
+ cache_->StoreNexe(key, nexe_buf, store_cb.callback());
// Using ERR_IO_PENDING here causes the callback to wait for the result
// which should be harmless even if it returns OK immediately. This is because
// we don't plumb the intermediate writing stages all the way out.
EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING));
}
+// Inspired by net::TestCompletionCallback. Instantiate a TestNexeCallback and
+// pass the NexeCallback returned by the callback() method to GetNexe.
+// Then call GetResult, which will pump the message loop until it gets a result,
+// return the resulting IOBuffer and fill in the return value
+class TestNexeCallback {
+ public:
+ TestNexeCallback()
+ : have_result_(false),
+ result_(-1),
+ cb_(base::Bind(&TestNexeCallback::SetResult, base::Unretained(this))) {}
+ NexeCallback callback() { return cb_; }
+ net::DrainableIOBuffer* GetResult(int* result) {
+ while (!have_result_)
+ base::RunLoop().RunUntilIdle();
+ have_result_ = false;
+ *result = result_;
+ return buf_.get();
+ }
+
+ private:
+ void SetResult(int rv, scoped_refptr<net::DrainableIOBuffer> buf) {
+ have_result_ = true;
+ result_ = rv;
+ buf_ = buf;
+ }
+ bool have_result_;
+ int result_;
+ scoped_refptr<net::DrainableIOBuffer> buf_;
+ const NexeCallback cb_;
+};
+
std::string PnaclTranslationCacheTest::GetNexe(const std::string& key) {
- net::TestCompletionCallback load_cb;
- std::string nexe;
- cache_->GetNexe(key, &nexe, load_cb.callback());
- EXPECT_EQ(net::OK, load_cb.GetResult(net::ERR_IO_PENDING));
+ TestNexeCallback load_cb;
+ cache_->GetNexe(key, load_cb.callback());
+ int rv;
+ scoped_refptr<net::DrainableIOBuffer> buf(load_cb.GetResult(&rv));
+ EXPECT_EQ(net::OK, rv);
+ std::string nexe(buf->data(), buf->size());
return nexe;
}
static const std::string test_key("1");
static const std::string test_store_val("testnexe");
-static const int kLargeNexeSize = 16 * 1024 *1024;
+static const int kLargeNexeSize = 16 * 1024 * 1024;
+
+TEST(PnaclTranslationCacheKeyTest, CacheKeyTest) {
+ nacl::PnaclCacheInfo info;
+ info.pexe_url = GURL("http://www.google.com");
+ info.abi_version = 0;
+ info.opt_level = 0;
+ // Basic check for URL and time components
+ EXPECT_EQ("http://www.google.com/;0;0;1601:1:1:0:0:0:0;",
+ PnaclTranslationCache::GetKey(info));
+ // Check that query portion of URL is not stripped
+ info.pexe_url = GURL("http://www.google.com/?foo=bar");
+ EXPECT_EQ("http://www.google.com/?foo=bar;0;0;1601:1:1:0:0:0:0;",
+ PnaclTranslationCache::GetKey(info));
+ // Check that username, password, and normal port are stripped
+ info.pexe_url = GURL("https://user:host@www.google.com:443/");
+ EXPECT_EQ("https://www.google.com/;0;0;1601:1:1:0:0:0:0;",
+ PnaclTranslationCache::GetKey(info));
+ // Check that unusual port is not stripped but ref is stripped
+ info.pexe_url = GURL("https://www.google.com:444/#foo");
+ EXPECT_EQ("https://www.google.com:444/;0;0;1601:1:1:0:0:0:0;",
+ PnaclTranslationCache::GetKey(info));
+ // Check that ABI version, opt level, and etag are in the key
+ info.pexe_url = GURL("http://www.google.com/");
+ info.abi_version = 2;
+ EXPECT_EQ("http://www.google.com/;2;0;1601:1:1:0:0:0:0;",
+ PnaclTranslationCache::GetKey(info));
+ info.opt_level = 2;
+ EXPECT_EQ("http://www.google.com/;2;2;1601:1:1:0:0:0:0;",
+ PnaclTranslationCache::GetKey(info));
+ info.etag = std::string("etag");
+ EXPECT_EQ("http://www.google.com/;2;2;1601:1:1:0:0:0:0;etag",
+ PnaclTranslationCache::GetKey(info));
+
+ // Check for all the time components
+ std::string some_time("Wed, 15 Nov 1995 06:25:24 GMT");
+ base::Time::FromString(some_time.c_str(), &info.last_modified);
+ EXPECT_EQ("http://www.google.com/;2;2;1995:11:15:6:25:24:0;etag",
+ PnaclTranslationCache::GetKey(info));
+ some_time.assign("Fri, 29 Feb 2008 13:04:12 GMT");
+ base::Time::FromString(some_time.c_str(), &info.last_modified);
+ EXPECT_EQ("http://www.google.com/;2;2;2008:2:29:13:4:12:0;etag",
+ PnaclTranslationCache::GetKey(info));
+}
TEST_F(PnaclTranslationCacheTest, StoreSmallInMem) {
// Test that a single store puts something in the mem backend
@@ -105,7 +184,9 @@ TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) {
TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) {
InitBackend(true);
- const std::string large_buffer(kMaxMemCacheSize + 1, 'a');
+ scoped_refptr<net::DrainableIOBuffer> large_buffer(new net::DrainableIOBuffer(
+ new net::StringIOBuffer(std::string(kMaxMemCacheSize + 1, 'a')),
+ kMaxMemCacheSize + 1));
net::TestCompletionCallback store_cb;
cache_->StoreNexe(test_key, large_buffer, store_cb.callback());
EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING));
@@ -120,6 +201,13 @@ TEST_F(PnaclTranslationCacheTest, GetOneInMem) {
EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
}
+TEST_F(PnaclTranslationCacheTest, GetOneOnDisk) {
+ InitBackend(false);
+ StoreNexe(test_key, test_store_val);
+ EXPECT_EQ(1, cache_->Size());
+ EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
+}
+
TEST_F(PnaclTranslationCacheTest, GetLargeOnDisk) {
InitBackend(false);
const std::string large_buffer(kLargeNexeSize, 'a');
@@ -149,10 +237,12 @@ TEST_F(PnaclTranslationCacheTest, StoreTwo) {
TEST_F(PnaclTranslationCacheTest, GetMiss) {
InitBackend(true);
StoreNexe(test_key, test_store_val);
- net::TestCompletionCallback load_cb;
+ TestNexeCallback load_cb;
std::string nexe;
- cache_->GetNexe(test_key + "a", &nexe, load_cb.callback());
- EXPECT_EQ(net::ERR_FAILED, load_cb.GetResult(net::ERR_IO_PENDING));
+ cache_->GetNexe(test_key + "a", load_cb.callback());
+ int rv;
+ scoped_refptr<net::DrainableIOBuffer> buf(load_cb.GetResult(&rv));
+ EXPECT_EQ(net::ERR_FAILED, rv);
}
} // namespace pnacl

Powered by Google App Engine
This is Rietveld 408576698