LoggerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. use OCP\ILogger;
  11. use OCP\Log\IWriter;
  12. class LoggerTest extends TestCase implements IWriter {
  13. /** @var \OC\SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  14. private $config;
  15. /** @var \OCP\Support\CrashReport\IRegistry|\PHPUnit_Framework_MockObject_MockObject */
  16. private $registry;
  17. /** @var \OCP\ILogger */
  18. private $logger;
  19. /** @var array */
  20. private $logs = [];
  21. protected function setUp() {
  22. parent::setUp();
  23. $this->logs = [];
  24. $this->config = $this->createMock(\OC\SystemConfig::class);
  25. $this->registry = $this->createMock(\OCP\Support\CrashReport\IRegistry::class);
  26. $this->logger = new Log($this, $this->config, null, $this->registry);
  27. }
  28. public function testInterpolation() {
  29. $logger = $this->logger;
  30. $logger->warning('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
  31. $expected = array('2 {Message {nothing} Bob Bar a}');
  32. $this->assertEquals($expected, $this->getLogs());
  33. }
  34. public function testAppCondition() {
  35. $this->config->expects($this->any())
  36. ->method('getValue')
  37. ->will(($this->returnValueMap([
  38. ['loglevel', ILogger::WARN, ILogger::WARN],
  39. ['log.condition', [], ['apps' => ['files']]]
  40. ])));
  41. $logger = $this->logger;
  42. $logger->info('Don\'t display info messages');
  43. $logger->info('Show info messages of files app', ['app' => 'files']);
  44. $logger->warning('Show warning messages of other apps');
  45. $expected = [
  46. '1 Show info messages of files app',
  47. '2 Show warning messages of other apps',
  48. ];
  49. $this->assertEquals($expected, $this->getLogs());
  50. }
  51. private function getLogs() {
  52. return $this->logs;
  53. }
  54. public function write(string $app, $message, int $level) {
  55. $this->logs[]= "$level $message";
  56. }
  57. public function userAndPasswordData() {
  58. return [
  59. ['abc', 'def'],
  60. ['mySpecialUsername', 'MySuperSecretPassword'],
  61. ['my-user', '324324()#ä234'],
  62. ['my-user', ')qwer'],
  63. ['my-user', 'qwer)asdf'],
  64. ['my-user', 'qwer)'],
  65. ['my-user', '(qwer'],
  66. ['my-user', 'qwer(asdf'],
  67. ['my-user', 'qwer('],
  68. ];
  69. }
  70. /**
  71. * @dataProvider userAndPasswordData
  72. */
  73. public function testDetectlogin($user, $password) {
  74. $e = new \Exception('test');
  75. $this->registry->expects($this->once())
  76. ->method('delegateReport')
  77. ->with($e, ['level' => 3]);
  78. $this->logger->logException($e);
  79. $logLines = $this->getLogs();
  80. foreach($logLines as $logLine) {
  81. if (is_array($logLine)) {
  82. $logLine = json_encode($logLine);
  83. }
  84. $this->assertNotContains($user, $logLine);
  85. $this->assertNotContains($password, $logLine);
  86. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  87. }
  88. }
  89. /**
  90. * @dataProvider userAndPasswordData
  91. */
  92. public function testDetectcheckPassword($user, $password) {
  93. $e = new \Exception('test');
  94. $this->registry->expects($this->once())
  95. ->method('delegateReport')
  96. ->with($e, ['level' => 3]);
  97. $this->logger->logException($e);
  98. $logLines = $this->getLogs();
  99. foreach($logLines as $logLine) {
  100. if (is_array($logLine)) {
  101. $logLine = json_encode($logLine);
  102. }
  103. $this->assertNotContains($user, $logLine);
  104. $this->assertNotContains($password, $logLine);
  105. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  106. }
  107. }
  108. /**
  109. * @dataProvider userAndPasswordData
  110. */
  111. public function testDetectvalidateUserPass($user, $password) {
  112. $e = new \Exception('test');
  113. $this->registry->expects($this->once())
  114. ->method('delegateReport')
  115. ->with($e, ['level' => 3]);
  116. $this->logger->logException($e);
  117. $logLines = $this->getLogs();
  118. foreach($logLines as $logLine) {
  119. if (is_array($logLine)) {
  120. $logLine = json_encode($logLine);
  121. }
  122. $this->assertNotContains($user, $logLine);
  123. $this->assertNotContains($password, $logLine);
  124. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  125. }
  126. }
  127. /**
  128. * @dataProvider userAndPasswordData
  129. */
  130. public function testDetecttryLogin($user, $password) {
  131. $e = new \Exception('test');
  132. $this->registry->expects($this->once())
  133. ->method('delegateReport')
  134. ->with($e, ['level' => 3]);
  135. $this->logger->logException($e);
  136. $logLines = $this->getLogs();
  137. foreach($logLines as $logLine) {
  138. if (is_array($logLine)) {
  139. $logLine = json_encode($logLine);
  140. }
  141. $this->assertNotContains($user, $logLine);
  142. $this->assertNotContains($password, $logLine);
  143. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  144. }
  145. }
  146. /**
  147. * @dataProvider userAndPasswordData
  148. */
  149. public function testDetectclosure($user, $password) {
  150. $a = function($user, $password) {
  151. throw new \Exception('test');
  152. };
  153. $this->registry->expects($this->once())
  154. ->method('delegateReport');
  155. try {
  156. $a($user, $password);
  157. } catch (\Exception $e) {
  158. $this->logger->logException($e);
  159. }
  160. $logLines = $this->getLogs();
  161. foreach($logLines as $logLine) {
  162. if (is_array($logLine)) {
  163. $logLine = json_encode($logLine);
  164. }
  165. $log = explode('\n', $logLine);
  166. unset($log[1]); // Remove `testDetectclosure(` because we are not testing this here, but the closure on stack trace 0
  167. $logLine = implode('\n', $log);
  168. $this->assertNotContains($user, $logLine);
  169. $this->assertNotContains($password, $logLine);
  170. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  171. }
  172. }
  173. }