blob: 91b73a9a4735b328853d6bede6b522fd33ee2877 [file] [log] [blame]
Dan Albert66a91b02015-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 Albert8cfc9fd2015-03-19 18:09:59 -070021#include <fcntl.h>
Dan Albert66a91b02015-02-24 21:26:58 -080022#include <stdio.h>
23#include <stdlib.h>
Dan Albert8cfc9fd2015-03-19 18:09:59 -070024#include <sys/stat.h>
25#include <sys/types.h>
Dan Albert66a91b02015-02-24 21:26:58 -080026#include <unistd.h>
27
28#include <string>
29
Elliott Hughesf55ead92015-12-04 22:00:26 -080030#include <android-base/file.h>
Dan Albert66a91b02015-02-24 21:26:58 -080031
Spencer Lowc9ddd812015-05-24 15:36:28 -070032// All of these tests fail on Windows because they use the C Runtime open(),
Spencer Low7d5e6e62015-09-01 14:57:58 -070033// but the adb_io APIs expect file descriptors from adb_open(). This could
34// theoretically be fixed by making adb_read()/adb_write() fallback to using
35// read()/write() if an unrecognized fd is used, and by making adb_open() return
36// fds far from the range that open() returns. But all of that might defeat the
37// purpose of the tests.
Spencer Lowc9ddd812015-05-24 15:36:28 -070038
Josh Gao22d043c2016-04-18 13:28:26 -070039#if defined(_WIN32)
40#define POSIX_TEST(x,y) TEST(DISABLED_ ## x,y)
41#else
42#define POSIX_TEST TEST
43#endif
44
45POSIX_TEST(io, ReadFdExactly_whole) {
Dan Albert66a91b02015-02-24 21:26:58 -080046 const char expected[] = "Foobar";
47 TemporaryFile tf;
48 ASSERT_NE(-1, tf.fd);
49
Dan Albert46cf4ee2015-03-16 10:08:46 -070050 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Elliott Hughesd3044e62015-10-20 13:18:22 -070051 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albert66a91b02015-02-24 21:26:58 -080052
53 // Test reading the whole file.
54 char buf[sizeof(expected)] = {};
55 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno);
56 EXPECT_STREQ(expected, buf);
57}
58
Josh Gao22d043c2016-04-18 13:28:26 -070059POSIX_TEST(io, ReadFdExactly_eof) {
Dan Albert66a91b02015-02-24 21:26:58 -080060 const char expected[] = "Foobar";
61 TemporaryFile tf;
62 ASSERT_NE(-1, tf.fd);
63
Dan Albert46cf4ee2015-03-16 10:08:46 -070064 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Elliott Hughesd3044e62015-10-20 13:18:22 -070065 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albert66a91b02015-02-24 21:26:58 -080066
67 // Test that not having enough data will fail.
68 char buf[sizeof(expected) + 1] = {};
69 ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf)));
70 EXPECT_EQ(0, errno) << strerror(errno);
71}
72
Josh Gao22d043c2016-04-18 13:28:26 -070073POSIX_TEST(io, ReadFdExactly_partial) {
Dan Albert66a91b02015-02-24 21:26:58 -080074 const char input[] = "Foobar";
75 TemporaryFile tf;
76 ASSERT_NE(-1, tf.fd);
77
Dan Albert46cf4ee2015-03-16 10:08:46 -070078 ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
Elliott Hughesd3044e62015-10-20 13:18:22 -070079 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albert66a91b02015-02-24 21:26:58 -080080
81 // Test reading a partial file.
82 char buf[sizeof(input) - 1] = {};
83 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1));
84
85 std::string expected(input);
86 expected.pop_back();
87 EXPECT_STREQ(expected.c_str(), buf);
88}
89
Josh Gao22d043c2016-04-18 13:28:26 -070090POSIX_TEST(io, WriteFdExactly_whole) {
Dan Albert66a91b02015-02-24 21:26:58 -080091 const char expected[] = "Foobar";
92 TemporaryFile tf;
93 ASSERT_NE(-1, tf.fd);
94
95 // Test writing the whole string to the file.
96 ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected)))
97 << strerror(errno);
Elliott Hughesd3044e62015-10-20 13:18:22 -070098 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albert66a91b02015-02-24 21:26:58 -080099
100 std::string s;
Dan Albert46cf4ee2015-03-16 10:08:46 -0700101 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albert66a91b02015-02-24 21:26:58 -0800102 EXPECT_STREQ(expected, s.c_str());
103}
104
Josh Gao22d043c2016-04-18 13:28:26 -0700105POSIX_TEST(io, WriteFdExactly_partial) {
Dan Albert66a91b02015-02-24 21:26:58 -0800106 const char buf[] = "Foobar";
107 TemporaryFile tf;
108 ASSERT_NE(-1, tf.fd);
109
110 // Test writing a partial string to the file.
111 ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno);
Elliott Hughesd3044e62015-10-20 13:18:22 -0700112 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albert66a91b02015-02-24 21:26:58 -0800113
114 std::string expected(buf);
115 expected.pop_back();
116
117 std::string s;
Dan Albert46cf4ee2015-03-16 10:08:46 -0700118 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albert66a91b02015-02-24 21:26:58 -0800119 EXPECT_EQ(expected, s);
120}
121
Josh Gao22d043c2016-04-18 13:28:26 -0700122POSIX_TEST(io, WriteFdExactly_ENOSPC) {
Dan Albert8cfc9fd2015-03-19 18:09:59 -0700123 int fd = open("/dev/full", O_WRONLY);
124 ASSERT_NE(-1, fd);
125
126 char buf[] = "foo";
127 ASSERT_FALSE(WriteFdExactly(fd, buf, sizeof(buf)));
128 ASSERT_EQ(ENOSPC, errno);
129}
130
Josh Gao22d043c2016-04-18 13:28:26 -0700131POSIX_TEST(io, WriteFdExactly_string) {
Dan Albert66a91b02015-02-24 21:26:58 -0800132 const char str[] = "Foobar";
133 TemporaryFile tf;
134 ASSERT_NE(-1, tf.fd);
135
136 // Test writing a partial string to the file.
Elliott Hughes88b4c852015-04-30 17:32:03 -0700137 ASSERT_TRUE(WriteFdExactly(tf.fd, str)) << strerror(errno);
Elliott Hughesd3044e62015-10-20 13:18:22 -0700138 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Dan Albert66a91b02015-02-24 21:26:58 -0800139
140 std::string s;
Dan Albert46cf4ee2015-03-16 10:08:46 -0700141 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albert66a91b02015-02-24 21:26:58 -0800142 EXPECT_STREQ(str, s.c_str());
143}
Elliott Hughesfb596842015-05-01 17:04:38 -0700144
Josh Gao22d043c2016-04-18 13:28:26 -0700145POSIX_TEST(io, WriteFdFmt) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700146 TemporaryFile tf;
147 ASSERT_NE(-1, tf.fd);
148
149 // Test writing a partial string to the file.
150 ASSERT_TRUE(WriteFdFmt(tf.fd, "Foo%s%d", "bar", 123)) << strerror(errno);
Elliott Hughesd3044e62015-10-20 13:18:22 -0700151 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
Elliott Hughesfb596842015-05-01 17:04:38 -0700152
153 std::string s;
154 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
155 EXPECT_STREQ("Foobar123", s.c_str());
156}