|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de> |
| 7 | + * |
| 8 | + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace lib\Log; |
| 28 | + |
| 29 | +use OC\Log\ExceptionSerializer; |
| 30 | +use OC\SystemConfig; |
| 31 | +use Test\TestCase; |
| 32 | + |
| 33 | +class ExceptionSerializerTest extends TestCase { |
| 34 | + private ExceptionSerializer $serializer; |
| 35 | + |
| 36 | + public function setUp(): void { |
| 37 | + parent::setUp(); |
| 38 | + |
| 39 | + $config = $this->createMock(SystemConfig::class); |
| 40 | + $this->serializer = new ExceptionSerializer($config); |
| 41 | + } |
| 42 | + |
| 43 | + private function emit($arguments) { |
| 44 | + \call_user_func_array([$this, 'bind'], $arguments); |
| 45 | + } |
| 46 | + |
| 47 | + private function bind(array &$myValues): void { |
| 48 | + throw new \Exception('my exception'); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * this test ensures that the serializer does not overwrite referenced |
| 53 | + * variables. It is crafted after a scenario we experienced: the DAV server |
| 54 | + * emitting the "validateTokens" event, of which later on a handled |
| 55 | + * exception was passed to the logger. The token was replaced, the original |
| 56 | + * variable overwritten. |
| 57 | + */ |
| 58 | + public function testSerializer() { |
| 59 | + try { |
| 60 | + $secret = ['Secret']; |
| 61 | + $this->emit([&$secret]); |
| 62 | + } catch (\Exception $e) { |
| 63 | + $serializedData = $this->serializer->serializeException($e); |
| 64 | + $this->assertSame(['Secret'], $secret); |
| 65 | + $this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][0]['args'][0]); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments