blob: 84e3db6f7a317550e65e670765c22f5b9acc6693 [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
Dan Albertc007bc32015-03-16 10:08:46 -070030#include "base/file.h"
Alex Vallée47d67c92015-05-06 16:26:00 -040031#include "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
Dan Albertcc731cc2015-02-24 21:26:58 -080040TEST(io, ReadFdExactly_whole) {
41 const char expected[] = "Foobar";
42 TemporaryFile tf;
43 ASSERT_NE(-1, tf.fd);
44
Dan Albertc007bc32015-03-16 10:08:46 -070045 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Dan Albertcc731cc2015-02-24 21:26:58 -080046 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
47
48 // Test reading the whole file.
49 char buf[sizeof(expected)] = {};
50 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno);
51 EXPECT_STREQ(expected, buf);
52}
53
54TEST(io, ReadFdExactly_eof) {
55 const char expected[] = "Foobar";
56 TemporaryFile tf;
57 ASSERT_NE(-1, tf.fd);
58
Dan Albertc007bc32015-03-16 10:08:46 -070059 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Dan Albertcc731cc2015-02-24 21:26:58 -080060 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
61
62 // Test that not having enough data will fail.
63 char buf[sizeof(expected) + 1] = {};
64 ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf)));
65 EXPECT_EQ(0, errno) << strerror(errno);
66}
67
68TEST(io, ReadFdExactly_partial) {
69 const char input[] = "Foobar";
70 TemporaryFile tf;
71 ASSERT_NE(-1, tf.fd);
72
Dan Albertc007bc32015-03-16 10:08:46 -070073 ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
Dan Albertcc731cc2015-02-24 21:26:58 -080074 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
75
76 // Test reading a partial file.
77 char buf[sizeof(input) - 1] = {};
78 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1));
79
80 std::string expected(input);
81 expected.pop_back();
82 EXPECT_STREQ(expected.c_str(), buf);
83}
84
85TEST(io, WriteFdExactly_whole) {
86 const char expected[] = "Foobar";
87 TemporaryFile tf;
88 ASSERT_NE(-1, tf.fd);
89
90 // Test writing the whole string to the file.
91 ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected)))
92 << strerror(errno);
93 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
94
95 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -070096 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -080097 EXPECT_STREQ(expected, s.c_str());
98}
99
100TEST(io, WriteFdExactly_partial) {
101 const char buf[] = "Foobar";
102 TemporaryFile tf;
103 ASSERT_NE(-1, tf.fd);
104
105 // Test writing a partial string to the file.
106 ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno);
107 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
108
109 std::string expected(buf);
110 expected.pop_back();
111
112 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -0700113 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -0800114 EXPECT_EQ(expected, s);
115}
116
Dan Albert08f66bc2015-03-19 18:09:59 -0700117TEST(io, WriteFdExactly_ENOSPC) {
118 int fd = open("/dev/full", O_WRONLY);
119 ASSERT_NE(-1, fd);
120
121 char buf[] = "foo";
122 ASSERT_FALSE(WriteFdExactly(fd, buf, sizeof(buf)));
123 ASSERT_EQ(ENOSPC, errno);
124}
125
Elliott Hughese67f1f82015-04-30 17:32:03 -0700126TEST(io, WriteFdExactly_string) {
Dan Albertcc731cc2015-02-24 21:26:58 -0800127 const char str[] = "Foobar";
128 TemporaryFile tf;
129 ASSERT_NE(-1, tf.fd);
130
131 // Test writing a partial string to the file.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700132 ASSERT_TRUE(WriteFdExactly(tf.fd, str)) << strerror(errno);
Dan Albertcc731cc2015-02-24 21:26:58 -0800133 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
134
135 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -0700136 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -0800137 EXPECT_STREQ(str, s.c_str());
138}
Elliott Hughesab52c182015-05-01 17:04:38 -0700139
140TEST(io, WriteFdFmt) {
141 TemporaryFile tf;
142 ASSERT_NE(-1, tf.fd);
143
144 // Test writing a partial string to the file.
145 ASSERT_TRUE(WriteFdFmt(tf.fd, "Foo%s%d", "bar", 123)) << strerror(errno);
146 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
147
148 std::string s;
149 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
150 EXPECT_STREQ("Foobar123", s.c_str());
151}