John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 1 | #include "aos/util/file.h" |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 2 | |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | #include <string> |
| 6 | |
| 7 | #include "gtest/gtest.h" |
| 8 | |
| 9 | #include "aos/testing/test_logging.h" |
| 10 | |
| 11 | namespace aos { |
| 12 | namespace util { |
| 13 | namespace testing { |
| 14 | |
| 15 | class FileTest : public ::testing::Test { |
| 16 | protected: |
| 17 | FileTest() { |
| 18 | ::aos::testing::EnableTestLogging(); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | // Basic test of reading a normal file. |
| 23 | TEST_F(FileTest, ReadNormalFile) { |
| 24 | const ::std::string tmpdir(getenv("TEST_TMPDIR")); |
| 25 | const ::std::string test_file = tmpdir + "/test_file"; |
| 26 | ASSERT_EQ(0, system(("echo contents > " + test_file).c_str())); |
| 27 | EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file)); |
| 28 | } |
| 29 | |
| 30 | // Tests reading a file with 0 size, among other weird things. |
| 31 | TEST_F(FileTest, ReadSpecialFile) { |
| 32 | const ::std::string stat = ReadFileToStringOrDie("/proc/self/stat"); |
| 33 | EXPECT_EQ('\n', stat[stat.size() - 1]); |
| 34 | const ::std::string my_pid = ::std::to_string(getpid()); |
| 35 | EXPECT_EQ(my_pid, stat.substr(0, my_pid.size())); |
| 36 | } |
| 37 | |
| 38 | } // namespace testing |
| 39 | } // namespace util |
| 40 | } // namespace aos |