blob: 6851302a65c492f98471fcb7a1c5dcc73ac024bb [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/util/file.h"
Brian Silverman61175fb2016-03-13 15:35:56 -04002
3#include <stdlib.h>
4
5#include <string>
6
7#include "gtest/gtest.h"
8
Brian Silverman61175fb2016-03-13 15:35:56 -04009namespace aos {
10namespace util {
11namespace testing {
12
Brian Silverman61175fb2016-03-13 15:35:56 -040013// Basic test of reading a normal file.
Alex Perrycb7da4b2019-08-28 19:35:56 -070014TEST(FileTest, ReadNormalFile) {
Brian Silverman61175fb2016-03-13 15:35:56 -040015 const ::std::string tmpdir(getenv("TEST_TMPDIR"));
16 const ::std::string test_file = tmpdir + "/test_file";
17 ASSERT_EQ(0, system(("echo contents > " + test_file).c_str()));
18 EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file));
19}
20
21// Tests reading a file with 0 size, among other weird things.
Alex Perrycb7da4b2019-08-28 19:35:56 -070022TEST(FileTest, ReadSpecialFile) {
Brian Silverman61175fb2016-03-13 15:35:56 -040023 const ::std::string stat = ReadFileToStringOrDie("/proc/self/stat");
24 EXPECT_EQ('\n', stat[stat.size() - 1]);
25 const ::std::string my_pid = ::std::to_string(getpid());
26 EXPECT_EQ(my_pid, stat.substr(0, my_pid.size()));
27}
28
James Kuszmaulf8178092020-05-10 18:46:45 -070029// Tests that the PathExists function works under normal conditions.
30TEST(FileTest, PathExistsTest) {
31 const std::string tmpdir(getenv("TEST_TMPDIR"));
32 const std::string test_file = tmpdir + "/test_file";
33 // Make sure the test_file doesn't exist.
34 unlink(test_file.c_str());
35 EXPECT_FALSE(PathExists(test_file));
36
37 WriteStringToFileOrDie(test_file, "abc");
38
39 EXPECT_TRUE(PathExists(test_file));
40}
41
Brian Silverman61175fb2016-03-13 15:35:56 -040042} // namespace testing
43} // namespace util
44} // namespace aos