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 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <cstdlib> |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 4 | #include <string> |
| 5 | |
| 6 | #include "gtest/gtest.h" |
| 7 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 8 | namespace aos { |
| 9 | namespace util { |
| 10 | namespace testing { |
| 11 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 12 | // Basic test of reading a normal file. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | TEST(FileTest, ReadNormalFile) { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 14 | const ::std::string tmpdir(getenv("TEST_TMPDIR")); |
| 15 | const ::std::string test_file = tmpdir + "/test_file"; |
| 16 | ASSERT_EQ(0, system(("echo contents > " + test_file).c_str())); |
| 17 | EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file)); |
| 18 | } |
| 19 | |
| 20 | // Tests reading a file with 0 size, among other weird things. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | TEST(FileTest, ReadSpecialFile) { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 22 | const ::std::string stat = ReadFileToStringOrDie("/proc/self/stat"); |
| 23 | EXPECT_EQ('\n', stat[stat.size() - 1]); |
| 24 | const ::std::string my_pid = ::std::to_string(getpid()); |
| 25 | EXPECT_EQ(my_pid, stat.substr(0, my_pid.size())); |
| 26 | } |
| 27 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 28 | // Tests that the PathExists function works under normal conditions. |
| 29 | TEST(FileTest, PathExistsTest) { |
| 30 | const std::string tmpdir(getenv("TEST_TMPDIR")); |
| 31 | const std::string test_file = tmpdir + "/test_file"; |
| 32 | // Make sure the test_file doesn't exist. |
| 33 | unlink(test_file.c_str()); |
| 34 | EXPECT_FALSE(PathExists(test_file)); |
| 35 | |
| 36 | WriteStringToFileOrDie(test_file, "abc"); |
| 37 | |
| 38 | EXPECT_TRUE(PathExists(test_file)); |
| 39 | } |
| 40 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 41 | } // namespace testing |
| 42 | } // namespace util |
| 43 | } // namespace aos |