• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <fcntl.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 
21 #include <iosfwd>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "android-base/stringprintf.h"
27 #include "base/bit_utils.h"
28 #include "base/common_art_test.h"
29 #include "base/os.h"
30 #include "base/unix_file/fd_file.h"
31 #include "odr_fs_utils.h"
32 
33 namespace art {
34 namespace odrefresh {
35 
36 class OdrFsUtilsTest : public CommonArtTest {};
37 namespace {
38 
CreateFile(const char * file_path,size_t bytes)39 static bool CreateFile(const char* file_path, size_t bytes) {
40   std::unique_ptr<File> fp(OS::CreateEmptyFile(file_path));
41   if (!fp) {
42     return false;
43   }
44 
45   std::vector<char> buffer(bytes, 0xa5);
46   if (!fp->WriteFully(buffer.data(), buffer.size())) {
47     fp->Erase();
48     return false;
49   }
50 
51   if (fp->FlushClose() != 0) {
52     fp->Erase();
53     return false;
54   }
55 
56   return true;
57 }
58 
59 }  // namespace
60 
TEST_F(OdrFsUtilsTest,RemoveDirectory)61 TEST_F(OdrFsUtilsTest, RemoveDirectory) {
62   ScratchDir scratch_dir(/*keep_files=*/false);
63 
64   // Create some sub-directories and files
65   const std::string dir_paths[] = {
66       scratch_dir.GetPath() + "/a",
67       scratch_dir.GetPath() + "/b",
68       scratch_dir.GetPath() + "/b/c",
69       scratch_dir.GetPath() + "/d"
70   };
71   for (const auto& dir_path : dir_paths) {
72     ASSERT_EQ(0, mkdir(dir_path.c_str(), S_IRWXU));
73   }
74 
75   const std::string file_paths[] = {
76       scratch_dir.GetPath() + "/zero.txt",
77       scratch_dir.GetPath() + "/a/one.txt",
78       scratch_dir.GetPath() + "/b/two.txt",
79       scratch_dir.GetPath() + "/b/c/three.txt",
80       scratch_dir.GetPath() + "/b/c/four.txt",
81   };
82   for (const auto& file_path : file_paths) {
83     ASSERT_TRUE(CreateFile(file_path.c_str(), 4096));
84   }
85 
86   // Remove directory, all files and sub-directories
87   ASSERT_TRUE(RemoveDirectory(scratch_dir.GetPath()));
88 
89   // Check nothing we created remains.
90   for (const auto& dir_path : dir_paths) {
91     ASSERT_FALSE(OS::DirectoryExists(dir_path.c_str()));
92   }
93 
94   for (const auto& file_path : file_paths) {
95     ASSERT_FALSE(OS::FileExists(file_path.c_str(), true));
96   }
97 
98   // And the original directory is also gone
99   ASSERT_FALSE(OS::DirectoryExists(scratch_dir.GetPath().c_str()));
100 }
101 
TEST_F(OdrFsUtilsTest,EnsureDirectoryExistsBadPath)102 TEST_F(OdrFsUtilsTest, EnsureDirectoryExistsBadPath) {
103   // Pick a path where not even a root test runner can write.
104   ASSERT_FALSE(EnsureDirectoryExists("/proc/unlikely/to/be/writable"));
105 }
106 
TEST_F(OdrFsUtilsTest,EnsureDirectoryExistsEmptyPath)107 TEST_F(OdrFsUtilsTest, EnsureDirectoryExistsEmptyPath) {
108   ASSERT_FALSE(EnsureDirectoryExists(""));
109 }
110 
TEST_F(OdrFsUtilsTest,EnsureDirectoryExistsRelativePath)111 TEST_F(OdrFsUtilsTest, EnsureDirectoryExistsRelativePath) {
112   ASSERT_FALSE(EnsureDirectoryExists("a/b/c"));
113 }
114 
TEST_F(OdrFsUtilsTest,EnsureDirectoryExistsSubDirs)115 TEST_F(OdrFsUtilsTest, EnsureDirectoryExistsSubDirs) {
116   ScratchDir scratch_dir(/*keep_files=*/false);
117 
118   const char* relative_sub_dirs[] = {"a", "b/c", "d/e/f/"};
119   for (const char* relative_sub_dir : relative_sub_dirs) {
120     ASSERT_TRUE(EnsureDirectoryExists(scratch_dir.GetPath() + "/" + relative_sub_dir));
121   }
122 }
123 
TEST_F(OdrFsUtilsTest,DISABLED_GetUsedSpace)124 TEST_F(OdrFsUtilsTest, DISABLED_GetUsedSpace) {
125   static constexpr size_t kFirstFileBytes = 1;
126   static constexpr size_t kSecondFileBytes = 16111;
127   static constexpr size_t kBytesPerBlock = 512;
128 
129   ScratchDir scratch_dir(/*keep_files=*/false);
130 
131   const std::string first_file_path = scratch_dir.GetPath() + "/1.dat";
132   ASSERT_TRUE(CreateFile(first_file_path.c_str(), kFirstFileBytes));
133 
134   struct stat sb;
135   ASSERT_EQ(0, stat(first_file_path.c_str(), &sb));
136   ASSERT_EQ(kFirstFileBytes, static_cast<decltype(kFirstFileBytes)>(sb.st_size));
137 
138   uint64_t bytes_used = 0;
139   ASSERT_TRUE(GetUsedSpace(scratch_dir.GetPath(), &bytes_used));
140 
141   const std::string second_file_path = scratch_dir.GetPath() + "/2.dat";
142   ASSERT_TRUE(CreateFile(second_file_path.c_str(), kSecondFileBytes));
143 
144   ASSERT_TRUE(GetUsedSpace(scratch_dir.GetPath(), &bytes_used));
145   uint64_t expected_bytes_used = RoundUp(kFirstFileBytes, sb.st_blocks * kBytesPerBlock) +
146                                  RoundUp(kSecondFileBytes, sb.st_blocks * kBytesPerBlock);
147   ASSERT_EQ(expected_bytes_used, bytes_used);
148 
149   const std::string sub_dir_path = scratch_dir.GetPath() + "/sub";
150   ASSERT_TRUE(EnsureDirectoryExists(sub_dir_path));
151   for (size_t i = 1; i < 32768; i *= 17) {
152     const std::string path = android::base::StringPrintf("%s/%zu", sub_dir_path.c_str(), i);
153     ASSERT_TRUE(CreateFile(path.c_str(), i));
154     expected_bytes_used += RoundUp(i, sb.st_blocks * kBytesPerBlock);
155     ASSERT_TRUE(GetUsedSpace(scratch_dir.GetPath(), &bytes_used));
156     ASSERT_EQ(expected_bytes_used, bytes_used);
157   }
158 }
159 
TEST_F(OdrFsUtilsTest,GetUsedSpaceBadPath)160 TEST_F(OdrFsUtilsTest, GetUsedSpaceBadPath) {
161   ScratchDir scratch_dir(/*keep_files=*/false);
162   const std::string bad_path = scratch_dir.GetPath() + "/bad_path";
163   uint64_t bytes_used = ~0ull;
164   ASSERT_TRUE(GetUsedSpace(bad_path, &bytes_used));
165   ASSERT_EQ(0ull, bytes_used);
166 }
167 
168 }  // namespace odrefresh
169 }  // namespace art
170