summaryrefslogtreecommitdiffstats
path: root/libutils/tests/file_test.cpp
diff options
context:
space:
mode:
authorNicolas Geoffray <ngeoffray@google.com>2015-03-16 10:29:36 +0000
committerNicolas Geoffray <ngeoffray@google.com>2015-03-16 11:07:29 +0000
commita7870d88167f619e758b5bcd15b410d16da7c16b (patch)
tree8db9bc4aa940a8b3a44cf31dacd3a7f932eadc80 /libutils/tests/file_test.cpp
parentacf0a8b3f632fe90132b9ce457a2237160f8cb9a (diff)
downloadsystem_core-a7870d88167f619e758b5bcd15b410d16da7c16b.tar.gz
system_core-a7870d88167f619e758b5bcd15b410d16da7c16b.tar.bz2
system_core-a7870d88167f619e758b5bcd15b410d16da7c16b.zip
Revert "Create libbase."
Breaks internal master. This reverts commit 98ff77204cef9bb8f0f27420833233622060a09e. Change-Id: I18dc6021cb43efff8aa88486c2d980dc2b8eedba
Diffstat (limited to 'libutils/tests/file_test.cpp')
-rw-r--r--libutils/tests/file_test.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/libutils/tests/file_test.cpp b/libutils/tests/file_test.cpp
new file mode 100644
index 000000000..cea18b617
--- /dev/null
+++ b/libutils/tests/file_test.cpp
@@ -0,0 +1,98 @@
+/*
+ * 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.
+ */
+
+#include "utils/file.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <gtest/gtest.h>
+
+class TemporaryFile {
+ public:
+ TemporaryFile() {
+ init("/data/local/tmp");
+ if (fd == -1) {
+ init("/tmp");
+ }
+ }
+
+ ~TemporaryFile() {
+ close(fd);
+ unlink(filename);
+ }
+
+ int fd;
+ char filename[1024];
+
+ private:
+ void init(const char* tmp_dir) {
+ snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
+ fd = mkstemp(filename);
+ }
+};
+
+TEST(file, ReadFileToString_ENOENT) {
+ std::string s("hello");
+ errno = 0;
+ ASSERT_FALSE(android::ReadFileToString("/proc/does-not-exist", &s));
+ EXPECT_EQ(ENOENT, errno);
+ EXPECT_EQ("", s); // s was cleared.
+}
+
+TEST(file, ReadFileToString_success) {
+ std::string s("hello");
+ ASSERT_TRUE(android::ReadFileToString("/proc/version", &s)) << errno;
+ EXPECT_GT(s.length(), 6U);
+ EXPECT_EQ('\n', s[s.length() - 1]);
+ s[5] = 0;
+ EXPECT_STREQ("Linux", s.c_str());
+}
+
+TEST(file, WriteStringToFile) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::WriteStringToFile("abc", tf.filename)) << errno;
+ std::string s;
+ ASSERT_TRUE(android::ReadFileToString(tf.filename, &s)) << errno;
+ EXPECT_EQ("abc", s);
+}
+
+TEST(file, WriteStringToFile2) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::WriteStringToFile("abc", tf.filename, 0660, getuid(), getgid())) << errno;
+ struct stat sb;
+ ASSERT_EQ(0, stat(tf.filename, &sb));
+ ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT));
+ ASSERT_EQ(getuid(), sb.st_uid);
+ ASSERT_EQ(getgid(), sb.st_gid);
+ std::string s;
+ ASSERT_TRUE(android::ReadFileToString(tf.filename, &s)) << errno;
+ EXPECT_EQ("abc", s);
+}
+
+TEST(file, WriteStringToFd) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::WriteStringToFd("abc", tf.fd));
+
+ ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
+
+ std::string s;
+ ASSERT_TRUE(android::ReadFdToString(tf.fd, &s)) << errno;
+ EXPECT_EQ("abc", s);
+}