Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 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 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 17 | #include "base/file.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 20 | |
| 21 | #include <errno.h> |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 24 | |
| 25 | #include <string> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 26 | |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 27 | #include "test_utils.h" |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 29 | TEST(file, ReadFileToString_ENOENT) { |
| 30 | std::string s("hello"); |
| 31 | errno = 0; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 32 | ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s)); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 33 | EXPECT_EQ(ENOENT, errno); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 34 | EXPECT_EQ("", s); // s was cleared. |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | TEST(file, ReadFileToString_success) { |
| 38 | std::string s("hello"); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 39 | ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno; |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 40 | 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 Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 45 | |
| 46 | TEST(file, WriteStringToFile) { |
| 47 | TemporaryFile tf; |
| 48 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 50 | std::string s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 52 | EXPECT_EQ("abc", s); |
| 53 | } |
| 54 | |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 55 | TEST(file, WriteStringToFile2) { |
| 56 | TemporaryFile tf; |
| 57 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 58 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660, |
| 59 | getuid(), getgid())) |
| 60 | << errno; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 61 | 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 Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 67 | ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 68 | EXPECT_EQ("abc", s); |
| 69 | } |
| 70 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 71 | TEST(file, WriteStringToFd) { |
| 72 | TemporaryFile tf; |
| 73 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 74 | ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 75 | |
| 76 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno; |
| 77 | |
| 78 | std::string s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 79 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 80 | EXPECT_EQ("abc", s); |
| 81 | } |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame^] | 82 | |
| 83 | TEST(file, ReadFully) { |
| 84 | int fd = open("/proc/version", O_RDONLY); |
| 85 | ASSERT_NE(-1, fd) << strerror(errno); |
| 86 | |
| 87 | char buf[1024]; |
| 88 | memset(buf, 0, sizeof(buf)); |
| 89 | ASSERT_TRUE(android::base::ReadFully(fd, buf, 5)); |
| 90 | ASSERT_STREQ("Linux", buf); |
| 91 | |
| 92 | ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno); |
| 93 | |
| 94 | ASSERT_FALSE(android::base::ReadFully(fd, buf, sizeof(buf))); |
| 95 | |
| 96 | close(fd); |
| 97 | } |
| 98 | |
| 99 | TEST(file, WriteFully) { |
| 100 | TemporaryFile tf; |
| 101 | ASSERT_TRUE(tf.fd != -1); |
| 102 | ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3)); |
| 103 | std::string s; |
| 104 | ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno; |
| 105 | EXPECT_EQ("abc", s); |
| 106 | } |