OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/nacl_host/pnacl_translation_cache.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/test/test_browser_thread.h" |
| 12 #include "net/base/test_completion_callback.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 using content::BrowserThread; |
| 16 |
| 17 namespace pnacl_cache { |
| 18 |
| 19 class PNaClTranslationCacheTest : public testing::Test { |
| 20 protected: |
| 21 PNaClTranslationCacheTest() |
| 22 : cache_thread_(BrowserThread::CACHE, &message_loop_), |
| 23 io_thread_(BrowserThread::IO, &message_loop_) {} |
| 24 virtual ~PNaClTranslationCacheTest() {} |
| 25 virtual void SetUp() { cache_ = new PNaClTranslationCache(); } |
| 26 virtual void TearDown() { delete cache_; } |
| 27 |
| 28 protected: |
| 29 PNaClTranslationCache* cache_; |
| 30 base::MessageLoopForIO message_loop_; |
| 31 content::TestBrowserThread cache_thread_; |
| 32 content::TestBrowserThread io_thread_; |
| 33 }; |
| 34 |
| 35 TEST_F(PNaClTranslationCacheTest, StoreOneInMem) { |
| 36 net::TestCompletionCallback init_cb; |
| 37 int rv = cache_->InitCache(base::FilePath(), true, init_cb.callback()); |
| 38 EXPECT_EQ(net::OK, rv); |
| 39 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); |
| 40 net::TestCompletionCallback store_cb; |
| 41 EXPECT_EQ(0, cache_->Size()); |
| 42 cache_->StoreNexe("1", "a", store_cb.callback()); |
| 43 // Using ERR_IO_PENDING here causes the callback to wait for the result |
| 44 // which should be harmless even if it returns OK immediately. This is because |
| 45 // we don't plumb the intermediate writing stages all the way out. |
| 46 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); |
| 47 EXPECT_EQ(1, cache_->Size()); |
| 48 } |
| 49 |
| 50 TEST_F(PNaClTranslationCacheTest, StoreOneOnDisk) { |
| 51 base::ScopedTempDir temp_dir; |
| 52 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 53 net::TestCompletionCallback init_cb; |
| 54 int rv = cache_->InitCache(temp_dir.path(), false, init_cb.callback()); |
| 55 EXPECT_TRUE(rv); |
| 56 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); |
| 57 EXPECT_EQ(0, cache_->Size()); |
| 58 net::TestCompletionCallback store_cb; |
| 59 cache_->StoreNexe("1", "a", store_cb.callback()); |
| 60 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); |
| 61 EXPECT_EQ(1, cache_->Size()); |
| 62 } |
| 63 |
| 64 TEST_F(PNaClTranslationCacheTest, InMemSizeLimit) { |
| 65 net::TestCompletionCallback init_cb; |
| 66 int rv = cache_->InitCache(base::FilePath(), true, init_cb.callback()); |
| 67 EXPECT_EQ(rv, net::OK); |
| 68 ASSERT_EQ(init_cb.GetResult(rv), net::OK); |
| 69 EXPECT_EQ(cache_->Size(), 0); |
| 70 std::string large_buffer(kMaxMemCacheSize + 1, 'a'); |
| 71 net::TestCompletionCallback store_cb; |
| 72 cache_->StoreNexe("1", large_buffer, store_cb.callback()); |
| 73 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING)); |
| 74 } |
| 75 |
| 76 } // namespace nacl_cache |
OLD | NEW |