download_db_conversions_unittest.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include "components/download/database/download_db_conversions.h"
  5. #include "base/test/scoped_feature_list.h"
  6. #include "base/time/time.h"
  7. #include "components/download/public/common/download_features.h"
  8. #include "components/download/public/common/download_url_parameters.h"
  9. #include "services/network/public/mojom/fetch_api.mojom-shared.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace download {
  13. namespace {
  14. InProgressInfo CreateInProgressInfo() {
  15. InProgressInfo info;
  16. // InProgressInfo with valid fields.
  17. info.current_path = base::FilePath(FILE_PATH_LITERAL("/tmp.crdownload"));
  18. info.target_path = base::FilePath(FILE_PATH_LITERAL("/tmp"));
  19. info.url_chain.emplace_back("http://foo");
  20. info.url_chain.emplace_back("http://foo2");
  21. info.referrer_url = GURL("http://foo1.com");
  22. info.serialized_embedder_download_data = std::string();
  23. info.tab_url = GURL("http://foo.com");
  24. info.tab_referrer_url = GURL("http://abc.com");
  25. info.start_time = base::Time::NowFromSystemTime().LocalMidnight();
  26. info.end_time = base::Time();
  27. info.etag = "A";
  28. info.last_modified = "Wed, 1 Oct 2018 07:00:00 GMT";
  29. info.received_bytes = 1000;
  30. info.mime_type = "text/html";
  31. info.original_mime_type = "text/html";
  32. info.total_bytes = 10000;
  33. info.state = DownloadItem::IN_PROGRESS;
  34. info.danger_type = DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS;
  35. info.interrupt_reason = DOWNLOAD_INTERRUPT_REASON_NONE;
  36. info.transient = false;
  37. info.paused = false;
  38. info.hash = "abcdefg";
  39. info.metered = true;
  40. info.received_slices.emplace_back(0, 500, false);
  41. info.received_slices.emplace_back(5000, 500, false);
  42. info.bytes_wasted = 1234;
  43. info.auto_resume_count = 3;
  44. info.fetch_error_body = true;
  45. info.request_headers.emplace_back(
  46. std::make_pair<std::string, std::string>("123", "456"));
  47. info.request_headers.emplace_back(
  48. std::make_pair<std::string, std::string>("ABC", "def"));
  49. info.credentials_mode = ::network::mojom::CredentialsMode::kOmit;
  50. return info;
  51. }
  52. InProgressInfo CreateInProgressInfoWithRerouteInfo(
  53. DownloadItemRerouteInfo reroute_info) {
  54. InProgressInfo info = CreateInProgressInfo();
  55. info.reroute_info = std::move(reroute_info);
  56. return info;
  57. }
  58. DownloadInfo CreateDownloadInfo() {
  59. DownloadInfo info;
  60. info.guid = "abcdefg";
  61. info.id = 1234567;
  62. info.in_progress_info = CreateInProgressInfo();
  63. info.ukm_info = UkmInfo(DownloadSource::FROM_RENDERER, 100);
  64. return info;
  65. }
  66. } // namespace
  67. class DownloadDBConversionsTest : public testing::Test,
  68. public DownloadDBConversions {
  69. public:
  70. ~DownloadDBConversionsTest() override = default;
  71. void SetUp() override {
  72. scoped_feature_list_.InitAndEnableFeature(features::kDownloadLater);
  73. }
  74. protected:
  75. base::test::ScopedFeatureList* scoped_feature_list() {
  76. return &scoped_feature_list_;
  77. }
  78. private:
  79. base::test::ScopedFeatureList scoped_feature_list_;
  80. };
  81. TEST_F(DownloadDBConversionsTest, DownloadEntry) {
  82. // Entry with no fields.
  83. DownloadEntry entry;
  84. EXPECT_EQ(false, entry.fetch_error_body);
  85. EXPECT_TRUE(entry.request_headers.empty());
  86. EXPECT_EQ(entry, DownloadEntryFromProto(DownloadEntryToProto(entry)));
  87. // Entry with guid, request origin and download source.
  88. entry.guid = "guid";
  89. entry.request_origin = "request origin";
  90. entry.download_source = DownloadSource::DRAG_AND_DROP;
  91. entry.ukm_download_id = 123;
  92. entry.bytes_wasted = 1234;
  93. entry.fetch_error_body = true;
  94. entry.request_headers.emplace_back(
  95. std::make_pair<std::string, std::string>("123", "456"));
  96. entry.request_headers.emplace_back(
  97. std::make_pair<std::string, std::string>("ABC", "def"));
  98. EXPECT_EQ(entry, DownloadEntryFromProto(DownloadEntryToProto(entry)));
  99. }
  100. TEST_F(DownloadDBConversionsTest, DownloadEntries) {
  101. // Entries vector with no entries.
  102. std::vector<DownloadEntry> entries;
  103. EXPECT_EQ(entries, DownloadEntriesFromProto(DownloadEntriesToProto(entries)));
  104. // Entries vector with one entry.
  105. DownloadUrlParameters::RequestHeadersType request_headers;
  106. entries.push_back(DownloadEntry("guid", "request origin",
  107. DownloadSource::UNKNOWN, false,
  108. request_headers, 123));
  109. EXPECT_EQ(entries, DownloadEntriesFromProto(DownloadEntriesToProto(entries)));
  110. // Entries vector with multiple entries.
  111. request_headers.emplace_back(
  112. DownloadUrlParameters::RequestHeadersNameValuePair("key", "value"));
  113. entries.push_back(DownloadEntry("guid2", "request origin",
  114. DownloadSource::UNKNOWN, true,
  115. request_headers, 456));
  116. EXPECT_EQ(entries, DownloadEntriesFromProto(DownloadEntriesToProto(entries)));
  117. }
  118. TEST_F(DownloadDBConversionsTest, DownloadSource) {
  119. DownloadSource sources[] = {
  120. DownloadSource::UNKNOWN, DownloadSource::NAVIGATION,
  121. DownloadSource::DRAG_AND_DROP, DownloadSource::FROM_RENDERER,
  122. DownloadSource::EXTENSION_API, DownloadSource::EXTENSION_INSTALLER,
  123. DownloadSource::INTERNAL_API, DownloadSource::WEB_CONTENTS_API,
  124. DownloadSource::OFFLINE_PAGE, DownloadSource::CONTEXT_MENU,
  125. DownloadSource::RETRY, DownloadSource::RETRY_FROM_BUBBLE};
  126. for (auto source : sources) {
  127. EXPECT_EQ(source, DownloadSourceFromProto(DownloadSourceToProto(source)));
  128. }
  129. }
  130. TEST_F(DownloadDBConversionsTest, HttpRequestHeaders) {
  131. std::pair<std::string, std::string> header;
  132. EXPECT_EQ(header,
  133. HttpRequestHeaderFromProto(HttpRequestHeaderToProto(header)));
  134. header = std::make_pair("123", "456");
  135. EXPECT_EQ(header,
  136. HttpRequestHeaderFromProto(HttpRequestHeaderToProto(header)));
  137. }
  138. TEST_F(DownloadDBConversionsTest, InProgressInfo) {
  139. // InProgressInfo with no fields.
  140. InProgressInfo info;
  141. EXPECT_EQ(false, info.fetch_error_body);
  142. EXPECT_TRUE(info.request_headers.empty());
  143. EXPECT_EQ(info, InProgressInfoFromProto(InProgressInfoToProto(info)));
  144. // InProgressInfo with valid fields.
  145. info = CreateInProgressInfo();
  146. EXPECT_EQ(info, InProgressInfoFromProto(InProgressInfoToProto(info)));
  147. info.range_request_from = 5;
  148. info.range_request_from = 10;
  149. EXPECT_EQ(info, InProgressInfoFromProto(InProgressInfoToProto(info)));
  150. }
  151. TEST_F(DownloadDBConversionsTest, RerouteInfo) {
  152. DownloadItemRerouteInfo reroute_info;
  153. reroute_info.set_service_provider(
  154. enterprise_connectors::FileSystemServiceProvider::BOX);
  155. reroute_info.mutable_box()->set_file_id("12345");
  156. // InProgressInfo with valid fields.
  157. InProgressInfo info = CreateInProgressInfoWithRerouteInfo(reroute_info);
  158. EXPECT_EQ(info, InProgressInfoFromProto(InProgressInfoToProto(info)));
  159. EXPECT_EQ(reroute_info.SerializeAsString(),
  160. info.reroute_info.SerializeAsString());
  161. }
  162. TEST_F(DownloadDBConversionsTest, UkmInfo) {
  163. UkmInfo info(DownloadSource::FROM_RENDERER, 100);
  164. EXPECT_EQ(info, UkmInfoFromProto(UkmInfoToProto(info)));
  165. }
  166. TEST_F(DownloadDBConversionsTest, DownloadInfo) {
  167. DownloadInfo info;
  168. EXPECT_EQ(info, DownloadInfoFromProto(DownloadInfoToProto(info)));
  169. info = CreateDownloadInfo();
  170. EXPECT_EQ(info, DownloadInfoFromProto(DownloadInfoToProto(info)));
  171. }
  172. TEST_F(DownloadDBConversionsTest, DownloadDBEntry) {
  173. DownloadDBEntry entry;
  174. EXPECT_EQ(entry, DownloadDBEntryFromProto(DownloadDBEntryToProto(entry)));
  175. entry.download_info = CreateDownloadInfo();
  176. EXPECT_EQ(entry, DownloadDBEntryFromProto(DownloadDBEntryToProto(entry)));
  177. }
  178. } // namespace download