Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ioctl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ LOCAL_LDFLAGS := -lpthread
LOCAL_LDFLAGS += -z stack-size=12288

$(eval $(call add_unity_test, test-ioctl))
$(eval $(call add_unity_test, test-ioctl-special))
317 changes: 317 additions & 0 deletions ioctl/test-ioctl-special.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
/*
* Phoenix-RTOS
*
* Tests for special ioctls:
* - SIOCIFCONF
* - SIOCETHTOOL
*
* (special - passed structure has a pointer
* to arbitrary memory -> needs flattening
* in userspace)
*
* Copyright 2025 Phoenix Systems
* Author: Julian Uziembło
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <sys/socket.h>

#include <phoenix/ethtool.h>

Check failure on line 32 in ioctl/test-ioctl-special.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a7-imx6ull-evk)

phoenix/ethtool.h: No such file or directory

Check failure on line 32 in ioctl/test-ioctl-special.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7m7-imxrt105x-evk)

phoenix/ethtool.h: No such file or directory

Check failure on line 32 in ioctl/test-ioctl-special.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a9-zynq7000-qemu)

phoenix/ethtool.h: No such file or directory

Check failure on line 32 in ioctl/test-ioctl-special.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv8r52-mps3an536-qemu)

phoenix/ethtool.h: No such file or directory

Check failure on line 32 in ioctl/test-ioctl-special.c

View workflow job for this annotation

GitHub Actions / call-ci / build (sparcv8leon-gr716-mini)

phoenix/ethtool.h: No such file or directory

Check failure on line 32 in ioctl/test-ioctl-special.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv8m33-mcxn94x-frdm)

phoenix/ethtool.h: No such file or directory

Check failure on line 32 in ioctl/test-ioctl-special.c

View workflow job for this annotation

GitHub Actions / call-ci / build (sparcv8leon-generic-qemu)

phoenix/ethtool.h: No such file or directory

Check failure on line 32 in ioctl/test-ioctl-special.c

View workflow job for this annotation

GitHub Actions / call-ci / build (sparcv8leon-gr712rc-board)

phoenix/ethtool.h: No such file or directory

#include "unity_fixture.h"

#define ERR_MSG_LEN 64


static int fd = -1;
static struct ifconf ifc;
static struct ifconf current_ifc;
static struct ifreq current_ifr;


static inline int get_ifconf(int fd, struct ifconf *ifc)
{
int err = ioctl(fd, SIOCGIFCONF, ifc);
if (err < 0) {
return -1;
}

ifc->ifc_req = malloc(ifc->ifc_len);
if (ifc->ifc_req == NULL) {
return -1;
}

err = ioctl(fd, SIOCGIFCONF, ifc);
if (err < 0) {
return -1;
}

return 0;
}


/* returns: 0 or errno on success, -1 on fail */
static inline int ethtool_ioctl(struct ifreq *ifr, void *ethtool_struct, uint32_t cmd, char *err_msg_buf)
{
*((uint32_t *)ethtool_struct) = cmd;
ifr->ifr_data = (void *)ethtool_struct;
if (ioctl(fd, SIOCETHTOOL, ifr) < 0) {
if (errno == ENXIO) {
snprintf(err_msg_buf, ERR_MSG_LEN, "Interface '%.*s', not found", IFNAMSIZ, ifr->ifr_name);
return -1;
}
if (errno == EOPNOTSUPP) {
return EOPNOTSUPP;
}
snprintf(err_msg_buf, ERR_MSG_LEN, "Interface '%.*s': %s", IFNAMSIZ, ifr->ifr_name, strerror(errno));
return -1;
}
return 0;
}


TEST_GROUP(test_ioctl_special);


TEST_SETUP(test_ioctl_special)
{
}


TEST_TEAR_DOWN(test_ioctl_special)
{
if (current_ifc.ifc_req != NULL) {
free(current_ifc.ifc_req);
memset(&current_ifc, 0, sizeof(current_ifc));
}
}


TEST(test_ioctl_special, ifconf)
{
TEST_ASSERT_EQUAL_MESSAGE(0, get_ifconf(fd, &current_ifc), strerror(errno));
}


TEST(test_ioctl_special, ifconf_not_enough_space)
{
struct ifreq ifr = { 0 };
struct ifconf ifc = {
.ifc_req = &ifr,
.ifc_len = sizeof(ifr),
};

int res = ioctl(fd, SIOCGIFCONF, &ifc);
TEST_ASSERT_EQUAL_MESSAGE(0, res, strerror(errno));
TEST_ASSERT_EQUAL(sizeof(ifr), ifc.ifc_len);

/* ifr_name should be 3 characters in lwip.
if net stack is ever changed - this should change too */
TEST_ASSERT_EQUAL(3, strnlen(ifc.ifc_req->ifr_name, IFNAMSIZ));
}


TEST(test_ioctl_special, ethtool_gset)
{
struct ethtool_cmd cmd = { 0 };
char err_msg_buf[ERR_MSG_LEN] = { 0 };

int err = ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_GSET, err_msg_buf);
if (err == EOPNOTSUPP) {
TEST_IGNORE_MESSAGE("Operation not supported for this interface");
}
else if (err < 0) {
TEST_FAIL_MESSAGE(err_msg_buf);
}
}


TEST(test_ioctl_special, ethtool_sset)
{
int err = 0;
int last_port = -1;
struct ethtool_cmd cmd = { 0 };
char err_msg_buf[ERR_MSG_LEN] = { 0 };
do {
err = ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_GSET, err_msg_buf);
if (err == EOPNOTSUPP) {
TEST_IGNORE_MESSAGE("Operation not supported for this interface");
}
else if (err < 0) {
break;
}

last_port = cmd.port;
cmd.port = 123;
err = ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_SSET, err_msg_buf);
if (err == EOPNOTSUPP) {
TEST_IGNORE_MESSAGE("Operation not supported for this interface");
}
else if (err < 0) {
break;
}
TEST_ASSERT_EQUAL(123, cmd.port);

} while (0);

