blob: 0ae21db5f8e98dd494ecb3f7ba5809d32d11e31a [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
33TEST(io, ReadFdExactly_whole) {
34 const char expected[] = "Foobar";
35 TemporaryFile tf;
36 ASSERT_NE(-1, tf.fd);
37
Dan Albertc007bc32015-03-16 10:08:46 -070038 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Dan Albertcc731cc2015-02-24 21:26:58 -080039 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
40
41 // Test reading the whole file.
42 char buf[sizeof(expected)] = {};
43 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno);
44 EXPECT_STREQ(expected, buf);
45}
46
47TEST(io, ReadFdExactly_eof) {
48 const char expected[] = "Foobar";
49 TemporaryFile tf;
50 ASSERT_NE(-1, tf.fd);
51
Dan Albertc007bc32015-03-16 10:08:46 -070052 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
Dan Albertcc731cc2015-02-24 21:26:58 -080053 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
54
55 // Test that not having enough data will fail.
56 char buf[sizeof(expected) + 1] = {};
57 ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf)));
58 EXPECT_EQ(0, errno) << strerror(errno);
59}
60
61TEST(io, ReadFdExactly_partial) {
62 const char input[] = "Foobar";
63 TemporaryFile tf;
64 ASSERT_NE(-1, tf.fd);
65
Dan Albertc007bc32015-03-16 10:08:46 -070066 ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
Dan Albertcc731cc2015-02-24 21:26:58 -080067 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
68
69 // Test reading a partial file.
70 char buf[sizeof(input) - 1] = {};
71 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1));
72
73 std::string expected(input);
74 expected.pop_back();
75 EXPECT_STREQ(expected.c_str(), buf);
76}
77
78TEST(io, WriteFdExactly_whole) {
79 const char expected[] = "Foobar";
80 TemporaryFile tf;
81 ASSERT_NE(-1, tf.fd);
82
83 // Test writing the whole string to the file.
84 ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected)))
85 << strerror(errno);
86 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
87
88 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -070089 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -080090 EXPECT_STREQ(expected, s.c_str());
91}
92
93TEST(io, WriteFdExactly_partial) {
94 const char buf[] = "Foobar";
95 TemporaryFile tf;
96 ASSERT_NE(-1, tf.fd);
97
98 // Test writing a partial string to the file.
99 ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno);
100 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
101
102 std::string expected(buf);
103 expected.pop_back();
104
105 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -0700106 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -0800107 EXPECT_EQ(expected, s);
108}
109
Dan Albert08f66bc2015-03-19 18:09:59 -0700110TEST(io, WriteFdExactly_ENOSPC) {
111 int fd = open("/dev/full", O_WRONLY);
112 ASSERT_NE(-1, fd);
113
114 char buf[] = "foo";
115 ASSERT_FALSE(WriteFdExactly(fd, buf, sizeof(buf)));
116 ASSERT_EQ(ENOSPC, errno);
117}
118
Elliott Hughese67f1f82015-04-30 17:32:03 -0700119TEST(io, WriteFdExactly_string) {
Dan Albertcc731cc2015-02-24 21:26:58 -0800120 const char str[] = "Foobar";
121 TemporaryFile tf;
122 ASSERT_NE(-1, tf.fd);
123
124 // Test writing a partial string to the file.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700125 ASSERT_TRUE(WriteFdExactly(tf.fd, str)) << strerror(errno);
Dan Albertcc731cc2015-02-24 21:26:58 -0800126 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
127
128 std::string s;
Dan Albertc007bc32015-03-16 10:08:46 -0700129 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
Dan Albertcc731cc2015-02-24 21:26:58 -0800130 EXPECT_STREQ(str, s.c_str());
131}
Elliott Hughesab52c182015-05-01 17:04:38 -0700132
133TEST(io, WriteFdFmt) {
134 TemporaryFile tf;
135 ASSERT_NE(-1, tf.fd);
136
137 // Test writing a partial string to the file.
138 ASSERT_TRUE(WriteFdFmt(tf.fd, "Foo%s%d", "bar", 123)) << strerror(errno);
139 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
140
141 std::string s;
142 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
143 EXPECT_STREQ("Foobar123", s.c_str());
144}