#include "gate/tests.hpp"
#include "gate/tech/portmapping.hpp"
#include "gate/net/sockets.hpp"
#include "gate/net/sockettools.hpp"
using namespace gate;
using namespace gate::tech;
GATEXX_TEST_UNIT(PortMapping)
{
static StaticString const serverAddr = "127.0.0.1:55042";
static StaticString const proxyAddr = "127.0.0.1:55024";
ControlStream server = gate::net::SocketStream::createServer(serverAddr);
PortMapper pm;
uint32_t mappingId = 0;
GATEXX_TEST_CHECK_NOTHROW(mappingId = pm.add(proxyAddr, serverAddr));
ControlStream client = gate::net::SocketStream::createClient(proxyAddr);
size_t len = 0;
char buffer[256];<--- The scope of the variable 'buffer' can be reduced. [+]The scope of the variable 'buffer' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:<--- Shadowed declaration
void f(int x)<--- Shadowed declaration
{<--- Shadowed declaration
int i = 0;<--- Shadowed declaration
if (x) {<--- Shadowed declaration
// it's safe to move 'int i = 0;' here<--- Shadowed declaration
for (int n = 0; n < 10; ++n) {<--- Shadowed declaration
// it is possible but not safe to move 'int i = 0;' here<--- Shadowed declaration
do_something(&i);<--- Shadowed declaration
}<--- Shadowed declaration
}<--- Shadowed declaration
}<--- Shadowed declaration
When you see this message it is always safe to reduce the variable scope 1 level.
<--- Shadowed declaration
{
// client to server
static char const* const message1 = "Hello";
static size_t const messageLen1 = 5;
GATEXX_TEST_CHECK_NOTHROW(len = client.writeBlock(message1, messageLen1));
GATEXX_TEST_CHECK_EQUAL(len, messageLen1);
GATEXX_TEST_CHECK_NOTHROW(client.flush());
GATEXX_TEST_CHECK_NOTHROW(client.close(ControlStream::Close_Output));
GATEXX_TEST_CHECK_NOTHROW(len = server.readBlock(buffer, sizeof(buffer)));
GATEXX_TEST_CHECK_EQUAL(len, messageLen1);
GATEXX_TEST_CHECK_EQUAL(Mem::compare(buffer, message1, messageLen1), 0);
}
{
// server to client
static char const* const message2 = "World";
static size_t const messageLen2 = 5;
GATEXX_TEST_CHECK_NOTHROW(len = server.writeBlock(message2, messageLen2));
GATEXX_TEST_CHECK_EQUAL(len, messageLen2);
GATEXX_TEST_CHECK_NOTHROW(server.flush());
GATEXX_TEST_CHECK_NOTHROW(server.close(ControlStream::Close_Output));
char buffer[256];<--- Shadow variable
GATEXX_TEST_CHECK_NOTHROW(len = client.readBlock(buffer, sizeof(buffer)));
GATEXX_TEST_CHECK_EQUAL(len, messageLen2);
GATEXX_TEST_CHECK_EQUAL(Mem::compare(buffer, message2, messageLen2), 0);
}
}