blob: f637073a724f43b725910b4abd1ef032a071bc5e [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
Dan Albert46cf4ee2015-03-16 10:08:46 -070030#include "base/file.h"
Alex Vallée28d1f8d2015-05-06 16:26:00 -040031#include "base/test_utils.h"
Dan Albert66a91b02015-02-24 21:26:58 -080032
Spencer Lowc9ddd812015-05-24 15:36:28 -070033// All of these tests fail on Windows because they use the C Runtime open(),
34// but the adb_io APIs expect file descriptors from adb_open(). Also, the
35// android::base file APIs use the C Runtime which uses CR/LF translation by
36// default (changeable with _setmode()), but the adb_io APIs use adb_read()
37// and adb_write() which do no translation.
38
Dan Albert66a91b02015-02-24 21:26:58 -080039TEST(io, ReadFdExactly_whole) {
40 const char expected[] = "Foobar";
41 TemporaryFile tf;
42 ASSERT_NE(-1, tf.fd);
43
Dan Albert46cf4ee2015-03-16 10:08:46 -070044 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Dan Albert66a91b02015-02-24 21:26:58 -080045 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
46
47 // Test reading the whole file.
48 char buf[sizeof(expected)] = {};
49 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno);
50 EXPECT_STREQ(expected, buf);
51}
52
53TEST(io, ReadFdExactly_eof) {
54 const char expected[] = "Foobar";
55 TemporaryFile tf;
56 ASSERT_NE(-1, tf.fd);
57
Dan Albert46cf4ee2015-03-16 10:08:46 -070058 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Dan Albert66a91b02015-02-24 21:26:58 -080059 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
60
61 // Test that not having enough data will fail.
62 char buf[sizeof(expected) + 1] = {};
63 ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf)));
64 EXPECT_EQ(0, errno) << strerror(errno);
65}
66
67TEST(io, ReadFdExactly_partial) {
68 const char input[] = "Foobar";
69 TemporaryFile tf;
70 ASSERT_NE(-1, tf.fd);
71
Dan Albert46cf4ee2015-03-16 10:08:46 -070072 ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
Dan Albert66a91b02015-02-24 21:26:58 -080073 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
74
75 // Test reading a partial file.
76 char buf[sizeof(input) - 1] = {};
77 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1));
78
79 std::string expected(input);
80 expected.pop_back();
81 EXPECT_STREQ(expected.c_str(), buf);
82}
83
84TEST(io, WriteFdExactly_whole) {
85 const char expected[] = "Foobar";
86 TemporaryFile tf;
87 ASSERT_NE(-1, tf.fd);
88
89 // Test writing the whole string to the file.
90 ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected)))
91 << strerror(errno);
92 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
93
94 std::string s;
Dan Albert46cf4ee2015-03-16 10:08:46 -070095 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albert66a91b02015-02-24 21:26:58 -080096 EXPECT_STREQ(expected, s.c_str());
97}
98
99TEST(io, WriteFdExactly_partial) {
100 const char buf[] = "Foobar";
101 TemporaryFile tf;
102 ASSERT_NE(-1, tf.fd);
103
104 // Test writing a partial string to the file.
105 ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno);
106 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
107
108 std::string expected(buf);
109 expected.pop_back();
110
111 std::string s;
Dan Albert46cf4ee2015-03-16 10:08:46 -0700112 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albert66a91b02015-02-24 21:26:58 -0800113 EXPECT_EQ(expected, s);
114}
115
Dan Albert8cfc9fd2015-03-19 18:09:59 -0700116TEST(io, WriteFdExactly_ENOSPC) {
117 int fd = open("/dev/full", O_WRONLY);
118 ASSERT_NE(-1, fd);
119
120 char buf[] = "foo";
121 ASSERT_FALSE(WriteFdExactly(fd, buf, sizeof(buf)));
122 ASSERT_EQ(ENOSPC, errno);
123}
124
Elliott Hughes88b4c852015-04-30 17:32:03 -0700125TEST(io, WriteFdExactly_string) {
Dan Albert66a91b02015-02-24 21:26:58 -0800126 const char str[] = "Foobar";
127 TemporaryFile tf;
128 ASSERT_NE(-1, tf.fd);
129
130 // Test writing a partial string to the file.
Elliott Hughes88b4c852015-04-30 17:32:03 -0700131 ASSERT_TRUE(WriteFdExactly(tf.fd, str)) << strerror(errno);
Dan Albert66a91b02015-02-24 21:26:58 -0800132 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
133
134 std::string s;
Dan Albert46cf4ee2015-03-16 10:08:46 -0700135 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albert66a91b02015-02-24 21:26:58 -0800136 EXPECT_STREQ(str, s.c_str());
137}
Elliott Hughesfb596842015-05-01 17:04:38 -0700138
139TEST(io, WriteFdFmt) {
140 TemporaryFile tf;
141 ASSERT_NE(-1, tf.fd);
142
143 // Test writing a partial string to the file.
144 ASSERT_TRUE(WriteFdFmt(tf.fd, "Foo%s%d", "bar", 123)) << strerror(errno);
145 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
146
147 std::string s;
148 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
149 EXPECT_STREQ("Foobar123", s.c_str());
150}