Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 5 | #include <grp.h> |
| 6 | #include <pwd.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <unistd.h> |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 9 | #include <memory> |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <base/files/file_path.h> |
| 14 | #include <base/files/file_util.h> |
| 15 | #include <base/files/scoped_temp_dir.h> |
Eric Caruso | f9091f8 | 2017-04-28 14:18:59 -0700 | [diff] [blame] | 16 | #include <dbus/mock_bus.h> |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 18 | #include <cryptohome/proto_bindings/rpc.pb.h> |
| 19 | #include <cryptohome-client-test/cryptohome/dbus-proxy-mocks.h> |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 20 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 21 | #include "debugd/src/log_tool.h" |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 22 | |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 23 | using testing::_; |
| 24 | using testing::Invoke; |
| 25 | using testing::Return; |
| 26 | using testing::WithArg; |
| 27 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | bool WriteFile(const base::FilePath& path, const std::string& contents) { |
| 30 | return base::CreateDirectory(path.DirName()) && |
| 31 | base::WriteFile(path, contents.c_str(), contents.length()) == |
| 32 | contents.length(); |
| 33 | } |
| 34 | } // namespace |
| 35 | |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 36 | namespace debugd { |
| 37 | |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 38 | class FakeLog : public LogTool::Log { |
| 39 | public: |
| 40 | MOCK_METHOD(std::string, GetLogData, (), (const, override)); |
| 41 | }; |
| 42 | |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 43 | class LogToolTest : public testing::Test { |
| 44 | protected: |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 45 | std::unique_ptr<LogTool> log_tool_; |
| 46 | base::ScopedTempDir temp_dir_; |
Eric Caruso | f9091f8 | 2017-04-28 14:18:59 -0700 | [diff] [blame] | 47 | |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 48 | void SetUp() override { |
| 49 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 50 | log_tool_ = std::unique_ptr<LogTool>(new LogTool( |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 51 | new dbus::MockBus(dbus::Bus::Options()), |
| 52 | std::make_unique<org::chromium::CryptohomeInterfaceProxyMock>(), |
| 53 | std::make_unique<FakeLog>(), temp_dir_.GetPath())); |
| 54 | |
| 55 | ON_CALL(*GetFakeLog(), GetLogData).WillByDefault(Return("fake")); |
| 56 | } |
| 57 | |
| 58 | FakeLog* GetFakeLog() { |
| 59 | return static_cast<FakeLog*>(log_tool_->arc_bug_report_log_.get()); |
| 60 | } |
| 61 | |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 62 | std::string GetArcBugReport(const std::string& username, bool* is_backup) { |
| 63 | return log_tool_->GetArcBugReport(username, is_backup); |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | org::chromium::CryptohomeInterfaceProxyMock* GetCryptHomeProxy() { |
| 67 | return static_cast<org::chromium::CryptohomeInterfaceProxyMock*>( |
| 68 | log_tool_->cryptohome_proxy_.get()); |
| 69 | } |
| 70 | |
| 71 | void SetArcBugReportBackup(const std::string& userhash) { |
| 72 | log_tool_->arc_bug_report_backups_.insert(userhash); |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 73 | } |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 76 | TEST_F(LogToolTest, GetArcBugReport_ReturnsContents_WhenFileExists) { |
mhasank | 86c46c7 | 2020-08-13 15:36:29 -0700 | [diff] [blame] | 77 | std::string userhash = "0abcdef1230abcdef1230abcdef1230abcdef123"; |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 78 | base::FilePath logPath = |
| 79 | temp_dir_.GetPath().Append(userhash).Append("arc-bugreport.log"); |
| 80 | EXPECT_TRUE(WriteFile(logPath, "test")); |
| 81 | EXPECT_TRUE(base::PathExists(logPath)); |
| 82 | SetArcBugReportBackup(userhash); |
| 83 | EXPECT_CALL(*GetCryptHomeProxy(), GetSanitizedUsername("username", _, _, _)) |
| 84 | .WillOnce(WithArg<1>(Invoke([&userhash](std::string* out_sanitized) { |
| 85 | *out_sanitized = userhash; |
| 86 | return true; |
| 87 | }))); |
| 88 | |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 89 | bool is_backup; |
| 90 | std::string report = GetArcBugReport("username", &is_backup); |
| 91 | |
| 92 | EXPECT_EQ(report, "test"); |
| 93 | EXPECT_TRUE(is_backup); |
| 94 | } |
| 95 | |
| 96 | TEST_F(LogToolTest, GetArcBugReport_Succeeds_WhenIsBackupIsNull) { |
mhasank | 86c46c7 | 2020-08-13 15:36:29 -0700 | [diff] [blame] | 97 | std::string userhash = "0abcdef1230abcdef1230abcdef1230abcdef123"; |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 98 | base::FilePath logPath = |
| 99 | temp_dir_.GetPath().Append(userhash).Append("arc-bugreport.log"); |
| 100 | EXPECT_TRUE(WriteFile(logPath, "test")); |
| 101 | SetArcBugReportBackup(userhash); |
| 102 | EXPECT_CALL(*GetCryptHomeProxy(), GetSanitizedUsername("username", _, _, _)) |
| 103 | .WillOnce(WithArg<1>(Invoke([&userhash](std::string* out_sanitized) { |
| 104 | *out_sanitized = userhash; |
| 105 | return true; |
| 106 | }))); |
| 107 | |
| 108 | std::string report = GetArcBugReport("username", nullptr /*is_backup*/); |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 109 | |
| 110 | EXPECT_EQ(report, "test"); |
| 111 | } |
| 112 | |
| 113 | TEST_F(LogToolTest, GetArcBugReport_DeletesFile_WhenBackupNotSet) { |
mhasank | 86c46c7 | 2020-08-13 15:36:29 -0700 | [diff] [blame] | 114 | std::string userhash = "0abcdef1230abcdef1230abcdef1230abcdef123"; |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 115 | base::FilePath logPath = |
| 116 | temp_dir_.GetPath().Append(userhash).Append("arc-bugreport.log"); |
| 117 | EXPECT_TRUE(WriteFile(logPath, "test")); |
| 118 | EXPECT_TRUE(base::PathExists(logPath)); |
| 119 | EXPECT_CALL(*GetFakeLog(), GetLogData); |
| 120 | EXPECT_CALL(*GetCryptHomeProxy(), GetSanitizedUsername("username", _, _, _)) |
| 121 | .WillOnce(WithArg<1>(Invoke([&userhash](std::string* out_sanitized) { |
| 122 | *out_sanitized = userhash; |
| 123 | return true; |
| 124 | }))); |
| 125 | |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 126 | bool is_backup; |
| 127 | std::string report = GetArcBugReport("username", &is_backup); |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 128 | |
| 129 | EXPECT_EQ(report, "fake"); |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 130 | EXPECT_FALSE(is_backup); |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 131 | EXPECT_FALSE(base::PathExists(logPath)); |
| 132 | } |
| 133 | |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 134 | TEST_F(LogToolTest, DeleteArcBugReportBackup) { |
mhasank | 86c46c7 | 2020-08-13 15:36:29 -0700 | [diff] [blame] | 135 | std::string userhash = "0abcdef1230abcdef1230abcdef1230abcdef123"; |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 136 | base::FilePath logPath = |
| 137 | temp_dir_.GetPath().Append(userhash).Append("arc-bugreport.log"); |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 138 | EXPECT_TRUE(WriteFile(logPath, userhash)); |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 139 | EXPECT_TRUE(base::PathExists(logPath)); |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 140 | log_tool_->DeleteArcBugReportBackup(userhash); |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 141 | EXPECT_FALSE(base::PathExists(logPath)); |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 144 | TEST_F(LogToolTest, EncodeString) { |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 145 | // U+1F600 GRINNING FACE |
| 146 | constexpr const char kGrinningFaceUTF8[] = "\xF0\x9F\x98\x80"; |
| 147 | constexpr const char kGrinningFaceBase64[] = "<base64>: 8J+YgA=="; |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 148 | EXPECT_EQ( |
| 149 | kGrinningFaceUTF8, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 150 | LogTool::EncodeString(kGrinningFaceUTF8, LogTool::Encoding::kAutodetect)); |
| 151 | EXPECT_EQ(kGrinningFaceUTF8, |
| 152 | LogTool::EncodeString(kGrinningFaceUTF8, LogTool::Encoding::kUtf8)); |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 153 | EXPECT_EQ( |
| 154 | kGrinningFaceBase64, |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 155 | LogTool::EncodeString(kGrinningFaceUTF8, LogTool::Encoding::kBase64)); |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 156 | |
| 157 | // .xz Stream Header Magic Bytes |
| 158 | constexpr const char kXzStreamHeaderMagicBytes[] = "\xFD\x37\x7A\x58\x5A\x00"; |
| 159 | constexpr const char kXzStreamHeaderMagicUTF8[] = |
| 160 | "\xEF\xBF\xBD" |
| 161 | "7zXZ"; |
| 162 | constexpr const char kXzStreamHeaderMagicBase64[] = "<base64>: /Td6WFo="; |
| 163 | EXPECT_EQ(kXzStreamHeaderMagicBase64, |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 164 | LogTool::EncodeString(kXzStreamHeaderMagicBytes, |
| 165 | LogTool::Encoding::kAutodetect)); |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 166 | EXPECT_EQ(kXzStreamHeaderMagicUTF8, |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 167 | LogTool::EncodeString(kXzStreamHeaderMagicBytes, |
| 168 | LogTool::Encoding::kUtf8)); |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 169 | EXPECT_EQ(kXzStreamHeaderMagicBase64, |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 170 | LogTool::EncodeString(kXzStreamHeaderMagicBytes, |
| 171 | LogTool::Encoding::kBase64)); |
| 172 | |
| 173 | EXPECT_EQ(kXzStreamHeaderMagicBytes, |
| 174 | LogTool::EncodeString(kXzStreamHeaderMagicBytes, |
| 175 | LogTool::Encoding::kBinary)); |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 178 | class LogTest : public testing::Test { |
| 179 | protected: |
| 180 | void SetUp() override { |
| 181 | std::vector<char> buf(1024); |
| 182 | |
| 183 | uid_t uid = getuid(); |
| 184 | struct passwd pw_entry; |
| 185 | struct passwd* pw_result; |
| 186 | ASSERT_EQ(getpwuid_r(uid, &pw_entry, &buf[0], buf.size(), &pw_result), 0); |
| 187 | ASSERT_NE(pw_result, nullptr); |
| 188 | user_name_ = pw_entry.pw_name; |
| 189 | |
| 190 | gid_t gid = getgid(); |
| 191 | struct group gr_entry; |
| 192 | struct group* gr_result; |
| 193 | ASSERT_EQ(getgrgid_r(gid, &gr_entry, &buf[0], buf.size(), &gr_result), 0); |
| 194 | ASSERT_NE(gr_result, nullptr); |
| 195 | group_name_ = gr_entry.gr_name; |
| 196 | } |
| 197 | |
| 198 | std::string user_name_; |
| 199 | std::string group_name_; |
| 200 | }; |
| 201 | |
| 202 | TEST_F(LogTest, GetFileLogData) { |
| 203 | base::ScopedTempDir temp; |
| 204 | ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 205 | |
| 206 | base::FilePath file_one = temp.GetPath().Append("test/file_one"); |
| 207 | ASSERT_TRUE(WriteFile(file_one, "test_one_contents")); |
| 208 | const LogTool::Log log_one(LogTool::Log::kFile, "test_log_one", |
| 209 | file_one.value(), user_name_, group_name_); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 210 | EXPECT_EQ(log_one.GetLogData(), "test_one_contents"); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 211 | |
| 212 | base::FilePath file_two = temp.GetPath().Append("test/file_two"); |
| 213 | ASSERT_TRUE(WriteFile(file_two, "")); |
| 214 | const LogTool::Log log_two(LogTool::Log::kFile, "test_log_two", |
| 215 | file_two.value(), user_name_, group_name_); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 216 | EXPECT_EQ(log_two.GetLogData(), "<empty>"); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 217 | |
| 218 | base::FilePath file_three = temp.GetPath().Append("test/file_three"); |
| 219 | ASSERT_TRUE(WriteFile(file_three, "long input value")); |
| 220 | const LogTool::Log log_three(LogTool::Log::kFile, "test_log_three", |
| 221 | file_three.value(), user_name_, group_name_, 5); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 222 | EXPECT_EQ(log_three.GetLogData(), "value"); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST_F(LogTest, GetCommandLogData) { |
| 226 | LogTool::Log log_one(LogTool::Log::kCommand, "test_log_one", "printf ''", |
| 227 | user_name_, group_name_); |
| 228 | log_one.DisableMinijailForTest(); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 229 | EXPECT_EQ(log_one.GetLogData(), "<empty>"); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 230 | |
| 231 | LogTool::Log log_two(LogTool::Log::kCommand, "test_log_two", |
| 232 | "printf 'test_output'", user_name_, group_name_); |
| 233 | log_two.DisableMinijailForTest(); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 234 | EXPECT_EQ(log_two.GetLogData(), "test_output"); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 235 | |
| 236 | LogTool::Log log_three(LogTool::Log::kCommand, "test_log_three", |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 237 | "echo a,b,c | cut -d, -f2", user_name_, group_name_); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 238 | log_three.DisableMinijailForTest(); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 239 | EXPECT_EQ(log_three.GetLogData(), "b\n"); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 240 | } |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 241 | } // namespace debugd |