2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <gtest/gtest.h>
25 #include <sys/types.h>
30 #include "base/file.h"
35 init("/data/local/tmp");
50 void init(const char* tmp_dir) {
51 snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
52 fd = mkstemp(filename);
56 TEST(io, ReadFdExactly_whole) {
57 const char expected[] = "Foobar";
61 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
62 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
64 // Test reading the whole file.
65 char buf[sizeof(expected)] = {};
66 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno);
67 EXPECT_STREQ(expected, buf);
70 TEST(io, ReadFdExactly_eof) {
71 const char expected[] = "Foobar";
75 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
76 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
78 // Test that not having enough data will fail.
79 char buf[sizeof(expected) + 1] = {};
80 ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf)));
81 EXPECT_EQ(0, errno) << strerror(errno);
84 TEST(io, ReadFdExactly_partial) {
85 const char input[] = "Foobar";
89 ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
90 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
92 // Test reading a partial file.
93 char buf[sizeof(input) - 1] = {};
94 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1));
96 std::string expected(input);
98 EXPECT_STREQ(expected.c_str(), buf);
101 TEST(io, WriteFdExactly_whole) {
102 const char expected[] = "Foobar";
104 ASSERT_NE(-1, tf.fd);
106 // Test writing the whole string to the file.
107 ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected)))
109 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
112 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
113 EXPECT_STREQ(expected, s.c_str());
116 TEST(io, WriteFdExactly_partial) {
117 const char buf[] = "Foobar";
119 ASSERT_NE(-1, tf.fd);
121 // Test writing a partial string to the file.
122 ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno);
123 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
125 std::string expected(buf);
129 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
130 EXPECT_EQ(expected, s);
133 TEST(io, WriteFdExactly_ENOSPC) {
134 int fd = open("/dev/full", O_WRONLY);
138 ASSERT_FALSE(WriteFdExactly(fd, buf, sizeof(buf)));
139 ASSERT_EQ(ENOSPC, errno);
142 TEST(io, WriteStringFully) {
143 const char str[] = "Foobar";
145 ASSERT_NE(-1, tf.fd);
147 // Test writing a partial string to the file.
148 ASSERT_TRUE(WriteStringFully(tf.fd, str)) << strerror(errno);
149 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
152 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
153 EXPECT_STREQ(str, s.c_str());