blob: 712447ecb20d94ac307f251daaa1e6811fb2a9ce [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/util/file.h"
Brian Silverman61175fb2016-03-13 15:35:56 -04002
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cstdlib>
Brian Silverman61175fb2016-03-13 15:35:56 -04004#include <string>
5
Philipp Schrader790cb542023-07-05 21:06:52 -07006#include "gtest/gtest.h"
7
James Kuszmaul0625b0d2022-09-21 11:38:48 -07008#include "aos/realtime.h"
James Kuszmaulfd43f4e2022-12-16 15:19:35 -08009#include "aos/testing/tmpdir.h"
James Kuszmaul0625b0d2022-09-21 11:38:48 -070010
Brian Silverman61175fb2016-03-13 15:35:56 -040011namespace aos {
12namespace util {
13namespace testing {
14
Brian Silverman61175fb2016-03-13 15:35:56 -040015// Basic test of reading a normal file.
Alex Perrycb7da4b2019-08-28 19:35:56 -070016TEST(FileTest, ReadNormalFile) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080017 const ::std::string tmpdir(aos::testing::TestTmpDir());
Brian Silverman61175fb2016-03-13 15:35:56 -040018 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 Perrycb7da4b2019-08-28 19:35:56 -070024TEST(FileTest, ReadSpecialFile) {
Brian Silverman61175fb2016-03-13 15:35:56 -040025 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 Kuszmaulf8178092020-05-10 18:46:45 -070031// Tests that the PathExists function works under normal conditions.
32TEST(FileTest, PathExistsTest) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080033 const std::string tmpdir(aos::testing::TestTmpDir());
James Kuszmaulf8178092020-05-10 18:46:45 -070034 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 Kuszmaul0625b0d2022-09-21 11:38:48 -070044// Basic test of reading a normal file.
45TEST(FileTest, ReadNormalFileNoMalloc) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080046 const ::std::string tmpdir(aos::testing::TestTmpDir());
James Kuszmaul0625b0d2022-09-21 11:38:48 -070047 const ::std::string test_file = tmpdir + "/test_file";
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080048 // 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 Kuszmaul0625b0d2022-09-21 11:38:48 -070051
52 FileReader reader(test_file);
53
James Kuszmaul0625b0d2022-09-21 11:38:48 -070054 aos::ScopedRealtime realtime;
James Kuszmaul5a88d412023-01-27 15:55:55 -080055 {
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 Kuszmaulfd43f4e2022-12-16 15:19:35 -080069}
70
71// Tests that we can write to a file without malloc'ing.
72TEST(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 Kuszmaulfd43f4e2022-12-16 15:19:35 -080078 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.
90TEST(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 Kuszmaulfd43f4e2022-12-16 15:19:35 -0800100 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 Kuszmaul0625b0d2022-09-21 11:38:48 -0700108}
109
Brian Silverman61175fb2016-03-13 15:35:56 -0400110} // namespace testing
111} // namespace util
112} // namespace aos