blob: fa259e80d4f9b690672d07ffe48f676dac3e205c [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
29} // namespace testing
30} // namespace util
31} // namespace aos