1
0

ExceptionSerializerTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace lib\Log;
  8. use OC\Log\ExceptionSerializer;
  9. use OC\SystemConfig;
  10. use Test\TestCase;
  11. class ExceptionSerializerTest extends TestCase {
  12. private ExceptionSerializer $serializer;
  13. public function setUp(): void {
  14. parent::setUp();
  15. $config = $this->createMock(SystemConfig::class);
  16. $this->serializer = new ExceptionSerializer($config);
  17. }
  18. private function emit($arguments) {
  19. \call_user_func_array([$this, 'bind'], $arguments);
  20. }
  21. private function bind(array &$myValues): void {
  22. throw new \Exception('my exception');
  23. }
  24. private function customMagicAuthThing(string $login, string $parole): void {
  25. throw new \Exception('expected custom auth exception');
  26. }
  27. /**
  28. * this test ensures that the serializer does not overwrite referenced
  29. * variables. It is crafted after a scenario we experienced: the DAV server
  30. * emitting the "validateTokens" event, of which later on a handled
  31. * exception was passed to the logger. The token was replaced, the original
  32. * variable overwritten.
  33. */
  34. public function testSerializer() {
  35. try {
  36. $secret = ['Secret'];
  37. $this->emit([&$secret]);
  38. } catch (\Exception $e) {
  39. $serializedData = $this->serializer->serializeException($e);
  40. $this->assertSame(['Secret'], $secret);
  41. $this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][0]['args'][0]);
  42. }
  43. }
  44. public function testSerializerWithRegisteredMethods() {
  45. $this->serializer->enlistSensitiveMethods(self::class, ['customMagicAuthThing']);
  46. try {
  47. $this->customMagicAuthThing('u57474', 'Secret');
  48. } catch (\Exception $e) {
  49. $serializedData = $this->serializer->serializeException($e);
  50. $this->assertSame('customMagicAuthThing', $serializedData['Trace'][0]['function']);
  51. $this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][0]['args'][0]);
  52. $this->assertFalse(isset($serializedData['Trace'][0]['args'][1]));
  53. }
  54. }
  55. }