blob: f5d60625a7f4a51aee1026b1503be96ab829fffe [file] [log] [blame]
Dan Albertaac6b7c2015-03-16 10:08:46 -07001/*
2 * Copyright (C) 2013 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
Elliott Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/file.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070018
19#include <gtest/gtest.h>
20
21#include <errno.h>
22#include <fcntl.h>
23#include <unistd.h>
24
25#include <string>
26
Elliott Hughesb6351622015-12-04 22:00:26 -080027#include "android-base/test_utils.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070028
29TEST(file, ReadFileToString_ENOENT) {
30 std::string s("hello");
31 errno = 0;
32 ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
33 EXPECT_EQ(ENOENT, errno);
34 EXPECT_EQ("", s); // s was cleared.
35}
36
Spencer Low482fa6d2015-05-24 15:36:28 -070037TEST(file, ReadFileToString_WriteStringToFile) {
Dan Albertaac6b7c2015-03-16 10:08:46 -070038 TemporaryFile tf;
39 ASSERT_TRUE(tf.fd != -1);
Alex Valléed0ac74c2015-05-06 16:26:00 -040040 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
Dan Albert1a932622015-04-29 17:09:53 -070041 << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070042 std::string s;
Alex Valléed0ac74c2015-05-06 16:26:00 -040043 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert1a932622015-04-29 17:09:53 -070044 << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070045 EXPECT_EQ("abc", s);
46}
47
Dan Albert26cb8ff2015-04-29 11:32:23 -070048// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
49// sense on Windows.
50#if !defined(_WIN32)
Dan Albertaac6b7c2015-03-16 10:08:46 -070051TEST(file, WriteStringToFile2) {
52 TemporaryFile tf;
53 ASSERT_TRUE(tf.fd != -1);
Alex Valléed0ac74c2015-05-06 16:26:00 -040054 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
Dan Albertaac6b7c2015-03-16 10:08:46 -070055 getuid(), getgid()))
Dan Albert1a932622015-04-29 17:09:53 -070056 << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070057 struct stat sb;
Alex Valléed0ac74c2015-05-06 16:26:00 -040058 ASSERT_EQ(0, stat(tf.path, &sb));
Colin Crosseebcd142015-04-30 15:12:21 -070059 ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
Dan Albertaac6b7c2015-03-16 10:08:46 -070060 ASSERT_EQ(getuid(), sb.st_uid);
61 ASSERT_EQ(getgid(), sb.st_gid);
62 std::string s;
Alex Valléed0ac74c2015-05-06 16:26:00 -040063 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert1a932622015-04-29 17:09:53 -070064 << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070065 EXPECT_EQ("abc", s);
66}
Dan Albert26cb8ff2015-04-29 11:32:23 -070067#endif
Dan Albertaac6b7c2015-03-16 10:08:46 -070068
69TEST(file, WriteStringToFd) {
70 TemporaryFile tf;
71 ASSERT_TRUE(tf.fd != -1);
72 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
73
Dan Albert1a932622015-04-29 17:09:53 -070074 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070075
76 std::string s;
Dan Albert1a932622015-04-29 17:09:53 -070077 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070078 EXPECT_EQ("abc", s);
79}
Elliott Hughes20abc872015-04-24 21:57:16 -070080
Elliott Hughes20abc872015-04-24 21:57:16 -070081TEST(file, WriteFully) {
82 TemporaryFile tf;
83 ASSERT_TRUE(tf.fd != -1);
84 ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
Spencer Low38d002e2015-08-03 20:43:24 -070085
86 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
87
Elliott Hughes20abc872015-04-24 21:57:16 -070088 std::string s;
Spencer Low38d002e2015-08-03 20:43:24 -070089 s.resize(3);
90 ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size()))
Dan Albert1a932622015-04-29 17:09:53 -070091 << strerror(errno);
Elliott Hughes20abc872015-04-24 21:57:16 -070092 EXPECT_EQ("abc", s);
Spencer Low38d002e2015-08-03 20:43:24 -070093
94 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
95
96 s.resize(1024);
97 ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
Elliott Hughes20abc872015-04-24 21:57:16 -070098}
Yabin Cui8f6a5a02016-01-29 17:25:54 -080099
100TEST(file, RemoveFileIfExist) {
101 TemporaryFile tf;
102 ASSERT_TRUE(tf.fd != -1);
103 close(tf.fd);
104 tf.fd = -1;
105 std::string err;
106 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err;
107 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path));
108 TemporaryDir td;
109 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
110 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
111 ASSERT_EQ("is not a regular or symbol link file", err);
112}
Elliott Hughesa634a9a2016-08-23 15:53:45 -0700113
114TEST(file, Readlink) {
115#if !defined(_WIN32)
116 // Linux doesn't allow empty symbolic links.
117 std::string min("x");
118 // ext2 and ext4 both have PAGE_SIZE limits.
119 std::string max(static_cast<size_t>(4096 - 1), 'x');
120
121 TemporaryDir td;
122 std::string min_path{std::string(td.path) + "/" + "min"};
123 std::string max_path{std::string(td.path) + "/" + "max"};
124
125 ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str()));
126 ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str()));
127
128 std::string result;
129
130 result = "wrong";
131 ASSERT_TRUE(android::base::Readlink(min_path, &result));
132 ASSERT_EQ(min, result);
133
134 result = "wrong";
135 ASSERT_TRUE(android::base::Readlink(max_path, &result));
136 ASSERT_EQ(max, result);
137#endif
138}
Elliott Hughes48f0eb52016-08-31 15:07:18 -0700139
140TEST(file, GetExecutablePath) {
141 ASSERT_NE("", android::base::GetExecutablePath());
142}