2014-05-12 08:16:54 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2014 Thomas Müller <thomas.mueller@tmit.eu>
|
2022-05-02 11:49:32 -04:00
|
|
|
*
|
|
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
|
|
|
|
*
|
2014-05-12 08:16:54 -04:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
|
* later.
|
|
|
|
|
* See the COPYING-README file.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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;
|
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 {
|
2022-05-02 11:49:32 -04:00
|
|
|
/** @var SystemConfig|MockObject */
|
2017-11-12 09:28:04 -05:00
|
|
|
private $config;
|
|
|
|
|
|
2022-05-02 11:49:32 -04:00
|
|
|
/** @var IRegistry|MockObject */
|
2017-11-12 09:28:04 -05:00
|
|
|
private $registry;
|
|
|
|
|
|
2022-05-02 11:49:32 -04:00
|
|
|
/** @var ILogger */
|
2014-05-12 08:16:54 -04:00
|
|
|
private $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);
|
2018-04-24 16:14:00 -04:00
|
|
|
$this->logger = new Log($this, $this->config, null, $this->registry);
|
2014-05-12 08:16:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInterpolation() {
|
|
|
|
|
$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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAppCondition() {
|
|
|
|
|
$this->config->expects($this->any())
|
2018-04-25 08:57:08 -04:00
|
|
|
->method('getValue')
|
2015-04-30 06:43:58 -04:00
|
|
|
->will(($this->returnValueMap([
|
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']]]
|
|
|
|
|
])));
|
|
|
|
|
$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());
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 11:49:32 -04:00
|
|
|
public function testLoggingWithDataArray(): void {
|
|
|
|
|
$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'];
|
|
|
|
|
}
|
|
|
|
|
$this->logs[] = $level . " " . $textMessage;
|
2014-05-12 08:16:54 -04:00
|
|
|
}
|
2015-09-24 11:01:31 -04:00
|
|
|
|
2020-01-06 03:06:52 -05:00
|
|
|
public 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('],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
|
*/
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetectlogin(string $user, string $password): void {
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
|
*/
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetectcheckPassword(string $user, string $password): void {
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
|
*/
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetectvalidateUserPass(string $user, string $password): void {
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
|
*/
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetecttryLogin(string $user, string $password): void {
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 05:46:20 -04:00
|
|
|
/**
|
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
|
*/
|
2020-01-06 03:06:52 -05:00
|
|
|
public function testDetectclosure(string $user, string $password): void {
|
2020-04-09 07:53:40 -04:00
|
|
|
$a = function ($user, $password) {
|
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
|
|
|
}
|