summaryrefslogtreecommitdiffstats
path: root/libutils/tests
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2015-02-06 12:19:48 -0800
committerElliott Hughes <enh@google.com>2015-02-06 14:20:30 -0800
commitf682b4786a4093efb23bf80d69bf80eb274b145b (patch)
tree8af209a035867fd30720e6e6da1b8b581aab871d /libutils/tests
parentd558530ba90cb6218fe8e255c71a034c3fe1ea58 (diff)
downloadsystem_core-f682b4786a4093efb23bf80d69bf80eb274b145b.zip
system_core-f682b4786a4093efb23bf80d69bf80eb274b145b.tar.gz
system_core-f682b4786a4093efb23bf80d69bf80eb274b145b.tar.bz2
Clean up reading and writing in init.
This isn't particularly useful in and of itself, but it does introduce the first (trivial) unit test, improves the documentation (including details about how to debug init crashes), and made me aware of how unpleasant the existing parser is. I also fixed a bug in passing --- unless you thought the "peboot" and "pm" commands were features... Bug: 19217569 Change-Id: I6ab76129a543ce3ed3dab52ef2c638009874c3de
Diffstat (limited to 'libutils/tests')
-rw-r--r--libutils/tests/file_test.cpp51
1 files changed, 49 insertions, 2 deletions
diff --git a/libutils/tests/file_test.cpp b/libutils/tests/file_test.cpp
index acf66a6..3703a49 100644
--- a/libutils/tests/file_test.cpp
+++ b/libutils/tests/file_test.cpp
@@ -17,21 +17,68 @@
#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;
- EXPECT_FALSE(android::ReadFileToString("/proc/does-not-exist", &s));
+ 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");
- EXPECT_TRUE(android::ReadFileToString("/proc/version", &s));
+ 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, 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);
+}