blob: a7f070d758004b489ecdcf883bc5db5a0c89d584 [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
Dan Albert58310b42015-03-13 23:06:01 -070027#include "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);
Dan Albert850188f2015-04-29 17:09:53 -070050 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename))
51 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080052 std::string s;
Dan Albert850188f2015-04-29 17:09:53 -070053 ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s))
54 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080055 EXPECT_EQ("abc", s);
56}
57
Elliott Hughes9d1f5152015-02-17 10:16:04 -080058TEST(file, WriteStringToFile2) {
59 TemporaryFile tf;
60 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070061 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660,
62 getuid(), getgid()))
Dan Albert850188f2015-04-29 17:09:53 -070063 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080064 struct stat sb;
65 ASSERT_EQ(0, stat(tf.filename, &sb));
66 ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT));
67 ASSERT_EQ(getuid(), sb.st_uid);
68 ASSERT_EQ(getgid(), sb.st_gid);
69 std::string s;
Dan Albert850188f2015-04-29 17:09:53 -070070 ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s))
71 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080072 EXPECT_EQ("abc", s);
73}
74
Elliott Hughesf682b472015-02-06 12:19:48 -080075TEST(file, WriteStringToFd) {
76 TemporaryFile tf;
77 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070078 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
Elliott Hughesf682b472015-02-06 12:19:48 -080079
Dan Albert850188f2015-04-29 17:09:53 -070080 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080081
82 std::string s;
Dan Albert850188f2015-04-29 17:09:53 -070083 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080084 EXPECT_EQ("abc", s);
85}
Elliott Hughes56085ed2015-04-24 21:57:16 -070086
87TEST(file, ReadFully) {
88 int fd = open("/proc/version", O_RDONLY);
89 ASSERT_NE(-1, fd) << strerror(errno);
90
91 char buf[1024];
92 memset(buf, 0, sizeof(buf));
93 ASSERT_TRUE(android::base::ReadFully(fd, buf, 5));
94 ASSERT_STREQ("Linux", buf);
95
96 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
97
98 ASSERT_FALSE(android::base::ReadFully(fd, buf, sizeof(buf)));
99
100 close(fd);
101}
102
103TEST(file, WriteFully) {
104 TemporaryFile tf;
105 ASSERT_TRUE(tf.fd != -1);
106 ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
107 std::string s;
Dan Albert850188f2015-04-29 17:09:53 -0700108 ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s))
109 << strerror(errno);
Elliott Hughes56085ed2015-04-24 21:57:16 -0700110 EXPECT_EQ("abc", s);
111}