blob: 34b8755c2ed7c751aaf86e139e2dac0d34723ce5 [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
Elliott Hughesf682b472015-02-06 12:19:48 -080027class TemporaryFile {
28 public:
29 TemporaryFile() {
30 init("/data/local/tmp");
31 if (fd == -1) {
32 init("/tmp");
33 }
34 }
35
36 ~TemporaryFile() {
37 close(fd);
38 unlink(filename);
39 }
40
41 int fd;
42 char filename[1024];
43
44 private:
45 void init(const char* tmp_dir) {
46 snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
47 fd = mkstemp(filename);
48 }
49};
50
Elliott Hughesdec12b22015-02-02 17:31:27 -080051TEST(file, ReadFileToString_ENOENT) {
52 std::string s("hello");
53 errno = 0;
Dan Albertc007bc32015-03-16 10:08:46 -070054 ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
Elliott Hughesdec12b22015-02-02 17:31:27 -080055 EXPECT_EQ(ENOENT, errno);
Dan Albertc007bc32015-03-16 10:08:46 -070056 EXPECT_EQ("", s); // s was cleared.
Elliott Hughesdec12b22015-02-02 17:31:27 -080057}
58
59TEST(file, ReadFileToString_success) {
60 std::string s("hello");
Dan Albertc007bc32015-03-16 10:08:46 -070061 ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno;
Elliott Hughesdec12b22015-02-02 17:31:27 -080062 EXPECT_GT(s.length(), 6U);
63 EXPECT_EQ('\n', s[s.length() - 1]);
64 s[5] = 0;
65 EXPECT_STREQ("Linux", s.c_str());
66}
Elliott Hughesf682b472015-02-06 12:19:48 -080067
68TEST(file, WriteStringToFile) {
69 TemporaryFile tf;
70 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070071 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno;
Elliott Hughesf682b472015-02-06 12:19:48 -080072 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -070073 ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
Elliott Hughesf682b472015-02-06 12:19:48 -080074 EXPECT_EQ("abc", s);
75}
76
Elliott Hughes9d1f5152015-02-17 10:16:04 -080077TEST(file, WriteStringToFile2) {
78 TemporaryFile tf;
79 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070080 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660,
81 getuid(), getgid()))
82 << errno;
Elliott Hughes9d1f5152015-02-17 10:16:04 -080083 struct stat sb;
84 ASSERT_EQ(0, stat(tf.filename, &sb));
85 ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT));
86 ASSERT_EQ(getuid(), sb.st_uid);
87 ASSERT_EQ(getgid(), sb.st_gid);
88 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -070089 ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
Elliott Hughes9d1f5152015-02-17 10:16:04 -080090 EXPECT_EQ("abc", s);
91}
92
Elliott Hughesf682b472015-02-06 12:19:48 -080093TEST(file, WriteStringToFd) {
94 TemporaryFile tf;
95 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070096 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
Elliott Hughesf682b472015-02-06 12:19:48 -080097
98 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
99
100 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -0700101 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno;
Elliott Hughesf682b472015-02-06 12:19:48 -0800102 EXPECT_EQ("abc", s);
103}