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