| OLD | NEW |
| 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.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using content::BrowserThread; | 16 using content::BrowserThread; |
| 17 using base::FilePath; | |
| 18 | 17 |
| 19 namespace pnacl_cache { | 18 namespace pnacl_cache { |
| 20 | 19 |
| 21 class PNaClTranslationCacheTest : public testing::Test { | 20 class PNaClTranslationCacheTest : public testing::Test { |
| 22 protected: | 21 protected: |
| 23 PNaClTranslationCacheTest() | 22 PNaClTranslationCacheTest() |
| 24 : cache_thread_(BrowserThread::CACHE, &message_loop_), | 23 : cache_thread_(BrowserThread::CACHE, &message_loop_), |
| 25 io_thread_(BrowserThread::IO, &message_loop_) {} | 24 io_thread_(BrowserThread::IO, &message_loop_) {} |
| 26 virtual ~PNaClTranslationCacheTest() {} | 25 virtual ~PNaClTranslationCacheTest() {} |
| 27 virtual void SetUp() { cache_ = new PNaClTranslationCache(); } | 26 virtual void SetUp() { cache_ = new PNaClTranslationCache(); } |
| 28 virtual void TearDown() { | 27 virtual void TearDown() { |
| 29 // The destructor of PNaClTranslationCacheWriteEntry posts a task to the IO | 28 // The destructor of PNaClTranslationCacheWriteEntry posts a task to the IO |
| 30 // 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 |
| 31 // 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 |
| 32 // 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 |
| 33 // here to ensure the task gets processed. | 32 // here to ensure the task gets processed. |
| 34 base::RunLoop().RunUntilIdle(); | 33 base::RunLoop().RunUntilIdle(); |
| 35 delete cache_; | 34 delete cache_; |
| 36 } | 35 } |
| 37 | 36 |
| 38 void InitBackend(bool in_mem); | |
| 39 void StoreNexe(const std::string& key, const std::string& nexe); | |
| 40 std::string GetNexe(const std::string& key); | |
| 41 | |
| 42 protected: | 37 protected: |
| 43 PNaClTranslationCache* cache_; | 38 PNaClTranslationCache* cache_; |
| 44 base::MessageLoopForIO message_loop_; | 39 base::MessageLoopForIO message_loop_; |
| 45 content::TestBrowserThread cache_thread_; | 40 content::TestBrowserThread cache_thread_; |
| 46 content::TestBrowserThread io_thread_; | 41 content::TestBrowserThread io_thread_; |
| 47 base::ScopedTempDir temp_dir_; | |
| 48 }; | 42 }; |
| 49 | 43 |
| 50 void PNaClTranslationCacheTest::InitBackend(bool in_mem) { | 44 TEST_F(PNaClTranslationCacheTest, StoreOneInMem) { |
| 51 net::TestCompletionCallback init_cb; | 45 net::TestCompletionCallback init_cb; |
| 52 if (!in_mem) { | 46 int rv = cache_->InitCache(base::FilePath(), true, init_cb.callback()); |
| 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 47 EXPECT_EQ(net::OK, rv); |
| 54 } | |
| 55 int rv = cache_->InitCache(temp_dir_.path(), in_mem, init_cb.callback()); | |
| 56 if (in_mem) | |
| 57 ASSERT_EQ(net::OK, rv); | |
| 58 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); | 48 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); |
| 59 ASSERT_EQ(0, cache_->Size()); | |
| 60 } | |
| 61 | |
| 62 void PNaClTranslationCacheTest::StoreNexe(const std::string& key, | |
| 63 const std::string& nexe) { | |
| 64 net::TestCompletionCallback store_cb; | 49 net::TestCompletionCallback store_cb; |
| 65 cache_->StoreNexe(key, nexe, store_cb.callback()); | 50 EXPECT_EQ(0, cache_->Size()); |
| 51 cache_->StoreNexe("1", "a", store_cb.callback()); |
| 66 // Using ERR_IO_PENDING here causes the callback to wait for the result | 52 // 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 | 53 // 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. | 54 // we don't plumb the intermediate writing stages all the way out. |
| 69 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); | 55 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); |
| 70 } | |
| 71 | |
| 72 std::string PNaClTranslationCacheTest::GetNexe(const std::string& key) { | |
| 73 net::TestCompletionCallback load_cb; | |
| 74 std::string nexe; | |
| 75 cache_->GetNexe(key, &nexe, load_cb.callback()); | |
| 76 EXPECT_EQ(net::OK, load_cb.GetResult(net::ERR_IO_PENDING)); | |
| 77 return nexe; | |
| 78 } | |
| 79 | |
| 80 static const std::string test_key("1"); | |
| 81 static const std::string test_store_val("testnexe"); | |
| 82 | |
| 83 TEST_F(PNaClTranslationCacheTest, StoreSmallInMem) { | |
| 84 // Test that a single store puts something in the mem backend | |
| 85 InitBackend(true); | |
| 86 StoreNexe(test_key, test_store_val); | |
| 87 EXPECT_EQ(1, cache_->Size()); | 56 EXPECT_EQ(1, cache_->Size()); |
| 88 } | 57 } |
| 89 | 58 |
| 90 TEST_F(PNaClTranslationCacheTest, StoreSmallOnDisk) { | 59 TEST_F(PNaClTranslationCacheTest, StoreOneOnDisk) { |
| 91 // Test that a single store puts something in the disk backend | 60 base::ScopedTempDir temp_dir; |
| 92 InitBackend(false); | 61 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 93 StoreNexe(test_key, test_store_val); | 62 net::TestCompletionCallback init_cb; |
| 94 EXPECT_EQ(1, cache_->Size()); | 63 int rv = cache_->InitCache(temp_dir.path(), false, init_cb.callback()); |
| 95 } | 64 EXPECT_TRUE(rv); |
| 96 | 65 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); |
| 97 TEST_F(PNaClTranslationCacheTest, StoreLargeOnDisk) { | 66 EXPECT_EQ(0, cache_->Size()); |
| 98 // Test a value too large(?) for a single I/O operation | 67 net::TestCompletionCallback store_cb; |
| 99 // TODO(dschuff): we only seem to ever have one operation go through into the | 68 cache_->StoreNexe("1", "a", store_cb.callback()); |
| 100 // backend. Find out what the 'offset' field means, and if it can ever require | 69 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); |
| 101 // multiple writes. | |
| 102 InitBackend(false); | |
| 103 const std::string large_buffer(kMaxMemCacheSize + 1, 'a'); | |
| 104 StoreNexe(test_key, large_buffer); | |
| 105 EXPECT_EQ(1, cache_->Size()); | 70 EXPECT_EQ(1, cache_->Size()); |
| 106 } | 71 } |
| 107 | 72 |
| 108 TEST_F(PNaClTranslationCacheTest, InMemSizeLimit) { | 73 TEST_F(PNaClTranslationCacheTest, InMemSizeLimit) { |
| 109 InitBackend(true); | 74 net::TestCompletionCallback init_cb; |
| 110 const std::string large_buffer(kMaxMemCacheSize + 1, 'a'); | 75 int rv = cache_->InitCache(base::FilePath(), true, init_cb.callback()); |
| 76 EXPECT_EQ(rv, net::OK); |
| 77 ASSERT_EQ(init_cb.GetResult(rv), net::OK); |
| 78 EXPECT_EQ(cache_->Size(), 0); |
| 79 std::string large_buffer(kMaxMemCacheSize + 1, 'a'); |
| 111 net::TestCompletionCallback store_cb; | 80 net::TestCompletionCallback store_cb; |
| 112 cache_->StoreNexe(test_key, large_buffer, store_cb.callback()); | 81 cache_->StoreNexe("1", large_buffer, store_cb.callback()); |
| 113 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING)); | 82 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING)); |
| 114 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed. | 83 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed. |
| 115 EXPECT_EQ(0, cache_->Size()); | 84 EXPECT_EQ(0, cache_->Size()); |
| 116 } | 85 } |
| 117 | 86 |
| 118 TEST_F(PNaClTranslationCacheTest, GetOneInMem) { | |
| 119 InitBackend(true); | |
| 120 StoreNexe(test_key, test_store_val); | |
| 121 EXPECT_EQ(1, cache_->Size()); | |
| 122 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); | |
| 123 } | |
| 124 | |
| 125 TEST_F(PNaClTranslationCacheTest, GetLargeOnDisk) { | |
| 126 InitBackend(false); | |
| 127 const std::string large_buffer(kMaxMemCacheSize + 1, 'a'); | |
| 128 StoreNexe(test_key, large_buffer); | |
| 129 EXPECT_EQ(1, cache_->Size()); | |
| 130 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer)); | |
| 131 } | |
| 132 | |
| 133 TEST_F(PNaClTranslationCacheTest, StoreTwice) { | |
| 134 // Test that storing twice with the same key overwrites | |
| 135 InitBackend(true); | |
| 136 StoreNexe(test_key, test_store_val); | |
| 137 StoreNexe(test_key, test_store_val + "aaa"); | |
| 138 EXPECT_EQ(1, cache_->Size()); | |
| 139 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa")); | |
| 140 } | |
| 141 | |
| 142 TEST_F(PNaClTranslationCacheTest, StoreTwo) { | |
| 143 InitBackend(true); | |
| 144 StoreNexe(test_key, test_store_val); | |
| 145 StoreNexe(test_key + "a", test_store_val + "aaa"); | |
| 146 EXPECT_EQ(2, cache_->Size()); | |
| 147 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); | |
| 148 EXPECT_EQ(0, GetNexe(test_key + "a").compare(test_store_val + "aaa")); | |
| 149 } | |
| 150 | |
| 151 TEST_F(PNaClTranslationCacheTest, GetMiss) { | |
| 152 InitBackend(true); | |
| 153 StoreNexe(test_key, test_store_val); | |
| 154 net::TestCompletionCallback load_cb; | |
| 155 std::string nexe; | |
| 156 cache_->GetNexe(test_key + "a", &nexe, load_cb.callback()); | |
| 157 EXPECT_EQ(net::ERR_FAILED, load_cb.GetResult(net::ERR_IO_PENDING)); | |
| 158 } | |
| 159 | |
| 160 } // namespace nacl_cache | 87 } // namespace nacl_cache |
| OLD | NEW |