2014-05-12 08: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 09:22:28 -04:00
|
|
|
use OCP\ILogger;
|
2018-04-24 20:27:43 -04:00
|
|
|
use OCP\Log\IWriter;
|
2014-05-12 08:16:54 -04:00
|
|
|
|
2018-04-24 20:27:43 -04:00
|
|
|
class LoggerTest extends TestCase implements IWriter {
|
2017-11-12 09:28:04 -05:00
|
|
|
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var \OC\SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-12 09:28:04 -05:00
|
|
|
private $config;
|
|
|
|
|
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var \OCP\Support\CrashReport\IRegistry|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-12 09:28:04 -05:00
|
|
|
private $registry;
|
|
|
|
|
|
|
|
|
|
/** @var \OCP\ILogger */
|
2014-05-12 08:16:54 -04:00
|
|
|
private $logger;
|
2017-11-12 09:28:04 -05:00
|
|
|
|
|
|
|
|
/** @var array */
|
2018-04-24 16:14:00 -04:00
|
|
|
private $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 = [];
|
2018-04-25 08:57:08 -04:00
|
|
|
$this->config = $this->createMock(\OC\SystemConfig::class);
|
2017-11-12 09:28:04 -05:00
|
|
|
$this->registry = $this->createMock(\OCP\Support\CrashReport\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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getLogs() {
|
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) {
|
2020-10-05 09:12:57 -04:00
|
|
|
$this->logs[] = "$level $message";
|
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
|
|
|
}
|