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 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 27 | class TemporaryFile { |
| 28 | public: |
| 29 | TemporaryFile() { |
| 30 | init("/data/local/tmp"); |
| 31 | if (fd == -1) { |
| 32 | init("/tmp"); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | ~TemporaryFile() { |
| 37 | close(fd); |
| 38 | unlink(filename); |
| 39 | } |
| 40 | |
| 41 | int fd; |
| 42 | char filename[1024]; |
| 43 | |
| 44 | private: |
| 45 | void init(const char* tmp_dir) { |
| 46 | snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir); |
| 47 | fd = mkstemp(filename); |
| 48 | } |
| 49 | }; |
| 50 | |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 51 | TEST(file, ReadFileToString_ENOENT) { |
| 52 | std::string s("hello"); |
| 53 | errno = 0; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 54 | ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s)); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 55 | EXPECT_EQ(ENOENT, errno); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 56 | EXPECT_EQ("", s); // s was cleared. |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | TEST(file, ReadFileToString_success) { |
| 60 | std::string s("hello"); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 61 | ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno; |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 62 | EXPECT_GT(s.length(), 6U); |
| 63 | EXPECT_EQ('\n', s[s.length() - 1]); |
| 64 | s[5] = 0; |
| 65 | EXPECT_STREQ("Linux", s.c_str()); |
| 66 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 67 | |
| 68 | TEST(file, WriteStringToFile) { |
| 69 | TemporaryFile tf; |
| 70 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 71 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 72 | std::string s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 73 | ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 74 | EXPECT_EQ("abc", s); |
| 75 | } |
| 76 | |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 77 | TEST(file, WriteStringToFile2) { |
| 78 | TemporaryFile tf; |
| 79 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 80 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660, |
| 81 | getuid(), getgid())) |
| 82 | << errno; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 83 | struct stat sb; |
| 84 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 85 | ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT)); |
| 86 | ASSERT_EQ(getuid(), sb.st_uid); |
| 87 | ASSERT_EQ(getgid(), sb.st_gid); |
| 88 | std::string s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 89 | ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 90 | EXPECT_EQ("abc", s); |
| 91 | } |
| 92 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 93 | TEST(file, WriteStringToFd) { |
| 94 | TemporaryFile tf; |
| 95 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 96 | ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 97 | |
| 98 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno; |
| 99 | |
| 100 | std::string s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame^] | 101 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 102 | EXPECT_EQ("abc", s); |
| 103 | } |