diff --git a/lib/Command/Stun/Add.php b/lib/Command/Stun/Add.php index 9c47eb31bb5..2e7bbaba871 100644 --- a/lib/Command/Stun/Add.php +++ b/lib/Command/Stun/Add.php @@ -50,6 +50,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int $servers = []; } + // check if the server is already in the list + foreach ($servers as $existingServer) { + if ($existingServer === "$host:$port") { + $output->writeln('Server already exists.'); + return 1; + } + } + $servers[] = "$host:$port"; $this->config->setAppValue('spreed', 'stun_servers', json_encode($servers)); diff --git a/lib/Command/Turn/Add.php b/lib/Command/Turn/Add.php index 7bae8645c62..6cec353d369 100644 --- a/lib/Command/Turn/Add.php +++ b/lib/Command/Turn/Add.php @@ -98,6 +98,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int $servers = []; } + //Checking if the server is already added + foreach ($servers as $existingServer) { + if ( + $existingServer['schemes'] === $schemes && + $existingServer['server'] === $server && + $existingServer['protocols'] === $protocols + ) { + $output->writeln('Server already exists with the same configuration.'); + return 1; + } + } + + $servers[] = [ 'schemes' => $schemes, 'server' => $server, diff --git a/tests/php/Command/Stun/AddTest.php b/tests/php/Command/Stun/AddTest.php index 2ae34c4442b..28cfd9e2533 100644 --- a/tests/php/Command/Stun/AddTest.php +++ b/tests/php/Command/Stun/AddTest.php @@ -85,4 +85,20 @@ public function testAddServerToNonEmptyList(): void { self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } + + public function testAddDuplicateServer(): void { + $this->input->method('getArgument') + ->with('server') + ->willReturn('stun.test.com:443'); + $this->config->method('getAppValue') + ->with('spreed', 'stun_servers') + ->willReturn(json_encode(['stun.test.com:443'])); + $this->config->expects($this->never()) + ->method('setAppValue'); + $this->output->expects($this->once()) + ->method('writeln') + ->with($this->equalTo('Server already exists.')); + + $this->assertSame(1, self::invokePrivate($this->command, 'execute', [$this->input, $this->output])); + } } diff --git a/tests/php/Command/Turn/AddTest.php b/tests/php/Command/Turn/AddTest.php index c9aed706ad9..31e531964e7 100644 --- a/tests/php/Command/Turn/AddTest.php +++ b/tests/php/Command/Turn/AddTest.php @@ -304,6 +304,7 @@ public function testAddServerToNonEmptyList(): void { ->with('spreed', 'turn_servers') ->willReturn(json_encode([ [ + 'schemes' => 'turn', 'server' => 'turn1.test.com', 'secret' => 'my-test-secret-1', 'protocols' => 'udp,tcp' @@ -316,6 +317,7 @@ public function testAddServerToNonEmptyList(): void { $this->equalTo('turn_servers'), $this->equalTo(json_encode([ [ + 'schemes' => 'turn', 'server' => 'turn1.test.com', 'secret' => 'my-test-secret-1', 'protocols' => 'udp,tcp' @@ -376,4 +378,43 @@ public function testServerSanitization(): void { self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } + + public function testAddDuplicateServer(): void { + $this->input->method('getArgument') + ->willReturnCallback(function ($arg) { + if ($arg === 'schemes') { + return 'turn,turns'; + } elseif ($arg === 'server') { + return 'turn.test.com'; + } elseif ($arg === 'protocols') { + return 'udp,tcp'; + } + throw new \Exception(); + }); + $this->input->method('getOption') + ->willReturnCallback(function ($arg) { + if ($arg === 'secret') { + return 'my-test-secret'; + } elseif ($arg === 'generate-secret') { + return false; + } + throw new \Exception(); + }); + $this->config->method('getAppValue') + ->with('spreed', 'turn_servers') + ->willReturn(json_encode([[ + 'schemes' => 'turn,turns', + 'server' => 'turn.test.com', + 'secret' => 'my-test-secret', + 'protocols' => 'udp,tcp' + ]])); + $this->config->expects($this->never()) + ->method('setAppValue'); + $this->output->expects($this->once()) + ->method('writeln') + ->with($this->equalTo('Server already exists with the same configuration.')); + + $this->assertSame(1, self::invokePrivate($this->command, 'execute', [$this->input, $this->output])); + } + }