xref: /aosp_15_r20/system/incremental_delivery/incfs/tests/include/IncFsTestBase.h (revision 9190c2a8bd3622b7aa9bd7bfe4b3aec77820f478)
1*9190c2a8SAndroid Build Coastguard Worker /*
2*9190c2a8SAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*9190c2a8SAndroid Build Coastguard Worker  *
4*9190c2a8SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*9190c2a8SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*9190c2a8SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*9190c2a8SAndroid Build Coastguard Worker  *
8*9190c2a8SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*9190c2a8SAndroid Build Coastguard Worker  *
10*9190c2a8SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*9190c2a8SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*9190c2a8SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9190c2a8SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*9190c2a8SAndroid Build Coastguard Worker  * limitations under the License.
15*9190c2a8SAndroid Build Coastguard Worker  */
16*9190c2a8SAndroid Build Coastguard Worker 
17*9190c2a8SAndroid Build Coastguard Worker #pragma once
18*9190c2a8SAndroid Build Coastguard Worker 
19*9190c2a8SAndroid Build Coastguard Worker #include <android-base/logging.h>
20*9190c2a8SAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
21*9190c2a8SAndroid Build Coastguard Worker #include <gtest/gtest.h>
22*9190c2a8SAndroid Build Coastguard Worker #include <selinux/selinux.h>
23*9190c2a8SAndroid Build Coastguard Worker 
24*9190c2a8SAndroid Build Coastguard Worker #include "incfs.h"
25*9190c2a8SAndroid Build Coastguard Worker #include "path.h"
26*9190c2a8SAndroid Build Coastguard Worker 
27*9190c2a8SAndroid Build Coastguard Worker namespace android::incfs {
28*9190c2a8SAndroid Build Coastguard Worker 
exists(std::string_view path)29*9190c2a8SAndroid Build Coastguard Worker static bool exists(std::string_view path) {
30*9190c2a8SAndroid Build Coastguard Worker     return access(path.data(), F_OK) == 0;
31*9190c2a8SAndroid Build Coastguard Worker }
32*9190c2a8SAndroid Build Coastguard Worker 
33*9190c2a8SAndroid Build Coastguard Worker class IncFsTestBase : public ::testing::Test {
34*9190c2a8SAndroid Build Coastguard Worker protected:
SetUp()35*9190c2a8SAndroid Build Coastguard Worker     virtual void SetUp() {
36*9190c2a8SAndroid Build Coastguard Worker         tmp_dir_for_mount_.emplace();
37*9190c2a8SAndroid Build Coastguard Worker         mount_dir_path_ = tmp_dir_for_mount_->path;
38*9190c2a8SAndroid Build Coastguard Worker         tmp_dir_for_image_.emplace();
39*9190c2a8SAndroid Build Coastguard Worker         image_dir_path_ = tmp_dir_for_image_->path;
40*9190c2a8SAndroid Build Coastguard Worker         ASSERT_TRUE(exists(image_dir_path_));
41*9190c2a8SAndroid Build Coastguard Worker         ASSERT_TRUE(exists(mount_dir_path_));
42*9190c2a8SAndroid Build Coastguard Worker         if (!enabled()) {
43*9190c2a8SAndroid Build Coastguard Worker             GTEST_SKIP() << "test not supported: IncFS is not enabled";
44*9190c2a8SAndroid Build Coastguard Worker         } else {
45*9190c2a8SAndroid Build Coastguard Worker             metrics_key_ = path::baseName(image_dir_path_);
46*9190c2a8SAndroid Build Coastguard Worker             control_ = mount(image_dir_path_, mount_dir_path_,
47*9190c2a8SAndroid Build Coastguard Worker                              MountOptions{.readLogBufferPages = 4,
48*9190c2a8SAndroid Build Coastguard Worker                                           .defaultReadTimeoutMs = getReadTimeout(),
49*9190c2a8SAndroid Build Coastguard Worker                                           .sysfsName = metrics_key_.c_str()});
50*9190c2a8SAndroid Build Coastguard Worker             ASSERT_TRUE(control_.cmd() >= 0) << "Expected >= 0 got " << control_.cmd();
51*9190c2a8SAndroid Build Coastguard Worker             ASSERT_TRUE(control_.pendingReads() >= 0);
52*9190c2a8SAndroid Build Coastguard Worker             ASSERT_TRUE(control_.logs() >= 0);
53*9190c2a8SAndroid Build Coastguard Worker             checkRestoreconResult(mountPath(INCFS_PENDING_READS_FILENAME));
54*9190c2a8SAndroid Build Coastguard Worker             checkRestoreconResult(mountPath(INCFS_LOG_FILENAME));
55*9190c2a8SAndroid Build Coastguard Worker         }
56*9190c2a8SAndroid Build Coastguard Worker     }
57*9190c2a8SAndroid Build Coastguard Worker 
TearDown()58*9190c2a8SAndroid Build Coastguard Worker     virtual void TearDown() {
59*9190c2a8SAndroid Build Coastguard Worker         unmount(mount_dir_path_);
60*9190c2a8SAndroid Build Coastguard Worker         tmp_dir_for_image_.reset();
61*9190c2a8SAndroid Build Coastguard Worker         tmp_dir_for_mount_.reset();
62*9190c2a8SAndroid Build Coastguard Worker         EXPECT_FALSE(exists(image_dir_path_));
63*9190c2a8SAndroid Build Coastguard Worker         EXPECT_FALSE(exists(mount_dir_path_));
64*9190c2a8SAndroid Build Coastguard Worker     }
65*9190c2a8SAndroid Build Coastguard Worker 
checkRestoreconResult(std::string_view path)66*9190c2a8SAndroid Build Coastguard Worker     static void checkRestoreconResult(std::string_view path) {
67*9190c2a8SAndroid Build Coastguard Worker         char* ctx = nullptr;
68*9190c2a8SAndroid Build Coastguard Worker         ASSERT_NE(-1, getfilecon(path.data(), &ctx));
69*9190c2a8SAndroid Build Coastguard Worker         ASSERT_EQ("u:object_r:shell_data_file:s0", std::string(ctx));
70*9190c2a8SAndroid Build Coastguard Worker         freecon(ctx);
71*9190c2a8SAndroid Build Coastguard Worker     }
72*9190c2a8SAndroid Build Coastguard Worker 
fileId(uint64_t i)73*9190c2a8SAndroid Build Coastguard Worker     static IncFsFileId fileId(uint64_t i) {
74*9190c2a8SAndroid Build Coastguard Worker         IncFsFileId id = {};
75*9190c2a8SAndroid Build Coastguard Worker         static_assert(sizeof(id) >= sizeof(i));
76*9190c2a8SAndroid Build Coastguard Worker         memcpy(&id, &i, sizeof(i));
77*9190c2a8SAndroid Build Coastguard Worker         return id;
78*9190c2a8SAndroid Build Coastguard Worker     }
79*9190c2a8SAndroid Build Coastguard Worker 
getReadTimeout()80*9190c2a8SAndroid Build Coastguard Worker     virtual int32_t getReadTimeout() {
81*9190c2a8SAndroid Build Coastguard Worker         return std::chrono::duration_cast<std::chrono::milliseconds>(kDefaultReadTimeout).count();
82*9190c2a8SAndroid Build Coastguard Worker     }
83*9190c2a8SAndroid Build Coastguard Worker 
84*9190c2a8SAndroid Build Coastguard Worker     template <class... Paths>
mountPath(Paths &&...paths)85*9190c2a8SAndroid Build Coastguard Worker     std::string mountPath(Paths&&... paths) const {
86*9190c2a8SAndroid Build Coastguard Worker         return path::join(mount_dir_path_, std::forward<Paths>(paths)...);
87*9190c2a8SAndroid Build Coastguard Worker     }
88*9190c2a8SAndroid Build Coastguard Worker 
makeFileWithHash(int id)89*9190c2a8SAndroid Build Coastguard Worker     virtual int makeFileWithHash(int id) {
90*9190c2a8SAndroid Build Coastguard Worker         // calculate the required size for two leaf hash blocks
91*9190c2a8SAndroid Build Coastguard Worker         constexpr auto size =
92*9190c2a8SAndroid Build Coastguard Worker                 (INCFS_DATA_FILE_BLOCK_SIZE / INCFS_MAX_HASH_SIZE + 1) * INCFS_DATA_FILE_BLOCK_SIZE;
93*9190c2a8SAndroid Build Coastguard Worker 
94*9190c2a8SAndroid Build Coastguard Worker         // assemble a signature/hashing data for it
95*9190c2a8SAndroid Build Coastguard Worker         struct __attribute__((packed)) Signature {
96*9190c2a8SAndroid Build Coastguard Worker             uint32_t version = INCFS_SIGNATURE_VERSION;
97*9190c2a8SAndroid Build Coastguard Worker             uint32_t hashingSize = sizeof(hashing);
98*9190c2a8SAndroid Build Coastguard Worker             struct __attribute__((packed)) Hashing {
99*9190c2a8SAndroid Build Coastguard Worker                 uint32_t algo = INCFS_HASH_TREE_SHA256;
100*9190c2a8SAndroid Build Coastguard Worker                 uint8_t log2Blocksize = 12;
101*9190c2a8SAndroid Build Coastguard Worker                 uint32_t saltSize = 0;
102*9190c2a8SAndroid Build Coastguard Worker                 uint32_t rootHashSize = INCFS_MAX_HASH_SIZE;
103*9190c2a8SAndroid Build Coastguard Worker                 char rootHash[INCFS_MAX_HASH_SIZE] = {};
104*9190c2a8SAndroid Build Coastguard Worker             } hashing;
105*9190c2a8SAndroid Build Coastguard Worker             uint32_t signingSize = 0;
106*9190c2a8SAndroid Build Coastguard Worker         } signature;
107*9190c2a8SAndroid Build Coastguard Worker 
108*9190c2a8SAndroid Build Coastguard Worker         int res = makeFile(control_, mountPath(test_file_name_), 0555, fileId(id),
109*9190c2a8SAndroid Build Coastguard Worker                            {.size = size,
110*9190c2a8SAndroid Build Coastguard Worker                             .signature = {.data = (char*)&signature, .size = sizeof(signature)}});
111*9190c2a8SAndroid Build Coastguard Worker         EXPECT_EQ(0, res);
112*9190c2a8SAndroid Build Coastguard Worker         return res ? -1 : size;
113*9190c2a8SAndroid Build Coastguard Worker     }
114*9190c2a8SAndroid Build Coastguard Worker 
115*9190c2a8SAndroid Build Coastguard Worker     std::string mount_dir_path_;
116*9190c2a8SAndroid Build Coastguard Worker     std::optional<TemporaryDir> tmp_dir_for_mount_;
117*9190c2a8SAndroid Build Coastguard Worker     std::string image_dir_path_;
118*9190c2a8SAndroid Build Coastguard Worker     std::optional<TemporaryDir> tmp_dir_for_image_;
119*9190c2a8SAndroid Build Coastguard Worker     inline static const std::string_view test_file_name_ = "test.txt";
120*9190c2a8SAndroid Build Coastguard Worker     inline static const std::string_view test_mapped_file_name_ = "mapped.txt";
121*9190c2a8SAndroid Build Coastguard Worker     inline static const std::string_view test_dir_name_ = "test_dir";
122*9190c2a8SAndroid Build Coastguard Worker     std::string metrics_key_;
123*9190c2a8SAndroid Build Coastguard Worker     Control control_;
124*9190c2a8SAndroid Build Coastguard Worker };
125*9190c2a8SAndroid Build Coastguard Worker 
126*9190c2a8SAndroid Build Coastguard Worker } // namespace android::incfs
127