0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-02-15 21:47:57 +00:00
nextcloud_server/tests/lib/Log/ExceptionSerializerTest.php
Andy Scherzinger 1f7e2ba599
chore: Add SPDX header
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
2024-05-13 17:41:36 +02:00

67 lines
2 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace lib\Log;
use OC\Log\ExceptionSerializer;
use OC\SystemConfig;
use Test\TestCase;
class ExceptionSerializerTest extends TestCase {
private ExceptionSerializer $serializer;
public function setUp(): void {
parent::setUp();
$config = $this->createMock(SystemConfig::class);
$this->serializer = new ExceptionSerializer($config);
}
private function emit($arguments) {
\call_user_func_array([$this, 'bind'], $arguments);
}
private function bind(array &$myValues): void {
throw new \Exception('my exception');
}
private function customMagicAuthThing(string $login, string $parole): void {
throw new \Exception('expected custom auth exception');
}
/**
* this test ensures that the serializer does not overwrite referenced
* variables. It is crafted after a scenario we experienced: the DAV server
* emitting the "validateTokens" event, of which later on a handled
* exception was passed to the logger. The token was replaced, the original
* variable overwritten.
*/
public function testSerializer() {
try {
$secret = ['Secret'];
$this->emit([&$secret]);
} catch (\Exception $e) {
$serializedData = $this->serializer->serializeException($e);
$this->assertSame(['Secret'], $secret);
$this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][0]['args'][0]);
}
}
public function testSerializerWithRegisteredMethods() {
$this->serializer->enlistSensitiveMethods(self::class, ['customMagicAuthThing']);
try {
$this->customMagicAuthThing('u57474', 'Secret');
} catch (\Exception $e) {
$serializedData = $this->serializer->serializeException($e);
$this->assertSame('customMagicAuthThing', $serializedData['Trace'][0]['function']);
$this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][0]['args'][0]);
$this->assertFalse(isset($serializedData['Trace'][0]['args'][1]));
}
}
}