[test] Add infrastructure for test network devices

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-06-10 13:32:10 +01:00
parent 5b3ebf8b24
commit 1ae75a3bde
2 changed files with 329 additions and 0 deletions

218
src/tests/netdev_test.c Normal file
View File

@@ -0,0 +1,218 @@
/*
* Copyright (C) 2025 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** @file
*
* Network device tests
*
*/
/* Forcibly enable assertions */
#undef NDEBUG
#include <string.h>
#include <stdio.h>
#include <ipxe/netdevice.h>
#include <ipxe/ethernet.h>
#include <ipxe/test.h>
#include "netdev_test.h"
/**
* Open network device
*
* @v netdev Network device
* @ret rc Return status code
*/
static int testnet_open ( struct net_device *netdev __unused ) {
/* Do nothing, successfully */
return 0;
}
/**
* Close network device
*
* @v netdev Network device
*/
static void testnet_close ( struct net_device *netdev __unused ) {
/* Do nothing */
}
/**
* Transmit packet
*
* @v netdev Network device
* @v iobuf I/O buffer
* @ret rc Return status code
*/
static int testnet_transmit ( struct net_device *netdev,
struct io_buffer *iobuf ) {
/* Complete immediately */
netdev_tx_complete ( netdev, iobuf );
return 0;
}
/**
* Poll for completed and received packets
*
* @v netdev Network device
*/
static void testnet_poll ( struct net_device *netdev __unused ) {
/* Do nothing */
}
/** Test network device operations */
static struct net_device_operations testnet_operations = {
.open = testnet_open,
.close = testnet_close,
.transmit = testnet_transmit,
.poll = testnet_poll,
};
/**
* Report a network device creation test result
*
* @v testnet Test network device
* @v file Test code file
* @v line Test code line
*/
void testnet_okx ( struct testnet *testnet, const char *file,
unsigned int line ) {
struct testnet_setting *testset;
unsigned int i;
/* Allocate device */
testnet->netdev = alloc_etherdev ( 0 );
okx ( testnet->netdev != NULL, file, line );
netdev_init ( testnet->netdev, &testnet_operations );
testnet->netdev->dev = &testnet->dev;
snprintf ( testnet->netdev->name, sizeof ( testnet->netdev->name ),
"%s", testnet->dev.name );
/* Register device */
okx ( register_netdev ( testnet->netdev ) == 0, file, line );
/* Open device */
testnet_open_okx ( testnet, file, line );
/* Apply initial settings */
for ( i = 0 ; i < testnet->count ; i++ ) {
testset = &testnet->testset[i];
testnet_set_okx ( testnet, testset->name, testset->value,
file, line );
}
}
/**
* Report a network device opening test result
*
* @v testnet Test network device
* @v file Test code file
* @v line Test code line
*/
void testnet_open_okx ( struct testnet *testnet, const char *file,
unsigned int line ) {
/* Sanity check */
okx ( testnet->netdev != NULL, file, line );
/* Open device */
okx ( netdev_open ( testnet->netdev ) == 0, file, line );
}
/**
* Report a network device setting test result
*
* @v testnet Test network device
* @v name Setting name (relative to network device's settings)
* @v value Setting value
* @v file Test code file
* @v line Test code line
*/
void testnet_set_okx ( struct testnet *testnet, const char *name,
const char *value, const char *file,
unsigned int line ) {
char fullname[ strlen ( testnet->dev.name ) + 1 /* "." or "/" */ +
strlen ( name ) + 1 /* NUL */ ];
struct settings *settings;
struct setting setting;
/* Sanity check */
okx ( testnet->netdev != NULL, file, line );
settings = netdev_settings ( testnet->netdev );
okx ( settings != NULL, file, line );
okx ( strcmp ( settings->name, testnet->dev.name ) == 0, file, line );
/* Construct setting name */
snprintf ( fullname, sizeof ( fullname ), "%s%c%s", testnet->dev.name,
( strchr ( name, '/' ) ? '.' : '/' ), name );
/* Parse setting name */
okx ( parse_setting_name ( fullname, autovivify_child_settings,
&settings, &setting ) == 0, file, line );
/* Apply setting */
okx ( storef_setting ( settings, &setting, value ) == 0, file, line );
}
/**
* Report a network device closing test result
*
* @v testnet Test network device
* @v file Test code file
* @v line Test code line
*/
void testnet_close_okx ( struct testnet *testnet, const char *file,
unsigned int line ) {
/* Sanity check */
okx ( testnet->netdev != NULL, file, line );
/* Close device */
netdev_close ( testnet->netdev );
}
/**
* Report a network device removal test result
*
* @v testnet Test network device
* @v file Test code file
* @v line Test code line
*/
void testnet_remove_okx ( struct testnet *testnet, const char *file,
unsigned int line ) {
/* Sanity check */
okx ( testnet->netdev != NULL, file, line );
/* Remove device */
unregister_netdev ( testnet->netdev );
netdev_nullify ( testnet->netdev );
netdev_put ( testnet->netdev );
testnet->netdev = NULL;
}

