LoggerTest.php 6.6 KB

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