blob: 611b23937222cafe482082ff075684fdb745ad2f [file] [log] [blame]
Dan Albertcc731cc2015-02-24 21:26:58 -08001/*
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 Albert08f66bc2015-03-19 18:09:59 -070021#include <fcntl.h>
Dan Albertcc731cc2015-02-24 21:26:58 -080022#include <stdio.h>
23#include <stdlib.h>
Dan Albert08f66bc2015-03-19 18:09:59 -070024#include <sys/stat.h>
25#include <sys/types.h>
Dan Albertcc731cc2015-02-24 21:26:58 -080026#include <unistd.h>
27
28#include <string>
29
Elliott Hughes4f713192015-12-04 22:00:26 -080030#include <android-base/file.h>
31#include <android-base/test_utils.h>
Dan Albertcc731cc2015-02-24 21:26:58 -080032
Spencer Lowcf168a82015-05-24 15:36:28 -070033// All of these tests fail on Windows because they use the C Runtime open(),
Spencer Low2fbeb0c2015-09-01 14:57:58 -070034// 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 Lowcf168a82015-05-24 15:36:28 -070039
Josh Gao68c63d72016-04-18 13:28:26 -070040#if defined(_WIN32)
41#define POSIX_TEST(x,y) TEST(DISABLED_ ## x,y)
42#else
43#define POSIX_TEST TEST
44#endif
45
46POSIX_TEST(io, ReadFdExactly_whole) {
Dan Albertcc731cc2015-02-24 21:26:58 -080047 const char expected[] = "Foobar";
48 TemporaryFile tf;
49 ASSERT_NE(-1, tf.fd);
50
Dan Albertc007bc32015-03-16 10:08:46 -070051 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Elliott Hughesc96de0d2015-10-20 13:18:22 -070052 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albertcc731cc2015-02-24 21:26:58 -080053
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 Gao68c63d72016-04-18 13:28:26 -070060POSIX_TEST(io, ReadFdExactly_eof) {
Dan Albertcc731cc2015-02-24 21:26:58 -080061 const char expected[] = "Foobar";
62 TemporaryFile tf;
63 ASSERT_NE(-1, tf.fd);
64
Dan Albertc007bc32015-03-16 10:08:46 -070065 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Elliott Hughesc96de0d2015-10-20 13:18:22 -070066 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albertcc731cc2015-02-24 21:26:58 -080067
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 Gao68c63d72016-04-18 13:28:26 -070074POSIX_TEST(io, ReadFdExactly_partial) {
Dan Albertcc731cc2015-02-24 21:26:58 -080075 const char input[] = "Foobar";
76 TemporaryFile tf;
77 ASSERT_NE(-1, tf.fd);
78
Dan Albertc007bc32015-03-16 10:08:46 -070079 ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
Elliott Hughesc96de0d2015-10-20 13:18:22 -070080 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albertcc731cc2015-02-24 21:26:58 -080081
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 Gao68c63d72016-04-18 13:28:26 -070091POSIX_TEST(io, WriteFdExactly_whole) {
Dan Albertcc731cc2015-02-24 21:26:58 -080092 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 Hughesc96de0d2015-10-20 13:18:22 -070099 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albertcc731cc2015-02-24 21:26:58 -0800100
101 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -0700102 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -0800103 EXPECT_STREQ(expected, s.c_str());
104}
105
Josh Gao68c63d72016-04-18 13:28:26 -0700106POSIX_TEST(io, WriteFdExactly_partial) {
Dan Albertcc731cc2015-02-24 21:26:58 -0800107 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 Hughesc96de0d2015-10-20 13:18:22 -0700113 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albertcc731cc2015-02-24 21:26:58 -0800114
115 std::string expected(buf);
116 expected.pop_back();
117
118 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -0700119 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -0800120 EXPECT_EQ(expected, s);
121}
122
Josh Gao68c63d72016-04-18 13:28:26 -0700123POSIX_TEST(io, WriteFdExactly_ENOSPC) {
Dan Albert08f66bc2015-03-19 18:09:59 -0700124 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 Gao68c63d72016-04-18 13:28:26 -0700132POSIX_TEST(io, WriteFdExactly_string) {
Dan Albertcc731cc2015-02-24 21:26:58 -0800133 const char str[] = "Foobar";
134 TemporaryFile tf;
135 ASSERT_NE(-1, tf.fd);
136
137 // Test writing a partial string to the file.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700138 ASSERT_TRUE(WriteFdExactly(tf.fd, str)) << strerror(errno);
Elliott Hughesc96de0d2015-10-20 13:18:22 -0700139 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albertcc731cc2015-02-24 21:26:58 -0800140
141 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -0700142 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -0800143 EXPECT_STREQ(str, s.c_str());
144}
Elliott Hughesab52c182015-05-01 17:04:38 -0700145
Josh Gao68c63d72016-04-18 13:28:26 -0700146POSIX_TEST(io, WriteFdFmt) {
Elliott Hughesab52c182015-05-01 17:04:38 -0700147 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 Hughesc96de0d2015-10-20 13:18:22 -0700152 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Elliott Hughesab52c182015-05-01 17:04:38 -0700153
154 std::string s;
155 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
156 EXPECT_STREQ("Foobar123", s.c_str());
157}