2015-02-03 01:31:27 +00: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-12-05 06:00:26 +00:00
|
|
|
#include "android-base/file.h"
|
2015-03-16 17:08:46 +00:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2015-02-03 01:31:27 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-02-06 20:19:48 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2015-03-16 17:08:46 +00:00
|
|
|
|
|
|
|
#include <string>
|
2015-02-03 01:31:27 +00:00
|
|
|
|
2015-12-05 06:00:26 +00:00
|
|
|
#include "android-base/test_utils.h"
|
2015-02-06 20:19:48 +00:00
|
|
|
|
2015-02-03 01:31:27 +00:00
|
|
|
TEST(file, ReadFileToString_ENOENT) {
|
|
|
|
std::string s("hello");
|
|
|
|
errno = 0;
|
2015-03-16 17:08:46 +00:00
|
|
|
ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
|
2015-02-03 01:31:27 +00:00
|
|
|
EXPECT_EQ(ENOENT, errno);
|
2015-03-16 17:08:46 +00:00
|
|
|
EXPECT_EQ("", s); // s was cleared.
|
2015-02-03 01:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 22:36:28 +00:00
|
|
|
TEST(file, ReadFileToString_WriteStringToFile) {
|
2015-02-06 20:19:48 +00:00
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-05-06 20:26:00 +00:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
|
2015-04-30 00:09:53 +00:00
|
|
|
<< strerror(errno);
|
2015-02-06 20:19:48 +00:00
|
|
|
std::string s;
|
2015-05-06 20:26:00 +00:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
|
2015-04-30 00:09:53 +00:00
|
|
|
<< strerror(errno);
|
2015-02-06 20:19:48 +00:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:23 +00:00
|
|
|
// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
|
|
|
|
// sense on Windows.
|
|
|
|
#if !defined(_WIN32)
|
2015-02-17 18:16:04 +00:00
|
|
|
TEST(file, WriteStringToFile2) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-05-06 20:26:00 +00:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
|
2015-03-16 17:08:46 +00:00
|
|
|
getuid(), getgid()))
|
2015-04-30 00:09:53 +00:00
|
|
|
<< strerror(errno);
|
2015-02-17 18:16:04 +00:00
|
|
|
struct stat sb;
|
2015-05-06 20:26:00 +00:00
|
|
|
ASSERT_EQ(0, stat(tf.path, &sb));
|
2015-04-30 22:12:21 +00:00
|
|
|
ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
|
2015-02-17 18:16:04 +00:00
|
|
|
ASSERT_EQ(getuid(), sb.st_uid);
|
|
|
|
ASSERT_EQ(getgid(), sb.st_gid);
|
|
|
|
std::string s;
|
2015-05-06 20:26:00 +00:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
|
2015-04-30 00:09:53 +00:00
|
|
|
<< strerror(errno);
|
2015-02-17 18:16:04 +00:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
2015-04-29 18:32:23 +00:00
|
|
|
#endif
|
2015-02-17 18:16:04 +00:00
|
|
|
|
2015-02-06 20:19:48 +00:00
|
|
|
TEST(file, WriteStringToFd) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-03-16 17:08:46 +00:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
|
2015-02-06 20:19:48 +00:00
|
|
|
|
2015-04-30 00:09:53 +00:00
|
|
|
ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
|
2015-02-06 20:19:48 +00:00
|
|
|
|
|
|
|
std::string s;
|
2015-04-30 00:09:53 +00:00
|
|
|
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
|
2015-02-06 20:19:48 +00:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
2015-04-25 04:57:16 +00:00
|
|
|
|
|
|
|
TEST(file, WriteFully) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
|
2015-08-04 03:43:24 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
|
|
|
|
|
2015-04-25 04:57:16 +00:00
|
|
|
std::string s;
|
2015-08-04 03:43:24 +00:00
|
|
|
s.resize(3);
|
|
|
|
ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size()))
|
2015-04-30 00:09:53 +00:00
|
|
|
<< strerror(errno);
|
2015-04-25 04:57:16 +00:00
|
|
|
EXPECT_EQ("abc", s);
|
2015-08-04 03:43:24 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
|
|
|
|
|
|
|
|
s.resize(1024);
|
|
|
|
ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
|
2015-04-25 04:57:16 +00:00
|
|
|
}
|