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> |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 4 | #include <optional> |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 7 | #include "gmock/gmock-matchers.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 8 | #include "gtest/gtest.h" |
| 9 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 10 | #include "aos/realtime.h" |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 11 | #include "aos/testing/tmpdir.h" |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 12 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 13 | namespace aos { |
| 14 | namespace util { |
| 15 | namespace testing { |
| 16 | |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 17 | using ::testing::ElementsAre; |
| 18 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 19 | // Basic test of reading a normal file. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | TEST(FileTest, ReadNormalFile) { |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 21 | const std::string tmpdir(aos::testing::TestTmpDir()); |
| 22 | const std::string test_file = tmpdir + "/test_file"; |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 23 | ASSERT_EQ(0, system(("echo contents > " + test_file).c_str())); |
| 24 | EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file)); |
| 25 | } |
| 26 | |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 27 | // Basic test of reading a normal file. |
| 28 | TEST(FileTest, ReadNormalFileToBytes) { |
| 29 | const std::string tmpdir(aos::testing::TestTmpDir()); |
| 30 | const std::string test_file = tmpdir + "/test_file"; |
| 31 | ASSERT_EQ(0, system(("echo contents > " + test_file).c_str())); |
| 32 | EXPECT_THAT(ReadFileToVecOrDie(test_file), |
| 33 | ElementsAre('c', 'o', 'n', 't', 'e', 'n', 't', 's', '\n')); |
| 34 | } |
| 35 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 36 | // Tests reading a file with 0 size, among other weird things. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 37 | TEST(FileTest, ReadSpecialFile) { |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 38 | const std::string stat = ReadFileToStringOrDie("/proc/self/stat"); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 39 | EXPECT_EQ('\n', stat[stat.size() - 1]); |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 40 | const std::string my_pid = ::std::to_string(getpid()); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 41 | EXPECT_EQ(my_pid, stat.substr(0, my_pid.size())); |
| 42 | } |
| 43 | |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 44 | // Basic test of maybe reading a normal file. |
| 45 | TEST(FileTest, MaybeReadNormalFile) { |
| 46 | const std::string tmpdir(aos::testing::TestTmpDir()); |
| 47 | const std::string test_file = tmpdir + "/test_file"; |
| 48 | ASSERT_EQ(0, system(("echo contents > " + test_file).c_str())); |
| 49 | EXPECT_EQ("contents\n", MaybeReadFileToString(test_file).value()); |
| 50 | } |
| 51 | |
| 52 | // Tests maybe reading a file with 0 size, among other weird things. |
| 53 | TEST(FileTest, MaybeReadSpecialFile) { |
| 54 | const std::optional<std::string> stat = |
| 55 | MaybeReadFileToString("/proc/self/stat"); |
| 56 | ASSERT_TRUE(stat.has_value()); |
| 57 | EXPECT_EQ('\n', (*stat)[stat->size() - 1]); |
| 58 | const std::string my_pid = std::to_string(getpid()); |
| 59 | EXPECT_EQ(my_pid, stat->substr(0, my_pid.size())); |
| 60 | } |
| 61 | |
| 62 | // Tests maybe reading a non-existent file, and not fatally erroring. |
| 63 | TEST(FileTest, MaybeReadNonexistentFile) { |
| 64 | const std::optional<std::string> contents = MaybeReadFileToString("/dne"); |
| 65 | ASSERT_FALSE(contents.has_value()); |
| 66 | } |
| 67 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 68 | // Tests that the PathExists function works under normal conditions. |
| 69 | TEST(FileTest, PathExistsTest) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 70 | const std::string tmpdir(aos::testing::TestTmpDir()); |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 71 | const std::string test_file = tmpdir + "/test_file"; |
| 72 | // Make sure the test_file doesn't exist. |
| 73 | unlink(test_file.c_str()); |
| 74 | EXPECT_FALSE(PathExists(test_file)); |
| 75 | |
| 76 | WriteStringToFileOrDie(test_file, "abc"); |
| 77 | |
| 78 | EXPECT_TRUE(PathExists(test_file)); |
| 79 | } |
| 80 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 81 | // Basic test of reading a normal file. |
| 82 | TEST(FileTest, ReadNormalFileNoMalloc) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 83 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 84 | const ::std::string test_file = tmpdir + "/test_file"; |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 85 | // Make sure to include a string long enough to avoid small string |
| 86 | // optimization. |
| 87 | ASSERT_EQ(0, system(("echo 123456789 > " + test_file).c_str())); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 88 | |
| 89 | FileReader reader(test_file); |
| 90 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 91 | aos::ScopedRealtime realtime; |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 92 | { |
| 93 | std::array<char, 20> contents; |
| 94 | absl::Span<char> read_result = |
| 95 | reader.ReadContents({contents.data(), contents.size()}); |
| 96 | EXPECT_EQ("123456789\n", |
| 97 | std::string_view(read_result.data(), read_result.size())); |
| 98 | } |
| 99 | { |
| 100 | std::optional<std::array<char, 10>> read_result = reader.ReadString<10>(); |
| 101 | ASSERT_TRUE(read_result.has_value()); |
| 102 | EXPECT_EQ("123456789\n", |
| 103 | std::string_view(read_result->data(), read_result->size())); |
| 104 | } |
| 105 | EXPECT_EQ(123456789, reader.ReadInt32()); |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Tests that we can write to a file without malloc'ing. |
| 109 | TEST(FileTest, WriteNormalFileNoMalloc) { |
| 110 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
| 111 | const ::std::string test_file = tmpdir + "/test_file"; |
| 112 | |
| 113 | FileWriter writer(test_file); |
| 114 | |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 115 | FileWriter::WriteResult result; |
| 116 | { |
| 117 | aos::ScopedRealtime realtime; |
| 118 | result = writer.WriteBytes("123456789"); |
| 119 | } |
| 120 | EXPECT_EQ(9, result.bytes_written); |
| 121 | EXPECT_EQ(9, result.return_code); |
| 122 | EXPECT_EQ("123456789", ReadFileToStringOrDie(test_file)); |
| 123 | } |
| 124 | |
| 125 | // Tests that if we fail to write a file that the error code propagates |
| 126 | // correctly. |
| 127 | TEST(FileTest, WriteFileError) { |
| 128 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
| 129 | const ::std::string test_file = tmpdir + "/test_file"; |
| 130 | |
| 131 | // Open with only read permissions; this should cause things to fail. |
| 132 | FileWriter writer(test_file, S_IRUSR); |
| 133 | |
| 134 | // Mess up the file management by closing the file descriptor. |
| 135 | PCHECK(0 == close(writer.fd())); |
| 136 | |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 137 | FileWriter::WriteResult result; |
| 138 | { |
| 139 | aos::ScopedRealtime realtime; |
| 140 | result = writer.WriteBytes("123456789"); |
| 141 | } |
| 142 | EXPECT_EQ(0, result.bytes_written); |
| 143 | EXPECT_EQ(-1, result.return_code); |
| 144 | EXPECT_EQ("", ReadFileToStringOrDie(test_file)); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 147 | } // namespace testing |
| 148 | } // namespace util |
| 149 | } // namespace aos |