1 /*
2 * Copyright (C) 2019 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 #include <android-base/file.h>
18 #include <android-base/logging.h>
19 #include <android-base/unique_fd.h>
20 #include <gtest/gtest.h>
21 #include <sys/select.h>
22 #include <unistd.h>
23
24 #include <optional>
25 #include <thread>
26
27 #include "MountRegistry.h"
28 #include "path.h"
29
30 using namespace android::incfs;
31 using namespace std::literals;
32
33 class MountRegistryTest : public ::testing::Test {
34 protected:
SetUp()35 virtual void SetUp() {}
TearDown()36 virtual void TearDown() {}
37
38 MountRegistry::Mounts mounts_;
39
r()40 MountRegistry::Mounts& r() { return mounts_; }
41 };
42
TEST_F(MountRegistryTest,RootForRoot)43 TEST_F(MountRegistryTest, RootForRoot) {
44 r().addRoot("/root", "/backing");
45 ASSERT_STREQ("/root", r().rootFor("/root").data());
46 ASSERT_STREQ("/root", r().rootFor("/root/1").data());
47 ASSERT_STREQ("/root", r().rootFor("/root/1/2").data());
48 ASSERT_STREQ(nullptr, r().rootFor("/root1/1/2").data());
49 ASSERT_STREQ(nullptr, r().rootFor("/1/root").data());
50 ASSERT_STREQ(nullptr, r().rootFor("root").data());
51 }
52
TEST_F(MountRegistryTest,OneBind)53 TEST_F(MountRegistryTest, OneBind) {
54 r().addRoot("/root", "/backing");
55 r().addBind("/root/1", "/bind");
56 ASSERT_STREQ("/root", r().rootFor("/root").data());
57 ASSERT_STREQ("/root", r().rootFor("/bind").data());
58 ASSERT_STREQ("/root", r().rootFor("/bind/1").data());
59 ASSERT_STREQ("/root", r().rootFor("/root/1").data());
60 ASSERT_STREQ(nullptr, r().rootFor("/1/bind").data());
61 ASSERT_STREQ(nullptr, r().rootFor("bind").data());
62 ASSERT_STREQ(nullptr, r().rootFor("/bind1").data());
63 ASSERT_STREQ(nullptr, r().rootFor("/.bind").data());
64 }
65
TEST_F(MountRegistryTest,MultiBind)66 TEST_F(MountRegistryTest, MultiBind) {
67 r().addRoot("/root", "/backing");
68 r().addBind("/root/1", "/bind");
69 r().addBind("/root/2/3", "/bind2");
70 r().addBind("/root/2/3", "/other/bind");
71 ASSERT_STREQ("/root", r().rootFor("/root").data());
72 ASSERT_STREQ("/root", r().rootFor("/bind").data());
73 ASSERT_STREQ("/root", r().rootFor("/bind2").data());
74 ASSERT_STREQ("/root", r().rootFor("/other/bind/dir").data());
75 ASSERT_EQ(std::pair("/root"sv, ""s), r().rootAndSubpathFor("/root"));
76 ASSERT_EQ(std::pair("/root"sv, "1"s), r().rootAndSubpathFor("/bind"));
77 ASSERT_EQ(std::pair("/root"sv, "2/3"s), r().rootAndSubpathFor("/bind2"));
78 ASSERT_EQ(std::pair("/root"sv, "2/3/blah"s), r().rootAndSubpathFor("/bind2/blah"));
79 ASSERT_EQ(std::pair("/root"sv, "2/3/blah"s), r().rootAndSubpathFor("/other/bind/blah"));
80 }
81