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 | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 39 | ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) |
| 40 | << strerror(errno); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 41 | 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 Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 46 | |
| 47 | TEST(file, WriteStringToFile) { |
| 48 | TemporaryFile tf; |
| 49 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 50 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) |
| 51 | << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 52 | std::string s; |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 53 | ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) |
| 54 | << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 55 | EXPECT_EQ("abc", s); |
| 56 | } |
| 57 | |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 58 | // WriteStringToFile2 is explicitly for setting Unix permissions, which make no |
| 59 | // sense on Windows. |
| 60 | #if !defined(_WIN32) |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 61 | TEST(file, WriteStringToFile2) { |
| 62 | TemporaryFile tf; |
| 63 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 64 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660, |
| 65 | getuid(), getgid())) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 66 | << strerror(errno); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 67 | struct stat sb; |
| 68 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
Colin Cross | 56b3734 | 2015-04-30 15:12:21 -0700 | [diff] [blame^] | 69 | ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT)); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 70 | ASSERT_EQ(getuid(), sb.st_uid); |
| 71 | ASSERT_EQ(getgid(), sb.st_gid); |
| 72 | std::string s; |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 73 | ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) |
| 74 | << strerror(errno); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 75 | EXPECT_EQ("abc", s); |
| 76 | } |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 77 | #endif |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 78 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 79 | TEST(file, WriteStringToFd) { |
| 80 | TemporaryFile tf; |
| 81 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 82 | ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 83 | |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 84 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 85 | |
| 86 | std::string s; |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 87 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 88 | EXPECT_EQ("abc", s); |
| 89 | } |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 90 | |
| 91 | TEST(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 | |
| 107 | TEST(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; |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 112 | ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) |
| 113 | << strerror(errno); |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 114 | EXPECT_EQ("abc", s); |
| 115 | } |