blob: 20f05b9ccb33841f996e32c400cd67f8a34a79d4 [file] [log] [blame]
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -07001//
2// Copyright (C) 2019 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/libcurl_http_fetcher.h"
18
19#include <string>
20
21#include <brillo/message_loops/fake_message_loop.h>
Amin Hassanid3d84212019-08-17 00:27:44 -070022#include <gmock/gmock.h>
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070023#include <gtest/gtest.h>
24
25#include "update_engine/common/fake_hardware.h"
26#include "update_engine/common/mock_proxy_resolver.h"
Amin Hassanid3d84212019-08-17 00:27:44 -070027#include "update_engine/mock_libcurl_http_fetcher.h"
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070028
29using std::string;
30
31namespace chromeos_update_engine {
32
33namespace {
34constexpr char kHeaderName[] = "X-Goog-Test-Header";
35}
36
37class LibcurlHttpFetcherTest : public ::testing::Test {
38 protected:
39 void SetUp() override {
40 loop_.SetAsCurrent();
41 fake_hardware_.SetIsOfficialBuild(true);
42 fake_hardware_.SetIsOOBEEnabled(false);
43 }
44
45 brillo::FakeMessageLoop loop_{nullptr};
46 FakeHardware fake_hardware_;
Amin Hassanid3d84212019-08-17 00:27:44 -070047 MockLibcurlHttpFetcher libcurl_fetcher_{nullptr, &fake_hardware_};
Xiaochu Liub5ba7972019-07-11 09:51:06 -070048 UnresolvedHostStateMachine state_machine_;
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070049};
50
51TEST_F(LibcurlHttpFetcherTest, GetEmptyHeaderValueTest) {
52 const string header_value = "";
53 string actual_header_value;
54 libcurl_fetcher_.SetHeader(kHeaderName, header_value);
55 EXPECT_TRUE(libcurl_fetcher_.GetHeader(kHeaderName, &actual_header_value));
56 EXPECT_EQ("", actual_header_value);
57}
58
59TEST_F(LibcurlHttpFetcherTest, GetHeaderTest) {
60 const string header_value = "This-is-value 123";
61 string actual_header_value;
62 libcurl_fetcher_.SetHeader(kHeaderName, header_value);
63 EXPECT_TRUE(libcurl_fetcher_.GetHeader(kHeaderName, &actual_header_value));
64 EXPECT_EQ(header_value, actual_header_value);
65}
66
67TEST_F(LibcurlHttpFetcherTest, GetNonExistentHeaderValueTest) {
68 string actual_header_value;
69 // Skip |SetHeaader()| call.
70 EXPECT_FALSE(libcurl_fetcher_.GetHeader(kHeaderName, &actual_header_value));
71 // Even after a failed |GetHeaderValue()|, enforce that the passed pointer to
72 // modifiable string was cleared to be empty.
73 EXPECT_EQ("", actual_header_value);
74}
75
76TEST_F(LibcurlHttpFetcherTest, GetHeaderEdgeCaseTest) {
77 const string header_value = "\a\b\t\v\f\r\\ edge:-case: \a\b\t\v\f\r\\";
78 string actual_header_value;
79 libcurl_fetcher_.SetHeader(kHeaderName, header_value);
80 EXPECT_TRUE(libcurl_fetcher_.GetHeader(kHeaderName, &actual_header_value));
81 EXPECT_EQ(header_value, actual_header_value);
82}
83
Xiaochu Liub5ba7972019-07-11 09:51:06 -070084TEST_F(LibcurlHttpFetcherTest, InvalidURLTest) {
85 int no_network_max_retries = 1;
86 libcurl_fetcher_.set_no_network_max_retries(no_network_max_retries);
87
Amin Hassanid3d84212019-08-17 00:27:44 -070088 libcurl_fetcher_.BeginTransfer("not-a-URL");
Xiaochu Liub5ba7972019-07-11 09:51:06 -070089 while (loop_.PendingTasks()) {
90 loop_.RunOnce(true);
91 }
92
93 EXPECT_EQ(libcurl_fetcher_.get_no_network_max_retries(),
94 no_network_max_retries);
95}
96
Amin Hassanid3d84212019-08-17 00:27:44 -070097TEST_F(LibcurlHttpFetcherTest, CouldNotResolveHostTest) {
Xiaochu Liub5ba7972019-07-11 09:51:06 -070098 int no_network_max_retries = 1;
99 libcurl_fetcher_.set_no_network_max_retries(no_network_max_retries);
100
Amin Hassanid3d84212019-08-17 00:27:44 -0700101 libcurl_fetcher_.BeginTransfer("https://An-uNres0lvable-uRl.invalid");
102
103 // The first time it can't resolve.
104 loop_.RunOnce(true);
105 EXPECT_EQ(libcurl_fetcher_.GetAuxiliaryErrorCode(),
106 ErrorCode::kUnresolvedHostError);
107
108 while (loop_.PendingTasks()) {
109 loop_.RunOnce(true);
110 }
111 // The auxilary error code should've have been changed.
112 EXPECT_EQ(libcurl_fetcher_.GetAuxiliaryErrorCode(),
113 ErrorCode::kUnresolvedHostError);
114
115 // If libcurl fails to resolve the name, we call res_init() to reload
116 // resolv.conf and retry exactly once more. See crbug.com/982813 for details.
117 EXPECT_EQ(libcurl_fetcher_.get_no_network_max_retries(),
118 no_network_max_retries + 1);
119}
120
121TEST_F(LibcurlHttpFetcherTest, HostResolvedTest) {
122 int no_network_max_retries = 2;
123 libcurl_fetcher_.set_no_network_max_retries(no_network_max_retries);
124
Xiaochu Liub5ba7972019-07-11 09:51:06 -0700125 // This test actually sends request to internet but according to
126 // https://tools.ietf.org/html/rfc2606#section-2, .invalid domain names are
127 // reserved and sure to be invalid. Ideally we should mock libcurl or
128 // reorganize LibcurlHttpFetcher so the part that sends request can be mocked
129 // easily.
130 // TODO(xiaochu) Refactor LibcurlHttpFetcher (and its relates) so it's
131 // easier to mock the part that depends on internet connectivity.
132 libcurl_fetcher_.BeginTransfer("https://An-uNres0lvable-uRl.invalid");
Amin Hassanid3d84212019-08-17 00:27:44 -0700133
134 // The first time it can't resolve.
135 loop_.RunOnce(true);
136 EXPECT_EQ(libcurl_fetcher_.GetAuxiliaryErrorCode(),
137 ErrorCode::kUnresolvedHostError);
138
139 // The second time, it will resolve, with error code 200 but we set the
140 // download size be smaller than the transfer size so it will retry again.
141 EXPECT_CALL(libcurl_fetcher_, GetHttpResponseCode())
142 .WillOnce(testing::Invoke(
143 [this]() { libcurl_fetcher_.http_response_code_ = 200; }))
144 .WillRepeatedly(testing::Invoke(
145 [this]() { libcurl_fetcher_.http_response_code_ = 0; }));
146 libcurl_fetcher_.transfer_size_ = 10;
147
148 // This time the host is resolved. But after that again we can't resolve
149 // anymore (See above).
150 loop_.RunOnce(true);
151 EXPECT_EQ(libcurl_fetcher_.GetAuxiliaryErrorCode(),
152 ErrorCode::kUnresolvedHostRecovered);
153
Xiaochu Liub5ba7972019-07-11 09:51:06 -0700154 while (loop_.PendingTasks()) {
155 loop_.RunOnce(true);
156 }
Amin Hassanid3d84212019-08-17 00:27:44 -0700157 // The auxilary error code should not have been changed.
158 EXPECT_EQ(libcurl_fetcher_.GetAuxiliaryErrorCode(),
159 ErrorCode::kUnresolvedHostRecovered);
Xiaochu Liub5ba7972019-07-11 09:51:06 -0700160
161 // If libcurl fails to resolve the name, we call res_init() to reload
162 // resolv.conf and retry exactly once more. See crbug.com/982813 for details.
163 EXPECT_EQ(libcurl_fetcher_.get_no_network_max_retries(),
164 no_network_max_retries + 1);
165}
166
167TEST_F(LibcurlHttpFetcherTest, HttpFetcherStateMachineRetryFailedTest) {
168 state_machine_.UpdateState(true);
169 state_machine_.UpdateState(true);
Amin Hassanid3d84212019-08-17 00:27:44 -0700170 EXPECT_EQ(state_machine_.GetState(),
Xiaochu Liub5ba7972019-07-11 09:51:06 -0700171 UnresolvedHostStateMachine::State::kNotRetry);
172}
173
174TEST_F(LibcurlHttpFetcherTest, HttpFetcherStateMachineRetrySucceedTest) {
175 state_machine_.UpdateState(true);
176 state_machine_.UpdateState(false);
Amin Hassanid3d84212019-08-17 00:27:44 -0700177 EXPECT_EQ(state_machine_.GetState(),
Xiaochu Liub5ba7972019-07-11 09:51:06 -0700178 UnresolvedHostStateMachine::State::kRetriedSuccess);
179}
180
181TEST_F(LibcurlHttpFetcherTest, HttpFetcherStateMachineNoRetryTest) {
182 state_machine_.UpdateState(false);
183 state_machine_.UpdateState(false);
Amin Hassanid3d84212019-08-17 00:27:44 -0700184 EXPECT_EQ(state_machine_.GetState(),
Xiaochu Liub5ba7972019-07-11 09:51:06 -0700185 UnresolvedHostStateMachine::State::kInit);
186}
187
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700188} // namespace chromeos_update_engine