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 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 6 | #include "aos/realtime.h" |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame^] | 7 | #include "aos/testing/tmpdir.h" |
| 8 | #include "gtest/gtest.h" |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 9 | |
| 10 | DECLARE_bool(die_on_malloc); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 11 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 12 | namespace aos { |
| 13 | namespace util { |
| 14 | namespace testing { |
| 15 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 16 | // Basic test of reading a normal file. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | TEST(FileTest, ReadNormalFile) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame^] | 18 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 19 | const ::std::string test_file = tmpdir + "/test_file"; |
| 20 | ASSERT_EQ(0, system(("echo contents > " + test_file).c_str())); |
| 21 | EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file)); |
| 22 | } |
| 23 | |
| 24 | // Tests reading a file with 0 size, among other weird things. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | TEST(FileTest, ReadSpecialFile) { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 26 | const ::std::string stat = ReadFileToStringOrDie("/proc/self/stat"); |
| 27 | EXPECT_EQ('\n', stat[stat.size() - 1]); |
| 28 | const ::std::string my_pid = ::std::to_string(getpid()); |
| 29 | EXPECT_EQ(my_pid, stat.substr(0, my_pid.size())); |
| 30 | } |
| 31 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 32 | // Tests that the PathExists function works under normal conditions. |
| 33 | TEST(FileTest, PathExistsTest) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame^] | 34 | const std::string tmpdir(aos::testing::TestTmpDir()); |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 35 | const std::string test_file = tmpdir + "/test_file"; |
| 36 | // Make sure the test_file doesn't exist. |
| 37 | unlink(test_file.c_str()); |
| 38 | EXPECT_FALSE(PathExists(test_file)); |
| 39 | |
| 40 | WriteStringToFileOrDie(test_file, "abc"); |
| 41 | |
| 42 | EXPECT_TRUE(PathExists(test_file)); |
| 43 | } |
| 44 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 45 | // Basic test of reading a normal file. |
| 46 | TEST(FileTest, ReadNormalFileNoMalloc) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame^] | 47 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 48 | const ::std::string test_file = tmpdir + "/test_file"; |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame^] | 49 | // Make sure to include a string long enough to avoid small string |
| 50 | // optimization. |
| 51 | ASSERT_EQ(0, system(("echo 123456789 > " + test_file).c_str())); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 52 | |
| 53 | FileReader reader(test_file); |
| 54 | |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame^] | 55 | gflags::FlagSaver flag_saver; |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 56 | FLAGS_die_on_malloc = true; |
| 57 | RegisterMallocHook(); |
| 58 | aos::ScopedRealtime realtime; |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame^] | 59 | EXPECT_EQ("123456789\n", reader.ReadContents()); |
| 60 | EXPECT_EQ(123456789, reader.ReadInt()); |
| 61 | } |
| 62 | |
| 63 | // Tests that we can write to a file without malloc'ing. |
| 64 | TEST(FileTest, WriteNormalFileNoMalloc) { |
| 65 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
| 66 | const ::std::string test_file = tmpdir + "/test_file"; |
| 67 | |
| 68 | FileWriter writer(test_file); |
| 69 | |
| 70 | gflags::FlagSaver flag_saver; |
| 71 | FLAGS_die_on_malloc = true; |
| 72 | RegisterMallocHook(); |
| 73 | FileWriter::WriteResult result; |
| 74 | { |
| 75 | aos::ScopedRealtime realtime; |
| 76 | result = writer.WriteBytes("123456789"); |
| 77 | } |
| 78 | EXPECT_EQ(9, result.bytes_written); |
| 79 | EXPECT_EQ(9, result.return_code); |
| 80 | EXPECT_EQ("123456789", ReadFileToStringOrDie(test_file)); |
| 81 | } |
| 82 | |
| 83 | // Tests that if we fail to write a file that the error code propagates |
| 84 | // correctly. |
| 85 | TEST(FileTest, WriteFileError) { |
| 86 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
| 87 | const ::std::string test_file = tmpdir + "/test_file"; |
| 88 | |
| 89 | // Open with only read permissions; this should cause things to fail. |
| 90 | FileWriter writer(test_file, S_IRUSR); |
| 91 | |
| 92 | // Mess up the file management by closing the file descriptor. |
| 93 | PCHECK(0 == close(writer.fd())); |
| 94 | |
| 95 | gflags::FlagSaver flag_saver; |
| 96 | FLAGS_die_on_malloc = true; |
| 97 | RegisterMallocHook(); |
| 98 | FileWriter::WriteResult result; |
| 99 | { |
| 100 | aos::ScopedRealtime realtime; |
| 101 | result = writer.WriteBytes("123456789"); |
| 102 | } |
| 103 | EXPECT_EQ(0, result.bytes_written); |
| 104 | EXPECT_EQ(-1, result.return_code); |
| 105 | EXPECT_EQ("", ReadFileToStringOrDie(test_file)); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 108 | } // namespace testing |
| 109 | } // namespace util |
| 110 | } // namespace aos |