[email protected] | af9db5f | 2011-10-05 05:13:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 5 | #include "extensions/browser/file_reader.h" |
| 6 | |
[email protected] | 1ec27eb5 | 2011-11-03 21:59:09 | [diff] [blame] | 7 | #include "base/bind.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 8 | #include "base/callback_helpers.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
thestig | 9471270 | 2014-09-10 07:46:59 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 11 | #include "base/path_service.h" |
fdoray | 2edf4dd | 2016-06-22 17:09:02 | [diff] [blame] | 12 | #include "base/run_loop.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 13 | #include "base/test/task_environment.h" |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 14 | #include "components/crx_file/id_util.h" |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 15 | #include "extensions/common/extension_paths.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 16 | #include "extensions/common/extension_resource.h" |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 17 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 19 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 20 | |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 21 | namespace extensions { |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 22 | |
| 23 | class FileReaderTest : public testing::Test { |
| 24 | public: |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 25 | FileReaderTest() {} |
| 26 | |
Peter Boström | c3d90717 | 2021-09-24 18:39:52 | [diff] [blame] | 27 | FileReaderTest(const FileReaderTest&) = delete; |
| 28 | FileReaderTest& operator=(const FileReaderTest&) = delete; |
| 29 | |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 30 | private: |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 31 | base::test::TaskEnvironment task_environment_; |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | class Receiver { |
| 35 | public: |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 36 | explicit Receiver(std::vector<ExtensionResource> resources) |
| 37 | : file_reader_(base::MakeRefCounted<FileReader>( |
| 38 | std::move(resources), |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 39 | FileReader::OptionalFileSequenceTask(), |
Jan Wilken Dörrie | 222bd83 | 2020-04-20 09:14:30 | [diff] [blame] | 40 | base::BindOnce(&Receiver::DidReadFile, base::Unretained(this)))) {} |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 41 | |
Peter Boström | c3d90717 | 2021-09-24 18:39:52 | [diff] [blame] | 42 | Receiver(const Receiver&) = delete; |
| 43 | Receiver& operator=(const Receiver&) = delete; |
| 44 | |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 45 | void Run() { |
| 46 | file_reader_->Start(); |
| 47 | run_loop_.Run(); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 48 | } |
| 49 | |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 50 | // Removes the pointer indirection from the read data for use with |
| 51 | // comparators. |
| 52 | std::vector<std::string> GetStringData() const { |
| 53 | std::vector<std::string> string_data; |
| 54 | string_data.reserve(data_.size()); |
| 55 | for (const auto& entry : data_) { |
| 56 | EXPECT_TRUE(entry); |
| 57 | string_data.push_back(*entry); |
| 58 | } |
| 59 | return string_data; |
| 60 | } |
| 61 | |
| 62 | const absl::optional<std::string>& error() const { return error_; } |
| 63 | bool succeeded() const { return !error_; } |
| 64 | const std::vector<std::unique_ptr<std::string>>& data() const { |
| 65 | return data_; |
| 66 | } |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 67 | |
| 68 | private: |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 69 | void DidReadFile(std::vector<std::unique_ptr<std::string>> data, |
| 70 | absl::optional<std::string> error) { |
| 71 | error_ = std::move(error); |
lazyboy | b81e69a | 2016-08-18 22:35:04 | [diff] [blame] | 72 | data_ = std::move(data); |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 73 | run_loop_.QuitWhenIdle(); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 74 | } |
| 75 | |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 76 | absl::optional<std::string> error_; |
| 77 | std::vector<std::unique_ptr<std::string>> data_; |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 78 | scoped_refptr<FileReader> file_reader_; |
| 79 | base::RunLoop run_loop_; |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 80 | }; |
| 81 | |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 82 | void RunBasicTest(const std::vector<std::string>& filenames) { |
| 83 | base::FilePath root_path; |
| 84 | base::PathService::Get(DIR_TEST_DATA, &root_path); |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 85 | std::string extension_id = crx_file::id_util::GenerateId("test"); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 86 | |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 87 | std::vector<ExtensionResource> resources; |
| 88 | resources.reserve(filenames.size()); |
| 89 | std::vector<std::string> expected_contents; |
| 90 | expected_contents.reserve(filenames.size()); |
| 91 | for (const auto& filename : filenames) { |
| 92 | resources.emplace_back(extension_id, root_path, |
| 93 | base::FilePath().AppendASCII(filename)); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 94 | |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 95 | base::FilePath path = root_path.AppendASCII(filename); |
| 96 | std::string file_contents; |
| 97 | ASSERT_TRUE(base::ReadFileToString(path, &file_contents)); |
| 98 | expected_contents.push_back(std::move(file_contents)); |
| 99 | } |
| 100 | |
| 101 | Receiver receiver(resources); |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 102 | receiver.Run(); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 103 | |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 104 | EXPECT_TRUE(receiver.succeeded()) << *receiver.error(); |
| 105 | EXPECT_THAT(receiver.GetStringData(), |
| 106 | ::testing::ElementsAreArray(expected_contents)); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(FileReaderTest, SmallFile) { |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 110 | RunBasicTest({"smallfile"}); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST_F(FileReaderTest, BiggerFile) { |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 114 | RunBasicTest({"bigfile"}); |
| 115 | } |
| 116 | |
| 117 | TEST_F(FileReaderTest, MultiFile) { |
| 118 | RunBasicTest({"smallfile", "bigfile"}); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 119 | } |
| 120 | |
Solomon Kinard | 015f38fb | 2020-10-15 21:58:34 | [diff] [blame] | 121 | TEST_F(FileReaderTest, NonExistentFile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 122 | base::FilePath path; |
Avi Drissman | 210441b7 | 2018-05-01 15:51:00 | [diff] [blame] | 123 | base::PathService::Get(DIR_TEST_DATA, &path); |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 124 | std::string extension_id = crx_file::id_util::GenerateId("test"); |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 125 | ExtensionResource resource(extension_id, path, base::FilePath( |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 126 | FILE_PATH_LITERAL("file_that_does_not_exist"))); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 127 | path = path.AppendASCII("file_that_does_not_exist"); |
| 128 | |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 129 | Receiver receiver({resource}); |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 130 | receiver.Run(); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 131 | |
| 132 | EXPECT_FALSE(receiver.succeeded()); |
Devlin Cronin | ac674c86 | 2021-07-08 18:46:37 | [diff] [blame] | 133 | EXPECT_EQ("Could not load file: 'file_that_does_not_exist'.", |
| 134 | *receiver.error()); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 137 | } // namespace extensions |