1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <android-base/logging.h>
20 #include <android-base/unique_fd.h>
21 #include <gtest/gtest.h>
22 #include <selinux/selinux.h>
23 
24 #include "incfs.h"
25 #include "path.h"
26 
27 namespace android::incfs {
28 
exists(std::string_view path)29 static bool exists(std::string_view path) {
30     return access(path.data(), F_OK) == 0;
31 }
32 
33 class IncFsTestBase : public ::testing::Test {
34 protected:
SetUp()35     virtual void SetUp() {
36         tmp_dir_for_mount_.emplace();
37         mount_dir_path_ = tmp_dir_for_mount_->path;
38         tmp_dir_for_image_.emplace();
39         image_dir_path_ = tmp_dir_for_image_->path;
40         ASSERT_TRUE(exists(image_dir_path_));
41         ASSERT_TRUE(exists(mount_dir_path_));
42         if (!enabled()) {
43             GTEST_SKIP() << "test not supported: IncFS is not enabled";
44         } else {
45             metrics_key_ = path::baseName(image_dir_path_);
46             control_ = mount(image_dir_path_, mount_dir_path_,
47                              MountOptions{.readLogBufferPages = 4,
48                                           .defaultReadTimeoutMs = getReadTimeout(),
49                                           .sysfsName = metrics_key_.c_str()});
50             ASSERT_TRUE(control_.cmd() >= 0) << "Expected >= 0 got " << control_.cmd();
51             ASSERT_TRUE(control_.pendingReads() >= 0);
52             ASSERT_TRUE(control_.logs() >= 0);
53             checkRestoreconResult(mountPath(INCFS_PENDING_READS_FILENAME));
54             checkRestoreconResult(mountPath(INCFS_LOG_FILENAME));
55         }
56     }
57 
TearDown()58     virtual void TearDown() {
59         unmount(mount_dir_path_);
60         tmp_dir_for_image_.reset();
61         tmp_dir_for_mount_.reset();
62         EXPECT_FALSE(exists(image_dir_path_));
63         EXPECT_FALSE(exists(mount_dir_path_));
64     }
65 
checkRestoreconResult(std::string_view path)66     static void checkRestoreconResult(std::string_view path) {
67         char* ctx = nullptr;
68         ASSERT_NE(-1, getfilecon(path.data(), &ctx));
69         ASSERT_EQ("u:object_r:shell_data_file:s0", std::string(ctx));
70         freecon(ctx);
71     }
72 
fileId(uint64_t i)73     static IncFsFileId fileId(uint64_t i) {
74         IncFsFileId id = {};
75         static_assert(sizeof(id) >= sizeof(i));
76         memcpy(&id, &i, sizeof(i));
77         return id;
78     }
79 
getReadTimeout()80     virtual int32_t getReadTimeout() {
81         return std::chrono::duration_cast<std::chrono::milliseconds>(kDefaultReadTimeout).count();
82     }
83 
84     template <class... Paths>
mountPath(Paths &&...paths)85     std::string mountPath(Paths&&... paths) const {
86         return path::join(mount_dir_path_, std::forward<Paths>(paths)...);
87     }
88 
makeFileWithHash(int id)89     virtual int makeFileWithHash(int id) {
90         // calculate the required size for two leaf hash blocks
91         constexpr auto size =
92                 (INCFS_DATA_FILE_BLOCK_SIZE / INCFS_MAX_HASH_SIZE + 1) * INCFS_DATA_FILE_BLOCK_SIZE;
93 
94         // assemble a signature/hashing data for it
95         struct __attribute__((packed)) Signature {
96             uint32_t version = INCFS_SIGNATURE_VERSION;
97             uint32_t hashingSize = sizeof(hashing);
98             struct __attribute__((packed)) Hashing {
99                 uint32_t algo = INCFS_HASH_TREE_SHA256;
100                 uint8_t log2Blocksize = 12;
101                 uint32_t saltSize = 0;
102                 uint32_t rootHashSize = INCFS_MAX_HASH_SIZE;
103                 char rootHash[INCFS_MAX_HASH_SIZE] = {};
104             } hashing;
105             uint32_t signingSize = 0;
106         } signature;
107 
108         int res = makeFile(control_, mountPath(test_file_name_), 0555, fileId(id),
109                            {.size = size,
110                             .signature = {.data = (char*)&signature, .size = sizeof(signature)}});
111         EXPECT_EQ(0, res);
112         return res ? -1 : size;
113     }
114 
115     std::string mount_dir_path_;
116     std::optional<TemporaryDir> tmp_dir_for_mount_;
117     std::string image_dir_path_;
118     std::optional<TemporaryDir> tmp_dir_for_image_;
119     inline static const std::string_view test_file_name_ = "test.txt";
120     inline static const std::string_view test_mapped_file_name_ = "mapped.txt";
121     inline static const std::string_view test_dir_name_ = "test_dir";
122     std::string metrics_key_;
123     Control control_;
124 };
125 
126 } // namespace android::incfs
127