blob: f741d89a06f06bf5325c13a686899539d29f0b71 [file] [log] [blame]
Elliott Hughesdec12b22015-02-02 17:31:27 -08001/*
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 Hughes4f713192015-12-04 22:00:26 -080017#include "android-base/file.h"
Dan Albertc007bc32015-03-16 10:08:46 -070018
19#include <gtest/gtest.h>
Elliott Hughesdec12b22015-02-02 17:31:27 -080020
21#include <errno.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080022#include <fcntl.h>
23#include <unistd.h>
Dan Albertc007bc32015-03-16 10:08:46 -070024
25#include <string>
Elliott Hughesdec12b22015-02-02 17:31:27 -080026
Elliott Hughes4f713192015-12-04 22:00:26 -080027#include "android-base/test_utils.h"
Elliott Hughesf682b472015-02-06 12:19:48 -080028
Elliott Hughesdec12b22015-02-02 17:31:27 -080029TEST(file, ReadFileToString_ENOENT) {
30 std::string s("hello");
31 errno = 0;
Dan Albertc007bc32015-03-16 10:08:46 -070032 ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
Elliott Hughesdec12b22015-02-02 17:31:27 -080033 EXPECT_EQ(ENOENT, errno);
Dan Albertc007bc32015-03-16 10:08:46 -070034 EXPECT_EQ("", s); // s was cleared.
Elliott Hughesdec12b22015-02-02 17:31:27 -080035}
36
Spencer Lowcf168a82015-05-24 15:36:28 -070037TEST(file, ReadFileToString_WriteStringToFile) {
Elliott Hughesf682b472015-02-06 12:19:48 -080038 TemporaryFile tf;
39 ASSERT_TRUE(tf.fd != -1);
Alex Vallée47d67c92015-05-06 16:26:00 -040040 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
Dan Albert850188f2015-04-29 17:09:53 -070041 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080042 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -040043 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -070044 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080045 EXPECT_EQ("abc", s);
46}
47
Josh Gaoffabc962016-09-14 16:11:45 -070048// symlinks require elevated privileges on Windows.
49#if !defined(_WIN32)
50TEST(file, ReadFileToString_WriteStringToFile_symlink) {
51 TemporaryFile target, link;
52 ASSERT_EQ(0, unlink(link.path));
53 ASSERT_EQ(0, symlink(target.path, link.path));
54 ASSERT_FALSE(android::base::WriteStringToFile("foo", link.path, false));
55 ASSERT_EQ(ELOOP, errno);
56 ASSERT_TRUE(android::base::WriteStringToFile("foo", link.path, true));
57
58 std::string s;
59 ASSERT_FALSE(android::base::ReadFileToString(link.path, &s));
60 ASSERT_EQ(ELOOP, errno);
61 ASSERT_TRUE(android::base::ReadFileToString(link.path, &s, true));
62 ASSERT_EQ("foo", s);
63}
64#endif
65
Dan Albert0c4b3a32015-04-29 11:32:23 -070066// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
67// sense on Windows.
68#if !defined(_WIN32)
Elliott Hughes9d1f5152015-02-17 10:16:04 -080069TEST(file, WriteStringToFile2) {
70 TemporaryFile tf;
71 ASSERT_TRUE(tf.fd != -1);
Alex Vallée47d67c92015-05-06 16:26:00 -040072 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
Dan Albertc007bc32015-03-16 10:08:46 -070073 getuid(), getgid()))
Dan Albert850188f2015-04-29 17:09:53 -070074 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080075 struct stat sb;
Alex Vallée47d67c92015-05-06 16:26:00 -040076 ASSERT_EQ(0, stat(tf.path, &sb));
Colin Cross56b37342015-04-30 15:12:21 -070077 ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
Elliott Hughes9d1f5152015-02-17 10:16:04 -080078 ASSERT_EQ(getuid(), sb.st_uid);
79 ASSERT_EQ(getgid(), sb.st_gid);
80 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -040081 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -070082 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080083 EXPECT_EQ("abc", s);
84}
Dan Albert0c4b3a32015-04-29 11:32:23 -070085#endif
Elliott Hughes9d1f5152015-02-17 10:16:04 -080086
Elliott Hughesf682b472015-02-06 12:19:48 -080087TEST(file, WriteStringToFd) {
88 TemporaryFile tf;
89 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070090 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
Elliott Hughesf682b472015-02-06 12:19:48 -080091
Dan Albert850188f2015-04-29 17:09:53 -070092 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080093
94 std::string s;
Dan Albert850188f2015-04-29 17:09:53 -070095 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080096 EXPECT_EQ("abc", s);
97}
Elliott Hughes56085ed2015-04-24 21:57:16 -070098
Elliott Hughes56085ed2015-04-24 21:57:16 -070099TEST(file, WriteFully) {
100 TemporaryFile tf;
101 ASSERT_TRUE(tf.fd != -1);
102 ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
Spencer Lowcbf26b72015-08-03 20:43:24 -0700103
104 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
105
Elliott Hughes56085ed2015-04-24 21:57:16 -0700106 std::string s;
Spencer Lowcbf26b72015-08-03 20:43:24 -0700107 s.resize(3);
108 ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size()))
Dan Albert850188f2015-04-29 17:09:53 -0700109 << strerror(errno);
Elliott Hughes56085ed2015-04-24 21:57:16 -0700110 EXPECT_EQ("abc", s);
Spencer Lowcbf26b72015-08-03 20:43:24 -0700111
112 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
113
114 s.resize(1024);
115 ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
Elliott Hughes56085ed2015-04-24 21:57:16 -0700116}
Yabin Cuib6e314a2016-01-29 17:25:54 -0800117
118TEST(file, RemoveFileIfExist) {
119 TemporaryFile tf;
120 ASSERT_TRUE(tf.fd != -1);
121 close(tf.fd);
122 tf.fd = -1;
123 std::string err;
124 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err;
125 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path));
126 TemporaryDir td;
127 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
128 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
129 ASSERT_EQ("is not a regular or symbol link file", err);
130}
Elliott Hughesd3ff6e52016-08-23 15:53:45 -0700131
132TEST(file, Readlink) {
133#if !defined(_WIN32)
134 // Linux doesn't allow empty symbolic links.
135 std::string min("x");
136 // ext2 and ext4 both have PAGE_SIZE limits.
137 std::string max(static_cast<size_t>(4096 - 1), 'x');
138
139 TemporaryDir td;
140 std::string min_path{std::string(td.path) + "/" + "min"};
141 std::string max_path{std::string(td.path) + "/" + "max"};
142
143 ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str()));
144 ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str()));
145
146 std::string result;
147
148 result = "wrong";
149 ASSERT_TRUE(android::base::Readlink(min_path, &result));
150 ASSERT_EQ(min, result);
151
152 result = "wrong";
153 ASSERT_TRUE(android::base::Readlink(max_path, &result));
154 ASSERT_EQ(max, result);
155#endif
156}
Elliott Hughes82ff3152016-08-31 15:07:18 -0700157
158TEST(file, GetExecutablePath) {
159 ASSERT_NE("", android::base::GetExecutablePath());
160}