Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [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 | |
Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/file.h" |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <string> |
| 26 | |
Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 27 | #include "android-base/test_utils.h" |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 28 | |
| 29 | TEST(file, ReadFileToString_ENOENT) { |
| 30 | std::string s("hello"); |
| 31 | errno = 0; |
| 32 | ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s)); |
| 33 | EXPECT_EQ(ENOENT, errno); |
| 34 | EXPECT_EQ("", s); // s was cleared. |
| 35 | } |
| 36 | |
Spencer Low | 482fa6d | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 37 | TEST(file, ReadFileToString_WriteStringToFile) { |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 38 | TemporaryFile tf; |
| 39 | ASSERT_TRUE(tf.fd != -1); |
Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 40 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path)) |
Dan Albert | 1a93262 | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 41 | << strerror(errno); |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 42 | std::string s; |
Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 43 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) |
Dan Albert | 1a93262 | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 44 | << strerror(errno); |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 45 | EXPECT_EQ("abc", s); |
| 46 | } |
| 47 | |
Dan Albert | 26cb8ff | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 48 | // WriteStringToFile2 is explicitly for setting Unix permissions, which make no |
| 49 | // sense on Windows. |
| 50 | #if !defined(_WIN32) |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 51 | TEST(file, WriteStringToFile2) { |
| 52 | TemporaryFile tf; |
| 53 | ASSERT_TRUE(tf.fd != -1); |
Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 54 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660, |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 55 | getuid(), getgid())) |
Dan Albert | 1a93262 | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 56 | << strerror(errno); |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 57 | struct stat sb; |
Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 58 | ASSERT_EQ(0, stat(tf.path, &sb)); |
Colin Cross | eebcd14 | 2015-04-30 15:12:21 -0700 | [diff] [blame] | 59 | ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT)); |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 60 | ASSERT_EQ(getuid(), sb.st_uid); |
| 61 | ASSERT_EQ(getgid(), sb.st_gid); |
| 62 | std::string s; |
Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 63 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) |
Dan Albert | 1a93262 | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 64 | << strerror(errno); |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 65 | EXPECT_EQ("abc", s); |
| 66 | } |
Dan Albert | 26cb8ff | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 67 | #endif |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 68 | |
| 69 | TEST(file, WriteStringToFd) { |
| 70 | TemporaryFile tf; |
| 71 | ASSERT_TRUE(tf.fd != -1); |
| 72 | ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); |
| 73 | |
Dan Albert | 1a93262 | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 74 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 75 | |
| 76 | std::string s; |
Dan Albert | 1a93262 | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 77 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); |
Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 78 | EXPECT_EQ("abc", s); |
| 79 | } |
Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 80 | |
Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 81 | TEST(file, WriteFully) { |
| 82 | TemporaryFile tf; |
| 83 | ASSERT_TRUE(tf.fd != -1); |
| 84 | ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3)); |
Spencer Low | 38d002e | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 85 | |
| 86 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
| 87 | |
Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 88 | std::string s; |
Spencer Low | 38d002e | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 89 | s.resize(3); |
| 90 | ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size())) |
Dan Albert | 1a93262 | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 91 | << strerror(errno); |
Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 92 | EXPECT_EQ("abc", s); |
Spencer Low | 38d002e | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 93 | |
| 94 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
| 95 | |
| 96 | s.resize(1024); |
| 97 | ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size())); |
Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 98 | } |