blob: ba03ea550af2f146f5aebd6e326df1c0b3e76263 [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
James Kuszmaul0625b0d2022-09-21 11:38:48 -07006#include "aos/realtime.h"
James Kuszmaulfd43f4e2022-12-16 15:19:35 -08007#include "aos/testing/tmpdir.h"
8#include "gtest/gtest.h"
James Kuszmaul0625b0d2022-09-21 11:38:48 -07009
Brian Silverman61175fb2016-03-13 15:35:56 -040010namespace aos {
11namespace util {
12namespace testing {
13
Brian Silverman61175fb2016-03-13 15:35:56 -040014// Basic test of reading a normal file.
Alex Perrycb7da4b2019-08-28 19:35:56 -070015TEST(FileTest, ReadNormalFile) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080016 const ::std::string tmpdir(aos::testing::TestTmpDir());
Brian Silverman61175fb2016-03-13 15:35:56 -040017 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 Perrycb7da4b2019-08-28 19:35:56 -070023TEST(FileTest, ReadSpecialFile) {
Brian Silverman61175fb2016-03-13 15:35:56 -040024 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 Kuszmaulf8178092020-05-10 18:46:45 -070030// Tests that the PathExists function works under normal conditions.
31TEST(FileTest, PathExistsTest) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080032 const std::string tmpdir(aos::testing::TestTmpDir());
James Kuszmaulf8178092020-05-10 18:46:45 -070033 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 Kuszmaul0625b0d2022-09-21 11:38:48 -070043// Basic test of reading a normal file.
44TEST(FileTest, ReadNormalFileNoMalloc) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080045 const ::std::string tmpdir(aos::testing::TestTmpDir());
James Kuszmaul0625b0d2022-09-21 11:38:48 -070046 const ::std::string test_file = tmpdir + "/test_file";
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080047 // 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 Kuszmaul0625b0d2022-09-21 11:38:48 -070050
51 FileReader reader(test_file);
52
James Kuszmaul0625b0d2022-09-21 11:38:48 -070053 aos::ScopedRealtime realtime;
James Kuszmaul5a88d412023-01-27 15:55:55 -080054 {
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 Kuszmaulfd43f4e2022-12-16 15:19:35 -080068}
69
70// Tests that we can write to a file without malloc'ing.
71TEST(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 Kuszmaulfd43f4e2022-12-16 15:19:35 -080077 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.
89TEST(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 Kuszmaulfd43f4e2022-12-16 15:19:35 -080099 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 Kuszmaul0625b0d2022-09-21 11:38:48 -0700107}
108
Brian Silverman61175fb2016-03-13 15:35:56 -0400109} // namespace testing
110} // namespace util
111} // namespace aos