1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "brillo/file_utils.h"
6 
7 #include <sys/stat.h>
8 #include <unistd.h>
9 
10 #include <string>
11 
12 #include <base/files/file_util.h>
13 #include <base/files/scoped_temp_dir.h>
14 #include <gtest/gtest.h>
15 
16 namespace brillo {
17 
18 class FileUtilsTest : public testing::Test {
19  public:
FileUtilsTest()20   FileUtilsTest() {
21     CHECK(temp_dir_.CreateUniqueTempDir());
22     file_path_ = temp_dir_.path().Append("test.temp");
23   }
24 
25  protected:
26   base::FilePath file_path_;
27   base::ScopedTempDir temp_dir_;
28 
29   // Writes |contents| to |file_path_|. Pulled into a separate function just
30   // to improve readability of tests.
WriteFile(const std::string & contents)31   void WriteFile(const std::string& contents) {
32     EXPECT_EQ(contents.length(),
33               base::WriteFile(file_path_, contents.c_str(), contents.length()));
34   }
35 
36   // Verifies that the file at |file_path_| exists and contains |contents|.
ExpectFileContains(const std::string & contents)37   void ExpectFileContains(const std::string& contents) {
38     EXPECT_TRUE(base::PathExists(file_path_));
39     std::string new_contents;
40     EXPECT_TRUE(base::ReadFileToString(file_path_, &new_contents));
41     EXPECT_EQ(contents, new_contents);
42   }
43 
44   // Verifies that the file at |file_path_| has |permissions|.
ExpectFilePermissions(int permissions)45   void ExpectFilePermissions(int permissions) {
46     int actual_permissions;
47     EXPECT_TRUE(base::GetPosixFilePermissions(file_path_, &actual_permissions));
48     EXPECT_EQ(permissions, actual_permissions);
49   }
50 };
51 
52 namespace {
53 
54 enum {
55   kPermissions600 =
56       base::FILE_PERMISSION_READ_BY_USER | base::FILE_PERMISSION_WRITE_BY_USER,
57   kPermissions700 = base::FILE_PERMISSION_USER_MASK,
58   kPermissions777 = base::FILE_PERMISSION_MASK
59 };
60 
61 }  // namespace
62 
TEST_F(FileUtilsTest,TouchFileCreate)63 TEST_F(FileUtilsTest, TouchFileCreate) {
64   EXPECT_TRUE(TouchFile(file_path_));
65   ExpectFileContains("");
66   ExpectFilePermissions(kPermissions600);
67 }
68 
TEST_F(FileUtilsTest,TouchFileCreateThroughUmask)69 TEST_F(FileUtilsTest, TouchFileCreateThroughUmask) {
70   mode_t old_umask = umask(kPermissions777);
71   EXPECT_TRUE(TouchFile(file_path_));
72   umask(old_umask);
73   ExpectFileContains("");
74   ExpectFilePermissions(kPermissions600);
75 }
76 
TEST_F(FileUtilsTest,TouchFileCreateDirectoryStructure)77 TEST_F(FileUtilsTest, TouchFileCreateDirectoryStructure) {
78   file_path_ = temp_dir_.path().Append("foo/bar/baz/test.temp");
79   EXPECT_TRUE(TouchFile(file_path_));
80   ExpectFileContains("");
81 }
82 
TEST_F(FileUtilsTest,TouchFileExisting)83 TEST_F(FileUtilsTest, TouchFileExisting) {
84   WriteFile("abcd");
85   EXPECT_TRUE(TouchFile(file_path_));
86   ExpectFileContains("abcd");
87 }
88 
TEST_F(FileUtilsTest,TouchFileReplaceDirectory)89 TEST_F(FileUtilsTest, TouchFileReplaceDirectory) {
90   EXPECT_TRUE(base::CreateDirectory(file_path_));
91   EXPECT_TRUE(TouchFile(file_path_));
92   EXPECT_FALSE(base::DirectoryExists(file_path_));
93   ExpectFileContains("");
94 }
95 
TEST_F(FileUtilsTest,TouchFileReplaceSymlink)96 TEST_F(FileUtilsTest, TouchFileReplaceSymlink) {
97   base::FilePath symlink_target = temp_dir_.path().Append("target.temp");
98   EXPECT_TRUE(base::CreateSymbolicLink(symlink_target, file_path_));
99   EXPECT_TRUE(TouchFile(file_path_));
100   EXPECT_FALSE(base::IsLink(file_path_));
101   ExpectFileContains("");
102 }
103 
TEST_F(FileUtilsTest,TouchFileReplaceOtherUser)104 TEST_F(FileUtilsTest, TouchFileReplaceOtherUser) {
105   WriteFile("abcd");
106   EXPECT_TRUE(TouchFile(file_path_, kPermissions777, geteuid() + 1, getegid()));
107   ExpectFileContains("");
108 }
109 
TEST_F(FileUtilsTest,TouchFileReplaceOtherGroup)110 TEST_F(FileUtilsTest, TouchFileReplaceOtherGroup) {
111   WriteFile("abcd");
112   EXPECT_TRUE(TouchFile(file_path_, kPermissions777, geteuid(), getegid() + 1));
113   ExpectFileContains("");
114 }
115 
TEST_F(FileUtilsTest,TouchFileCreateWithAllPermissions)116 TEST_F(FileUtilsTest, TouchFileCreateWithAllPermissions) {
117   EXPECT_TRUE(TouchFile(file_path_, kPermissions777, geteuid(), getegid()));
118   ExpectFileContains("");
119   ExpectFilePermissions(kPermissions777);
120 }
121 
TEST_F(FileUtilsTest,TouchFileCreateWithOwnerPermissions)122 TEST_F(FileUtilsTest, TouchFileCreateWithOwnerPermissions) {
123   EXPECT_TRUE(TouchFile(file_path_, kPermissions700, geteuid(), getegid()));
124   ExpectFileContains("");
125   ExpectFilePermissions(kPermissions700);
126 }
127 
TEST_F(FileUtilsTest,TouchFileExistingPermissionsUnchanged)128 TEST_F(FileUtilsTest, TouchFileExistingPermissionsUnchanged) {
129   EXPECT_TRUE(TouchFile(file_path_, kPermissions777, geteuid(), getegid()));
130   EXPECT_TRUE(TouchFile(file_path_, kPermissions700, geteuid(), getegid()));
131   ExpectFileContains("");
132   ExpectFilePermissions(kPermissions777);
133 }
134 
135 }  // namespace brillo
136