1 /*
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
17 #include "android-base/file.h"
18
19 #include <gtest/gtest.h>
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24
25 #include <string>
26
27 #include "android-base/test_utils.h"
28
TEST(file,ReadFileToString_ENOENT)29 TEST(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
TEST(file,ReadFileToString_WriteStringToFile)37 TEST(file, ReadFileToString_WriteStringToFile) {
38 TemporaryFile tf;
39 ASSERT_TRUE(tf.fd != -1);
40 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
41 << strerror(errno);
42 std::string s;
43 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
44 << strerror(errno);
45 EXPECT_EQ("abc", s);
46 }
47
48 // WriteStringToFile2 is explicitly for setting Unix permissions, which make no
49 // sense on Windows.
50 #if !defined(_WIN32)
TEST(file,WriteStringToFile2)51 TEST(file, WriteStringToFile2) {
52 TemporaryFile tf;
53 ASSERT_TRUE(tf.fd != -1);
54 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
55 getuid(), getgid()))
56 << strerror(errno);
57 struct stat sb;
58 ASSERT_EQ(0, stat(tf.path, &sb));
59 ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
60 ASSERT_EQ(getuid(), sb.st_uid);
61 ASSERT_EQ(getgid(), sb.st_gid);
62 std::string s;
63 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
64 << strerror(errno);
65 EXPECT_EQ("abc", s);
66 }
67 #endif
68
TEST(file,WriteStringToFd)69 TEST(file, WriteStringToFd) {
70 TemporaryFile tf;
71 ASSERT_TRUE(tf.fd != -1);
72 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
73
74 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
75
76 std::string s;
77 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
78 EXPECT_EQ("abc", s);
79 }
80
TEST(file,WriteFully)81 TEST(file, WriteFully) {
82 TemporaryFile tf;
83 ASSERT_TRUE(tf.fd != -1);
84 ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
85
86 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
87
88 std::string s;
89 s.resize(3);
90 ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size()))
91 << strerror(errno);
92 EXPECT_EQ("abc", s);
93
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()));
98 }
99
TEST(file,RemoveFileIfExist)100 TEST(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 }
113