blob: 4056684ef63a9c8c4b83654d8180775a25accc5e [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
Dan Albertc007bc32015-03-16 10:08:46 -070017#include "base/file.h"
18
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
Alex Vallée47d67c92015-05-06 16:26:00 -040027#include "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
37TEST(file, ReadFileToString_success) {
38 std::string s("hello");
Dan Albert850188f2015-04-29 17:09:53 -070039 ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s))
40 << strerror(errno);
Elliott Hughesdec12b22015-02-02 17:31:27 -080041 EXPECT_GT(s.length(), 6U);
42 EXPECT_EQ('\n', s[s.length() - 1]);
43 s[5] = 0;
44 EXPECT_STREQ("Linux", s.c_str());
45}
Elliott Hughesf682b472015-02-06 12:19:48 -080046
47TEST(file, WriteStringToFile) {
48 TemporaryFile tf;
49 ASSERT_TRUE(tf.fd != -1);
Alex Vallée47d67c92015-05-06 16:26:00 -040050 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
Dan Albert850188f2015-04-29 17:09:53 -070051 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080052 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -040053 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -070054 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080055 EXPECT_EQ("abc", s);
56}
57
Dan Albert0c4b3a32015-04-29 11:32:23 -070058// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
59// sense on Windows.
60#if !defined(_WIN32)
Elliott Hughes9d1f5152015-02-17 10:16:04 -080061TEST(file, WriteStringToFile2) {
62 TemporaryFile tf;
63 ASSERT_TRUE(tf.fd != -1);
Alex Vallée47d67c92015-05-06 16:26:00 -040064 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
Dan Albertc007bc32015-03-16 10:08:46 -070065 getuid(), getgid()))
Dan Albert850188f2015-04-29 17:09:53 -070066 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080067 struct stat sb;
Alex Vallée47d67c92015-05-06 16:26:00 -040068 ASSERT_EQ(0, stat(tf.path, &sb));
Colin Cross56b37342015-04-30 15:12:21 -070069 ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
Elliott Hughes9d1f5152015-02-17 10:16:04 -080070 ASSERT_EQ(getuid(), sb.st_uid);
71 ASSERT_EQ(getgid(), sb.st_gid);
72 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -040073 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -070074 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080075 EXPECT_EQ("abc", s);
76}
Dan Albert0c4b3a32015-04-29 11:32:23 -070077#endif
Elliott Hughes9d1f5152015-02-17 10:16:04 -080078
Elliott Hughesf682b472015-02-06 12:19:48 -080079TEST(file, WriteStringToFd) {
80 TemporaryFile tf;
81 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070082 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
Elliott Hughesf682b472015-02-06 12:19:48 -080083
Dan Albert850188f2015-04-29 17:09:53 -070084 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080085
86 std::string s;
Dan Albert850188f2015-04-29 17:09:53 -070087 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080088 EXPECT_EQ("abc", s);
89}
Elliott Hughes56085ed2015-04-24 21:57:16 -070090
91TEST(file, ReadFully) {
92 int fd = open("/proc/version", O_RDONLY);
93 ASSERT_NE(-1, fd) << strerror(errno);
94
95 char buf[1024];
96 memset(buf, 0, sizeof(buf));
97 ASSERT_TRUE(android::base::ReadFully(fd, buf, 5));
98 ASSERT_STREQ("Linux", buf);
99
100 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
101
102 ASSERT_FALSE(android::base::ReadFully(fd, buf, sizeof(buf)));
103
104 close(fd);
105}
106
107TEST(file, WriteFully) {
108 TemporaryFile tf;
109 ASSERT_TRUE(tf.fd != -1);
110 ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
111 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -0400112 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -0700113 << strerror(errno);
Elliott Hughes56085ed2015-04-24 21:57:16 -0700114 EXPECT_EQ("abc", s);
115}