Skip to content

Commit

Permalink
🐛 Empty address creates QHostAddress::Any like in Qt5 (#10)
Browse files Browse the repository at this point in the history
The behavior changed in Qt6, a `QHostAddress("")` now create a
`QHostAddress::Any` in qt5 & qt6.
This is how I'm using this library, so I expected that as part of the
public API behavior.
  • Loading branch information
OlivierLDff authored Aug 16, 2024
1 parent 0eeccf7 commit fc7c4a9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ bool Server::startWorker()
clear();

// Start to listen
const auto result = _worker->listen(QHostAddress(address()), port());
const auto hostAddress = address().isEmpty() ? QHostAddress::Any : QHostAddress(address());
const auto result = _worker->listen(hostAddress, port());
setListening(result);
return result;
}
Expand Down
21 changes: 19 additions & 2 deletions tests/ServerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class ServerTests : public ::testing::Test

QTimer timer;

void echoTest(quint16 port)
void echoTest(quint16 port, QString serverAddress = "127.0.0.1")
{
QSignalSpy connectedSpy(&client, &MySocket::isConnectedChanged);
client.start("127.0.0.1", port);
server.start("127.0.0.1", port);
server.start(serverAddress, port);
if(!client.isConnected())
{
ASSERT_TRUE(connectedSpy.wait());
Expand Down Expand Up @@ -120,6 +120,23 @@ class ServerTests : public ::testing::Test
}
};

TEST_F(ServerTests, echoTestEmptyServerHostAddress)
{
server.setUseWorkerThread(false);
client.setUseWorkerThread(false);
// Empty QString -> QHostAddress is creating a QHostAddress::Any in Qt5, but QHostAddress::Null in Qt6
// We want to keep the behavior of Qt5
echoTest(30000, "");
}

TEST_F(ServerTests, echoTest0000ServerHostAddress)
{
server.setUseWorkerThread(false);
client.setUseWorkerThread(false);
// This should create a QHostAddress::Any
echoTest(30000, "0.0.0.0");
}

TEST_F(ServerTests, echoTestMonoThread)
{
server.setUseWorkerThread(false);
Expand Down

0 comments on commit fc7c4a9

Please sign in to comment.