feat(server): added tests for ip4 and ip6 address creation
This commit is contained in:
parent
6f39b4d502
commit
001eaf17e4
4 changed files with 84 additions and 2 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
50
tests/unit/network.c
Normal file
50
tests/unit/network.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
||||
*
|
||||
* 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;
|
||||
)
|
||||
26
tests/unit/network.h
Normal file
26
tests/unit/network.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
||||
*
|
||||
* 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
|
||||
|
|
@ -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 */
|
||||
Loading…
Add table
Reference in a new issue