2014-05-12 16:16:54 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
|
|
|
use OC\Log;
|
2018-04-25 16:22:28 +03:00
|
|
|
use OCP\ILogger;
|
2018-04-25 03:27:43 +03:00
|
|
|
use OCP\Log\IWriter;
|
2014-05-12 16:16:54 +04:00
|
|
|
|
2018-04-25 03:27:43 +03:00
|
|
|
class LoggerTest extends TestCase implements IWriter {
|
2017-11-12 17:28:04 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-12 17:28:04 +03:00
|
|
|
private $config;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\Support\CrashReport\IRegistry|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-12 17:28:04 +03:00
|
|
|
private $registry;
|
|
|
|
|
|
|
|
/** @var \OCP\ILogger */
|
2014-05-12 16:16:54 +04:00
|
|
|
private $logger;
|
2017-11-12 17:28:04 +03:00
|
|
|
|
|
|
|
/** @var array */
|
2018-04-24 23:14:00 +03:00
|
|
|
private $logs = [];
|
2014-05-12 16:16:54 +04:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 00:59:50 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2018-04-24 23:14:00 +03:00
|
|
|
$this->logs = [];
|
2018-04-25 15:57:08 +03:00
|
|
|
$this->config = $this->createMock(\OC\SystemConfig::class);
|
2017-11-12 17:28:04 +03:00
|
|
|
$this->registry = $this->createMock(\OCP\Support\CrashReport\IRegistry::class);
|
2018-04-24 23:14:00 +03:00
|
|
|
$this->logger = new Log($this, $this->config, null, $this->registry);
|
2014-05-12 16:16:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInterpolation() {
|
|
|
|
$logger = $this->logger;
|
2020-03-26 11:30:18 +03:00
|
|
|
$logger->warning('{Message {nothing} {user} {foo.bar} a}', ['user' => 'Bob', 'foo.bar' => 'Bar']);
|
2014-05-12 16:16:54 +04:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$expected = ['2 {Message {nothing} Bob Bar a}'];
|
2015-04-30 13:43:58 +03:00
|
|
|
$this->assertEquals($expected, $this->getLogs());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppCondition() {
|
|
|
|
$this->config->expects($this->any())
|
2018-04-25 15:57:08 +03:00
|
|
|
->method('getValue')
|
2015-04-30 13:43:58 +03:00
|
|
|
->will(($this->returnValueMap([
|
2018-04-25 16:22:28 +03:00
|
|
|
['loglevel', ILogger::WARN, ILogger::WARN],
|
2015-04-30 13:43:58 +03: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 16:16:54 +04:00
|
|
|
$this->assertEquals($expected, $this->getLogs());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getLogs() {
|
2018-04-24 23:14:00 +03:00
|
|
|
return $this->logs;
|
2014-05-12 16:16:54 +04:00
|
|
|
}
|
|
|
|
|
2018-04-25 03:27:43 +03:00
|
|
|
public function write(string $app, $message, int $level) {
|
2020-10-05 16:12:57 +03:00
|
|
|
$this->logs[] = "$level $message";
|
2014-05-12 16:16:54 +04:00
|
|
|
}
|
2015-09-24 18:01:31 +03:00
|
|
|
|
2020-01-06 11:06:52 +03:00
|
|
|
public function userAndPasswordData(): array {
|
2015-09-24 18:01:31 +03: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 11:06:52 +03:00
|
|
|
public function testDetectlogin(string $user, string $password): void {
|
2015-09-24 18:01:31 +03:00
|
|
|
$e = new \Exception('test');
|
2017-11-12 17:28:04 +03:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport')
|
2017-11-27 17:35:29 +03:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 17:28:04 +03:00
|
|
|
|
2015-09-24 18:01:31 +03:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 17:52:46 +03:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2020-01-06 11:06:52 +03:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2015-09-24 18:01:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
*/
|
2020-01-06 11:06:52 +03:00
|
|
|
public function testDetectcheckPassword(string $user, string $password): void {
|
2015-09-24 18:01:31 +03:00
|
|
|
$e = new \Exception('test');
|
2017-11-12 17:28:04 +03:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport')
|
2017-11-27 17:35:29 +03:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 17:28:04 +03:00
|
|
|
|
2015-09-24 18:01:31 +03:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
2017-11-12 17:28:04 +03:00
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 17:52:46 +03:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2020-01-06 11:06:52 +03:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2015-09-24 18:01:31 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 10:23:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
*/
|
2020-01-06 11:06:52 +03:00
|
|
|
public function testDetectvalidateUserPass(string $user, string $password): void {
|
2016-04-29 10:23:36 +03:00
|
|
|
$e = new \Exception('test');
|
2017-11-12 17:28:04 +03:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport')
|
2017-11-27 17:35:29 +03:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 17:28:04 +03:00
|
|
|
|
2016-04-29 10:23:36 +03:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
2017-11-12 17:28:04 +03:00
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 17:52:46 +03:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2020-01-06 11:06:52 +03:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2016-04-29 10:23:36 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 17:56:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
*/
|
2020-01-06 11:06:52 +03:00
|
|
|
public function testDetecttryLogin(string $user, string $password): void {
|
2016-08-22 17:56:00 +03:00
|
|
|
$e = new \Exception('test');
|
2017-11-12 17:28:04 +03:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport')
|
2017-11-27 17:35:29 +03:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 17:28:04 +03:00
|
|
|
|
2016-08-22 17:56:00 +03:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
2017-11-12 17:28:04 +03:00
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 17:52:46 +03:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2020-01-06 11:06:52 +03:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2016-08-22 17:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 12:46:20 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
*/
|
2020-01-06 11:06:52 +03:00
|
|
|
public function testDetectclosure(string $user, string $password): void {
|
2020-04-09 14:53:40 +03:00
|
|
|
$a = function ($user, $password) {
|
2017-10-27 12:46:20 +03:00
|
|
|
throw new \Exception('test');
|
|
|
|
};
|
2017-11-12 17:28:04 +03:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport');
|
2017-10-27 12:46:20 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$a($user, $password);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->logException($e);
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:28:04 +03:00
|
|
|
$logLines = $this->getLogs();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($logLines as $logLine) {
|
2018-03-22 17:52:46 +03:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2017-10-27 12:46:20 +03: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 11:06:52 +03:00
|
|
|
$this->assertStringNotContainsString($user, $logLine);
|
|
|
|
$this->assertStringNotContainsString($password, $logLine);
|
|
|
|
$this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
|
2017-10-27 12:46:20 +03:00
|
|
|
}
|
|
|
|
}
|
2014-05-12 16:16:54 +04:00
|
|
|
}
|