web_bundle_parser_factory_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2019 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. #include "components/web_package/web_bundle_parser_factory.h"
  5. #include "base/files/file.h"
  6. #include "base/files/file_path.h"
  7. #include "base/path_service.h"
  8. #include "base/test/task_environment.h"
  9. #include "base/test/test_future.h"
  10. #include "mojo/public/cpp/bindings/pending_receiver.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace web_package {
  16. namespace {
  17. base::FilePath GetTestFilePath(const base::FilePath& path) {
  18. base::FilePath test_path;
  19. base::PathService::Get(base::DIR_SOURCE_ROOT, &test_path);
  20. test_path = test_path.Append(
  21. base::FilePath(FILE_PATH_LITERAL("components/test/data/web_package")));
  22. return test_path.Append(path);
  23. }
  24. } // namespace
  25. class WebBundleParserFactoryTest : public testing::Test {
  26. public:
  27. WebBundleParserFactoryTest()
  28. : factory_(std::make_unique<WebBundleParserFactory>()) {}
  29. std::unique_ptr<mojom::BundleDataSource> CreateFileDataSource(
  30. mojo::PendingReceiver<mojom::BundleDataSource> receiver,
  31. base::File file) {
  32. return factory_->CreateFileDataSourceForTesting(std::move(receiver),
  33. std::move(file));
  34. }
  35. void GetParserForFile(mojo::PendingReceiver<mojom::WebBundleParser> receiver,
  36. base::File file) {
  37. mojom::WebBundleParserFactory* factory = factory_.get();
  38. return factory->GetParserForFile(std::move(receiver), std::move(file));
  39. }
  40. private:
  41. std::unique_ptr<WebBundleParserFactory> factory_;
  42. base::test::TaskEnvironment task_environment_;
  43. };
  44. TEST_F(WebBundleParserFactoryTest, FileDataSource) {
  45. base::FilePath test_file =
  46. GetTestFilePath(base::FilePath(FILE_PATH_LITERAL("hello_b2.wbn")));
  47. base::File file(test_file, base::File::FLAG_OPEN | base::File::FLAG_READ);
  48. ASSERT_TRUE(file.IsValid());
  49. int64_t file_length = file.GetLength();
  50. constexpr int64_t test_length = 16;
  51. ASSERT_LE(test_length, file_length);
  52. std::vector<uint8_t> first16b(test_length);
  53. ASSERT_EQ(test_length, file.Read(0, reinterpret_cast<char*>(first16b.data()),
  54. first16b.size()));
  55. std::vector<uint8_t> last16b(test_length);
  56. ASSERT_EQ(test_length,
  57. file.Read(file_length - test_length,
  58. reinterpret_cast<char*>(last16b.data()), last16b.size()));
  59. mojo::PendingRemote<mojom::BundleDataSource> remote;
  60. auto data_source = CreateFileDataSource(
  61. remote.InitWithNewPipeAndPassReceiver(), std::move(file));
  62. {
  63. base::test::TestFuture<const absl::optional<std::vector<uint8_t>>&> future;
  64. data_source->Read(/*offset=*/0, test_length, future.GetCallback());
  65. ASSERT_TRUE(future.Get());
  66. EXPECT_EQ(first16b, *future.Get());
  67. }
  68. {
  69. base::test::TestFuture<const absl::optional<std::vector<uint8_t>>&> future;
  70. data_source->Read(file_length - test_length, test_length,
  71. future.GetCallback());
  72. ASSERT_TRUE(future.Get());
  73. EXPECT_EQ(last16b, *future.Get());
  74. }
  75. {
  76. base::test::TestFuture<const absl::optional<std::vector<uint8_t>>&> future;
  77. data_source->Read(file_length - test_length, test_length + 1,
  78. future.GetCallback());
  79. ASSERT_TRUE(future.Get());
  80. EXPECT_EQ(last16b, *future.Get());
  81. }
  82. {
  83. base::test::TestFuture<const absl::optional<std::vector<uint8_t>>&> future;
  84. data_source->Read(file_length + 1, test_length, future.GetCallback());
  85. ASSERT_FALSE(future.Get());
  86. }
  87. {
  88. base::test::TestFuture<int64_t> future;
  89. data_source->Length(future.GetCallback());
  90. EXPECT_EQ(file_length, future.Get());
  91. }
  92. {
  93. base::test::TestFuture<bool> future;
  94. data_source->IsRandomAccessContext(future.GetCallback());
  95. EXPECT_TRUE(future.Get());
  96. }
  97. }
  98. TEST_F(WebBundleParserFactoryTest, GetParserForFile) {
  99. base::File file(
  100. GetTestFilePath(base::FilePath(FILE_PATH_LITERAL("hello_b2.wbn"))),
  101. base::File::FLAG_OPEN | base::File::FLAG_READ);
  102. ASSERT_TRUE(file.IsValid());
  103. mojo::Remote<mojom::WebBundleParser> parser;
  104. GetParserForFile(parser.BindNewPipeAndPassReceiver(), std::move(file));
  105. mojom::BundleMetadataPtr metadata;
  106. {
  107. base::test::TestFuture<mojom::BundleMetadataPtr,
  108. mojom::BundleMetadataParseErrorPtr>
  109. future;
  110. parser->ParseMetadata(/*offset=*/-1, future.GetCallback());
  111. metadata = std::get<0>(future.Take());
  112. }
  113. ASSERT_TRUE(metadata);
  114. ASSERT_EQ(metadata->requests.size(), 4u);
  115. std::map<std::string, mojom::BundleResponsePtr> responses;
  116. for (const auto& item : metadata->requests) {
  117. base::test::TestFuture<mojom::BundleResponsePtr,
  118. mojom::BundleResponseParseErrorPtr>
  119. future;
  120. parser->ParseResponse(item.second->offset, item.second->length,
  121. future.GetCallback());
  122. auto [response, error] = future.Take();
  123. ASSERT_TRUE(response);
  124. ASSERT_FALSE(error);
  125. responses[item.first.spec()] = std::move(response);
  126. }
  127. ASSERT_TRUE(responses["https://test.example.org/"]);
  128. EXPECT_EQ(responses["https://test.example.org/"]->response_code, 200);
  129. EXPECT_EQ(
  130. responses["https://test.example.org/"]->response_headers["content-type"],
  131. "text/html; charset=utf-8");
  132. EXPECT_TRUE(responses["https://test.example.org/index.html"]);
  133. EXPECT_TRUE(responses["https://test.example.org/manifest.webmanifest"]);
  134. EXPECT_TRUE(responses["https://test.example.org/script.js"]);
  135. }
  136. } // namespace web_package