2014-05-12 08:16:54 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2014-05-12 08:16:54 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2014-05-12 08:16:54 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
|
|
|
|
|
use OC\Log;
|
2022-05-02 11:49:32 -04:00
|
|
|
use OC\SystemConfig;
|
2018-04-25 09:22:28 -04:00
|
|
|
use OCP\ILogger;
|
2024-05-12 10:33:34 -04:00
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\IUserSession;
|
2018-04-24 20:27:43 -04:00
|
|
|
use OCP\Log\IWriter;
|
2022-05-02 11:49:32 -04:00
|
|
|
use OCP\Support\CrashReport\IRegistry;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2014-05-12 08:16:54 -04:00
|
|
|
|
2018-04-24 20:27:43 -04:00
|
|
|
class LoggerTest extends TestCase implements IWriter {
|
2024-08-16 18:04:55 -04:00
|
|
|
private SystemConfig&MockObject $config;
|
2017-11-12 09:28:04 -05:00
|
|
|
|
2024-08-16 18:04:55 -04:00
|
|
|
private IRegistry&MockObject $registry;
|
2017-11-12 09:28:04 -05:00
|
|
|
|
2024-08-16 18:04:55 -04:00
|
|
|
private Log $logger;
|
2017-11-12 09:28:04 -05:00
|
|
|
|
|
|
|
|
/** @var array */
|
2022-05-02 11:49:32 -04:00
|
|
|
private array $logs = [];
|
2014-05-12 08:16:54 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 16:59:50 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2018-04-24 16:14:00 -04:00
|
|
|
$this->logs = [];
|
2022-05-02 11:49:32 -04:00
|
|
|
$this->config = $this->createMock(SystemConfig::class);
|
|
|
|
|
$this->registry = $this->createMock(IRegistry::class);
|
2024-08-16 18:04:55 -04:00
|
|
|
$this->logger = new Log($this, $this->config, crashReporters: $this->registry);
|
2014-05-12 08:16:54 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-03 09:51:36 -04:00
|
|
|
private function mockDefaultLogLevel(): void {
|
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
|
->method('getValue')
|
2025-06-12 12:34:54 -04:00
|
|
|
->willReturnMap([
|
2024-07-03 09:51:36 -04:00
|
|
|
['loglevel', ILogger::WARN, ILogger::WARN],
|
2025-06-12 12:34:54 -04:00
|
|
|
]);
|
2024-07-03 09:51:36 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testInterpolation(): void {
|
2024-07-03 09:51:36 -04:00
|
|
|
$this->mockDefaultLogLevel();
|
2014-05-12 08:16:54 -04:00
|
|
|
$logger = $this->logger;
|
2020-03-26 04:30:18 -04:00
|
|
|
$logger->warning('{Message {nothing} {user} {foo.bar} a}', ['user' => 'Bob', 'foo.bar' => 'Bar']);
|
2014-05-12 08:16:54 -04:00
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$expected = ['2 {Message {nothing} Bob Bar a}'];
|
2015-04-30 06:43:58 -04:00
|
|
|
$this->assertEquals($expected, $this->getLogs());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testAppCondition(): void {
|
2015-04-30 06:43:58 -04:00
|
|
|
$this->config->expects($this->any())
|
2018-04-25 08:57:08 -04:00
|
|
|
->method('getValue')
|
2025-06-12 12:34:54 -04:00
|
|
|
->willReturnMap([
|
2018-04-25 09:22:28 -04:00
|
|
|
['loglevel', ILogger::WARN, ILogger::WARN],
|
2015-04-30 06:43:58 -04:00
|
|
|
['log.condition', [], ['apps' => ['files']]]
|
2025-06-12 12:34:54 -04:00
|
|
|
]);
|
2015-04-30 06:43:58 -04:00
|
|
|
$logger = $this->logger;
|
|
|
|
|
|
|
|
|
|
$logger->info('Don\'t display info messages');
|
|
|
|
|
$logger->info('Show info messages of files app', ['app' => 'files']);
|
|
|
|
|
$logger->warning('Show warning messages of other apps');
|
|
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
|
'1 Show info messages of files app',
|
|
|
|
|
'2 Show warning messages of other apps',
|
|
|
|
|
];
|
2014-05-12 08:16:54 -04:00
|
|
|
$this->assertEquals($expected, $this->getLogs());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 04:10:13 -04:00
|
|
|
public static function dataMatchesCondition(): array {
|
2024-05-12 10:33:34 -04:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'user0',
|
|
|
|
|
[
|
|
|
|
|
'apps' => ['app2'],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'1 Info of app2',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'user2',
|
|
|
|
|
[
|
|
|
|
|
'users' => ['user1', 'user2'],
|
|
|
|
|
'apps' => ['app1'],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'1 Info of app1',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'user3',
|
|
|
|
|
[
|
|
|
|
|
'users' => ['user3'],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'1 Info without app',
|
|
|
|
|
'1 Info of app1',
|
|
|
|
|
'1 Info of app2',
|
|
|
|
|
'0 Debug of app3',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'user4',
|
|
|
|
|
[
|
|
|
|
|
'users' => ['user4'],
|
|
|
|
|
'apps' => ['app3'],
|
|
|
|
|
'loglevel' => 0,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'0 Debug of app3',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'user4',
|
|
|
|
|
[
|
|
|
|
|
'message' => ' of ',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'1 Info of app1',
|
|
|
|
|
'1 Info of app2',
|
|
|
|
|
'0 Debug of app3',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('dataMatchesCondition')]
|
2024-05-12 10:33:34 -04:00
|
|
|
public function testMatchesCondition(string $userId, array $conditions, array $expectedLogs): void {
|
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
|
->method('getValue')
|
|
|
|
|
->willReturnMap([
|
|
|
|
|
['loglevel', ILogger::WARN, ILogger::WARN],
|
|
|
|
|
['log.condition', [], ['matches' => [
|
|
|
|
|
$conditions,
|
|
|
|
|
]]],
|
|
|
|
|
]);
|
|
|
|
|
$logger = $this->logger;
|
|
|
|
|
|
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
$user->method('getUID')
|
|
|
|
|
->willReturn($userId);
|
|
|
|
|
$userSession = $this->createMock(IUserSession::class);
|
|
|
|
|
$userSession->method('getUser')
|
|
|
|
|
->willReturn($user);
|
|
|
|
|
$this->overwriteService(IUserSession::class, $userSession);
|
|
|
|
|
|
|
|
|
|
$logger->info('Info without app');
|
|
|
|
|
$logger->info('Info of app1', ['app' => 'app1']);
|
|
|
|
|
$logger->info('Info of app2', ['app' => 'app2']);
|
|
|
|
|
$logger->debug('Debug of app3', ['app' => 'app3']);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expectedLogs, $this->getLogs());
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 11:49:32 -04:00
|
|
|
public function testLoggingWithDataArray(): void {
|
2024-07-03 09:51:36 -04:00
|
|
|
$this->mockDefaultLogLevel();
|
2024-08-16 18:04:55 -04:00
|
|
|
/** @var IWriter&MockObject */
|
2022-05-02 11:49:32 -04:00
|
|
|
$writerMock = $this->createMock(IWriter::class);
|
|
|
|
|
$logFile = new Log($writerMock, $this->config);
|
|
|
|
|
$writerMock->expects($this->once())->method('write')->with('no app in context', ['something' => 'extra', 'message' => 'Testing logging with john']);
|
|
|
|
|
$logFile->error('Testing logging with {user}', ['something' => 'extra', 'user' => 'john']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getLogs(): array {
|
2018-04-24 16:14:00 -04:00
|
|
|
return $this->logs;
|
2014-05-12 08:16:54 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-24 20:27:43 -04:00
|
|
|
public function write(string $app, $message, int $level) {
|
2022-05-02 11:49:32 -04:00
|
|
|
$textMessage = $message;
|
|
|
|
|
if (is_array($message)) {
|
|
|
|
|
$textMessage = $message['message'];
|
|
|
|
|
}
|
2024-08-23 09:10:27 -04:00
|
|
|
$this->logs[] = $level . ' ' . $textMessage;
|
2014-05-12 08:16:54 -04:00
|
|
|
}
|
2015-09-24 11:01:31 -04:00
|
|
|
|
2025-05-13 04:10:13 -04:00
|
|
|
public static function userAndPasswordData(): array {
|
2015-09-24 11:01:31 -04:00
|
|
|
return [
|
|
|
|
|
['mySpecialUsername', 'MySuperSecretPassword'],
|
|
|
|
|
['my-user', '324324()#ä234'],
|
|
|
|
|
['my-user', ')qwer'],
|
|
|
|
|
['my-user', 'qwer)asdf'],
|
|
|
|
|
['my-user', 'qwer)'],
|
|
|
|
|
['my-user', '(qwer'],
|
|
|
|
|
['my-user', 'qwer(asdf'],
|
|
|
|
|
['my-user', 'qwer('],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('userAndPasswordData')]
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetectlogin(string $user, string $password): void {
|
2024-07-03 09:51:36 -04:00
|
|
|
$this->mockDefaultLogLevel();
|
2015-09-24 11:01:31 -04:00
|
|
|
$e = new \Exception('test');
|
2017-11-12 09:28:04 -05:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
|
->method('delegateReport')
|
2017-11-27 09:35:29 -05:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 09:28:04 -05:00
|
|
|
|
2015-09-24 11:01:31 -04:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
|
|
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 10:52:46 -04:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
|
}
|
2020-01-06 03:06:52 -05:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2015-09-24 11:01:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('userAndPasswordData')]
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetectcheckPassword(string $user, string $password): void {
|
2024-07-03 09:51:36 -04:00
|
|
|
$this->mockDefaultLogLevel();
|
2015-09-24 11:01:31 -04:00
|
|
|
$e = new \Exception('test');
|
2017-11-12 09:28:04 -05:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
|
->method('delegateReport')
|
2017-11-27 09:35:29 -05:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 09:28:04 -05:00
|
|
|
|
2015-09-24 11:01:31 -04:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
|
2017-11-12 09:28:04 -05:00
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 10:52:46 -04:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
|
}
|
2020-01-06 03:06:52 -05:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2015-09-24 11:01:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-29 03:23:36 -04:00
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('userAndPasswordData')]
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetectvalidateUserPass(string $user, string $password): void {
|
2024-07-03 09:51:36 -04:00
|
|
|
$this->mockDefaultLogLevel();
|
2016-04-29 03:23:36 -04:00
|
|
|
$e = new \Exception('test');
|
2017-11-12 09:28:04 -05:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
|
->method('delegateReport')
|
2017-11-27 09:35:29 -05:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 09:28:04 -05:00
|
|
|
|
2016-04-29 03:23:36 -04:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
|
2017-11-12 09:28:04 -05:00
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 10:52:46 -04:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
|
}
|
2020-01-06 03:06:52 -05:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2016-04-29 03:23:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
2016-08-22 10:56:00 -04:00
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('userAndPasswordData')]
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetecttryLogin(string $user, string $password): void {
|
2024-07-03 09:51:36 -04:00
|
|
|
$this->mockDefaultLogLevel();
|
2016-08-22 10:56:00 -04:00
|
|
|
$e = new \Exception('test');
|
2017-11-12 09:28:04 -05:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
|
->method('delegateReport')
|
2017-11-27 09:35:29 -05:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 09:28:04 -05:00
|
|
|
|
2016-08-22 10:56:00 -04:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
|
2017-11-12 09:28:04 -05:00
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 10:52:46 -04:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
|
}
|
2020-01-06 03:06:52 -05:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2016-08-22 10:56:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('userAndPasswordData')]
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetectclosure(string $user, string $password): void {
|
2024-07-03 09:51:36 -04:00
|
|
|
$this->mockDefaultLogLevel();
|
2025-06-12 12:31:58 -04:00
|
|
|
$a = function ($user, $password): void {
|
2017-10-27 05:46:20 -04:00
|
|
|
throw new \Exception('test');
|
|
|
|
|
};
|
2017-11-12 09:28:04 -05:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
|
->method('delegateReport');
|
2017-10-27 05:46:20 -04:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$a($user, $password);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->logger->logException($e);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 09:28:04 -05:00
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 10:52:46 -04:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
|
}
|
2017-10-27 05:46:20 -04:00
|
|
|
$log = explode('\n', $logLine);
|
|
|
|
|
unset($log[1]); // Remove `testDetectclosure(` because we are not testing this here, but the closure on stack trace 0
|
|
|
|
|
$logLine = implode('\n', $log);
|
|
|
|
|
|
2020-01-06 03:06:52 -05:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2017-10-27 05:46:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
2014-05-12 08:16:54 -04:00
|
|
|
}
|