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 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 6 | #include "gtest/gtest.h" |
| 7 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 8 | #include "aos/realtime.h" |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 9 | #include "aos/testing/tmpdir.h" |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [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) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 17 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 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) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 33 | const std::string tmpdir(aos::testing::TestTmpDir()); |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 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) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 46 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 47 | const ::std::string test_file = tmpdir + "/test_file"; |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 48 | // Make sure to include a string long enough to avoid small string |
| 49 | // optimization. |
| 50 | ASSERT_EQ(0, system(("echo 123456789 > " + test_file).c_str())); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 51 | |
| 52 | FileReader reader(test_file); |
| 53 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 54 | aos::ScopedRealtime realtime; |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 55 | { |
| 56 | std::array<char, 20> contents; |
| 57 | absl::Span<char> read_result = |
| 58 | reader.ReadContents({contents.data(), contents.size()}); |
| 59 | EXPECT_EQ("123456789\n", |
| 60 | std::string_view(read_result.data(), read_result.size())); |
| 61 | } |
| 62 | { |
| 63 | std::optional<std::array<char, 10>> read_result = reader.ReadString<10>(); |
| 64 | ASSERT_TRUE(read_result.has_value()); |
| 65 | EXPECT_EQ("123456789\n", |
| 66 | std::string_view(read_result->data(), read_result->size())); |
| 67 | } |
| 68 | EXPECT_EQ(123456789, reader.ReadInt32()); |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Tests that we can write to a file without malloc'ing. |
| 72 | TEST(FileTest, WriteNormalFileNoMalloc) { |
| 73 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
| 74 | const ::std::string test_file = tmpdir + "/test_file"; |
| 75 | |
| 76 | FileWriter writer(test_file); |
| 77 | |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 78 | FileWriter::WriteResult result; |
| 79 | { |
| 80 | aos::ScopedRealtime realtime; |
| 81 | result = writer.WriteBytes("123456789"); |
| 82 | } |
| 83 | EXPECT_EQ(9, result.bytes_written); |
| 84 | EXPECT_EQ(9, result.return_code); |
| 85 | EXPECT_EQ("123456789", ReadFileToStringOrDie(test_file)); |
| 86 | } |
| 87 | |
| 88 | // Tests that if we fail to write a file that the error code propagates |
| 89 | // correctly. |
| 90 | TEST(FileTest, WriteFileError) { |
| 91 | const ::std::string tmpdir(aos::testing::TestTmpDir()); |
| 92 | const ::std::string test_file = tmpdir + "/test_file"; |
| 93 | |
| 94 | // Open with only read permissions; this should cause things to fail. |
| 95 | FileWriter writer(test_file, S_IRUSR); |
| 96 | |
| 97 | // Mess up the file management by closing the file descriptor. |
| 98 | PCHECK(0 == close(writer.fd())); |
| 99 | |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 100 | FileWriter::WriteResult result; |
| 101 | { |
| 102 | aos::ScopedRealtime realtime; |
| 103 | result = writer.WriteBytes("123456789"); |
| 104 | } |
| 105 | EXPECT_EQ(0, result.bytes_written); |
| 106 | EXPECT_EQ(-1, result.return_code); |
| 107 | EXPECT_EQ("", ReadFileToStringOrDie(test_file)); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 110 | } // namespace testing |
| 111 | } // namespace util |
| 112 | } // namespace aos |