blob: fc48b32a79279827765a323c424315d109905c57 [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 Albertc007bc32015-03-16 10:08:46 -070039 ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno;
Elliott Hughesdec12b22015-02-02 17:31:27 -080040 EXPECT_GT(s.length(), 6U);
41 EXPECT_EQ('\n', s[s.length() - 1]);
42 s[5] = 0;
43 EXPECT_STREQ("Linux", s.c_str());
44}
Elliott Hughesf682b472015-02-06 12:19:48 -080045
46TEST(file, WriteStringToFile) {
47 TemporaryFile tf;
48 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070049 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno;
Elliott Hughesf682b472015-02-06 12:19:48 -080050 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -070051 ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
Elliott Hughesf682b472015-02-06 12:19:48 -080052 EXPECT_EQ("abc", s);
53}
54
Elliott Hughes9d1f5152015-02-17 10:16:04 -080055TEST(file, WriteStringToFile2) {
56 TemporaryFile tf;
57 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070058 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660,
59 getuid(), getgid()))
60 << errno;
Elliott Hughes9d1f5152015-02-17 10:16:04 -080061 struct stat sb;
62 ASSERT_EQ(0, stat(tf.filename, &sb));
63 ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT));
64 ASSERT_EQ(getuid(), sb.st_uid);
65 ASSERT_EQ(getgid(), sb.st_gid);
66 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -070067 ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
Elliott Hughes9d1f5152015-02-17 10:16:04 -080068 EXPECT_EQ("abc", s);
69}
70
Elliott Hughesf682b472015-02-06 12:19:48 -080071TEST(file, WriteStringToFd) {
72 TemporaryFile tf;
73 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070074 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
Elliott Hughesf682b472015-02-06 12:19:48 -080075
76 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
77
78 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -070079 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno;
Elliott Hughesf682b472015-02-06 12:19:48 -080080 EXPECT_EQ("abc", s);
81}