| 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_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.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; | 17 using base::FilePath; |
| 18 | 18 |
| 19 namespace pnacl_cache { | 19 namespace pnacl_cache { |
| 20 | 20 |
| 21 class PNaClTranslationCacheTest : public testing::Test { | 21 class PnaclTranslationCacheTest : public testing::Test { |
| 22 protected: | 22 protected: |
| 23 PNaClTranslationCacheTest() | 23 PnaclTranslationCacheTest() |
| 24 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 24 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 25 virtual ~PNaClTranslationCacheTest() {} | 25 virtual ~PnaclTranslationCacheTest() {} |
| 26 virtual void SetUp() { cache_ = new PNaClTranslationCache(); } | 26 virtual void SetUp() { cache_ = new PnaclTranslationCache(); } |
| 27 virtual void TearDown() { | 27 virtual void TearDown() { |
| 28 // The destructor of PNaClTranslationCacheWriteEntry posts a task to the IO | 28 // 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 | 29 // 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 | 30 // 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 | 31 // for the memory backend has a DCHECK to verify this), so we run the loop |
| 32 // here to ensure the task gets processed. | 32 // here to ensure the task gets processed. |
| 33 base::RunLoop().RunUntilIdle(); | 33 base::RunLoop().RunUntilIdle(); |
| 34 delete cache_; | 34 delete cache_; |
| 35 } | 35 } |
| 36 | 36 |
| 37 void InitBackend(bool in_mem); | 37 void InitBackend(bool in_mem); |
| 38 void StoreNexe(const std::string& key, const std::string& nexe); | 38 void StoreNexe(const std::string& key, const std::string& nexe); |
| 39 std::string GetNexe(const std::string& key); | 39 std::string GetNexe(const std::string& key); |
| 40 | 40 |
| 41 protected: | 41 protected: |
| 42 PNaClTranslationCache* cache_; | 42 PnaclTranslationCache* cache_; |
| 43 content::TestBrowserThreadBundle thread_bundle_; | 43 content::TestBrowserThreadBundle thread_bundle_; |
| 44 base::ScopedTempDir temp_dir_; | 44 base::ScopedTempDir temp_dir_; |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 void PNaClTranslationCacheTest::InitBackend(bool in_mem) { | 47 void PnaclTranslationCacheTest::InitBackend(bool in_mem) { |
| 48 net::TestCompletionCallback init_cb; | 48 net::TestCompletionCallback init_cb; |
| 49 if (!in_mem) { | 49 if (!in_mem) { |
| 50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 51 } | 51 } |
| 52 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()); |
| 53 if (in_mem) | 53 if (in_mem) |
| 54 ASSERT_EQ(net::OK, rv); | 54 ASSERT_EQ(net::OK, rv); |
| 55 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); | 55 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); |
| 56 ASSERT_EQ(0, cache_->Size()); | 56 ASSERT_EQ(0, cache_->Size()); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void PNaClTranslationCacheTest::StoreNexe(const std::string& key, | 59 void PnaclTranslationCacheTest::StoreNexe(const std::string& key, |
| 60 const std::string& nexe) { | 60 const std::string& nexe) { |
| 61 net::TestCompletionCallback store_cb; | 61 net::TestCompletionCallback store_cb; |
| 62 cache_->StoreNexe(key, nexe, store_cb.callback()); | 62 cache_->StoreNexe(key, nexe, store_cb.callback()); |
| 63 // 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 |
| 64 // 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 |
| 65 // 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. |
| 66 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); | 66 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); |
| 67 } | 67 } |
| 68 | 68 |
| 69 std::string PNaClTranslationCacheTest::GetNexe(const std::string& key) { | 69 std::string PnaclTranslationCacheTest::GetNexe(const std::string& key) { |
| 70 net::TestCompletionCallback load_cb; | 70 net::TestCompletionCallback load_cb; |
| 71 std::string nexe; | 71 std::string nexe; |
| 72 cache_->GetNexe(key, &nexe, load_cb.callback()); | 72 cache_->GetNexe(key, &nexe, load_cb.callback()); |
| 73 EXPECT_EQ(net::OK, load_cb.GetResult(net::ERR_IO_PENDING)); | 73 EXPECT_EQ(net::OK, load_cb.GetResult(net::ERR_IO_PENDING)); |
| 74 return nexe; | 74 return nexe; |
| 75 } | 75 } |
| 76 | 76 |
| 77 static const std::string test_key("1"); | 77 static const std::string test_key("1"); |
| 78 static const std::string test_store_val("testnexe"); | 78 static const std::string test_store_val("testnexe"); |
| 79 static const int kLargeNexeSize = 16 * 1024 *1024; | 79 static const int kLargeNexeSize = 16 * 1024 *1024; |
| 80 | 80 |
| 81 TEST_F(PNaClTranslationCacheTest, StoreSmallInMem) { | 81 TEST_F(PnaclTranslationCacheTest, StoreSmallInMem) { |
| 82 // Test that a single store puts something in the mem backend | 82 // Test that a single store puts something in the mem backend |
| 83 InitBackend(true); | 83 InitBackend(true); |
| 84 StoreNexe(test_key, test_store_val); | 84 StoreNexe(test_key, test_store_val); |
| 85 EXPECT_EQ(1, cache_->Size()); | 85 EXPECT_EQ(1, cache_->Size()); |
| 86 } | 86 } |
| 87 | 87 |
| 88 TEST_F(PNaClTranslationCacheTest, StoreSmallOnDisk) { | 88 TEST_F(PnaclTranslationCacheTest, StoreSmallOnDisk) { |
| 89 // Test that a single store puts something in the disk backend | 89 // Test that a single store puts something in the disk backend |
| 90 InitBackend(false); | 90 InitBackend(false); |
| 91 StoreNexe(test_key, test_store_val); | 91 StoreNexe(test_key, test_store_val); |
| 92 EXPECT_EQ(1, cache_->Size()); | 92 EXPECT_EQ(1, cache_->Size()); |
| 93 } | 93 } |
| 94 | 94 |
| 95 TEST_F(PNaClTranslationCacheTest, StoreLargeOnDisk) { | 95 TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) { |
| 96 // 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 | 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 | 98 // backend. Find out what the 'offset' field means, and if it can ever require |
| 99 // multiple writes. | 99 // multiple writes. |
| 100 InitBackend(false); | 100 InitBackend(false); |
| 101 const std::string large_buffer(kLargeNexeSize, 'a'); | 101 const std::string large_buffer(kLargeNexeSize, 'a'); |
| 102 StoreNexe(test_key, large_buffer); | 102 StoreNexe(test_key, large_buffer); |
| 103 EXPECT_EQ(1, cache_->Size()); | 103 EXPECT_EQ(1, cache_->Size()); |
| 104 } | 104 } |
| 105 | 105 |
| 106 TEST_F(PNaClTranslationCacheTest, InMemSizeLimit) { | 106 TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) { |
| 107 InitBackend(true); | 107 InitBackend(true); |
| 108 const std::string large_buffer(kMaxMemCacheSize + 1, 'a'); | 108 const std::string large_buffer(kMaxMemCacheSize + 1, 'a'); |
| 109 net::TestCompletionCallback store_cb; | 109 net::TestCompletionCallback store_cb; |
| 110 cache_->StoreNexe(test_key, large_buffer, store_cb.callback()); | 110 cache_->StoreNexe(test_key, large_buffer, store_cb.callback()); |
| 111 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)); |
| 112 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed. | 112 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed. |
| 113 EXPECT_EQ(0, cache_->Size()); | 113 EXPECT_EQ(0, cache_->Size()); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST_F(PNaClTranslationCacheTest, GetOneInMem) { | 116 TEST_F(PnaclTranslationCacheTest, GetOneInMem) { |
| 117 InitBackend(true); | 117 InitBackend(true); |
| 118 StoreNexe(test_key, test_store_val); | 118 StoreNexe(test_key, test_store_val); |
| 119 EXPECT_EQ(1, cache_->Size()); | 119 EXPECT_EQ(1, cache_->Size()); |
| 120 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); | 120 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 TEST_F(PNaClTranslationCacheTest, GetLargeOnDisk) { | 123 TEST_F(PnaclTranslationCacheTest, GetLargeOnDisk) { |
| 124 InitBackend(false); | 124 InitBackend(false); |
| 125 const std::string large_buffer(kLargeNexeSize, 'a'); | 125 const std::string large_buffer(kLargeNexeSize, 'a'); |
| 126 StoreNexe(test_key, large_buffer); | 126 StoreNexe(test_key, large_buffer); |
| 127 EXPECT_EQ(1, cache_->Size()); | 127 EXPECT_EQ(1, cache_->Size()); |
| 128 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer)); | 128 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer)); |
| 129 } | 129 } |
| 130 | 130 |
| 131 TEST_F(PNaClTranslationCacheTest, StoreTwice) { | 131 TEST_F(PnaclTranslationCacheTest, StoreTwice) { |
| 132 // Test that storing twice with the same key overwrites | 132 // Test that storing twice with the same key overwrites |
| 133 InitBackend(true); | 133 InitBackend(true); |
| 134 StoreNexe(test_key, test_store_val); | 134 StoreNexe(test_key, test_store_val); |
| 135 StoreNexe(test_key, test_store_val + "aaa"); | 135 StoreNexe(test_key, test_store_val + "aaa"); |
| 136 EXPECT_EQ(1, cache_->Size()); | 136 EXPECT_EQ(1, cache_->Size()); |
| 137 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa")); | 137 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa")); |
| 138 } | 138 } |
| 139 | 139 |
| 140 TEST_F(PNaClTranslationCacheTest, StoreTwo) { | 140 TEST_F(PnaclTranslationCacheTest, StoreTwo) { |
| 141 InitBackend(true); | 141 InitBackend(true); |
| 142 StoreNexe(test_key, test_store_val); | 142 StoreNexe(test_key, test_store_val); |
| 143 StoreNexe(test_key + "a", test_store_val + "aaa"); | 143 StoreNexe(test_key + "a", test_store_val + "aaa"); |
| 144 EXPECT_EQ(2, cache_->Size()); | 144 EXPECT_EQ(2, cache_->Size()); |
| 145 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); | 145 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val)); |
| 146 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")); |
| 147 } | 147 } |
| 148 | 148 |
| 149 TEST_F(PNaClTranslationCacheTest, GetMiss) { | 149 TEST_F(PnaclTranslationCacheTest, GetMiss) { |
| 150 InitBackend(true); | 150 InitBackend(true); |
| 151 StoreNexe(test_key, test_store_val); | 151 StoreNexe(test_key, test_store_val); |
| 152 net::TestCompletionCallback load_cb; | 152 net::TestCompletionCallback load_cb; |
| 153 std::string nexe; | 153 std::string nexe; |
| 154 cache_->GetNexe(test_key + "a", &nexe, load_cb.callback()); | 154 cache_->GetNexe(test_key + "a", &nexe, load_cb.callback()); |
| 155 EXPECT_EQ(net::ERR_FAILED, load_cb.GetResult(net::ERR_IO_PENDING)); | 155 EXPECT_EQ(net::ERR_FAILED, load_cb.GetResult(net::ERR_IO_PENDING)); |
| 156 } | 156 } |
| 157 | 157 |
| 158 } // namespace pnacl_cache | 158 } // namespace pnacl_cache |
| OLD | NEW |