From 001eaf17e420d7121ab11d03f69c138b0d56f5c4 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 23 Jan 2026 23:04:03 +0100 Subject: [PATCH] feat(server): added tests for ip4 and ip6 address creation --- src/network.c | 8 +++++-- tests/unit/network.c | 50 ++++++++++++++++++++++++++++++++++++++++++++ tests/unit/network.h | 26 +++++++++++++++++++++++ tests/unit/unit.h | 2 ++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 tests/unit/network.c create mode 100644 tests/unit/network.h diff --git a/src/network.c b/src/network.c index d673d33..4bc13ea 100644 --- a/src/network.c +++ b/src/network.c @@ -11,7 +11,9 @@ QNetNetworkError qnet_ip4_address(const char *ip, unsigned short port, struct sockaddr_in *address) { address->sin_family = AF_INET; address->sin_port = htons(port); - if (inet_pton(address->sin_family, ip, &address->sin_addr.s_addr) != 0) { + + int result = inet_pton(address->sin_family, ip, &address->sin_addr.s_addr); + if (result != 1) { return QNET_NETWORK_ERROR_INVALID_IP; } @@ -21,7 +23,9 @@ QNetNetworkError qnet_ip4_address(const char *ip, unsigned short port, struct so QNetNetworkError qnet_ip6_address(const char *ip, unsigned short port, struct sockaddr_in6 *address) { address->sin6_family = AF_INET6; address->sin6_port = htons(port); - if (inet_pton(address->sin6_family, ip, &address->sin6_addr.s6_addr) != 0) { + + int result = inet_pton(address->sin6_family, ip, &address->sin6_addr.s6_addr); + if (result != 1) { return QNET_NETWORK_ERROR_INVALID_IP; } diff --git a/tests/unit/network.c b/tests/unit/network.c new file mode 100644 index 0000000..089b5f6 --- /dev/null +++ b/tests/unit/network.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#include "unit/network.h" +#include "qnet/network.h" + +QNET_TEST_IMPLEMENT(network_create_ip4_address, + struct sockaddr_in address; + QNetNetworkError error = qnet_ip4_address("127.0.0.1", 8080, &address); + if (error != QNET_NETWORK_ERROR_OK) { + return "Failed to create ip4 address, address invalid"; + } + + return NULL; +) + +QNET_TEST_IMPLEMENT(network_create_invalid_ip4_address, + struct sockaddr_in address; + QNetNetworkError error = qnet_ip4_address("3012.23.54546.2.1", 8080, &address); + if (error != QNET_NETWORK_ERROR_INVALID_IP) { + return "Was able to create ip4 address with invalid ip"; + } + + return NULL; +) + +QNET_TEST_IMPLEMENT(network_create_ip6_address, + struct sockaddr_in6 address; + QNetNetworkError error = qnet_ip6_address("0:0:0:0:0:0:0:1", 8080, &address); + if (error != QNET_NETWORK_ERROR_OK) { + return "Failed to create ip6 address, address invalid"; + } + + return NULL; +) + +QNET_TEST_IMPLEMENT(network_create_invalid_ip6_address, + struct sockaddr_in6 address; + QNetNetworkError error = qnet_ip6_address("0:0:0:0:0:0:0:1:3:2:1", 8080, &address); + if (error != QNET_NETWORK_ERROR_INVALID_IP) { + return "Was able to create ip6 address with invalid ip"; + } + + return NULL; +) \ No newline at end of file diff --git a/tests/unit/network.h b/tests/unit/network.h new file mode 100644 index 0000000..0fb8546 --- /dev/null +++ b/tests/unit/network.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#ifndef QNET_NETWORK_TEST +#define QNET_NETWORK_TEST + +#include "test.h" + +QNET_TEST_DEFINE(network_create_ip4_address); +QNET_TEST_DEFINE(network_create_invalid_ip4_address); +QNET_TEST_DEFINE(network_create_ip6_address); +QNET_TEST_DEFINE(network_create_invalid_ip6_address); + +QNET_TEST_EXPORT(network, + QNET_TEST_EXPORT_TEST(network_create_ip4_address), + QNET_TEST_EXPORT_TEST(network_create_invalid_ip4_address), + QNET_TEST_EXPORT_TEST(network_create_ip6_address), + QNET_TEST_EXPORT_TEST(network_create_invalid_ip6_address) +); + +#endif \ No newline at end of file diff --git a/tests/unit/unit.h b/tests/unit/unit.h index a4ff6d3..92b9873 100644 --- a/tests/unit/unit.h +++ b/tests/unit/unit.h @@ -10,8 +10,10 @@ #define QNET_UNIT #include "test.h" +#include "network.h" QNET_TEST_START + QNET_TEST_IMPORT(network) QNET_TEST_END #endif /* QNET_UNIT */ \ No newline at end of file