LoggerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. class LoggerTest extends TestCase {
  12. /** @var \OC\SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  13. private $config;
  14. /** @var \OCP\Support\CrashReport\IRegistry|\PHPUnit_Framework_MockObject_MockObject */
  15. private $registry;
  16. /** @var \OCP\ILogger */
  17. private $logger;
  18. /** @var array */
  19. static private $logs = array();
  20. protected function setUp() {
  21. parent::setUp();
  22. self::$logs = array();
  23. $this->config = $this->createMock(\OC\SystemConfig::class);
  24. $this->registry = $this->createMock(\OCP\Support\CrashReport\IRegistry::class);
  25. $this->logger = new Log('Test\LoggerTest', $this->config, null, $this->registry);
  26. }
  27. public function testInterpolation() {
  28. $logger = $this->logger;
  29. $logger->warning('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
  30. $expected = array('2 {Message {nothing} Bob Bar a}');
  31. $this->assertEquals($expected, $this->getLogs());
  32. }
  33. public function testAppCondition() {
  34. $this->config->expects($this->any())
  35. ->method('getValue')
  36. ->will(($this->returnValueMap([
  37. ['loglevel', ILogger::WARN, ILogger::WARN],
  38. ['log.condition', [], ['apps' => ['files']]]
  39. ])));
  40. $logger = $this->logger;
  41. $logger->info('Don\'t display info messages');
  42. $logger->info('Show info messages of files app', ['app' => 'files']);
  43. $logger->warning('Show warning messages of other apps');
  44. $expected = [
  45. '1 Show info messages of files app',
  46. '2 Show warning messages of other apps',
  47. ];
  48. $this->assertEquals($expected, $this->getLogs());
  49. }
  50. private function getLogs() {
  51. return self::$logs;
  52. }
  53. public static function write($app, $message, $level) {
  54. self::$logs[]= "$level $message";
  55. }
  56. public function userAndPasswordData() {
  57. return [
  58. ['abc', 'def'],
  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($user, $password) {
  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->assertNotContains($user, $logLine);
  84. $this->assertNotContains($password, $logLine);
  85. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  86. }
  87. }
  88. /**
  89. * @dataProvider userAndPasswordData
  90. */
  91. public function testDetectcheckPassword($user, $password) {
  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->assertNotContains($user, $logLine);
  103. $this->assertNotContains($password, $logLine);
  104. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  105. }
  106. }
  107. /**
  108. * @dataProvider userAndPasswordData
  109. */
  110. public function testDetectvalidateUserPass($user, $password) {
  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->assertNotContains($user, $logLine);
  122. $this->assertNotContains($password, $logLine);
  123. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  124. }
  125. }
  126. /**
  127. * @dataProvider userAndPasswordData
  128. */
  129. public function testDetecttryLogin($user, $password) {
  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->assertNotContains($user, $logLine);
  141. $this->assertNotContains($password, $logLine);
  142. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  143. }
  144. }
  145. /**
  146. * @dataProvider userAndPasswordData
  147. */
  148. public function testDetectclosure($user, $password) {
  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->assertNotContains($user, $logLine);
  168. $this->assertNotContains($password, $logLine);
  169. $this->assertContains('*** sensitive parameters replaced ***', $logLine);
  170. }
  171. }
  172. public function dataGetLogClass() {
  173. return [
  174. ['file', \OC\Log\File::class],
  175. ['errorlog', \OC\Log\Errorlog::class],
  176. ['syslog', \OC\Log\Syslog::class],
  177. ['owncloud', \OC\Log\File::class],
  178. ['nextcloud', \OC\Log\File::class],
  179. ['foobar', \OC\Log\File::class],
  180. ];
  181. }
  182. /**
  183. * @dataProvider dataGetLogClass
  184. */
  185. public function testGetLogClass($type, $class) {
  186. $this->assertEquals($class, Log::getLogClass($type));
  187. }
  188. }