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>
27 #include "base/file.h"
32 init("/data/local/tmp");
47 void init(const char* tmp_dir) {
48 snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
49 fd = mkstemp(filename);
53 TEST(io, ReadFdExactly_whole) {
54 const char expected[] = "Foobar";
58 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
59 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
61 // Test reading the whole file.
62 char buf[sizeof(expected)] = {};
63 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno);
64 EXPECT_STREQ(expected, buf);
67 TEST(io, ReadFdExactly_eof) {
68 const char expected[] = "Foobar";
72 ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
73 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
75 // Test that not having enough data will fail.
76 char buf[sizeof(expected) + 1] = {};
77 ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf)));
78 EXPECT_EQ(0, errno) << strerror(errno);
81 TEST(io, ReadFdExactly_partial) {
82 const char input[] = "Foobar";
86 ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
87 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
89 // Test reading a partial file.
90 char buf[sizeof(input) - 1] = {};
91 ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1));
93 std::string expected(input);
95 EXPECT_STREQ(expected.c_str(), buf);
98 TEST(io, WriteFdExactly_whole) {
99 const char expected[] = "Foobar";
101 ASSERT_NE(-1, tf.fd);
103 // Test writing the whole string to the file.
104 ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected)))
106 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
109 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
110 EXPECT_STREQ(expected, s.c_str());
113 TEST(io, WriteFdExactly_partial) {
114 const char buf[] = "Foobar";
116 ASSERT_NE(-1, tf.fd);
118 // Test writing a partial string to the file.
119 ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno);
120 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
122 std::string expected(buf);
126 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
127 EXPECT_EQ(expected, s);
130 TEST(io, WriteStringFully) {
131 const char str[] = "Foobar";
133 ASSERT_NE(-1, tf.fd);
135 // Test writing a partial string to the file.
136 ASSERT_TRUE(WriteStringFully(tf.fd, str)) << strerror(errno);
137 ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
140 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
141 EXPECT_STREQ(str, s.c_str());