LoggerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC\Log;
  9. use OC\SystemConfig;
  10. use OCP\ILogger;
  11. use OCP\Log\IWriter;
  12. use OCP\Support\CrashReport\IRegistry;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. class LoggerTest extends TestCase implements IWriter {
  15. /** @var SystemConfig|MockObject */
  16. private $config;
  17. /** @var IRegistry|MockObject */
  18. private $registry;
  19. /** @var ILogger */
  20. private $logger;
  21. /** @var array */
  22. private array $logs = [];
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->logs = [];
  26. $this->config = $this->createMock(SystemConfig::class);
  27. $this->registry = $this->createMock(IRegistry::class);
  28. $this->logger = new Log($this, $this->config, null, $this->registry);
  29. }
  30. private function mockDefaultLogLevel(): void {
  31. $this->config->expects($this->any())
  32. ->method('getValue')
  33. ->will(($this->returnValueMap([
  34. ['loglevel', ILogger::WARN, ILogger::WARN],
  35. ])));
  36. }
  37. public function testInterpolation() {
  38. $this->mockDefaultLogLevel();
  39. $logger = $this->logger;
  40. $logger->warning('{Message {nothing} {user} {foo.bar} a}', ['user' => 'Bob', 'foo.bar' => 'Bar']);
  41. $expected = ['2 {Message {nothing} Bob Bar a}'];
  42. $this->assertEquals($expected, $this->getLogs());
  43. }
  44. public function testAppCondition() {
  45. $this->config->expects($this->any())
  46. ->method('getValue')
  47. ->will(($this->returnValueMap([
  48. ['loglevel', ILogger::WARN, ILogger::WARN],
  49. ['log.condition', [], ['apps' => ['files']]]
  50. ])));
  51. $logger = $this->logger;
  52. $logger->info('Don\'t display info messages');
  53. $logger->info('Show info messages of files app', ['app' => 'files']);
  54. $logger->warning('Show warning messages of other apps');
  55. $expected = [
  56. '1 Show info messages of files app',
  57. '2 Show warning messages of other apps',
  58. ];
  59. $this->assertEquals($expected, $this->getLogs());
  60. }
  61. public function testLoggingWithDataArray(): void {
  62. $this->mockDefaultLogLevel();
  63. $writerMock = $this->createMock(IWriter::class);
  64. $logFile = new Log($writerMock, $this->config);
  65. $writerMock->expects($this->once())->method('write')->with('no app in context', ['something' => 'extra', 'message' => 'Testing logging with john']);
  66. $logFile->error('Testing logging with {user}', ['something' => 'extra', 'user' => 'john']);
  67. }
  68. private function getLogs(): array {
  69. return $this->logs;
  70. }
  71. public function write(string $app, $message, int $level) {
  72. $textMessage = $message;
  73. if (is_array($message)) {
  74. $textMessage = $message['message'];
  75. }
  76. $this->logs[] = $level . " " . $textMessage;
  77. }
  78. public function userAndPasswordData(): array {
  79. return [
  80. ['mySpecialUsername', 'MySuperSecretPassword'],
  81. ['my-user', '324324()#ä234'],
  82. ['my-user', ')qwer'],
  83. ['my-user', 'qwer)asdf'],
  84. ['my-user', 'qwer)'],
  85. ['my-user', '(qwer'],
  86. ['my-user', 'qwer(asdf'],
  87. ['my-user', 'qwer('],
  88. ];
  89. }
  90. /**
  91. * @dataProvider userAndPasswordData
  92. */
  93. public function testDetectlogin(string $user, string $password): void {
  94. $this->mockDefaultLogLevel();
  95. $e = new \Exception('test');
  96. $this->registry->expects($this->once())
  97. ->method('delegateReport')
  98. ->with($e, ['level' => 3]);
  99. $this->logger->logException($e);
  100. $logLines = $this->getLogs();
  101. foreach ($logLines as $logLine) {
  102. if (is_array($logLine)) {
  103. $logLine = json_encode($logLine);
  104. }
  105. $this->assertStringNotContainsString($user, $logLine);
  106. $this->assertStringNotContainsString($password, $logLine);
  107. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  108. }
  109. }
  110. /**
  111. * @dataProvider userAndPasswordData
  112. */
  113. public function testDetectcheckPassword(string $user, string $password): void {
  114. $this->mockDefaultLogLevel();
  115. $e = new \Exception('test');
  116. $this->registry->expects($this->once())
  117. ->method('delegateReport')
  118. ->with($e, ['level' => 3]);
  119. $this->logger->logException($e);
  120. $logLines = $this->getLogs();
  121. foreach ($logLines as $logLine) {
  122. if (is_array($logLine)) {
  123. $logLine = json_encode($logLine);
  124. }
  125. $this->assertStringNotContainsString($user, $logLine);
  126. $this->assertStringNotContainsString($password, $logLine);
  127. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  128. }
  129. }
  130. /**
  131. * @dataProvider userAndPasswordData
  132. */
  133. public function testDetectvalidateUserPass(string $user, string $password): void {
  134. $this->mockDefaultLogLevel();
  135. $e = new \Exception('test');
  136. $this->registry->expects($this->once())
  137. ->method('delegateReport')
  138. ->with($e, ['level' => 3]);
  139. $this->logger->logException($e);
  140. $logLines = $this->getLogs();
  141. foreach ($logLines as $logLine) {
  142. if (is_array($logLine)) {
  143. $logLine = json_encode($logLine);
  144. }
  145. $this->assertStringNotContainsString($user, $logLine);
  146. $this->assertStringNotContainsString($password, $logLine);
  147. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  148. }
  149. }
  150. /**
  151. * @dataProvider userAndPasswordData
  152. */
  153. public function testDetecttryLogin(string $user, string $password): void {
  154. $this->mockDefaultLogLevel();
  155. $e = new \Exception('test');
  156. $this->registry->expects($this->once())
  157. ->method('delegateReport')
  158. ->with($e, ['level' => 3]);
  159. $this->logger->logException($e);
  160. $logLines = $this->getLogs();
  161. foreach ($logLines as $logLine) {
  162. if (is_array($logLine)) {
  163. $logLine = json_encode($logLine);
  164. }
  165. $this->assertStringNotContainsString($user, $logLine);
  166. $this->assertStringNotContainsString($password, $logLine);
  167. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  168. }
  169. }
  170. /**
  171. * @dataProvider userAndPasswordData
  172. */
  173. public function testDetectclosure(string $user, string $password): void {
  174. $this->mockDefaultLogLevel();
  175. $a = function ($user, $password) {
  176. throw new \Exception('test');
  177. };
  178. $this->registry->expects($this->once())
  179. ->method('delegateReport');
  180. try {
  181. $a($user, $password);
  182. } catch (\Exception $e) {
  183. $this->logger->logException($e);
  184. }
  185. $logLines = $this->getLogs();
  186. foreach ($logLines as $logLine) {
  187. if (is_array($logLine)) {
  188. $logLine = json_encode($logLine);
  189. }
  190. $log = explode('\n', $logLine);
  191. unset($log[1]); // Remove `testDetectclosure(` because we are not testing this here, but the closure on stack trace 0
  192. $logLine = implode('\n', $log);
  193. $this->assertStringNotContainsString($user, $logLine);
  194. $this->assertStringNotContainsString($password, $logLine);
  195. $this->assertStringContainsString('*** sensitive parameters replaced ***', $logLine);
  196. }
  197. }
  198. }