blob: 9b0def046de835998f19b3bb7520d24d82c2e047 [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
10DECLARE_bool(die_on_malloc);
Brian Silverman61175fb2016-03-13 15:35:56 -040011
Brian Silverman61175fb2016-03-13 15:35:56 -040012namespace aos {
13namespace util {
14namespace testing {
15
Brian Silverman61175fb2016-03-13 15:35:56 -040016// Basic test of reading a normal file.
Alex Perrycb7da4b2019-08-28 19:35:56 -070017TEST(FileTest, ReadNormalFile) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080018 const ::std::string tmpdir(aos::testing::TestTmpDir());
Brian Silverman61175fb2016-03-13 15:35:56 -040019 const ::std::string test_file = tmpdir + "/test_file";
20 ASSERT_EQ(0, system(("echo contents > " + test_file).c_str()));
21 EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file));
22}
23
24// Tests reading a file with 0 size, among other weird things.
Alex Perrycb7da4b2019-08-28 19:35:56 -070025TEST(FileTest, ReadSpecialFile) {
Brian Silverman61175fb2016-03-13 15:35:56 -040026 const ::std::string stat = ReadFileToStringOrDie("/proc/self/stat");
27 EXPECT_EQ('\n', stat[stat.size() - 1]);
28 const ::std::string my_pid = ::std::to_string(getpid());
29 EXPECT_EQ(my_pid, stat.substr(0, my_pid.size()));
30}
31
James Kuszmaulf8178092020-05-10 18:46:45 -070032// Tests that the PathExists function works under normal conditions.
33TEST(FileTest, PathExistsTest) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080034 const std::string tmpdir(aos::testing::TestTmpDir());
James Kuszmaulf8178092020-05-10 18:46:45 -070035 const std::string test_file = tmpdir + "/test_file";
36 // Make sure the test_file doesn't exist.
37 unlink(test_file.c_str());
38 EXPECT_FALSE(PathExists(test_file));
39
40 WriteStringToFileOrDie(test_file, "abc");
41
42 EXPECT_TRUE(PathExists(test_file));
43}
44
James Kuszmaul0625b0d2022-09-21 11:38:48 -070045// Basic test of reading a normal file.
46TEST(FileTest, ReadNormalFileNoMalloc) {
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080047 const ::std::string tmpdir(aos::testing::TestTmpDir());
James Kuszmaul0625b0d2022-09-21 11:38:48 -070048 const ::std::string test_file = tmpdir + "/test_file";
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080049 // Make sure to include a string long enough to avoid small string
50 // optimization.
51 ASSERT_EQ(0, system(("echo 123456789 > " + test_file).c_str()));
James Kuszmaul0625b0d2022-09-21 11:38:48 -070052
53 FileReader reader(test_file);
54
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080055 gflags::FlagSaver flag_saver;
James Kuszmaul0625b0d2022-09-21 11:38:48 -070056 FLAGS_die_on_malloc = true;
57 RegisterMallocHook();
58 aos::ScopedRealtime realtime;
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080059 EXPECT_EQ("123456789\n", reader.ReadContents());
60 EXPECT_EQ(123456789, reader.ReadInt());
61}
62
63// Tests that we can write to a file without malloc'ing.
64TEST(FileTest, WriteNormalFileNoMalloc) {
65 const ::std::string tmpdir(aos::testing::TestTmpDir());
66 const ::std::string test_file = tmpdir + "/test_file";
67
68 FileWriter writer(test_file);
69
70 gflags::FlagSaver flag_saver;
71 FLAGS_die_on_malloc = true;
72 RegisterMallocHook();
73 FileWriter::WriteResult result;
74 {
75 aos::ScopedRealtime realtime;
76 result = writer.WriteBytes("123456789");
77 }
78 EXPECT_EQ(9, result.bytes_written);
79 EXPECT_EQ(9, result.return_code);
80 EXPECT_EQ("123456789", ReadFileToStringOrDie(test_file));
81}
82
83// Tests that if we fail to write a file that the error code propagates
84// correctly.
85TEST(FileTest, WriteFileError) {
86 const ::std::string tmpdir(aos::testing::TestTmpDir());
87 const ::std::string test_file = tmpdir + "/test_file";
88
89 // Open with only read permissions; this should cause things to fail.
90 FileWriter writer(test_file, S_IRUSR);
91
92 // Mess up the file management by closing the file descriptor.
93 PCHECK(0 == close(writer.fd()));
94
95 gflags::FlagSaver flag_saver;
96 FLAGS_die_on_malloc = true;
97 RegisterMallocHook();
98 FileWriter::WriteResult result;
99 {
100 aos::ScopedRealtime realtime;
101 result = writer.WriteBytes("123456789");
102 }
103 EXPECT_EQ(0, result.bytes_written);
104 EXPECT_EQ(-1, result.return_code);
105 EXPECT_EQ("", ReadFileToStringOrDie(test_file));
James Kuszmaul0625b0d2022-09-21 11:38:48 -0700106}
107
Brian Silverman61175fb2016-03-13 15:35:56 -0400108} // namespace testing
109} // namespace util
110} // namespace aos