1
0

LoggerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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(): void {
  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}', ['user' => 'Bob', 'foo.bar' => 'Bar']);
  31. $expected = ['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(): array {
  58. return [
  59. ['mySpecialUsername', 'MySuperSecretPassword'],
  60. ['my-user', '324324()#ä234'],
  61. ['my-user', ')qwer'],
  62. ['my-user', 'qwer)asdf'],
  63. ['my-user', 'qwer)'],
  64. ['my-user', '(qwer'],
  65. ['my-user', 'qwer(asdf'],
  66. ['my-user', 'qwer('],
  67. ];
  68. }
  69. /**
  70. * @dataProvider userAndPasswordData
  71. */
  72. public function testDetectlogin(string $user, string $password): void {
  73. $e = new \Exception('test');
  74. $this->registry->expects($this->once())
  75. ->method('delegateReport')
  76. ->with($e, ['level' => 3]);
  77. $this->logger->logException($e);
  78. $logLines = $this->getLogs();
  79. foreach ($logLines as $logLine) {
  80. if (is_array($logLine)) {
  81. $logLine = json_encode($logLine);
  82. }
  83. $this->assertStringNotContainsString($user, $logLine);
  84. $this->assertStringNotContainsString($password, $logLine);
  85. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  86. }
  87. }
  88. /**
  89. * @dataProvider userAndPasswordData
  90. */
  91. public function testDetectcheckPassword(string $user, string $password): void {
  92. $e = new \Exception('test');
  93. $this->registry->expects($this->once())
  94. ->method('delegateReport')
  95. ->with($e, ['level' => 3]);
  96. $this->logger->logException($e);
  97. $logLines = $this->getLogs();
  98. foreach ($logLines as $logLine) {
  99. if (is_array($logLine)) {
  100. $logLine = json_encode($logLine);
  101. }
  102. $this->assertStringNotContainsString($user, $logLine);
  103. $this->assertStringNotContainsString($password, $logLine);
  104. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  105. }
  106. }
  107. /**
  108. * @dataProvider userAndPasswordData
  109. */
  110. public function testDetectvalidateUserPass(string $user, string $password): void {
  111. $e = new \Exception('test');
  112. $this->registry->expects($this->once())
  113. ->method('delegateReport')
  114. ->with($e, ['level' => 3]);
  115. $this->logger->logException($e);
  116. $logLines = $this->getLogs();
  117. foreach ($logLines as $logLine) {
  118. if (is_array($logLine)) {
  119. $logLine = json_encode($logLine);
  120. }
  121. $this->assertStringNotContainsString($user, $logLine);
  122. $this->assertStringNotContainsString($password, $logLine);
  123. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  124. }
  125. }
  126. /**
  127. * @dataProvider userAndPasswordData
  128. */
  129. public function testDetecttryLogin(string $user, string $password): void {
  130. $e = new \Exception('test');
  131. $this->registry->expects($this->once())
  132. ->method('delegateReport')
  133. ->with($e, ['level' => 3]);
  134. $this->logger->logException($e);
  135. $logLines = $this->getLogs();
  136. foreach ($logLines as $logLine) {
  137. if (is_array($logLine)) {
  138. $logLine = json_encode($logLine);
  139. }
  140. $this->assertStringNotContainsString($user, $logLine);
  141. $this->assertStringNotContainsString($password, $logLine);
  142. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  143. }
  144. }
  145. /**
  146. * @dataProvider userAndPasswordData
  147. */
  148. public function testDetectclosure(string $user, string $password): void {
  149. $a = function ($user, $password) {
  150. throw new \Exception('test');
  151. };
  152. $this->registry->expects($this->once())
  153. ->method('delegateReport');
  154. try {
  155. $a($user, $password);
  156. } catch (\Exception $e) {
  157. $this->logger->logException($e);
  158. }
  159. $logLines = $this->getLogs();
  160. foreach ($logLines as $logLine) {
  161. if (is_array($logLine)) {
  162. $logLine = json_encode($logLine);
  163. }
  164. $log = explode('\n', $logLine);
  165. unset($log[1]); // Remove `testDetectclosure(` because we are not testing this here, but the closure on stack trace 0
  166. $logLine = implode('\n', $log);
  167. $this->assertStringNotContainsString($user, $logLine);
  168. $this->assertStringNotContainsString($password, $logLine);
  169. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  170. }
  171. }
  172. }