if (last_port != -1) {
cmd.port = last_port;
(void)ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_SSET, err_msg_buf);
}
if (err < 0) {
TEST_FAIL_MESSAGE(err_msg_buf);
}
}


TEST(test_ioctl_special, ethtool_test)
{
struct ethtool_test cmd = { 0 };
char err_msg_buf[ERR_MSG_LEN] = { 0 };
cmd.flags = ETH_TEST_FL_OFFLINE;

int err = ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_TEST, err_msg_buf);
if (err == EOPNOTSUPP) {
TEST_IGNORE_MESSAGE("Operation not supported for this interface");
}
else if (err < 0) {
TEST_FAIL_MESSAGE(err_msg_buf);
}
TEST_ASSERT_EQUAL_MESSAGE(0, cmd.flags & ETH_TEST_FL_FAILED, "driver PHYSELFTEST failed");
}


TEST(test_ioctl_special, ethtool_gloopback)
{
struct ethtool_value cmd = { 0 };
char err_msg_buf[ERR_MSG_LEN] = { 0 };

int err = ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_GLOOPBACK, err_msg_buf);
if (err == EOPNOTSUPP) {
TEST_IGNORE_MESSAGE("Operation not supported for this interface");
}
else if (err < 0) {
TEST_FAIL_MESSAGE(err_msg_buf);
}
}


TEST(test_ioctl_special, ethtool_sloopback)
{
int err = 0;
int last_loopback = -1;
struct ethtool_value cmd = { 0 };
char err_msg_buf[ERR_MSG_LEN] = { 0 };

do {
err = ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_GLOOPBACK, err_msg_buf);
if (err == EOPNOTSUPP) {
TEST_IGNORE_MESSAGE("Operation not supported for this interface");
}
else if (err < 0) {
break;
}
last_loopback = cmd.data;
uint32_t expected = (cmd.data != 0) ? ETH_PHY_LOOPBACK_DISABLED : ETH_PHY_LOOPBACK_ENABLED;

cmd.data = expected;
err = ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_SLOOPBACK, err_msg_buf);
if (err == EOPNOTSUPP) {
TEST_IGNORE_MESSAGE("Operation not supported for this interface");
}
if (err < 0) {
break;
}

if (cmd.data == ETH_PHY_LOOPBACK_SET_FAILED) {
snprintf(err_msg_buf, ERR_MSG_LEN, "Interface %.*s: couldn't set loopback", IFNAMSIZ, current_ifr.ifr_name);
err = -1;
break;
}

cmd.data = 0;
err = ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_GLOOPBACK, err_msg_buf);
if (err == EOPNOTSUPP) {
TEST_IGNORE_MESSAGE("Operation not supported for this interface");
}
else if (err < 0) {
break;
}
else if (cmd.data != expected) {
snprintf(err_msg_buf, ERR_MSG_LEN, "Interface %.*s: loopback set incorrectly", IFNAMSIZ, current_ifr.ifr_name);
err = -1;
break;
}
} while (0);

if (last_loopback != -1) {
cmd.data = last_loopback;
(void)ethtool_ioctl(&current_ifr, &cmd, ETHTOOL_SLOOPBACK, err_msg_buf);
}

if (err < 0) {
TEST_FAIL_MESSAGE(err_msg_buf);
}
}


TEST_GROUP_RUNNER(test_ioctl_special)
{
RUN_TEST_CASE(test_ioctl_special, ifconf);
RUN_TEST_CASE(test_ioctl_special, ifconf_not_enough_space);

for (int i = 0; i < (ifc.ifc_len / sizeof(struct ifreq)); i++) {
current_ifr = ifc.ifc_req[i];
fprintf(stderr, "IF: %.*s\n", IFNAMSIZ, current_ifr.ifr_name);
RUN_TEST_CASE(test_ioctl_special, ethtool_gset);
RUN_TEST_CASE(test_ioctl_special, ethtool_sset);
RUN_TEST_CASE(test_ioctl_special, ethtool_test);
RUN_TEST_CASE(test_ioctl_special, ethtool_gloopback);
RUN_TEST_CASE(test_ioctl_special, ethtool_sloopback);
}
}


void runner(void)
{
RUN_TEST_GROUP(test_ioctl_special);
}


int main(int argc, char *argv[])
{
int err;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("Couldn't open socket");
return EXIT_FAILURE;
}

if (get_ifconf(fd, &ifc) < 0) {
perror("Couldn't get ifconf");
return EXIT_FAILURE;
}

err = UnityMain(argc, (const char **)argv, runner);
if (ifc.ifc_req != NULL) {
free(ifc.ifc_req);
}
if (fd != -1) {
close(fd);
}

return (err == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
2 changes: 1 addition & 1 deletion ioctl/test-ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ TEST(ioctl, regular_file)
int fdReg = open(PATH_REG, O_RDWR | O_CREAT | O_TRUNC, S_IFREG);
TEST_ASSERT_NOT_EQUAL_INT(-1, fdReg);
errno = 0;
int ret = ioctl(fdReg, TEST_IOCTL_SIG, NULL);
int ret = ioctl(fdReg, TEST_IOCTL_SIG);
TEST_ASSERT_NOT_EQUAL_INT(0, ret);
close(fdReg);
remove(PATH_REG);
Expand Down
Loading
Loading