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