|
| 1 | +// |
| 2 | +// Copyright (C) 2026 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 | +#include "cuttlefish/host/libs/web/build_api.h" |
| 17 | + |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "gmock/gmock.h" |
| 21 | +#include "gtest/gtest.h" |
| 22 | + |
| 23 | +#include "cuttlefish/host/libs/web/android_build.h" |
| 24 | +#include "cuttlefish/host/libs/web/android_build_string.h" |
| 25 | +#include "cuttlefish/host/libs/zip/libzip_cc/seekable_source.h" |
| 26 | +#include "cuttlefish/result/result.h" |
| 27 | +#include "cuttlefish/result/result_matchers.h" |
| 28 | + |
| 29 | +namespace cuttlefish { |
| 30 | +namespace { |
| 31 | + |
| 32 | +using ::testing::_; |
| 33 | +using ::testing::Return; |
| 34 | + |
| 35 | +class MockBuildApi : public BuildApi { |
| 36 | + public: |
| 37 | + MOCK_METHOD(Result<Build>, GetBuild, (const BuildString&), (override)); |
| 38 | + MOCK_METHOD(Result<std::string>, DownloadFile, |
| 39 | + (const Build&, const std::string&, const std::string&), |
| 40 | + (override)); |
| 41 | + MOCK_METHOD(Result<SeekableZipSource>, FileReader, |
| 42 | + (const Build&, const std::string&), (override)); |
| 43 | +}; |
| 44 | + |
| 45 | +TEST(BuildApiTest, DownloadFileWithBackupSuccessFirst) { |
| 46 | + MockBuildApi mock_api; |
| 47 | + Build build = DeviceBuild{.id = "123", .target = "test"}; |
| 48 | + std::string target_dir = "/tmp"; |
| 49 | + std::string artifact_name = "primary.zip"; |
| 50 | + std::string backup_artifact_name = "backup.zip"; |
| 51 | + std::string expected_path = "/tmp/primary.zip"; |
| 52 | + |
| 53 | + EXPECT_CALL(mock_api, DownloadFile(_, target_dir, artifact_name)) |
| 54 | + .WillOnce(Return(expected_path)); |
| 55 | + EXPECT_CALL(mock_api, DownloadFile(_, target_dir, backup_artifact_name)) |
| 56 | + .Times(0); |
| 57 | + |
| 58 | + Result<std::string> result = DownloadFileWithBackup( |
| 59 | + mock_api, build, target_dir, artifact_name, backup_artifact_name); |
| 60 | + |
| 61 | + ASSERT_THAT(result, IsOkAndValue(expected_path)); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(BuildApiTest, DownloadFileWithBackupFallback) { |
| 65 | + MockBuildApi mock_api; |
| 66 | + Build build = DeviceBuild{.id = "123", .target = "test"}; |
| 67 | + std::string target_dir = "/tmp"; |
| 68 | + std::string artifact_name = "primary.zip"; |
| 69 | + std::string backup_artifact_name = "backup.zip"; |
| 70 | + std::string expected_path = "/tmp/backup.zip"; |
| 71 | + |
| 72 | + EXPECT_CALL(mock_api, DownloadFile(_, target_dir, artifact_name)) |
| 73 | + .WillOnce(Return(CF_ERR("File not found"))); |
| 74 | + EXPECT_CALL(mock_api, DownloadFile(_, target_dir, backup_artifact_name)) |
| 75 | + .WillOnce(Return(expected_path)); |
| 76 | + |
| 77 | + Result<std::string> result = DownloadFileWithBackup( |
| 78 | + mock_api, build, target_dir, artifact_name, backup_artifact_name); |
| 79 | + |
| 80 | + ASSERT_THAT(result, IsOkAndValue(expected_path)); |
| 81 | +} |
| 82 | + |
| 83 | +TEST(BuildApiTest, DownloadFileWithBackupBothFail) { |
| 84 | + MockBuildApi mock_api; |
| 85 | + Build build = DeviceBuild{.id = "123", .target = "test"}; |
| 86 | + std::string target_dir = "/tmp"; |
| 87 | + std::string artifact_name = "primary.zip"; |
| 88 | + std::string backup_artifact_name = "backup.zip"; |
| 89 | + |
| 90 | + EXPECT_CALL(mock_api, DownloadFile(_, target_dir, artifact_name)) |
| 91 | + .WillOnce(Return(CF_ERR("Primary file not found"))); |
| 92 | + EXPECT_CALL(mock_api, DownloadFile(_, target_dir, backup_artifact_name)) |
| 93 | + .WillOnce(Return(CF_ERR("Backup file not found"))); |
| 94 | + |
| 95 | + Result<std::string> result = DownloadFileWithBackup( |
| 96 | + mock_api, build, target_dir, artifact_name, backup_artifact_name); |
| 97 | + |
| 98 | + ASSERT_THAT(result, IsError()); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace |
| 102 | +} // namespace cuttlefish |
0 commit comments