mirror of
https://gitlab.com/apparmor/apparmor
synced 2025-09-04 16:25:10 +00:00
tests: fix timeout in the inet tests
The child which sends the message was winning the race and causing a timeout when the receiver was waiting for the message. Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
This commit is contained in:
@@ -11,42 +11,56 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include "net_inet.h"
|
#include "net_inet.h"
|
||||||
|
|
||||||
|
enum protocol {
|
||||||
|
UDP,
|
||||||
|
TCP,
|
||||||
|
ICMP
|
||||||
|
};
|
||||||
|
|
||||||
struct connection_info {
|
struct connection_info {
|
||||||
char *bind_ip;
|
char *bind_ip;
|
||||||
char *bind_port;
|
char *bind_port;
|
||||||
char *remote_ip;
|
char *remote_ip;
|
||||||
char *remote_port;
|
char *remote_port;
|
||||||
char *protocol;
|
char *protocol;
|
||||||
|
enum protocol prot;
|
||||||
int timeout;
|
int timeout;
|
||||||
} net_info;
|
} net_info;
|
||||||
|
|
||||||
|
int receive_bind()
|
||||||
int receive_udp()
|
|
||||||
{
|
{
|
||||||
|
|
||||||
int sock;
|
int sock;
|
||||||
char *buf;
|
|
||||||
struct sockaddr_in local;
|
struct sockaddr_in local;
|
||||||
struct sockaddr_in6 local6;
|
struct sockaddr_in6 local6;
|
||||||
int ret = -1;
|
|
||||||
int select_return;
|
|
||||||
|
|
||||||
fd_set read_set, err_set;
|
|
||||||
struct timeval timeout;
|
|
||||||
|
|
||||||
buf = (char *) malloc(255);
|
|
||||||
memset(buf, '\0', 255);
|
|
||||||
|
|
||||||
struct ip_address bind_addr;
|
struct ip_address bind_addr;
|
||||||
|
|
||||||
if (!parse_ip(net_info.bind_ip, net_info.bind_port, &bind_addr)) {
|
if (!parse_ip(net_info.bind_ip, net_info.bind_port, &bind_addr)) {
|
||||||
fprintf(stderr, "FAIL - could not parse bind ip address\n");
|
fprintf(stderr, "FAIL - could not parse bind ip address\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch(net_info.prot) {
|
||||||
|
case UDP:
|
||||||
if ((sock = socket(bind_addr.family, SOCK_DGRAM, 0)) < 0) {
|
if ((sock = socket(bind_addr.family, SOCK_DGRAM, 0)) < 0) {
|
||||||
perror("FAIL - Socket error: ");
|
perror("FAIL - Socket error: ");
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case TCP:
|
||||||
|
if ((sock = socket(bind_addr.family, SOCK_STREAM, 0)) < 0) {
|
||||||
|
perror("FAIL - Socket error: ");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ICMP:
|
||||||
|
if ((sock = socket(bind_addr.family, SOCK_DGRAM, IPPROTO_ICMP)) < 0) {
|
||||||
|
perror("FAIL - Socket error: ");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
const int enable = 1;
|
const int enable = 1;
|
||||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &enable, sizeof(int)) < 0)
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &enable, sizeof(int)) < 0)
|
||||||
@@ -63,12 +77,35 @@ int receive_udp()
|
|||||||
local6 = convert_to_sockaddr_in6(bind_addr);
|
local6 = convert_to_sockaddr_in6(bind_addr);
|
||||||
if (bind(sock, (struct sockaddr *) &local6, sizeof(local6)) < 0)
|
if (bind(sock, (struct sockaddr *) &local6, sizeof(local6)) < 0)
|
||||||
{
|
{
|
||||||
printf("errno %d\n", errno);
|
|
||||||
perror("FAIL - Bind error: ");
|
perror("FAIL - Bind error: ");
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (net_info.prot == TCP) {
|
||||||
|
if (listen(sock, 5) == -1)
|
||||||
|
{
|
||||||
|
perror("FAIL - Could not listen: ");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
int receive_udp(int sock)
|
||||||
|
{
|
||||||
|
|
||||||
|
char *buf;
|
||||||
|
int ret = -1;
|
||||||
|
int select_return;
|
||||||
|
|
||||||
|
fd_set read_set, err_set;
|
||||||
|
struct timeval timeout;
|
||||||
|
|
||||||
|
buf = (char *) malloc(255);
|
||||||
|
memset(buf, '\0', 255);
|
||||||
|
|
||||||
FD_ZERO(&read_set);
|
FD_ZERO(&read_set);
|
||||||
FD_SET(sock, &read_set);
|
FD_SET(sock, &read_set);
|
||||||
FD_ZERO(&err_set);
|
FD_ZERO(&err_set);
|
||||||
@@ -83,8 +120,6 @@ int receive_udp()
|
|||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ((select_return > 0) && (FD_ISSET(sock, &read_set)) && (!FD_ISSET(sock, &err_set)))
|
if ((select_return > 0) && (FD_ISSET(sock, &read_set)) && (!FD_ISSET(sock, &err_set)))
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -99,17 +134,19 @@ int receive_udp()
|
|||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (select_return == 0)
|
||||||
|
printf("FAIL - select timedout\n");
|
||||||
|
|
||||||
free(buf);
|
free(buf);
|
||||||
return(ret);
|
return(ret);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int receive_tcp()
|
int receive_tcp(int sock)
|
||||||
{
|
{
|
||||||
int sock, cli_sock;
|
int cli_sock;
|
||||||
char *buf;
|
char *buf;
|
||||||
struct sockaddr_in local;
|
|
||||||
struct sockaddr_in6 local6;
|
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
int select_return;
|
int select_return;
|
||||||
|
|
||||||
@@ -119,44 +156,6 @@ int receive_tcp()
|
|||||||
buf = (char *) malloc(255);
|
buf = (char *) malloc(255);
|
||||||
memset(buf, '\0', 255);
|
memset(buf, '\0', 255);
|
||||||
|
|
||||||
struct ip_address bind_addr;
|
|
||||||
if (!parse_ip(net_info.bind_ip, net_info.bind_port, &bind_addr)) {
|
|
||||||
fprintf(stderr, "FAIL - could not parse bind ip address\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((sock = socket(bind_addr.family, SOCK_STREAM, 0)) < 0)
|
|
||||||
{
|
|
||||||
perror("FAIL - Socket error:");
|
|
||||||
return(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const int enable = 1;
|
|
||||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &enable, sizeof(int)) < 0)
|
|
||||||
perror("FAIL - setsockopt(SO_REUSEADDR) failed");
|
|
||||||
|
|
||||||
if (bind_addr.family == AF_INET) {
|
|
||||||
local = convert_to_sockaddr_in(bind_addr);
|
|
||||||
if (bind(sock, (struct sockaddr *) &local, sizeof(local)) < 0)
|
|
||||||
{
|
|
||||||
perror("FAIL - Bind error: ");
|
|
||||||
return(-1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
local6 = convert_to_sockaddr_in6(bind_addr);
|
|
||||||
if (bind(sock, (struct sockaddr *) &local6, sizeof(local6)) < 0)
|
|
||||||
{
|
|
||||||
perror("FAIL - Bind error: ");
|
|
||||||
return(-1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (listen(sock, 5) == -1)
|
|
||||||
{
|
|
||||||
perror("FAIL - Could not listen: ");
|
|
||||||
return(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
FD_ZERO(&read_set);
|
FD_ZERO(&read_set);
|
||||||
FD_SET(sock, &read_set);
|
FD_SET(sock, &read_set);
|
||||||
FD_ZERO(&err_set);
|
FD_ZERO(&err_set);
|
||||||
@@ -201,12 +200,9 @@ int receive_tcp()
|
|||||||
return(ret);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
int receive_icmp()
|
int receive_icmp(int sock)
|
||||||
{
|
{
|
||||||
|
|
||||||
int sock;
|
|
||||||
char *buf;
|
char *buf;
|
||||||
struct sockaddr_in local;
|
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
int select_return;
|
int select_return;
|
||||||
|
|
||||||
@@ -215,25 +211,6 @@ int receive_icmp()
|
|||||||
|
|
||||||
buf = (char *) malloc(255);
|
buf = (char *) malloc(255);
|
||||||
memset(buf, '\0', 255);
|
memset(buf, '\0', 255);
|
||||||
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP)) < 0)
|
|
||||||
{
|
|
||||||
perror("FAIL - Socket error: ");
|
|
||||||
return(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const int enable = 1;
|
|
||||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &enable, sizeof(int)) < 0)
|
|
||||||
perror("FAIL - setsockopt(SO_REUSEADDR) failed");
|
|
||||||
|
|
||||||
local.sin_family = AF_INET;
|
|
||||||
local.sin_port = htons(atoi(net_info.bind_port));
|
|
||||||
inet_aton(net_info.bind_ip, &local.sin_addr);
|
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr *) &local, sizeof(local)) < 0)
|
|
||||||
{
|
|
||||||
perror("FAIL - Bind error: ");
|
|
||||||
return(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
FD_ZERO(&read_set);
|
FD_ZERO(&read_set);
|
||||||
FD_SET(sock, &read_set);
|
FD_SET(sock, &read_set);
|
||||||
@@ -249,8 +226,6 @@ int receive_icmp()
|
|||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ((select_return > 0) && (FD_ISSET(sock, &read_set)) && (!FD_ISSET(sock, &err_set)))
|
if ((select_return > 0) && (FD_ISSET(sock, &read_set)) && (!FD_ISSET(sock, &err_set)))
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -321,6 +296,14 @@ int main(int argc, char *argv[])
|
|||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
net_info.protocol = optarg;
|
net_info.protocol = optarg;
|
||||||
|
if (strcmp(net_info.protocol, "udp") == 0)
|
||||||
|
net_info.prot = UDP;
|
||||||
|
else if (strcmp(net_info.protocol, "tcp") == 0)
|
||||||
|
net_info.prot = TCP;
|
||||||
|
else if (strcmp(net_info.protocol, "icmp") == 0)
|
||||||
|
net_info.prot = ICMP;
|
||||||
|
else
|
||||||
|
printf("FAIL - Unknown protocol.\n");
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
net_info.timeout = atoi(optarg);
|
net_info.timeout = atoi(optarg);
|
||||||
@@ -333,6 +316,13 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* get the server to bind/listen, so the child has something
|
||||||
|
* to connect to if it wins the race. */
|
||||||
|
int sockfd = receive_bind();
|
||||||
|
if (sockfd == -1) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
/* exec the sender */
|
/* exec the sender */
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == -1) {
|
if (pid == -1) {
|
||||||
@@ -357,14 +347,17 @@ int main(int argc, char *argv[])
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(net_info.protocol, "udp") == 0)
|
switch(net_info.prot) {
|
||||||
ret = receive_udp(net_info);
|
case UDP:
|
||||||
else if (strcmp(net_info.protocol, "tcp") == 0)
|
ret = receive_udp(sockfd);
|
||||||
ret = receive_tcp(net_info);
|
break;
|
||||||
else if (strcmp(net_info.protocol, "icmp") == 0)
|
case TCP:
|
||||||
ret = receive_icmp(net_info);
|
ret = receive_tcp(sockfd);
|
||||||
else
|
break;
|
||||||
printf("FAIL - Unknown protocol.\n");
|
case ICMP:
|
||||||
|
ret = receive_icmp(sockfd);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user