You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Thomas Müller <thomas.mueller@tmit.eu>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. use OC\Log;
  10. class Logger extends TestCase {
  11. /**
  12. * @var \OCP\ILogger
  13. */
  14. private $logger;
  15. static private $logs = array();
  16. protected function setUp() {
  17. parent::setUp();
  18. self::$logs = array();
  19. $this->config = $this->getMockBuilder(
  20. '\OC\SystemConfig')
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $this->logger = new Log('Test\Logger', $this->config);
  24. }
  25. public function testInterpolation() {
  26. $logger = $this->logger;
  27. $logger->warning('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
  28. $expected = array('2 {Message {nothing} Bob Bar a}');
  29. $this->assertEquals($expected, $this->getLogs());
  30. }
  31. public function testAppCondition() {
  32. $this->config->expects($this->any())
  33. ->method('getValue')
  34. ->will(($this->returnValueMap([
  35. ['loglevel', \OCP\Util::WARN, \OCP\Util::WARN],
  36. ['log.condition', [], ['apps' => ['files']]]
  37. ])));
  38. $logger = $this->logger;
  39. $logger->info('Don\'t display info messages');
  40. $logger->info('Show info messages of files app', ['app' => 'files']);
  41. $logger->warning('Show warning messages of other apps');
  42. $expected = [
  43. '1 Show info messages of files app',
  44. '2 Show warning messages of other apps',
  45. ];
  46. $this->assertEquals($expected, $this->getLogs());
  47. }
  48. private function getLogs() {
  49. return self::$logs;
  50. }
  51. public static function write($app, $message, $level) {
  52. self::$logs[]= "$level $message";
  53. }
  54. public function userAndPasswordData() {
  55. return [
  56. ['abc', 'def'],
  57. ['mySpecialUsername', 'MySuperSecretPassword'],
  58. ['my-user', '324324()#ä234'],
  59. ['my-user', ')qwer'],
  60. ['my-user', 'qwer)asdf'],
  61. ['my-user', 'qwer)'],
  62. ['my-user', '(qwer'],
  63. ['my-user', 'qwer(asdf'],
  64. ['my-user', 'qwer('],
  65. ];
  66. }
  67. /**
  68. * @dataProvider userAndPasswordData
  69. */
  70. public function testDetectlogin($user, $password) {
  71. $e = new \Exception('test');
  72. $this->logger->logException($e);
  73. $logLines = $this->getLogs();
  74. foreach($logLines as $logLine) {
  75. $this->assertNotContains($user, $logLine);
  76. $this->assertNotContains($password, $logLine);
  77. $this->assertContains('login(*** username and password replaced ***)', $logLine);
  78. }
  79. }
  80. /**
  81. * @dataProvider userAndPasswordData
  82. */
  83. public function testDetectcheckPassword($user, $password) {
  84. $e = new \Exception('test');
  85. $this->logger->logException($e);
  86. $logLines = $this->getLogs();
  87. foreach($logLines as $logLine) {
  88. $this->assertNotContains($user, $logLine);
  89. $this->assertNotContains($password, $logLine);
  90. $this->assertContains('checkPassword(*** username and password replaced ***)', $logLine);
  91. }
  92. }
  93. /**
  94. * @dataProvider userAndPasswordData
  95. */
  96. public function testDetectvalidateUserPass($user, $password) {
  97. $e = new \Exception('test');
  98. $this->logger->logException($e);
  99. $logLines = $this->getLogs();
  100. foreach($logLines as $logLine) {
  101. $this->assertNotContains($user, $logLine);
  102. $this->assertNotContains($password, $logLine);
  103. $this->assertContains('validateUserPass(*** username and password replaced ***)', $logLine);
  104. }
  105. }
  106. }