2014-05-12 12:16:54 +00: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 13:22:28 +00:00
|
|
|
use OCP\ILogger;
|
2018-04-25 00:27:43 +00:00
|
|
|
use OCP\Log\IWriter;
|
2014-05-12 12:16:54 +00:00
|
|
|
|
2018-04-25 00:27:43 +00:00
|
|
|
class LoggerTest extends TestCase implements IWriter {
|
2017-11-12 14:28:04 +00:00
|
|
|
|
|
|
|
/** @var \OC\SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var \OCP\Support\CrashReport\IRegistry|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $registry;
|
|
|
|
|
|
|
|
/** @var \OCP\ILogger */
|
2014-05-12 12:16:54 +00:00
|
|
|
private $logger;
|
2017-11-12 14:28:04 +00:00
|
|
|
|
|
|
|
/** @var array */
|
2018-04-24 20:14:00 +00:00
|
|
|
private $logs = [];
|
2014-05-12 12:16:54 +00:00
|
|
|
|
2014-11-10 21:59:50 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-04-24 20:14:00 +00:00
|
|
|
$this->logs = [];
|
2018-04-25 12:57:08 +00:00
|
|
|
$this->config = $this->createMock(\OC\SystemConfig::class);
|
2017-11-12 14:28:04 +00:00
|
|
|
$this->registry = $this->createMock(\OCP\Support\CrashReport\IRegistry::class);
|
2018-04-24 20:14:00 +00:00
|
|
|
$this->logger = new Log($this, $this->config, null, $this->registry);
|
2014-05-12 12:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInterpolation() {
|
|
|
|
$logger = $this->logger;
|
2015-04-30 10:43:58 +00:00
|
|
|
$logger->warning('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
|
2014-05-12 12:16:54 +00:00
|
|
|
|
2015-04-30 10:43:58 +00:00
|
|
|
$expected = array('2 {Message {nothing} Bob Bar a}');
|
|
|
|
$this->assertEquals($expected, $this->getLogs());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppCondition() {
|
|
|
|
$this->config->expects($this->any())
|
2018-04-25 12:57:08 +00:00
|
|
|
->method('getValue')
|
2015-04-30 10:43:58 +00:00
|
|
|
->will(($this->returnValueMap([
|
2018-04-25 13:22:28 +00:00
|
|
|
['loglevel', ILogger::WARN, ILogger::WARN],
|
2015-04-30 10:43:58 +00: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 12:16:54 +00:00
|
|
|
$this->assertEquals($expected, $this->getLogs());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getLogs() {
|
2018-04-24 20:14:00 +00:00
|
|
|
return $this->logs;
|
2014-05-12 12:16:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 00:27:43 +00:00
|
|
|
public function write(string $app, $message, int $level) {
|
2018-04-24 20:14:00 +00:00
|
|
|
$this->logs[]= "$level $message";
|
2014-05-12 12:16:54 +00:00
|
|
|
}
|
2015-09-24 15:01:31 +00:00
|
|
|
|
|
|
|
public function userAndPasswordData() {
|
|
|
|
return [
|
|
|
|
['abc', 'def'],
|
|
|
|
['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
|
|
|
|
*/
|
|
|
|
public function testDetectlogin($user, $password) {
|
|
|
|
$e = new \Exception('test');
|
2017-11-12 14:28:04 +00:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport')
|
2017-11-27 14:35:29 +00:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 14:28:04 +00:00
|
|
|
|
2015-09-24 15:01:31 +00:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
|
|
|
$logLines = $this->getLogs();
|
|
|
|
foreach($logLines as $logLine) {
|
2018-03-22 14:52:46 +00:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2015-09-24 15:01:31 +00:00
|
|
|
$this->assertNotContains($user, $logLine);
|
|
|
|
$this->assertNotContains($password, $logLine);
|
2018-03-22 14:52:46 +00:00
|
|
|
$this->assertContains('*** sensitive parameters replaced ***', $logLine);
|
2015-09-24 15:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
*/
|
|
|
|
public function testDetectcheckPassword($user, $password) {
|
|
|
|
$e = new \Exception('test');
|
2017-11-12 14:28:04 +00:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport')
|
2017-11-27 14:35:29 +00:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 14:28:04 +00:00
|
|
|
|
2015-09-24 15:01:31 +00:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
2017-11-12 14:28:04 +00:00
|
|
|
$logLines = $this->getLogs();
|
2015-09-24 15:01:31 +00:00
|
|
|
foreach($logLines as $logLine) {
|
2018-03-22 14:52:46 +00:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2015-09-24 15:01:31 +00:00
|
|
|
$this->assertNotContains($user, $logLine);
|
|
|
|
$this->assertNotContains($password, $logLine);
|
2018-03-22 14:52:46 +00:00
|
|
|
$this->assertContains('*** sensitive parameters replaced ***', $logLine);
|
2015-09-24 15:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 07:23:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
*/
|
|
|
|
public function testDetectvalidateUserPass($user, $password) {
|
|
|
|
$e = new \Exception('test');
|
2017-11-12 14:28:04 +00:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport')
|
2017-11-27 14:35:29 +00:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 14:28:04 +00:00
|
|
|
|
2016-04-29 07:23:36 +00:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
2017-11-12 14:28:04 +00:00
|
|
|
$logLines = $this->getLogs();
|
2016-04-29 07:23:36 +00:00
|
|
|
foreach($logLines as $logLine) {
|
2018-03-22 14:52:46 +00:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2016-04-29 07:23:36 +00:00
|
|
|
$this->assertNotContains($user, $logLine);
|
|
|
|
$this->assertNotContains($password, $logLine);
|
2018-03-22 14:52:46 +00:00
|
|
|
$this->assertContains('*** sensitive parameters replaced ***', $logLine);
|
2016-04-29 07:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 14:56:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
*/
|
|
|
|
public function testDetecttryLogin($user, $password) {
|
|
|
|
$e = new \Exception('test');
|
2017-11-12 14:28:04 +00:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport')
|
2017-11-27 14:35:29 +00:00
|
|
|
->with($e, ['level' => 3]);
|
2017-11-12 14:28:04 +00:00
|
|
|
|
2016-08-22 14:56:00 +00:00
|
|
|
$this->logger->logException($e);
|
|
|
|
|
2017-11-12 14:28:04 +00:00
|
|
|
$logLines = $this->getLogs();
|
2016-08-22 14:56:00 +00:00
|
|
|
foreach($logLines as $logLine) {
|
2018-03-22 14:52:46 +00:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2016-08-22 14:56:00 +00:00
|
|
|
$this->assertNotContains($user, $logLine);
|
|
|
|
$this->assertNotContains($password, $logLine);
|
2018-03-22 14:52:46 +00:00
|
|
|
$this->assertContains('*** sensitive parameters replaced ***', $logLine);
|
2016-08-22 14:56:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 09:46:20 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider userAndPasswordData
|
|
|
|
*/
|
|
|
|
public function testDetectclosure($user, $password) {
|
|
|
|
$a = function($user, $password) {
|
|
|
|
throw new \Exception('test');
|
|
|
|
};
|
2017-11-12 14:28:04 +00:00
|
|
|
$this->registry->expects($this->once())
|
|
|
|
->method('delegateReport');
|
2017-10-27 09:46:20 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$a($user, $password);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->logException($e);
|
|
|
|
}
|
|
|
|
|
2017-11-12 14:28:04 +00:00
|
|
|
$logLines = $this->getLogs();
|
2017-10-27 09:46:20 +00:00
|
|
|
foreach($logLines as $logLine) {
|
2018-03-22 14:52:46 +00:00
|
|
|
if (is_array($logLine)) {
|
|
|
|
$logLine = json_encode($logLine);
|
|
|
|
}
|
2017-10-27 09:46:20 +00: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);
|
|
|
|
|
|
|
|
$this->assertNotContains($user, $logLine);
|
|
|
|
$this->assertNotContains($password, $logLine);
|
2018-03-22 14:52:46 +00:00
|
|
|
$this->assertContains('*** sensitive parameters replaced ***', $logLine);
|
2017-10-27 09:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-12 12:16:54 +00:00
|
|
|
}
|