blob: 7c93fb39d35faa744f99ff5a2ba89202534fe434 [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
6#include "gtest/gtest.h"
7
Brian Silverman61175fb2016-03-13 15:35:56 -04008namespace aos {
9namespace util {
10namespace testing {
11
Brian Silverman61175fb2016-03-13 15:35:56 -040012// Basic test of reading a normal file.
Alex Perrycb7da4b2019-08-28 19:35:56 -070013TEST(FileTest, ReadNormalFile) {
Brian Silverman61175fb2016-03-13 15:35:56 -040014 const ::std::string tmpdir(getenv("TEST_TMPDIR"));
15 const ::std::string test_file = tmpdir + "/test_file";
16 ASSERT_EQ(0, system(("echo contents > " + test_file).c_str()));
17 EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file));
18}
19
20// Tests reading a file with 0 size, among other weird things.
Alex Perrycb7da4b2019-08-28 19:35:56 -070021TEST(FileTest, ReadSpecialFile) {
Brian Silverman61175fb2016-03-13 15:35:56 -040022 const ::std::string stat = ReadFileToStringOrDie("/proc/self/stat");
23 EXPECT_EQ('\n', stat[stat.size() - 1]);
24 const ::std::string my_pid = ::std::to_string(getpid());
25 EXPECT_EQ(my_pid, stat.substr(0, my_pid.size()));
26}
27
James Kuszmaulf8178092020-05-10 18:46:45 -070028// Tests that the PathExists function works under normal conditions.
29TEST(FileTest, PathExistsTest) {
30 const std::string tmpdir(getenv("TEST_TMPDIR"));
31 const std::string test_file = tmpdir + "/test_file";
32 // Make sure the test_file doesn't exist.
33 unlink(test_file.c_str());
34 EXPECT_FALSE(PathExists(test_file));
35
36 WriteStringToFileOrDie(test_file, "abc");
37
38 EXPECT_TRUE(PathExists(test_file));
39}
40
Brian Silverman61175fb2016-03-13 15:35:56 -040041} // namespace testing
42} // namespace util
43} // namespace aos