2015-02-03 09:31:27 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
#include "base/file.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2015-02-03 09:31:27 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-02-07 04:19:48 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2015-03-17 01:08:46 +08:00
|
|
|
|
|
|
|
#include <string>
|
2015-02-03 09:31:27 +08:00
|
|
|
|
2015-03-14 14:06:01 +08:00
|
|
|
#include "test_utils.h"
|
2015-02-07 04:19:48 +08:00
|
|
|
|
2015-02-03 09:31:27 +08:00
|
|
|
TEST(file, ReadFileToString_ENOENT) {
|
|
|
|
std::string s("hello");
|
|
|
|
errno = 0;
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
|
2015-02-03 09:31:27 +08:00
|
|
|
EXPECT_EQ(ENOENT, errno);
|
2015-03-17 01:08:46 +08:00
|
|
|
EXPECT_EQ("", s); // s was cleared.
|
2015-02-03 09:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(file, ReadFileToString_success) {
|
|
|
|
std::string s("hello");
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno;
|
2015-02-03 09:31:27 +08:00
|
|
|
EXPECT_GT(s.length(), 6U);
|
|
|
|
EXPECT_EQ('\n', s[s.length() - 1]);
|
|
|
|
s[5] = 0;
|
|
|
|
EXPECT_STREQ("Linux", s.c_str());
|
|
|
|
}
|
2015-02-07 04:19:48 +08:00
|
|
|
|
|
|
|
TEST(file, WriteStringToFile) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno;
|
2015-02-07 04:19:48 +08:00
|
|
|
std::string s;
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
|
2015-02-07 04:19:48 +08:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
|
|
|
|
2015-02-18 02:16:04 +08:00
|
|
|
TEST(file, WriteStringToFile2) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660,
|
|
|
|
getuid(), getgid()))
|
|
|
|
<< errno;
|
2015-02-18 02:16:04 +08:00
|
|
|
struct stat sb;
|
|
|
|
ASSERT_EQ(0, stat(tf.filename, &sb));
|
2015-05-01 06:12:21 +08:00
|
|
|
ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
|
2015-02-18 02:16:04 +08:00
|
|
|
ASSERT_EQ(getuid(), sb.st_uid);
|
|
|
|
ASSERT_EQ(getgid(), sb.st_gid);
|
|
|
|
std::string s;
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
|
2015-02-18 02:16:04 +08:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
TEST(file, WriteStringToFd) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
|
2015-02-07 04:19:48 +08:00
|
|
|
|
|
|
|
ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
|
|
|
|
|
|
|
|
std::string s;
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno;
|
2015-02-07 04:19:48 +08:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
2015-04-25 12:57:16 +08:00
|
|
|
|
|
|
|
TEST(file, ReadFully) {
|
|
|
|
int fd = open("/proc/version", O_RDONLY);
|
|
|
|
ASSERT_NE(-1, fd) << strerror(errno);
|
|
|
|
|
|
|
|
char buf[1024];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
ASSERT_TRUE(android::base::ReadFully(fd, buf, 5));
|
|
|
|
ASSERT_STREQ("Linux", buf);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
|
|
|
|
|
|
|
|
ASSERT_FALSE(android::base::ReadFully(fd, buf, sizeof(buf)));
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(file, WriteFully) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
|
|
|
|
std::string s;
|
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
|
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|