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" |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame^] | 7 | #include "aos/realtime.h" |
| 8 | |
| 9 | DECLARE_bool(die_on_malloc); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 10 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 11 | namespace aos { |
| 12 | namespace util { |
| 13 | namespace testing { |
| 14 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 15 | // Basic test of reading a normal file. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | TEST(FileTest, ReadNormalFile) { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 17 | const ::std::string tmpdir(getenv("TEST_TMPDIR")); |
| 18 | const ::std::string test_file = tmpdir + "/test_file"; |
| 19 | ASSERT_EQ(0, system(("echo contents > " + test_file).c_str())); |
| 20 | EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file)); |
| 21 | } |
| 22 | |
| 23 | // Tests reading a file with 0 size, among other weird things. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | TEST(FileTest, ReadSpecialFile) { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 25 | const ::std::string stat = ReadFileToStringOrDie("/proc/self/stat"); |
| 26 | EXPECT_EQ('\n', stat[stat.size() - 1]); |
| 27 | const ::std::string my_pid = ::std::to_string(getpid()); |
| 28 | EXPECT_EQ(my_pid, stat.substr(0, my_pid.size())); |
| 29 | } |
| 30 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 31 | // Tests that the PathExists function works under normal conditions. |
| 32 | TEST(FileTest, PathExistsTest) { |
| 33 | const std::string tmpdir(getenv("TEST_TMPDIR")); |
| 34 | const std::string test_file = tmpdir + "/test_file"; |
| 35 | // Make sure the test_file doesn't exist. |
| 36 | unlink(test_file.c_str()); |
| 37 | EXPECT_FALSE(PathExists(test_file)); |
| 38 | |
| 39 | WriteStringToFileOrDie(test_file, "abc"); |
| 40 | |
| 41 | EXPECT_TRUE(PathExists(test_file)); |
| 42 | } |
| 43 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame^] | 44 | // Basic test of reading a normal file. |
| 45 | TEST(FileTest, ReadNormalFileNoMalloc) { |
| 46 | const ::std::string tmpdir(getenv("TEST_TMPDIR")); |
| 47 | const ::std::string test_file = tmpdir + "/test_file"; |
| 48 | ASSERT_EQ(0, system(("echo 971 > " + test_file).c_str())); |
| 49 | |
| 50 | FileReader reader(test_file); |
| 51 | |
| 52 | FLAGS_die_on_malloc = true; |
| 53 | RegisterMallocHook(); |
| 54 | aos::ScopedRealtime realtime; |
| 55 | EXPECT_EQ("971\n", reader.ReadContents()); |
| 56 | EXPECT_EQ(971, reader.ReadInt()); |
| 57 | } |
| 58 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 59 | } // namespace testing |
| 60 | } // namespace util |
| 61 | } // namespace aos |