| 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 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/file.h" | 
| Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 27 | #include "android-base/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 |  | 
| Spencer Low | cf168a8 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 37 | TEST(file, ReadFileToString_WriteStringToFile) { | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 38 | TemporaryFile tf; | 
|  | 39 | ASSERT_TRUE(tf.fd != -1); | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 40 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path)) | 
| Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 41 | << strerror(errno); | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 42 | std::string s; | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 43 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) | 
| Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 44 | << strerror(errno); | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 45 | EXPECT_EQ("abc", s); | 
|  | 46 | } | 
|  | 47 |  | 
| Dan Albert | 0c4b3a3 | 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) | 
| Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 51 | TEST(file, WriteStringToFile2) { | 
|  | 52 | TemporaryFile tf; | 
|  | 53 | ASSERT_TRUE(tf.fd != -1); | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 54 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660, | 
| Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 55 | getuid(), getgid())) | 
| Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 56 | << strerror(errno); | 
| Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 57 | struct stat sb; | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 58 | ASSERT_EQ(0, stat(tf.path, &sb)); | 
| Colin Cross | 56b3734 | 2015-04-30 15:12:21 -0700 | [diff] [blame] | 59 | ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT)); | 
| Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 60 | ASSERT_EQ(getuid(), sb.st_uid); | 
|  | 61 | ASSERT_EQ(getgid(), sb.st_gid); | 
|  | 62 | std::string s; | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 63 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) | 
| Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 64 | << strerror(errno); | 
| Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 65 | EXPECT_EQ("abc", s); | 
|  | 66 | } | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 67 | #endif | 
| Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 68 |  | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 69 | TEST(file, WriteStringToFd) { | 
|  | 70 | TemporaryFile tf; | 
|  | 71 | ASSERT_TRUE(tf.fd != -1); | 
| Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 73 |  | 
| Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 74 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 75 |  | 
|  | 76 | std::string s; | 
| Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 77 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 78 | EXPECT_EQ("abc", s); | 
|  | 79 | } | 
| Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 80 |  | 
| Elliott Hughes | 56085ed | 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 | cbf26b7 | 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 | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 88 | std::string s; | 
| Spencer Low | cbf26b7 | 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 | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 91 | << strerror(errno); | 
| Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 92 | EXPECT_EQ("abc", s); | 
| Spencer Low | cbf26b7 | 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 | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 98 | } | 
| Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 99 |  | 
|  | 100 | TEST(file, RemoveFileIfExist) { | 
|  | 101 | TemporaryFile tf; | 
|  | 102 | ASSERT_TRUE(tf.fd != -1); | 
|  | 103 | close(tf.fd); | 
|  | 104 | tf.fd = -1; | 
|  | 105 | std::string err; | 
|  | 106 | ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err; | 
|  | 107 | ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path)); | 
|  | 108 | TemporaryDir td; | 
|  | 109 | ASSERT_FALSE(android::base::RemoveFileIfExists(td.path)); | 
|  | 110 | ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err)); | 
|  | 111 | ASSERT_EQ("is not a regular or symbol link file", err); | 
|  | 112 | } |