LoggerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 LoggerTest 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\LoggerTest', $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(*** sensitive parameters 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(*** sensitive parameters 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(*** sensitive parameters replaced ***)', $logLine);
  104. }
  105. }
  106. /**
  107. * @dataProvider userAndPasswordData
  108. */
  109. public function testDetecttryLogin($user, $password) {
  110. $e = new \Exception('test');
  111. $this->logger->logException($e);
  112. $logLines = $this->getLogs();
  113. foreach($logLines as $logLine) {
  114. $this->assertNotContains($user, $logLine);
  115. $this->assertNotContains($password, $logLine);
  116. $this->assertContains('tryLogin(*** sensitive parameters replaced ***)', $logLine);
  117. }
  118. }
  119. }