Skip to content

Commit

Permalink
Merge pull request #12531 from nextcloud/skalidindi53/11852/Duplicate…
Browse files Browse the repository at this point in the history
…-Stun-and-Turn-Servers

fix: duplicate stun and turn servers cannot be added
  • Loading branch information
nickvergessen authored Jun 19, 2024
2 parents d87d7fb + 34f46f6 commit 807578b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Command/Stun/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<error>Server already exists.</error>');
return 1;
}
}

$servers[] = "$host:$port";

$this->config->setAppValue('spreed', 'stun_servers', json_encode($servers));
Expand Down
13 changes: 13 additions & 0 deletions lib/Command/Turn/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<error>Server already exists with the same configuration.</error>');
return 1;
}
}


$servers[] = [
'schemes' => $schemes,
'server' => $server,
Expand Down
16 changes: 16 additions & 0 deletions tests/php/Command/Stun/AddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<error>Server already exists.</error>'));

$this->assertSame(1, self::invokePrivate($this->command, 'execute', [$this->input, $this->output]));
}
}
41 changes: 41 additions & 0 deletions tests/php/Command/Turn/AddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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('<error>Server already exists with the same configuration.</error>'));

$this->assertSame(1, self::invokePrivate($this->command, 'execute', [$this->input, $this->output]));
}

}

0 comments on commit 807578b

Please sign in to comment.