Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #include "adb_io.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
Dan Albert | 08f66bc | 2015-03-19 18:09:59 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Dan Albert | 08f66bc | 2015-03-19 18:09:59 -0700 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <string> |
| 29 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 30 | #include <android-base/file.h> |
| 31 | #include <android-base/test_utils.h> |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 32 | |
Spencer Low | cf168a8 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 33 | // All of these tests fail on Windows because they use the C Runtime open(), |
Spencer Low | 2fbeb0c | 2015-09-01 14:57:58 -0700 | [diff] [blame] | 34 | // but the adb_io APIs expect file descriptors from adb_open(). This could |
| 35 | // theoretically be fixed by making adb_read()/adb_write() fallback to using |
| 36 | // read()/write() if an unrecognized fd is used, and by making adb_open() return |
| 37 | // fds far from the range that open() returns. But all of that might defeat the |
| 38 | // purpose of the tests. |
Spencer Low | cf168a8 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 39 | |
Josh Gao | 68c63d7 | 2016-04-18 13:28:26 -0700 | [diff] [blame] | 40 | #if defined(_WIN32) |
| 41 | #define POSIX_TEST(x,y) TEST(DISABLED_ ## x,y) |
| 42 | #else |
| 43 | #define POSIX_TEST TEST |
| 44 | #endif |
| 45 | |
| 46 | POSIX_TEST(io, ReadFdExactly_whole) { |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 47 | const char expected[] = "Foobar"; |
| 48 | TemporaryFile tf; |
| 49 | ASSERT_NE(-1, tf.fd); |
| 50 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno); |
Elliott Hughes | c96de0d | 2015-10-20 13:18:22 -0700 | [diff] [blame] | 52 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 53 | |
| 54 | // Test reading the whole file. |
| 55 | char buf[sizeof(expected)] = {}; |
| 56 | ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno); |
| 57 | EXPECT_STREQ(expected, buf); |
| 58 | } |
| 59 | |
Josh Gao | 68c63d7 | 2016-04-18 13:28:26 -0700 | [diff] [blame] | 60 | POSIX_TEST(io, ReadFdExactly_eof) { |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 61 | const char expected[] = "Foobar"; |
| 62 | TemporaryFile tf; |
| 63 | ASSERT_NE(-1, tf.fd); |
| 64 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno); |
Elliott Hughes | c96de0d | 2015-10-20 13:18:22 -0700 | [diff] [blame] | 66 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 67 | |
| 68 | // Test that not having enough data will fail. |
| 69 | char buf[sizeof(expected) + 1] = {}; |
| 70 | ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf))); |
| 71 | EXPECT_EQ(0, errno) << strerror(errno); |
| 72 | } |
| 73 | |
Josh Gao | 68c63d7 | 2016-04-18 13:28:26 -0700 | [diff] [blame] | 74 | POSIX_TEST(io, ReadFdExactly_partial) { |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 75 | const char input[] = "Foobar"; |
| 76 | TemporaryFile tf; |
| 77 | ASSERT_NE(-1, tf.fd); |
| 78 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 79 | ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno); |
Elliott Hughes | c96de0d | 2015-10-20 13:18:22 -0700 | [diff] [blame] | 80 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 81 | |
| 82 | // Test reading a partial file. |
| 83 | char buf[sizeof(input) - 1] = {}; |
| 84 | ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)); |
| 85 | |
| 86 | std::string expected(input); |
| 87 | expected.pop_back(); |
| 88 | EXPECT_STREQ(expected.c_str(), buf); |
| 89 | } |
| 90 | |
Josh Gao | 68c63d7 | 2016-04-18 13:28:26 -0700 | [diff] [blame] | 91 | POSIX_TEST(io, WriteFdExactly_whole) { |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 92 | const char expected[] = "Foobar"; |
| 93 | TemporaryFile tf; |
| 94 | ASSERT_NE(-1, tf.fd); |
| 95 | |
| 96 | // Test writing the whole string to the file. |
| 97 | ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected))) |
| 98 | << strerror(errno); |
Elliott Hughes | c96de0d | 2015-10-20 13:18:22 -0700 | [diff] [blame] | 99 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 100 | |
| 101 | std::string s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 102 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 103 | EXPECT_STREQ(expected, s.c_str()); |
| 104 | } |
| 105 | |
Josh Gao | 68c63d7 | 2016-04-18 13:28:26 -0700 | [diff] [blame] | 106 | POSIX_TEST(io, WriteFdExactly_partial) { |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 107 | const char buf[] = "Foobar"; |
| 108 | TemporaryFile tf; |
| 109 | ASSERT_NE(-1, tf.fd); |
| 110 | |
| 111 | // Test writing a partial string to the file. |
| 112 | ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno); |
Elliott Hughes | c96de0d | 2015-10-20 13:18:22 -0700 | [diff] [blame] | 113 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 114 | |
| 115 | std::string expected(buf); |
| 116 | expected.pop_back(); |
| 117 | |
| 118 | std::string s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 119 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 120 | EXPECT_EQ(expected, s); |
| 121 | } |
| 122 | |
Josh Gao | 68c63d7 | 2016-04-18 13:28:26 -0700 | [diff] [blame] | 123 | POSIX_TEST(io, WriteFdExactly_ENOSPC) { |
Dan Albert | 08f66bc | 2015-03-19 18:09:59 -0700 | [diff] [blame] | 124 | int fd = open("/dev/full", O_WRONLY); |
| 125 | ASSERT_NE(-1, fd); |
| 126 | |
| 127 | char buf[] = "foo"; |
| 128 | ASSERT_FALSE(WriteFdExactly(fd, buf, sizeof(buf))); |
| 129 | ASSERT_EQ(ENOSPC, errno); |
| 130 | } |
| 131 | |
Josh Gao | 68c63d7 | 2016-04-18 13:28:26 -0700 | [diff] [blame] | 132 | POSIX_TEST(io, WriteFdExactly_string) { |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 133 | const char str[] = "Foobar"; |
| 134 | TemporaryFile tf; |
| 135 | ASSERT_NE(-1, tf.fd); |
| 136 | |
| 137 | // Test writing a partial string to the file. |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 138 | ASSERT_TRUE(WriteFdExactly(tf.fd, str)) << strerror(errno); |
Elliott Hughes | c96de0d | 2015-10-20 13:18:22 -0700 | [diff] [blame] | 139 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 140 | |
| 141 | std::string s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 142 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 143 | EXPECT_STREQ(str, s.c_str()); |
| 144 | } |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 145 | |
Josh Gao | 68c63d7 | 2016-04-18 13:28:26 -0700 | [diff] [blame] | 146 | POSIX_TEST(io, WriteFdFmt) { |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 147 | TemporaryFile tf; |
| 148 | ASSERT_NE(-1, tf.fd); |
| 149 | |
| 150 | // Test writing a partial string to the file. |
| 151 | ASSERT_TRUE(WriteFdFmt(tf.fd, "Foo%s%d", "bar", 123)) << strerror(errno); |
Elliott Hughes | c96de0d | 2015-10-20 13:18:22 -0700 | [diff] [blame] | 152 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 153 | |
| 154 | std::string s; |
| 155 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)); |
| 156 | EXPECT_STREQ("Foobar123", s.c_str()); |
| 157 | } |