|
| 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/image_aggregator/composite_io.h" |
| 17 | + |
| 18 | +#include <string> |
| 19 | +#include <string_view> |
| 20 | + |
| 21 | +#include "absl/strings/str_cat.h" |
| 22 | +#include "gtest/gtest.h" |
| 23 | + |
| 24 | +#include "cuttlefish/io/io.h" |
| 25 | +#include "cuttlefish/host/libs/image_aggregator/composite_disk.h" |
| 26 | +#include "cuttlefish/io/filesystem.h" |
| 27 | +#include "cuttlefish/io/in_memory.h" |
| 28 | +#include "cuttlefish/io/string.h" |
| 29 | +#include "cuttlefish/result/result_matchers.h" |
| 30 | +#include "cuttlefish/result/result_type.h" |
| 31 | + |
| 32 | +namespace cuttlefish { |
| 33 | +namespace { |
| 34 | + |
| 35 | +TEST(CompositeIoTest, SeekValue) { |
| 36 | + std::unique_ptr<ReadWriteFilesystem> fs = InMemoryFilesystem(); |
| 37 | + ASSERT_NE(fs.get(), nullptr); |
| 38 | + |
| 39 | + static constexpr std::string_view kAPath = "a"; |
| 40 | + static constexpr std::string_view kAContent = "hello "; |
| 41 | + Result<std::unique_ptr<ReaderWriterSeeker>> file_a = fs->CreateFile(kAPath); |
| 42 | + ASSERT_THAT(file_a, IsOk()); |
| 43 | + ASSERT_NE(file_a->get(), nullptr); |
| 44 | + ASSERT_THAT(WriteString(**file_a, kAContent), IsOk()); |
| 45 | + |
| 46 | + static constexpr std::string_view kBPath = "b"; |
| 47 | + static constexpr std::string_view kBContent = "world"; |
| 48 | + Result<std::unique_ptr<ReaderWriterSeeker>> file_b = fs->CreateFile(kBPath); |
| 49 | + ASSERT_THAT(file_b, IsOk()); |
| 50 | + ASSERT_NE(file_b->get(), nullptr); |
| 51 | + ASSERT_THAT(WriteString(**file_b, kBContent), IsOk()); |
| 52 | + |
| 53 | + CompositeDisk disk; |
| 54 | + disk.set_length(kAContent.size() + kBContent.size()); |
| 55 | + |
| 56 | + ComponentDisk& member_a = *disk.add_component_disks(); |
| 57 | + member_a.set_file_path(kAPath); |
| 58 | + member_a.set_offset(0); |
| 59 | + |
| 60 | + ComponentDisk& member_b = *disk.add_component_disks(); |
| 61 | + member_b.set_file_path(kBPath); |
| 62 | + member_b.set_offset(kAContent.size()); |
| 63 | + |
| 64 | + CompositeDiskImage image(std::move(disk)); |
| 65 | + |
| 66 | + Result<CompositeDiskReaderIo> io = |
| 67 | + CompositeDiskReaderIo::Create(std::move(image), *fs); |
| 68 | + ASSERT_THAT(io, IsOk()); |
| 69 | + |
| 70 | + ASSERT_THAT(ReadToString(*io), |
| 71 | + IsOkAndValue(absl::StrCat(kAContent, kBContent))); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace |
| 75 | +} // namespace cuttlefish |
0 commit comments