111
src/tests/netdev_test.h Normal file
View File

@@ -0,0 +1,111 @@
#ifndef _NETDEV_TEST_H
#define _NETDEV_TEST_H
/** @file
*
* Network device tests
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/device.h>
#include <ipxe/netdevice.h>
/** A test network device setting */
struct testnet_setting {
/** Setting name (relative to network device's settings) */
const char *name;
/** Value */
const char *value;
};
/** A test network device */
struct testnet {
/** Network device */
struct net_device *netdev;
/** Dummy physical device */
struct device dev;
/** Initial settings */
struct testnet_setting *testset;
/** Number of initial settings */
unsigned int count;
};
/**
* Declare a test network device
*
* @v NAME Network device name
* @v ... Initial network device settings
*/
#define TESTNET( NAME, ... ) \
static struct testnet_setting NAME ## _setting[] = { \
__VA_ARGS__ \
}; \
static struct testnet NAME = { \
.dev = { \
.name = #NAME, \
.driver_name = "testnet", \
.siblings = \
LIST_HEAD_INIT ( NAME.dev.siblings ), \
.children = \
LIST_HEAD_INIT ( NAME.dev.children ), \
}, \
.testset = NAME ## _setting, \
.count = ( sizeof ( NAME ## _setting ) / \
sizeof ( NAME ## _setting[0] ) ), \
};
/**
* Report a network device creation test result
*
* @v testnet Test network device
*/
#define testnet_ok( testnet ) testnet_okx ( testnet, __FILE__, __LINE__ )
extern void testnet_okx ( struct testnet *testnet, const char *file,
unsigned int line );
/**
* Report a network device opening test result
*
* @v testnet Test network device
*/
#define testnet_open_ok( testnet ) \
testnet_open_okx ( testnet, __FILE__, __LINE__ )
extern void testnet_open_okx ( struct testnet *testnet, const char *file,
unsigned int line );
/**
* Report a network device setting test result
*
* @v testnet Test network device
* @v name Setting name (relative to network device's settings)
* @v value Setting value
*/
#define testnet_set_ok( testnet, name, value ) \
testnet_set_okx ( testnet, name, value, __FILE__, __LINE__ )
extern void testnet_set_okx ( struct testnet *testnet, const char *name,
const char *value, const char *file,
unsigned int line );
/**
* Report a network device closing test result
*
* @v testnet Test network device
*/
#define testnet_close_ok( testnet ) \
testnet_close_okx ( testnet, __FILE__, __LINE__ )
extern void testnet_close_okx ( struct testnet *testnet, const char *file,
unsigned int line );
/**
* Report a network device removal test result
*
* @v testnet Test network device
*/
#define testnet_remove_ok( testnet ) \
testnet_remove_okx ( testnet, __FILE__, __LINE__ )
extern void testnet_remove_okx ( struct testnet *testnet, const char *file,
unsigned int line );
#endif /* _NETDEV_TEST_H */