Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame^] | 1 | // |
| 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> |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | #include "update_engine/common/fake_hardware.h" |
| 25 | #include "update_engine/common/mock_proxy_resolver.h" |
| 26 | |
| 27 | using std::string; |
| 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
| 31 | namespace { |
| 32 | constexpr char kHeaderName[] = "X-Goog-Test-Header"; |
| 33 | } |
| 34 | |
| 35 | class LibcurlHttpFetcherTest : public ::testing::Test { |
| 36 | protected: |
| 37 | void SetUp() override { |
| 38 | loop_.SetAsCurrent(); |
| 39 | fake_hardware_.SetIsOfficialBuild(true); |
| 40 | fake_hardware_.SetIsOOBEEnabled(false); |
| 41 | } |
| 42 | |
| 43 | brillo::FakeMessageLoop loop_{nullptr}; |
| 44 | FakeHardware fake_hardware_; |
| 45 | LibcurlHttpFetcher libcurl_fetcher_{nullptr, &fake_hardware_}; |
| 46 | }; |
| 47 | |
| 48 | TEST_F(LibcurlHttpFetcherTest, GetEmptyHeaderValueTest) { |
| 49 | const string header_value = ""; |
| 50 | string actual_header_value; |
| 51 | libcurl_fetcher_.SetHeader(kHeaderName, header_value); |
| 52 | EXPECT_TRUE(libcurl_fetcher_.GetHeader(kHeaderName, &actual_header_value)); |
| 53 | EXPECT_EQ("", actual_header_value); |
| 54 | } |
| 55 | |
| 56 | TEST_F(LibcurlHttpFetcherTest, GetHeaderTest) { |
| 57 | const string header_value = "This-is-value 123"; |
| 58 | string actual_header_value; |
| 59 | libcurl_fetcher_.SetHeader(kHeaderName, header_value); |
| 60 | EXPECT_TRUE(libcurl_fetcher_.GetHeader(kHeaderName, &actual_header_value)); |
| 61 | EXPECT_EQ(header_value, actual_header_value); |
| 62 | } |
| 63 | |
| 64 | TEST_F(LibcurlHttpFetcherTest, GetNonExistentHeaderValueTest) { |
| 65 | string actual_header_value; |
| 66 | // Skip |SetHeaader()| call. |
| 67 | EXPECT_FALSE(libcurl_fetcher_.GetHeader(kHeaderName, &actual_header_value)); |
| 68 | // Even after a failed |GetHeaderValue()|, enforce that the passed pointer to |
| 69 | // modifiable string was cleared to be empty. |
| 70 | EXPECT_EQ("", actual_header_value); |
| 71 | } |
| 72 | |
| 73 | TEST_F(LibcurlHttpFetcherTest, GetHeaderEdgeCaseTest) { |
| 74 | const string header_value = "\a\b\t\v\f\r\\ edge:-case: \a\b\t\v\f\r\\"; |
| 75 | string actual_header_value; |
| 76 | libcurl_fetcher_.SetHeader(kHeaderName, header_value); |
| 77 | EXPECT_TRUE(libcurl_fetcher_.GetHeader(kHeaderName, &actual_header_value)); |
| 78 | EXPECT_EQ(header_value, actual_header_value); |
| 79 | } |
| 80 | |
| 81 | } // namespace chromeos_update_engine |