summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'base/file_test.cpp')
-rw-r--r--base/file_test.cpp42
1 files changed, 40 insertions, 2 deletions
diff --git a/base/file_test.cpp b/base/file_test.cpp
index 02b431de7..67946523c 100644
--- a/base/file_test.cpp
+++ b/base/file_test.cpp
@@ -26,6 +26,10 @@
26 26
27#include "android-base/test_utils.h" 27#include "android-base/test_utils.h"
28 28
29#if !defined(_WIN32)
30#include <pwd.h>
31#endif
32
29TEST(file, ReadFileToString_ENOENT) { 33TEST(file, ReadFileToString_ENOENT) {
30 std::string s("hello"); 34 std::string s("hello");
31 errno = 0; 35 errno = 0;
@@ -115,7 +119,7 @@ TEST(file, WriteFully) {
115 ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size())); 119 ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
116} 120}
117 121
118TEST(file, RemoveFileIfExist) { 122TEST(file, RemoveFileIfExists) {
119 TemporaryFile tf; 123 TemporaryFile tf;
120 ASSERT_TRUE(tf.fd != -1); 124 ASSERT_TRUE(tf.fd != -1);
121 close(tf.fd); 125 close(tf.fd);
@@ -126,9 +130,43 @@ TEST(file, RemoveFileIfExist) {
126 TemporaryDir td; 130 TemporaryDir td;
127 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path)); 131 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
128 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err)); 132 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
129 ASSERT_EQ("is not a regular or symbol link file", err); 133 ASSERT_EQ("is not a regular file or symbolic link", err);
130} 134}
131 135
136TEST(file, RemoveFileIfExists_ENOTDIR) {
137 TemporaryFile tf;
138 close(tf.fd);
139 tf.fd = -1;
140 std::string err{"xxx"};
141 ASSERT_TRUE(android::base::RemoveFileIfExists(std::string{tf.path} + "/abc", &err));
142 ASSERT_EQ("xxx", err);
143}
144
145#if !defined(_WIN32)
146TEST(file, RemoveFileIfExists_EACCES) {
147 // EACCES -- one of the directories in the path has no search permission
148 // root can bypass permission restrictions, so drop root.
149 if (getuid() == 0) {
150 passwd* shell = getpwnam("shell");
151 setgid(shell->pw_gid);
152 setuid(shell->pw_uid);
153 }
154
155 TemporaryDir td;
156 TemporaryFile tf(td.path);
157 close(tf.fd);
158 tf.fd = -1;
159 std::string err{"xxx"};
160 // Remove dir's search permission.
161 ASSERT_TRUE(chmod(td.path, S_IRUSR | S_IWUSR) == 0);
162 ASSERT_FALSE(android::base::RemoveFileIfExists(tf.path, &err));
163 ASSERT_EQ("Permission denied", err);
164 // Set dir's search permission again.
165 ASSERT_TRUE(chmod(td.path, S_IRWXU) == 0);
166 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err));
167}
168#endif
169
132TEST(file, Readlink) { 170TEST(file, Readlink) {
133#if !defined(_WIN32) 171#if !defined(_WIN32)
134 // Linux doesn't allow empty symbolic links. 172 // Linux doesn't allow empty symbolic links.