File: proto_conversions_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 7,100 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/download/internal/background_service/proto_conversions.h"

#include <memory>
#include <utility>

#include "base/uuid.h"
#include "components/download/internal/background_service/entry.h"
#include "components/download/internal/background_service/test/entry_utils.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
constexpr char kTestUrl[] = "https://www.example.com";
constexpr char kKey[] = "k";
constexpr char kValue[] = "v";
}  // namespace

namespace download {

class ProtoConversionsTest : public testing::Test, public ProtoConversions {
 public:
  ~ProtoConversionsTest() override = default;
};

TEST_F(ProtoConversionsTest, StateConversion) {
  Entry::State states[] = {Entry::State::NEW, Entry::State::AVAILABLE,
                           Entry::State::ACTIVE, Entry::State::PAUSED,
                           Entry::State::COMPLETE};
  for (auto state : states) {
    ASSERT_EQ(state, RequestStateFromProto(RequestStateToProto(state)));
  }
}

TEST_F(ProtoConversionsTest, DownloadClientConversion) {
  DownloadClient clients[] = {DownloadClient::INVALID, DownloadClient::TEST,
                              DownloadClient::TEST_2, DownloadClient::BOUNDARY};
  for (auto client : clients) {
    ASSERT_EQ(client, DownloadClientFromProto(DownloadClientToProto(client)));
  }
}

TEST_F(ProtoConversionsTest, NetworkRequirementsConversion) {
  SchedulingParams::NetworkRequirements values[] = {
      SchedulingParams::NetworkRequirements::NONE,
      SchedulingParams::NetworkRequirements::OPTIMISTIC,
      SchedulingParams::NetworkRequirements::UNMETERED};
  for (auto value : values) {
    ASSERT_EQ(value,
              NetworkRequirementsFromProto(NetworkRequirementsToProto(value)));
  }
}

TEST_F(ProtoConversionsTest, BatteryRequirementsConversion) {
  SchedulingParams::BatteryRequirements values[] = {
      SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE,
      SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE,
      SchedulingParams::BatteryRequirements::BATTERY_CHARGING};
  for (auto value : values) {
    ASSERT_EQ(value,
              BatteryRequirementsFromProto(BatteryRequirementsToProto(value)));
  }
}

TEST_F(ProtoConversionsTest, SchedulingPriorityConversion) {
  SchedulingParams::Priority values[] = {
      SchedulingParams::Priority::LOW, SchedulingParams::Priority::NORMAL,
      SchedulingParams::Priority::HIGH, SchedulingParams::Priority::UI,
      SchedulingParams::Priority::DEFAULT};
  for (auto value : values) {
    ASSERT_EQ(value,
              SchedulingPriorityFromProto(SchedulingPriorityToProto(value)));
  }
}

TEST_F(ProtoConversionsTest, SchedulingParamsConversion) {
  SchedulingParams expected;
  expected.cancel_time = base::Time::Now();
  expected.priority = SchedulingParams::Priority::DEFAULT;
  expected.network_requirements =
      SchedulingParams::NetworkRequirements::OPTIMISTIC;
  expected.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE;

  protodb::SchedulingParams proto;
  SchedulingParamsToProto(expected, &proto);
  SchedulingParams actual = SchedulingParamsFromProto(proto);
  EXPECT_EQ(expected.cancel_time, actual.cancel_time);
  EXPECT_EQ(SchedulingParams::Priority::DEFAULT, actual.priority);
  EXPECT_EQ(SchedulingParams::NetworkRequirements::OPTIMISTIC,
            actual.network_requirements);
  EXPECT_EQ(SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE,
            actual.battery_requirements);
}

TEST_F(ProtoConversionsTest, RequestParamsWithHeadersConversion) {
  RequestParams expected;
  expected.url = GURL(kTestUrl);
  expected.method = "GET";
  expected.fetch_error_body = true;
  expected.require_safety_checks = false;
  expected.credentials_mode = ::network::mojom::CredentialsMode::kInclude;
  expected.request_headers.SetHeader("key1", "value1");
  expected.request_headers.SetHeader("key2", "value2");

  protodb::RequestParams proto;
  RequestParamsToProto(expected, &proto);
  RequestParams actual = RequestParamsFromProto(proto);

  EXPECT_EQ(expected.url, actual.url);
  EXPECT_EQ(expected.method, actual.method);
  EXPECT_EQ(expected.fetch_error_body, actual.fetch_error_body);
  EXPECT_EQ(expected.require_safety_checks, actual.require_safety_checks);
  EXPECT_EQ(expected.credentials_mode, actual.credentials_mode);

  EXPECT_EQ("value1", actual.request_headers.GetHeader("key1"));
  EXPECT_EQ("value2", actual.request_headers.GetHeader("key2"));
  EXPECT_EQ(expected.request_headers.ToString(),
            actual.request_headers.ToString());
}

TEST_F(ProtoConversionsTest, RequestParamsWithMissingCredentialsMode) {
  RequestParams expected;
  expected.url = GURL(kTestUrl);
  expected.method = "GET";

  protodb::RequestParams proto;
  RequestParamsToProto(expected, &proto);
  RequestParams actual = RequestParamsFromProto(proto);

  EXPECT_EQ(expected.credentials_mode,
            ::network::mojom::CredentialsMode::kInclude);
}

TEST_F(ProtoConversionsTest, EntryConversion) {
  Entry expected = test::BuildEntry(
      DownloadClient::TEST, base::Uuid::GenerateRandomV4().AsLowercaseString());
  Entry actual = EntryFromProto(EntryToProto(expected));
  EXPECT_TRUE(test::CompareEntry(&expected, &actual));

  expected = test::BuildEntry(
      DownloadClient::TEST, base::Uuid::GenerateRandomV4().AsLowercaseString(),
      base::Time::Now(), SchedulingParams::NetworkRequirements::OPTIMISTIC,
      SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE,
      SchedulingParams::Priority::HIGH, GURL(kTestUrl), "GET",
      Entry::State::ACTIVE, base::FilePath(FILE_PATH_LITERAL("/test/xyz")),
      base::Time::Now(), base::Time::Now(), base::Time::Now(), 1024u, 3, 8);
  expected.custom_data = {{kKey, kValue}};
  actual = EntryFromProto(EntryToProto(expected));
  EXPECT_TRUE(test::CompareEntry(&expected, &actual));
}

TEST_F(ProtoConversionsTest, EntryVectorConversion) {
  std::vector<Entry> expected;
  expected.push_back(
      test::BuildEntry(DownloadClient::TEST,
                       base::Uuid::GenerateRandomV4().AsLowercaseString()));
  expected.push_back(
      test::BuildEntry(DownloadClient::TEST_2,
                       base::Uuid::GenerateRandomV4().AsLowercaseString()));
  expected.push_back(test::BuildEntry(
      DownloadClient::TEST, base::Uuid::GenerateRandomV4().AsLowercaseString(),
      base::Time::Now(), SchedulingParams::NetworkRequirements::OPTIMISTIC,
      SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE,
      SchedulingParams::Priority::HIGH, GURL(kTestUrl), "GET",
      Entry::State::ACTIVE, base::FilePath(FILE_PATH_LITERAL("/test/xyz")),
      base::Time::Now(), base::Time::Now(), base::Time::Now(), 1024u, 2, 8));

  auto actual = EntryVectorFromProto(
      EntryVectorToProto(std::make_unique<std::vector<Entry>>(expected)));
  EXPECT_TRUE(test::CompareEntryList(expected, *actual));
}

}  // namespace download