OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "base/bind.h" |
| 6 #include "base/files/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/hash.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "chromeos/printing/ppd_cache.h" |
| 15 #include "net/url_request/test_url_request_interceptor.h" |
| 16 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chromeos { |
| 20 namespace printing { |
| 21 namespace { |
| 22 |
| 23 const char kTestManufacturer[] = "FooPrinters, Inc."; |
| 24 const char kTestModel[] = "Laser BarMatic 1000"; |
| 25 const char kTestUserUrl[] = "/some/path/to/some/ppd/file"; |
| 26 const char kTestPpdContents[] = "This is the ppd for the first printer"; |
| 27 // Output of 'gzip -9' on a file with contents 'ppd contents' |
| 28 const char kTestGZippedPpdContents[] = { |
| 29 0x1f, 0x8b, 0x08, 0x08, 0xe4, 0x2e, 0x00, 0x58, 0x02, 0x03, 0x70, |
| 30 0x70, 0x64, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x2b, 0x28, 0x48, 0x51, |
| 31 0x48, 0xce, 0xcf, 0x2b, 0x49, 0xcd, 0x2b, 0x29, 0xe6, 0x02, 0x00, |
| 32 0x2b, 0x51, 0x91, 0x24, 0x0d, 0x00, 0x00, 0x00}; |
| 33 |
| 34 // Generate and return a PPDReference that has the fields set from the kTest |
| 35 // constants above. |
| 36 Printer::PpdReference TestReference() { |
| 37 Printer::PpdReference ret; |
| 38 ret.effective_manufacturer = kTestManufacturer; |
| 39 ret.effective_model = kTestModel; |
| 40 ret.user_supplied_ppd_url = kTestUserUrl; |
| 41 return ret; |
| 42 } |
| 43 |
| 44 // This fixture just points the cache at a temporary directory for the life of |
| 45 // the test. |
| 46 class PpdCacheTest : public ::testing::Test { |
| 47 public: |
| 48 void SetUp() override { |
| 49 ASSERT_TRUE(ppd_cache_temp_dir_.CreateUniqueTempDir()); |
| 50 } |
| 51 |
| 52 // Make and return a cache for the test that uses a temporary directory |
| 53 // which is cleaned up at the end of the test. |
| 54 std::unique_ptr<PpdCache> CreateTestCache() { |
| 55 return PpdCache::Create(ppd_cache_temp_dir_.GetPath()); |
| 56 } |
| 57 |
| 58 protected: |
| 59 // Overrider for DIR_CHROMEOS_PPD_CACHE that points it at a temporary |
| 60 // directory for the life of the test. |
| 61 base::ScopedTempDir ppd_cache_temp_dir_; |
| 62 }; |
| 63 |
| 64 // Check that path has a value, and the contents of the referenced file are |
| 65 // |expected_contents|. |
| 66 void CheckFileContentsAre(base::Optional<base::FilePath> result, |
| 67 const std::string& expected_contents) { |
| 68 // Make sure we have a value. |
| 69 ASSERT_TRUE(result); |
| 70 // Make sure we get the ppd back that we stored. |
| 71 std::string contents; |
| 72 ASSERT_TRUE(base::ReadFileToString(result.value(), &contents)); |
| 73 EXPECT_EQ(expected_contents, contents); |
| 74 } |
| 75 |
| 76 // Test that we miss on an empty cache. |
| 77 TEST_F(PpdCacheTest, SimpleMiss) { |
| 78 auto cache = CreateTestCache(); |
| 79 EXPECT_FALSE(cache->Find(TestReference())); |
| 80 } |
| 81 |
| 82 // Test that when we store stuff, we get it back. |
| 83 TEST_F(PpdCacheTest, MissThenHit) { |
| 84 auto cache = CreateTestCache(); |
| 85 auto ref = TestReference(); |
| 86 EXPECT_FALSE(cache->Find(ref)); |
| 87 |
| 88 // Store should give us a reference to the file. |
| 89 CheckFileContentsAre(cache->Store(ref, kTestPpdContents), kTestPpdContents); |
| 90 |
| 91 // We should also get it back in response to a Find. |
| 92 CheckFileContentsAre(cache->Find(ref), kTestPpdContents); |
| 93 } |
| 94 |
| 95 // Test that mutating any field in the reference causes us to miss in the cache |
| 96 // when we change the highest priority resolve criteria. |
| 97 TEST_F(PpdCacheTest, FieldChangeMeansCacheMiss) { |
| 98 auto cache = CreateTestCache(); |
| 99 auto ref = TestReference(); |
| 100 CheckFileContentsAre(cache->Store(ref, kTestPpdContents), kTestPpdContents); |
| 101 |
| 102 // We have a user url, so should still cache hit on manufacturer change. |
| 103 ref.effective_manufacturer = "Something else"; |
| 104 EXPECT_TRUE(cache->Find(ref)); |
| 105 ref = TestReference(); |
| 106 |
| 107 // We have a user url, so should still cache hit on model change. |
| 108 ref.effective_model = "Something else"; |
| 109 EXPECT_TRUE(cache->Find(ref)); |
| 110 ref = TestReference(); |
| 111 |
| 112 // But if we change th user url, that's a cache miss. |
| 113 ref.user_supplied_ppd_url = "Something else"; |
| 114 EXPECT_FALSE(cache->Find(ref)); |
| 115 ref = TestReference(); |
| 116 // Should still find the initial Store when ref *is* identical. |
| 117 CheckFileContentsAre(cache->Find(ref), kTestPpdContents); |
| 118 |
| 119 // Now store the reference with no test url. |
| 120 ref.user_supplied_ppd_url.clear(); |
| 121 CheckFileContentsAre(cache->Store(ref, kTestPpdContents), kTestPpdContents); |
| 122 |
| 123 // Now changing the model or manufacturer should cause a cache miss. |
| 124 ref.effective_manufacturer = "Something else"; |
| 125 EXPECT_FALSE(cache->Find(ref)); |
| 126 ref = TestReference(); |
| 127 ref.user_supplied_ppd_url.clear(); |
| 128 ref.effective_model = "Something else"; |
| 129 EXPECT_FALSE(cache->Find(ref)); |
| 130 } |
| 131 |
| 132 // Test that gzipped contents get stored as .ppd.gz, and non-gzipped |
| 133 // contents get stored as .ppd |
| 134 TEST_F(PpdCacheTest, StoredExtensions) { |
| 135 auto cache = CreateTestCache(); |
| 136 auto ref = TestReference(); |
| 137 |
| 138 // Not compressed, so should store as .ppd |
| 139 auto result = cache->Store(ref, kTestPpdContents); |
| 140 EXPECT_EQ(".ppd", cache->Store(ref, kTestPpdContents).value().Extension()); |
| 141 |
| 142 // Compressed, so should identify this and store as .ppd.gz |
| 143 std::string gzipped_contents(kTestGZippedPpdContents, |
| 144 sizeof(kTestGZippedPpdContents)); |
| 145 EXPECT_EQ(".ppd.gz", cache->Store(ref, gzipped_contents).value().Extension()); |
| 146 } |
| 147 |
| 148 } // namespace |
| 149 } // namespace printing |
| 150 } // namespace chromeos |
OLD | NEW |