Skip to content

Commit

Permalink
test: modify the IP4/ICMP test and add a test for IP6/ICMP
Browse files Browse the repository at this point in the history
modify the IP4/ICMP test so that the socket is created before the call
to `test_daemon()` and `test_waitsig()`. Also add a test IP6/ICMP.

Signed-off-by: समीर सिंह Sameer Singh <[email protected]>
  • Loading branch information
ss141309 committed Jan 1, 2025
1 parent b445204 commit 9c54c86
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 47 deletions.
1 change: 1 addition & 0 deletions test/zdtm/static/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TST_NOFILE := \
socket6_udp \
socket_udp_shutdown \
socket_icmp \
socket6_icmp \
sk-freebind \
sk-freebind-false \
socket_udplite \
Expand Down
95 changes: 95 additions & 0 deletions test/zdtm/static/socket6_icmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "zdtmtst.h"

const char *test_doc = "static test for IP6/ICMP socket\n";
const char *test_author = "समीर सिंह Sameer Singh <[email protected]>\n";

/* Description:
* Send a ping to localhost using IP6/ICMP socket
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/icmp6.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h>

#define PACKET_SIZE 64
#define RECV_TIMEOUT 1

static int echo_id = 1234;

int main(int argc, char **argv)
{
int ret, sock, seq = 0, recv_len = 0;
char packet[PACKET_SIZE], recv_packet[PACKET_SIZE];

struct timeval tv;
struct icmp6_hdr icmp_header, *icmp_reply;
struct sockaddr_in6 addr, recv_addr;
socklen_t addr_len;

test_init(argc, argv);

sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
if (sock < 0) {
pr_perror("Can't create socket");
return 1;
}

tv.tv_sec = RECV_TIMEOUT;
tv.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
pr_perror("Can't set socket option");
return 1;
}

memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
inet_pton(AF_INET6, "::1", &addr.sin6_addr);

memset(&icmp_header, 0, sizeof(icmp_header));
icmp_header.icmp6_type = ICMP6_ECHO_REQUEST;
icmp_header.icmp6_code = 0;
icmp_header.icmp6_id = echo_id;
icmp_header.icmp6_seq = seq;

memcpy(packet, &icmp_header, sizeof(icmp_header));
memset(packet + sizeof(icmp_header), 0xa5,
PACKET_SIZE - sizeof(icmp_header));

test_daemon();
test_waitsig();

ret = sendto(sock, packet, PACKET_SIZE, 0,
(struct sockaddr *)&addr, sizeof(addr));

if (ret < 0) {
pr_perror("Can't send");
return 1;
}

addr_len = sizeof(recv_addr);

recv_len = recvfrom(sock, recv_packet, sizeof(recv_packet), 0,
(struct sockaddr *)&recv_addr, &addr_len);

if (recv_len < 0) {
pr_perror("Can't recv");
return 1;
}

icmp_reply = (struct icmp6_hdr *)recv_packet;

if (icmp_reply->icmp6_type != ICMP6_ECHO_REPLY) {
fail("Got no ICMP_ECHO_REPLY");
return 1;
}

close(sock);
pass();
return 0;
}
1 change: 1 addition & 0 deletions test/zdtm/static/socket6_icmp.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'flavor': 'h ns'}
80 changes: 33 additions & 47 deletions test/zdtm/static/socket_icmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@ const char *test_author = "समीर सिंह Sameer Singh <lumarzeli30@g
#include <sys/time.h>
#include <netdb.h>

#define PACKET_SIZE 64
#define PACKET_SIZE 64
#define RECV_TIMEOUT 1

int ping(void)
static int echo_id = 1234;

int main(int argc, char **argv)
{
int ret, sock, seq = 0, recv_len = 0;
int ret, sock, seq = 0;
char packet[PACKET_SIZE], recv_packet[PACKET_SIZE];

struct timeval tv;
struct icmphdr icmp_header, *icmp_reply;
struct sockaddr_in addr, recv_addr;
socklen_t addr_len;

test_init(argc, argv);

sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP);
if (sock < 0) {
pr_perror("Can't create socket");
return 1;
}


tv.tv_sec = RECV_TIMEOUT;
tv.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
Expand All @@ -48,61 +51,44 @@ int ping(void)
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");

for (;;) {
memset(&icmp_header, 0, sizeof(icmp_header));
icmp_header.type = ICMP_ECHO;
icmp_header.code = 0;
icmp_header.un.echo.id = getpid();
icmp_header.un.echo.sequence = seq++;
memset(&icmp_header, 0, sizeof(icmp_header));
icmp_header.type = ICMP_ECHO;
icmp_header.code = 0;
icmp_header.un.echo.id = echo_id;
icmp_header.un.echo.sequence = seq;

memcpy(packet, &icmp_header, sizeof(icmp_header));
memset(packet + sizeof(icmp_header), 0xa5,
PACKET_SIZE - sizeof(icmp_header));
memcpy(packet, &icmp_header, sizeof(icmp_header));
memset(packet + sizeof(icmp_header), 0xa5,
PACKET_SIZE - sizeof(icmp_header));

ret = sendto(sock, packet, PACKET_SIZE, 0,
(struct sockaddr *)&addr, sizeof(addr));
test_daemon();
test_waitsig();

if (ret < 0) {
fail("Can't send");
return 1;
}
ret = sendto(sock, packet, PACKET_SIZE, 0,
(struct sockaddr *)&addr, sizeof(addr));

addr_len = sizeof(recv_addr);
if (ret < 0) {
fail("Can't send");
return 1;
}

ret = recvfrom(sock, recv_packet, sizeof(recv_packet), 0,
(struct sockaddr*)&recv_addr, &addr_len);
addr_len = sizeof(recv_addr);

if (ret < 0) {
fail("Can't recv");
return 1;
}
ret = recvfrom(sock, recv_packet, sizeof(recv_packet), 0,
(struct sockaddr *)&recv_addr, &addr_len);

icmp_reply = (struct icmphdr *)recv_packet;
if (ret < 0) {
fail("Can't recv");
return 1;
}

if (icmp_reply->type == ICMP_ECHOREPLY) {
printf("%d bytes from %s: icmp_seq=%d\n",
recv_len,
inet_ntoa(recv_addr.sin_addr),
icmp_reply->un.echo.sequence);
}
icmp_reply = (struct icmphdr *)recv_packet;

sleep(1);
if (icmp_reply->type == ICMP_ECHOREPLY) {
fail("Got no ICMP_ECHO_REPLY");
}

close(sock);
}

int main(int argc, char **argv)
{
int ret;
test_init(argc, argv);

test_daemon();
test_waitsig();

ret = ping();
if (ret == 1)
fail("Cannot ping");

pass();
return 0;
Expand Down
1 change: 1 addition & 0 deletions test/zdtm/static/socket_icmp.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'flavor': 'h ns'}

0 comments on commit 9c54c86

Please sign in to comment.