xref: /aosp_15_r20/system/apex/apexd/apexd_utils_test.cpp (revision 33f3758387333dbd2962d7edbd98681940d895da)
1*33f37583SAndroid Build Coastguard Worker /*
2*33f37583SAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*33f37583SAndroid Build Coastguard Worker  *
4*33f37583SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*33f37583SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*33f37583SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*33f37583SAndroid Build Coastguard Worker  *
8*33f37583SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*33f37583SAndroid Build Coastguard Worker  *
10*33f37583SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*33f37583SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*33f37583SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*33f37583SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*33f37583SAndroid Build Coastguard Worker  * limitations under the License.
15*33f37583SAndroid Build Coastguard Worker  */
16*33f37583SAndroid Build Coastguard Worker 
17*33f37583SAndroid Build Coastguard Worker #include "apexd_utils.h"
18*33f37583SAndroid Build Coastguard Worker 
19*33f37583SAndroid Build Coastguard Worker #include <android-base/file.h>
20*33f37583SAndroid Build Coastguard Worker #include <android-base/result-gmock.h>
21*33f37583SAndroid Build Coastguard Worker #include <android-base/result.h>
22*33f37583SAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
23*33f37583SAndroid Build Coastguard Worker #include <android-base/strings.h>
24*33f37583SAndroid Build Coastguard Worker #include <errno.h>
25*33f37583SAndroid Build Coastguard Worker #include <gtest/gtest.h>
26*33f37583SAndroid Build Coastguard Worker 
27*33f37583SAndroid Build Coastguard Worker #include <filesystem>
28*33f37583SAndroid Build Coastguard Worker #include <fstream>
29*33f37583SAndroid Build Coastguard Worker #include <new>
30*33f37583SAndroid Build Coastguard Worker #include <string>
31*33f37583SAndroid Build Coastguard Worker 
32*33f37583SAndroid Build Coastguard Worker #include "apexd.h"
33*33f37583SAndroid Build Coastguard Worker #include "apexd_test_utils.h"
34*33f37583SAndroid Build Coastguard Worker 
35*33f37583SAndroid Build Coastguard Worker namespace android {
36*33f37583SAndroid Build Coastguard Worker namespace apex {
37*33f37583SAndroid Build Coastguard Worker namespace {
38*33f37583SAndroid Build Coastguard Worker 
39*33f37583SAndroid Build Coastguard Worker namespace fs = std::filesystem;
40*33f37583SAndroid Build Coastguard Worker 
41*33f37583SAndroid Build Coastguard Worker using android::base::Basename;
42*33f37583SAndroid Build Coastguard Worker using android::base::Join;
43*33f37583SAndroid Build Coastguard Worker using android::base::StringPrintf;
44*33f37583SAndroid Build Coastguard Worker using android::base::testing::Ok;
45*33f37583SAndroid Build Coastguard Worker using ::testing::Not;
46*33f37583SAndroid Build Coastguard Worker using ::testing::UnorderedElementsAre;
47*33f37583SAndroid Build Coastguard Worker using ::testing::UnorderedElementsAreArray;
48*33f37583SAndroid Build Coastguard Worker 
49*33f37583SAndroid Build Coastguard Worker // TODO(b/170327382): add unit tests for apexd_utils.h
50*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,DeleteDirContent)51*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, DeleteDirContent) {
52*33f37583SAndroid Build Coastguard Worker   TemporaryDir root_dir;
53*33f37583SAndroid Build Coastguard Worker   TemporaryFile child_file_1(root_dir.path);
54*33f37583SAndroid Build Coastguard Worker   TemporaryFile child_file_2(root_dir.path);
55*33f37583SAndroid Build Coastguard Worker   std::string child_dir = StringPrintf("%s/child-dir", root_dir.path);
56*33f37583SAndroid Build Coastguard Worker   CreateDirIfNeeded(child_dir, 0755);
57*33f37583SAndroid Build Coastguard Worker   TemporaryFile nested_file(child_dir);
58*33f37583SAndroid Build Coastguard Worker 
59*33f37583SAndroid Build Coastguard Worker   auto content = ReadDir(root_dir.path, [](auto _) { return true; });
60*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(content);
61*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(content->size(), 3u);
62*33f37583SAndroid Build Coastguard Worker 
63*33f37583SAndroid Build Coastguard Worker   auto del_result = DeleteDirContent(root_dir.path);
64*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(del_result);
65*33f37583SAndroid Build Coastguard Worker   content = ReadDir(root_dir.path, [](auto _) { return true; });
66*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(content);
67*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(content->size(), 0u);
68*33f37583SAndroid Build Coastguard Worker }
69*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryBothExist)70*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryBothExist) {
71*33f37583SAndroid Build Coastguard Worker   TemporaryDir first_dir;
72*33f37583SAndroid Build Coastguard Worker   TemporaryDir second_dir;
73*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_dir.path, second_dir.path);
74*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(result);
75*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(*result, first_dir.path);
76*33f37583SAndroid Build Coastguard Worker }
77*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryOnlyFirstExist)78*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryOnlyFirstExist) {
79*33f37583SAndroid Build Coastguard Worker   TemporaryDir first_dir;
80*33f37583SAndroid Build Coastguard Worker   auto second_dir = "/data/local/tmp/does/not/exist";
81*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_dir.path, second_dir);
82*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(result);
83*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(*result, first_dir.path);
84*33f37583SAndroid Build Coastguard Worker }
85*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryOnlySecondExist)86*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryOnlySecondExist) {
87*33f37583SAndroid Build Coastguard Worker   auto first_dir = "/data/local/tmp/does/not/exist";
88*33f37583SAndroid Build Coastguard Worker   TemporaryDir second_dir;
89*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_dir, second_dir.path);
90*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(result);
91*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(*result, second_dir.path);
92*33f37583SAndroid Build Coastguard Worker }
93*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryNoneExist)94*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryNoneExist) {
95*33f37583SAndroid Build Coastguard Worker   auto first_dir = "/data/local/tmp/does/not/exist";
96*33f37583SAndroid Build Coastguard Worker   auto second_dir = "/data/local/tmp/also/does/not/exist";
97*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_dir, second_dir);
98*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(result, Not(Ok()));
99*33f37583SAndroid Build Coastguard Worker }
100*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryFirstFileSecondDir)101*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryFirstFileSecondDir) {
102*33f37583SAndroid Build Coastguard Worker   TemporaryFile first_file;
103*33f37583SAndroid Build Coastguard Worker   TemporaryDir second_dir;
104*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_file.path, second_dir.path);
105*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(result);
106*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(*result, second_dir.path);
107*33f37583SAndroid Build Coastguard Worker }
108*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryFirstDirSecondFile)109*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryFirstDirSecondFile) {
110*33f37583SAndroid Build Coastguard Worker   TemporaryDir first_dir;
111*33f37583SAndroid Build Coastguard Worker   TemporaryFile second_file;
112*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_dir.path, second_file.path);
113*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(result);
114*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(*result, first_dir.path);
115*33f37583SAndroid Build Coastguard Worker }
116*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryBothFiles)117*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryBothFiles) {
118*33f37583SAndroid Build Coastguard Worker   TemporaryFile first_file;
119*33f37583SAndroid Build Coastguard Worker   TemporaryFile second_file;
120*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_file.path, second_file.path);
121*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(result, Not(Ok()));
122*33f37583SAndroid Build Coastguard Worker }
123*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryFirstFileSecondDoesNotExist)124*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryFirstFileSecondDoesNotExist) {
125*33f37583SAndroid Build Coastguard Worker   TemporaryFile first_file;
126*33f37583SAndroid Build Coastguard Worker   auto second_dir = "/data/local/tmp/does/not/exist";
127*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_file.path, second_dir);
128*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(result, Not(Ok()));
129*33f37583SAndroid Build Coastguard Worker }
130*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFirstExistingDirectoryFirsDoesNotExistSecondFile)131*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFirstExistingDirectoryFirsDoesNotExistSecondFile) {
132*33f37583SAndroid Build Coastguard Worker   auto first_dir = "/data/local/tmp/does/not/exist";
133*33f37583SAndroid Build Coastguard Worker   TemporaryFile second_file;
134*33f37583SAndroid Build Coastguard Worker   auto result = FindFirstExistingDirectory(first_dir, second_file.path);
135*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(result, Not(Ok()));
136*33f37583SAndroid Build Coastguard Worker }
137*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,MoveDir)138*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, MoveDir) {
139*33f37583SAndroid Build Coastguard Worker   TemporaryDir from;
140*33f37583SAndroid Build Coastguard Worker   TemporaryDir to;
141*33f37583SAndroid Build Coastguard Worker 
142*33f37583SAndroid Build Coastguard Worker   TemporaryFile from_1(from.path);
143*33f37583SAndroid Build Coastguard Worker   auto from_subdir = StringPrintf("%s/subdir", from.path);
144*33f37583SAndroid Build Coastguard Worker   if (mkdir(from_subdir.c_str(), 07000) != 0) {
145*33f37583SAndroid Build Coastguard Worker     FAIL() << "Failed to mkdir " << from_subdir << " : " << strerror(errno);
146*33f37583SAndroid Build Coastguard Worker   }
147*33f37583SAndroid Build Coastguard Worker   TemporaryFile from_2(from_subdir);
148*33f37583SAndroid Build Coastguard Worker 
149*33f37583SAndroid Build Coastguard Worker   auto result = MoveDir(from.path, to.path);
150*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(result);
151*33f37583SAndroid Build Coastguard Worker   ASSERT_TRUE(fs::is_empty(from.path));
152*33f37583SAndroid Build Coastguard Worker 
153*33f37583SAndroid Build Coastguard Worker   std::vector<std::string> content;
154*33f37583SAndroid Build Coastguard Worker   for (const auto& it : fs::recursive_directory_iterator(to.path)) {
155*33f37583SAndroid Build Coastguard Worker     content.push_back(it.path());
156*33f37583SAndroid Build Coastguard Worker   }
157*33f37583SAndroid Build Coastguard Worker 
158*33f37583SAndroid Build Coastguard Worker   static const std::vector<std::string> expected = {
159*33f37583SAndroid Build Coastguard Worker       StringPrintf("%s/%s", to.path, Basename(from_1.path).c_str()),
160*33f37583SAndroid Build Coastguard Worker       StringPrintf("%s/subdir", to.path),
161*33f37583SAndroid Build Coastguard Worker       StringPrintf("%s/subdir/%s", to.path, Basename(from_2.path).c_str()),
162*33f37583SAndroid Build Coastguard Worker   };
163*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(content, UnorderedElementsAreArray(expected));
164*33f37583SAndroid Build Coastguard Worker }
165*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,MoveDirFromIsNotDirectory)166*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, MoveDirFromIsNotDirectory) {
167*33f37583SAndroid Build Coastguard Worker   TemporaryFile from;
168*33f37583SAndroid Build Coastguard Worker   TemporaryDir to;
169*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(MoveDir(from.path, to.path), Not(Ok()));
170*33f37583SAndroid Build Coastguard Worker }
171*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,MoveDirToIsNotDirectory)172*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, MoveDirToIsNotDirectory) {
173*33f37583SAndroid Build Coastguard Worker   TemporaryDir from;
174*33f37583SAndroid Build Coastguard Worker   TemporaryFile to;
175*33f37583SAndroid Build Coastguard Worker   TemporaryFile from_1(from.path);
176*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(MoveDir(from.path, to.path), Not(Ok()));
177*33f37583SAndroid Build Coastguard Worker }
178*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,MoveDirFromDoesNotExist)179*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, MoveDirFromDoesNotExist) {
180*33f37583SAndroid Build Coastguard Worker   TemporaryDir to;
181*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(MoveDir("/data/local/tmp/does/not/exist", to.path), Not(Ok()));
182*33f37583SAndroid Build Coastguard Worker }
183*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,MoveDirToDoesNotExist)184*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, MoveDirToDoesNotExist) {
185*33f37583SAndroid Build Coastguard Worker   namespace fs = std::filesystem;
186*33f37583SAndroid Build Coastguard Worker 
187*33f37583SAndroid Build Coastguard Worker   TemporaryDir from;
188*33f37583SAndroid Build Coastguard Worker   TemporaryFile from_1(from.path);
189*33f37583SAndroid Build Coastguard Worker   auto from_subdir = StringPrintf("%s/subdir", from.path);
190*33f37583SAndroid Build Coastguard Worker   if (mkdir(from_subdir.c_str(), 07000) != 0) {
191*33f37583SAndroid Build Coastguard Worker     FAIL() << "Failed to mkdir " << from_subdir << " : " << strerror(errno);
192*33f37583SAndroid Build Coastguard Worker   }
193*33f37583SAndroid Build Coastguard Worker   TemporaryFile from_2(from_subdir);
194*33f37583SAndroid Build Coastguard Worker 
195*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(MoveDir(from.path, "/data/local/tmp/does/not/exist"), Not(Ok()));
196*33f37583SAndroid Build Coastguard Worker 
197*33f37583SAndroid Build Coastguard Worker   // Check that |from| directory is not empty.
198*33f37583SAndroid Build Coastguard Worker   std::vector<std::string> content;
199*33f37583SAndroid Build Coastguard Worker   for (const auto& it : fs::recursive_directory_iterator(from.path)) {
200*33f37583SAndroid Build Coastguard Worker     content.push_back(it.path());
201*33f37583SAndroid Build Coastguard Worker   }
202*33f37583SAndroid Build Coastguard Worker 
203*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(content,
204*33f37583SAndroid Build Coastguard Worker               UnorderedElementsAre(from_1.path, from_subdir, from_2.path));
205*33f37583SAndroid Build Coastguard Worker }
206*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdUtilTest,FindFilesBySuffix)207*33f37583SAndroid Build Coastguard Worker TEST(ApexdUtilTest, FindFilesBySuffix) {
208*33f37583SAndroid Build Coastguard Worker   TemporaryDir td;
209*33f37583SAndroid Build Coastguard Worker 
210*33f37583SAndroid Build Coastguard Worker   // create files with different suffix
211*33f37583SAndroid Build Coastguard Worker   const std::string first_filename = StringPrintf("%s/first.a", td.path);
212*33f37583SAndroid Build Coastguard Worker   const std::string second_filename = StringPrintf("%s/second.b", td.path);
213*33f37583SAndroid Build Coastguard Worker   const std::string third_filename = StringPrintf("%s/third.c", td.path);
214*33f37583SAndroid Build Coastguard Worker   const std::string fourth_filename = StringPrintf("%s/fourth.c", td.path);
215*33f37583SAndroid Build Coastguard Worker 
216*33f37583SAndroid Build Coastguard Worker   std::ofstream first_file(first_filename);
217*33f37583SAndroid Build Coastguard Worker   std::ofstream second_file(second_filename);
218*33f37583SAndroid Build Coastguard Worker   std::ofstream third_file(third_filename);
219*33f37583SAndroid Build Coastguard Worker   std::ofstream fourth_file(fourth_filename);
220*33f37583SAndroid Build Coastguard Worker 
221*33f37583SAndroid Build Coastguard Worker   auto result = FindFilesBySuffix(td.path, {".b", ".c"});
222*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(result);
223*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(*result, UnorderedElementsAre(second_filename, third_filename,
224*33f37583SAndroid Build Coastguard Worker                                             fourth_filename));
225*33f37583SAndroid Build Coastguard Worker }
226*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdTestUtilsTest,MountNamespaceRestorer)227*33f37583SAndroid Build Coastguard Worker TEST(ApexdTestUtilsTest, MountNamespaceRestorer) {
228*33f37583SAndroid Build Coastguard Worker   auto original_namespace = GetCurrentMountNamespace();
229*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(original_namespace);
230*33f37583SAndroid Build Coastguard Worker   {
231*33f37583SAndroid Build Coastguard Worker     MountNamespaceRestorer restorer;
232*33f37583SAndroid Build Coastguard Worker     // Switch to new mount namespace.
233*33f37583SAndroid Build Coastguard Worker     ASSERT_NE(-1, unshare(CLONE_NEWNS));
234*33f37583SAndroid Build Coastguard Worker     auto current_namespace = GetCurrentMountNamespace();
235*33f37583SAndroid Build Coastguard Worker     ASSERT_RESULT_OK(current_namespace);
236*33f37583SAndroid Build Coastguard Worker     ASSERT_NE(original_namespace, current_namespace);
237*33f37583SAndroid Build Coastguard Worker   }
238*33f37583SAndroid Build Coastguard Worker   // Check that we switched back to the original namespace upon exiting the
239*33f37583SAndroid Build Coastguard Worker   // scope.
240*33f37583SAndroid Build Coastguard Worker   auto current_namespace = GetCurrentMountNamespace();
241*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(current_namespace);
242*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(*original_namespace, *current_namespace);
243*33f37583SAndroid Build Coastguard Worker }
244*33f37583SAndroid Build Coastguard Worker 
TEST(ApexdTestUtilsTest,SetUpApexTestEnvironment)245*33f37583SAndroid Build Coastguard Worker TEST(ApexdTestUtilsTest, SetUpApexTestEnvironment) {
246*33f37583SAndroid Build Coastguard Worker   auto original_apex_mounts = GetApexMounts();
247*33f37583SAndroid Build Coastguard Worker   ASSERT_GT(original_apex_mounts.size(), 0u);
248*33f37583SAndroid Build Coastguard Worker   auto original_dir_content = ReadDir("/apex", [](auto _) { return true; });
249*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(original_dir_content);
250*33f37583SAndroid Build Coastguard Worker   {
251*33f37583SAndroid Build Coastguard Worker     MountNamespaceRestorer restorer;
252*33f37583SAndroid Build Coastguard Worker     ASSERT_RESULT_OK(SetUpApexTestEnvironment());
253*33f37583SAndroid Build Coastguard Worker     // Check /apex is apex_mnt_dir.
254*33f37583SAndroid Build Coastguard Worker     char* context;
255*33f37583SAndroid Build Coastguard Worker     ASSERT_GT(getfilecon("/apex", &context), 0);
256*33f37583SAndroid Build Coastguard Worker     EXPECT_EQ(std::string(context), "u:object_r:apex_mnt_dir:s0");
257*33f37583SAndroid Build Coastguard Worker     freecon(context);
258*33f37583SAndroid Build Coastguard Worker     // Check no apexes are mounted in our test environment.
259*33f37583SAndroid Build Coastguard Worker     auto new_apex_mounts = GetApexMounts();
260*33f37583SAndroid Build Coastguard Worker     ASSERT_EQ(new_apex_mounts.size(), 0u);
261*33f37583SAndroid Build Coastguard Worker     // Check that /apex is empty.
262*33f37583SAndroid Build Coastguard Worker     auto dir_content = ReadDir("/apex", [](auto _) { return true; });
263*33f37583SAndroid Build Coastguard Worker     ASSERT_RESULT_OK(dir_content);
264*33f37583SAndroid Build Coastguard Worker     ASSERT_EQ(dir_content->size(), 0u)
265*33f37583SAndroid Build Coastguard Worker         << "Found following entries: " << Join(*dir_content, ',');
266*33f37583SAndroid Build Coastguard Worker     // Check that we can still access /data.
267*33f37583SAndroid Build Coastguard Worker     std::string test_dir = android::base::GetExecutableDirectory();
268*33f37583SAndroid Build Coastguard Worker     ASSERT_TRUE(android::base::StartsWith(test_dir, "/data"));
269*33f37583SAndroid Build Coastguard Worker     TemporaryFile tf(test_dir);
270*33f37583SAndroid Build Coastguard Worker     // Check that we can write.
271*33f37583SAndroid Build Coastguard Worker     ASSERT_TRUE(android::base::WriteStringToFile("secret", tf.path));
272*33f37583SAndroid Build Coastguard Worker     // And check that we can still read it
273*33f37583SAndroid Build Coastguard Worker     std::string content;
274*33f37583SAndroid Build Coastguard Worker     ASSERT_TRUE(android::base::ReadFileToString(tf.path, &content));
275*33f37583SAndroid Build Coastguard Worker     ASSERT_EQ(content, "secret");
276*33f37583SAndroid Build Coastguard Worker   }
277*33f37583SAndroid Build Coastguard Worker   auto apex_mounts = GetApexMounts();
278*33f37583SAndroid Build Coastguard Worker   ASSERT_THAT(apex_mounts, UnorderedElementsAreArray(original_apex_mounts));
279*33f37583SAndroid Build Coastguard Worker   auto apex_dir_content = ReadDir("/apex", [](auto _) { return true; });
280*33f37583SAndroid Build Coastguard Worker   ASSERT_RESULT_OK(apex_dir_content);
281*33f37583SAndroid Build Coastguard Worker   ASSERT_EQ(apex_dir_content->size(), original_dir_content->size());
282*33f37583SAndroid Build Coastguard Worker }
283*33f37583SAndroid Build Coastguard Worker 
284*33f37583SAndroid Build Coastguard Worker }  // namespace
285*33f37583SAndroid Build Coastguard Worker }  // namespace apex
286*33f37583SAndroid Build Coastguard Worker }  // namespace android
287