OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/files/file_path.h" |
| 6 #include "base/files/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/version.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "extensions/browser/content_hash_reader.h" |
| 15 #include "extensions/browser/extensions_test.h" |
| 16 #include "extensions/common/constants.h" |
| 17 #include "extensions/common/extension_paths.h" |
| 18 #include "extensions/common/file_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/zlib/google/zip.h" |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 namespace { |
| 25 |
| 26 scoped_refptr<ContentHashReader> CreateContentHashReader( |
| 27 const Extension& extension, |
| 28 base::FilePath& extension_resource_path) { |
| 29 return make_scoped_refptr(new ContentHashReader( |
| 30 extension.id(), *extension.version(), extension.path(), |
| 31 extension_resource_path, |
| 32 ContentVerifierKey(kWebstoreSignaturesPublicKey, |
| 33 kWebstoreSignaturesPublicKeySize))); |
| 34 } |
| 35 |
| 36 void DoNothingWithReasonParam(ContentVerifyJob::FailureReason reason) {} |
| 37 |
| 38 class JobTestObserver : public ContentVerifyJob::TestObserver { |
| 39 public: |
| 40 JobTestObserver(const std::string& extension_id, |
| 41 const base::FilePath& relative_path) |
| 42 : extension_id_(extension_id), relative_path_(relative_path) { |
| 43 ContentVerifyJob::SetObserverForTests(this); |
| 44 } |
| 45 ~JobTestObserver() { ContentVerifyJob::SetObserverForTests(nullptr); } |
| 46 |
| 47 void JobStarted(const std::string& extension_id, |
| 48 const base::FilePath& relative_path) override {} |
| 49 |
| 50 void JobFinished(const std::string& extension_id, |
| 51 const base::FilePath& relative_path, |
| 52 ContentVerifyJob::FailureReason reason) override { |
| 53 if (extension_id != extension_id_ || relative_path != relative_path_) |
| 54 return; |
| 55 failure_reason_ = reason; |
| 56 run_loop_.Quit(); |
| 57 } |
| 58 |
| 59 ContentVerifyJob::FailureReason WaitAndGetFailureReason() { |
| 60 // Run() returns immediately if Quit() has already been called. |
| 61 run_loop_.Run(); |
| 62 EXPECT_TRUE(failure_reason_.has_value()); |
| 63 return failure_reason_.value_or(ContentVerifyJob::FAILURE_REASON_MAX); |
| 64 } |
| 65 |
| 66 private: |
| 67 base::RunLoop run_loop_; |
| 68 std::string extension_id_; |
| 69 base::FilePath relative_path_; |
| 70 base::Optional<ContentVerifyJob::FailureReason> failure_reason_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(JobTestObserver); |
| 73 }; |
| 74 |
| 75 } // namespace |
| 76 |
| 77 class ContentVerifyJobUnittest : public ExtensionsTest { |
| 78 public: |
| 79 ContentVerifyJobUnittest() {} |
| 80 ~ContentVerifyJobUnittest() override {} |
| 81 |
| 82 void SetUp() override { |
| 83 ExtensionsTest::SetUp(); |
| 84 |
| 85 // Needed for ContentVerifyJob::Start(). |
| 86 browser_threads_ = base::MakeUnique<content::TestBrowserThreadBundle>( |
| 87 content::TestBrowserThreadBundle::REAL_IO_THREAD); |
| 88 } |
| 89 |
| 90 // Helper to get files from our subdirectory in the general extensions test |
| 91 // data dir. |
| 92 base::FilePath GetTestPath(const base::FilePath& relative_path) { |
| 93 base::FilePath base_path; |
| 94 EXPECT_TRUE(PathService::Get(DIR_TEST_DATA, &base_path)); |
| 95 base_path = base_path.AppendASCII("content_hash_fetcher"); |
| 96 return base_path.Append(relative_path); |
| 97 } |
| 98 |
| 99 // Unzips the extension source from |extension_zip| into a temporary |
| 100 // directory and loads it. Returns the resuling Extension object. |
| 101 // |destination| points to the path where the extension was extracted. |
| 102 scoped_refptr<Extension> UnzipToTempDirAndLoad( |
| 103 const base::FilePath& extension_zip, |
| 104 base::FilePath* destination) { |
| 105 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 106 *destination = temp_dir_.GetPath(); |
| 107 EXPECT_TRUE(zip::Unzip(extension_zip, *destination)); |
| 108 |
| 109 std::string error; |
| 110 scoped_refptr<Extension> extension = file_util::LoadExtension( |
| 111 *destination, Manifest::INTERNAL, 0 /* flags */, &error); |
| 112 EXPECT_NE(nullptr, extension.get()) << " error:'" << error << "'"; |
| 113 return extension; |
| 114 } |
| 115 |
| 116 private: |
| 117 base::ScopedTempDir temp_dir_; |
| 118 std::unique_ptr<content::TestBrowserThreadBundle> browser_threads_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(ContentVerifyJobUnittest); |
| 121 }; |
| 122 |
| 123 // Tests that deleted legitimate files trigger content verification failure. |
| 124 // Also tests that non-existent file request does not trigger content |
| 125 // verification failure. |
| 126 TEST_F(ContentVerifyJobUnittest, DeletedAndMissingFiles) { |
| 127 base::FilePath unzipped_path; |
| 128 base::FilePath test_dir_base = |
| 129 GetTestPath(base::FilePath(FILE_PATH_LITERAL("with_verified_contents"))); |
| 130 scoped_refptr<Extension> extension = UnzipToTempDirAndLoad( |
| 131 test_dir_base.AppendASCII("source_all.zip"), &unzipped_path); |
| 132 ASSERT_TRUE(extension.get()); |
| 133 // Make sure there is a verified_contents.json file there as this test cannot |
| 134 // fetch it. |
| 135 EXPECT_TRUE( |
| 136 base::PathExists(file_util::GetVerifiedContentsPath(extension->path()))); |
| 137 |
| 138 const base::FilePath::CharType kExistentResource[] = |
| 139 FILE_PATH_LITERAL("background.js"); |
| 140 base::FilePath existent_resource_path(kExistentResource); |
| 141 { |
| 142 // Make sure background.js passes verification correctly. |
| 143 JobTestObserver observer(extension->id(), existent_resource_path); |
| 144 |
| 145 scoped_refptr<ContentHashReader> content_hash_reader = |
| 146 CreateContentHashReader(*extension.get(), existent_resource_path); |
| 147 scoped_refptr<ContentVerifyJob> verify_job = new ContentVerifyJob( |
| 148 content_hash_reader.get(), base::Bind(&DoNothingWithReasonParam)); |
| 149 verify_job->Start(); |
| 150 { |
| 151 // Simulate serving background.js. |
| 152 std::string background_contents; |
| 153 base::ReadFileToString( |
| 154 unzipped_path.Append(base::FilePath(kExistentResource)), |
| 155 &background_contents); |
| 156 verify_job->BytesRead(background_contents.size(), |
| 157 base::string_as_array(&background_contents)); |
| 158 verify_job->DoneReading(); |
| 159 } |
| 160 ContentVerifyJob::FailureReason reason = observer.WaitAndGetFailureReason(); |
| 161 // Expect no content-verification failure. |
| 162 EXPECT_EQ(ContentVerifyJob::NONE, reason); |
| 163 } |
| 164 |
| 165 { |
| 166 // Once background.js is deleted, verification will result in HASH_MISMATCH. |
| 167 JobTestObserver observer(extension->id(), existent_resource_path); |
| 168 // Now delete the existent file. |
| 169 EXPECT_TRUE(base::DeleteFile( |
| 170 unzipped_path.Append(base::FilePath(kExistentResource)), false)); |
| 171 |
| 172 scoped_refptr<ContentHashReader> content_hash_reader = |
| 173 CreateContentHashReader(*extension.get(), existent_resource_path); |
| 174 scoped_refptr<ContentVerifyJob> verify_job = new ContentVerifyJob( |
| 175 content_hash_reader.get(), base::Bind(&DoNothingWithReasonParam)); |
| 176 verify_job->Start(); |
| 177 { |
| 178 // Simulate serving deleted background.js. |
| 179 std::string tmp; |
| 180 verify_job->BytesRead(0, base::string_as_array(&tmp)); |
| 181 verify_job->DoneReading(); |
| 182 } |
| 183 ContentVerifyJob::FailureReason reason = observer.WaitAndGetFailureReason(); |
| 184 EXPECT_EQ(ContentVerifyJob::HASH_MISMATCH, reason); |
| 185 } |
| 186 |
| 187 { |
| 188 // Now ask for a non-existent resource non-existent.js. Verification should |
| 189 // skip this file as it is not listed in our verified_contents.json file. |
| 190 const base::FilePath::CharType kNonExistentResource[] = |
| 191 FILE_PATH_LITERAL("non-existent.js"); |
| 192 base::FilePath non_existent_resource_path(kNonExistentResource); |
| 193 JobTestObserver observer(extension->id(), non_existent_resource_path); |
| 194 |
| 195 scoped_refptr<ContentHashReader> content_hash_reader = |
| 196 CreateContentHashReader(*extension.get(), non_existent_resource_path); |
| 197 scoped_refptr<ContentVerifyJob> verify_job = new ContentVerifyJob( |
| 198 content_hash_reader.get(), base::Bind(&DoNothingWithReasonParam)); |
| 199 verify_job->Start(); |
| 200 { |
| 201 // Simulate non existent file read. |
| 202 std::string tmp; |
| 203 verify_job->BytesRead(0, base::string_as_array(&tmp)); |
| 204 verify_job->DoneReading(); |
| 205 } |
| 206 ContentVerifyJob::FailureReason reason = observer.WaitAndGetFailureReason(); |
| 207 // Expect no content-verification failure. |
| 208 EXPECT_EQ(ContentVerifyJob::NONE, reason); |
| 209 } |
| 210 } |
| 211 |
| 212 // Tests that content modification causes content verification failure. |
| 213 TEST_F(ContentVerifyJobUnittest, ContentMismatch) { |
| 214 base::FilePath unzipped_path; |
| 215 base::FilePath test_dir_base = |
| 216 GetTestPath(base::FilePath(FILE_PATH_LITERAL("with_verified_contents"))); |
| 217 scoped_refptr<Extension> extension = UnzipToTempDirAndLoad( |
| 218 test_dir_base.AppendASCII("source_all.zip"), &unzipped_path); |
| 219 ASSERT_TRUE(extension.get()); |
| 220 // Make sure there is a verified_contents.json file there as this test cannot |
| 221 // fetch it. |
| 222 EXPECT_TRUE( |
| 223 base::PathExists(file_util::GetVerifiedContentsPath(extension->path()))); |
| 224 |
| 225 const base::FilePath::CharType kResource[] = |
| 226 FILE_PATH_LITERAL("background.js"); |
| 227 base::FilePath existent_resource_path(kResource); |
| 228 { |
| 229 // Make sure modified background.js fails content verification. |
| 230 JobTestObserver observer(extension->id(), existent_resource_path); |
| 231 |
| 232 scoped_refptr<ContentHashReader> content_hash_reader = |
| 233 CreateContentHashReader(*extension.get(), existent_resource_path); |
| 234 scoped_refptr<ContentVerifyJob> verify_job = new ContentVerifyJob( |
| 235 content_hash_reader.get(), base::Bind(&DoNothingWithReasonParam)); |
| 236 verify_job->Start(); |
| 237 { |
| 238 // Simulate serving *modified* background.js. |
| 239 std::string modified_contents; |
| 240 base::ReadFileToString(unzipped_path.Append(base::FilePath(kResource)), |
| 241 &modified_contents); |
| 242 modified_contents.append("console.log('modified');"); |
| 243 verify_job->BytesRead(modified_contents.size(), |
| 244 base::string_as_array(&modified_contents)); |
| 245 verify_job->DoneReading(); |
| 246 } |
| 247 ContentVerifyJob::FailureReason reason = observer.WaitAndGetFailureReason(); |
| 248 EXPECT_EQ(ContentVerifyJob::HASH_MISMATCH, reason); |
| 249 } |
| 250 } |
| 251 |
| 252 } // namespace extensions |
OLD | NEW |