summaryrefslogtreecommitdiffstats
path: root/src/tests/netdev_test.c
blob: 388a5af88af1e6996c410c7f1c08afdc265ea40d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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;
}