blob: f9042598d37f727c6e8873d5d276383ecd562644 [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
9#include "aos/testing/test_logging.h"
10
11namespace aos {
12namespace util {
13namespace testing {
14
15class FileTest : public ::testing::Test {
16 protected:
17 FileTest() {
18 ::aos::testing::EnableTestLogging();
19 }
20};
21
22// Basic test of reading a normal file.
23TEST_F(FileTest, ReadNormalFile) {
24 const ::std::string tmpdir(getenv("TEST_TMPDIR"));
25 const ::std::string test_file = tmpdir + "/test_file";
26 ASSERT_EQ(0, system(("echo contents > " + test_file).c_str()));
27 EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file));
28}
29
30// Tests reading a file with 0 size, among other weird things.
31TEST_F(FileTest, ReadSpecialFile) {
32 const ::std::string stat = ReadFileToStringOrDie("/proc/self/stat");
33 EXPECT_EQ('\n', stat[stat.size() - 1]);
34 const ::std::string my_pid = ::std::to_string(getpid());
35 EXPECT_EQ(my_pid, stat.substr(0, my_pid.size()));
36}
37
38} // namespace testing
39} // namespace util
40} // namespace aos