Skip to content

Commit 625e3de

Browse files
committed
Add CompositeIoTest
Bug: b/495556334
1 parent 23e93aa commit 625e3de

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

base/cvd/cuttlefish/host/libs/image_aggregator/BUILD.bazel

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
22
load("@protobuf//bazel:proto_library.bzl", "proto_library")
3-
load("//cuttlefish/bazel:rules.bzl", "COPTS", "cf_cc_library")
3+
load("//cuttlefish/bazel:rules.bzl", "COPTS", "cf_cc_library", "cf_cc_test")
44

55
package(
66
default_visibility = ["//:android_cuttlefish"],
@@ -48,6 +48,25 @@ cf_cc_library(
4848
],
4949
)
5050

51+
cf_cc_test(
52+
name = "composite_io_test",
53+
srcs = ["composite_io_test.cc"],
54+
deps = [
55+
"//cuttlefish/host/libs/image_aggregator:cdisk_spec_cc_proto",
56+
"//cuttlefish/host/libs/image_aggregator:composite_disk",
57+
"//cuttlefish/host/libs/image_aggregator:composite_io",
58+
"//cuttlefish/io",
59+
"//cuttlefish/io:filesystem",
60+
"//cuttlefish/io:in_memory",
61+
"//cuttlefish/io:string",
62+
"//cuttlefish/result:result_matchers",
63+
"//cuttlefish/result:result_type",
64+
"@abseil-cpp//absl/strings",
65+
"@googletest//:gtest",
66+
"@googletest//:gtest_main",
67+
],
68+
)
69+
5170
cf_cc_library(
5271
name = "disk_image",
5372
hdrs = ["disk_image.h"],

base/cvd/cuttlefish/host/libs/image_aggregator/composite_disk.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace cuttlefish {
2929
/** File representing a virtual disk made of separate component files. */
3030
class CompositeDiskImage : public DiskImage {
3131
public:
32+
CompositeDiskImage(CompositeDisk);
33+
3234
static Result<CompositeDiskImage> OpenExisting(Reader&);
3335
static Result<CompositeDiskImage> OpenExisting(const std::string& path);
3436

@@ -51,7 +53,6 @@ class CompositeDiskImage : public DiskImage {
5153
}
5254

5355
private:
54-
CompositeDiskImage(CompositeDisk);
5556
CompositeDisk cdisk_;
5657
};
5758

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)