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