summaryrefslogtreecommitdiffstats
path: root/test-cmdline.c
blob: c48be879d5fb35651b82bf0078b39f6be24373ab (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/****************************************************************************
 * Test cases for ethtool command-line parsing
 * Copyright 2011 Solarflare Communications Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, incorporated herein by reference.
 */

#include <stdio.h>
#include <stdlib.h>
#define TEST_NO_WRAPPERS
#include "internal.h"

#ifdef ETHTOOL_ENABLE_NETLINK
#define IS_NL 1
#else
#define IS_NL 0
#endif

static struct test_case {
	int rc;
	const char *args;
} test_cases[] = {
	{ 1, "" },
	{ 0, "devname" },
	{ 0, "15_char_devname" },
	{ !IS_NL, "--json devname" },
	/* netlink interface allows names up to 127 characters */
	{ !IS_NL, "16_char_devname!" },
	{ !IS_NL, "127_char_devname0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde" },
	{ 1, "128_char_devname0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" },
	/* Argument parsing for -s is specialised */
	{ 0, "-s devname" },
	{ 0, "--change devname speed 100 duplex half mdix auto" },
	{ 1, "-s devname speed foo" },
	{ 1, "--change devname speed" },
	{ 0, "-s devname duplex half" },
	{ 1, "--change devname duplex foo" },
	{ 1, "-s devname duplex" },
	{ 1, "--change devname mdix foo" },
	{ 1, "-s devname mdix" },
	{ 0, "--change devname port tp" },
	{ 1, "-s devname port foo" },
	{ 1, "--change devname port" },
	{ 0, "-s devname autoneg on" },
	{ 1, "--change devname autoneg foo" },
	{ 1, "-s devname autoneg" },
	{ 0, "--change devname advertise 0x1" },
	{ 0, "--change devname advertise 0xf" },
	{ 0, "--change devname advertise 0Xf" },
	{ 0, "--change devname advertise 1" },
	{ 0, "--change devname advertise f" },
	{ 0, "--change devname advertise 01" },
	{ 0, "--change devname advertise 0f" },
	{ 0, "--change devname advertise 0xfffffffffffffffffffffffffffffffff" },
	{ 0, "--change devname advertise fffffffffffffffffffffffffffffffff" },
	{ 0, "--change devname advertise 0x0000fffffffffffffffffffffffffffff" },
	{ 0, "--change devname advertise 0000fffffffffffffffffffffffffffff" },
	{ 1, "-s devname advertise" },
	{ 1, "-s devname advertise 0x" },
	{ 1, "-s devname advertise foo" },
	{ 1, "-s devname advertise 0xfoo" },
	{ 1, "--change devname advertise" },
	{ 0, "-s devname phyad 1" },
	{ 1, "--change devname phyad foo" },
	{ 1, "-s devname phyad" },
	/* Deprecated 'xcvr' detected by netlink parser */
	{ IS_NL, "--change devname xcvr external" },
	{ 1, "-s devname xcvr foo" },
	{ 1, "--change devname xcvr" },
	{ 0, "-s devname wol p" },
	{ 1, "--change devname wol" },
	{ 0, "-s devname sopass 01:23:45:67:89:ab" },
	{ 1, "--change devname sopass 01:23:45:67:89:" },
	{ 1, "-s devname sopass 01:23:45:67:89" },
	{ 1, "--change devname sopass" },
	{ 0, "-s devname msglvl 1" },
	{ 1, "--change devname msglvl" },
	{ 0, "-s devname msglvl hw on rx_status off" },
	{ 1, "--change devname msglvl hw foo" },
	{ 1, "-s devname msglvl hw" },
	{ 0, "--change devname speed 100 duplex half port tp autoneg on advertise 0x1 phyad 1 wol p sopass 01:23:45:67:89:ab msglvl 1" },
	/* Deprecated 'xcvr' detected by netlink parser */
	{ IS_NL, "--change devname speed 100 duplex half port tp autoneg on advertise 0x1 phyad 1 xcvr external wol p sopass 01:23:45:67:89:ab msglvl 1" },
	{ 1, "-s devname foo" },
	{ 1, "-s" },
	{ 0, "-a devname" },
	{ 0, "--show-pause devname" },
	{ 1, "-a" },
	/* Many other sub-commands use parse_generic_cmdline() and
	 * don't need to be check in that much detail. */
	{ 0, "-A devname autoneg on" },
	{ 1, "--pause devname autoneg foo" },
	{ 1, "-A devname autoneg" },
	{ 0, "--pause devname rx off" },
	{ 0, "-A devname tx on rx on autoneg off" },
	{ 1, "--pause devname foo on" },
	{ 1, "-A" },
	{ 0, "-c devname" },
	{ 0, "--show-coalesce devname" },
	{ 0, "-C devname adaptive-rx on adaptive-tx off rx-usecs 1 rx-frames 2 rx-usecs-irq 3 rx-frames-irq 4 tx-usecs 5 tx-frames 6 tx-usecs-irq 7 tx-frames-irq 8 stats-block-usecs 9 pkt-rate-low 10" },
	{ 0, "--coalesce devname rx-usecs-low 11 rx-frames-low 12 tx-usecs-low 13 tx-frames-low 14 pkt-rate-high 15 rx-usecs-high 16 rx-frames-high 17 tx-usecs-high 18 tx-frames-high 19 sample-interval 20" },
	{ 1, "-C devname adaptive-rx foo" },
	{ 1, "--coalesce devname adaptive-rx" },
	{ 1, "-C devname foo on" },
	{ 1, "-C" },
	{ 0, "-g devname" },
	{ 0, "--show-ring devname" },
	{ 1, "-g" },
	{ 0, "-G devname rx 1 rx-mini 2 rx-jumbo 3 tx 4" },
	{ 0, "--set-ring devname rx 1 rx-mini 2 rx-jumbo 3 tx 4" },
	{ 1, "-G devname rx foo" },
	{ 1, "--set-ring devname rx" },
	{ 1, "-G devname foo 1" },
	{ 1, "-G" },
	{ 1, "-k" },
	{ 1, "-K" },
	{ 0, "-i devname" },
	{ 0, "--driver devname" },
	{ 1, "-i" },
	{ 0, "-d devname" },
	{ 0, "--register-dump devname raw on file foo" },
	{ 1, "-d devname raw foo" },
	{ 1, "--register-dump devname file" },
	{ 1, "-d devname foo" },
	{ 1, "-d" },
	{ 0, "-e devname" },
	{ 0, "--eeprom-dump devname raw on offset 1 length 2" },
	{ 1, "-e devname raw foo" },
	{ 1, "--eeprom-dump devname offset foo" },
	{ 1, "-e devname length" },
	{ 1, "--eeprom-dump devname foo" },
	{ 1, "-e" },
	{ 0, "-E devname" },
	{ 0, "--change-eeprom devname magic 0x87654321 offset 0 value 1" },
	{ 0, "-E devname magic 0x87654321 offset 0 length 2" },
	{ 1, "-E" },
	{ 0, "-r devname" },
	{ 0, "--negotiate devname" },
	{ 1, "-r" },
	{ 0, "-p devname" },
	{ 0, "--identify devname 1" },
	{ 1, "-p devname 1 foo" },
	{ 1, "--identify devname foo" },
	{ 1, "-p" },
	/* Argument parsing for -t is specialised */
	{ 0, "-t devname" },
	{ 0, "--test devname online" },
	{ 1, "-t devname foo" },
	{ 1, "--test devname online foo" },
	{ 0, "-S devname" },
	{ 0, "--statistics devname" },
	{ 1, "-S" },
	/* Argument parsing for -n/-u is specialised */
	{ 0, "-n devname rx-flow-hash tcp4" },
	{ 0, "-u devname rx-flow-hash sctp4" },
	{ 0, "--show-nfc devname rx-flow-hash udp6" },
	{ 0, "--show-ntuple devname rx-flow-hash esp6" },
	{ 1, "-n devname rx-flow-hash foo" },
	{ 1, "-u devname rx-flow-hash foo" },
	{ 1, "--show-nfc devname rx-flow-hash" },
	{ 1, "--show-ntuple devname rx-flow-hash" },
	{ 1, "-n" },
	/* Argument parsing for -f is specialised */
	{ 1, "-f devname" },
	{ 0, "--flash devname filename" },
	{ 0, "-f devname filename 1" },
	{ 1, "-f devname filename 1 foo" },
	{ 1, "-f" },
	/* Argument parsing for -N/-U is specialised */
	{ 0, "-N devname rx-flow-hash tcp4 mvtsdfn" },
	{ 0, "--config-ntuple devname rx-flow-hash tcp4 r" },
	{ 1, "-U devname rx-flow-hash tcp4" },
	{ 1, "--config-nfc devname rx-flow-hash foo" },
	{ 1, "-N devname rx-flow-hash" },
	{ 1, "--config-ntuple devname foo" },
	{ 0, "-U devname delete 1" },
	{ 1, "--config-nfc devname delete foo" },
	{ 1, "-N devname delete" },
	{ 0, "--config-ntuple devname flow-type ether src 01:23:45:67:89:ab m cd:ef:01:23:45:67 dst 89:ab:cd:ef:01:23 m 45:67:89:ab:cd:ef proto 0x0123 m 0x4567 vlan 0x89ab m 0xcdef action 0" },
	{ 0, "-U devname flow-type ether src 01:23:45:67:89:ab src-mask cd:ef:01:23:45:67 dst 89:ab:cd:ef:01:23 dst-mask 45:67:89:ab:cd:ef proto 0x0123 proto-mask 0x4567 vlan 0x89ab vlan-mask 0xcdef action 1" },
	{ 1, "--config-nfc devname flow-type ether src 01:23:45:67:89: action 3" },
	{ 1, "-N devname flow-type ether src 01:23:45:67:89 action 4" },
	{ 0, "--config-ntuple devname flow-type ip4 src-ip 0.123.45.67 m 89.0.123.45 dst-ip 67.89.0.123 m 45.67.89.0 tos 1 m 1 l4proto 0x23 m 0x45 l4data 0xfedcba98 m 76543210 vlan 0x89ab m 0xcdef action 6" },
	{ 0, "-U devname flow-type ip4 src-ip 0.123.45.67 src-ip-mask 89.0.123.45 dst-ip 67.89.0.123 dst-ip-mask 45.67.89.0 tos 1 tos-mask 1 l4proto 0x23 l4proto-mask 0x45 l4data 0xfedcba98 l4data-mask 76543210 vlan 0x89ab vlan-mask 0xcdef action 7" },
	{ 0, "--config-nfc devname flow-type tcp4 src-ip 0.123.45.67 m 89.0.123.45 dst-ip 67.89.0.123 m 45.67.89.0 tos 1 m 1 src-port 23456 m 7890 dst-port 12345 m 6789 vlan 0x89ab m 0xcdef action 8" },
	{ 0, "-N devname flow-type tcp4 src-ip 0.123.45.67 src-ip-mask 89.0.123.45 dst-ip 67.89.0.123 dst-ip-mask 45.67.89.0 tos 1 tos-mask 1 src-port 23456 src-port-mask 7890 dst-port 12345 dst-port-mask 6789 vlan 0x89ab vlan-mask 0xcdef action 9" },
	{ 0, "--config-ntuple devname flow-type ah4 src-ip 0.123.45.67 m 89.0.123.45 dst-ip 67.89.0.123 m 45.67.89.0 tos 1 m 1 spi 2 m 3 vlan 0x89ab m 0xcdef action 10" },
	{ 0, "-U devname flow-type ah4 src-ip 0.123.45.67 src-ip-mask 89.0.123.45 dst-ip 67.89.0.123 dst-ip-mask 45.67.89.0 tos 1 tos-mask 1 spi 2 spi-mask 3 vlan 0x89ab vlan-mask 0xcdef action 11" },
	{ 1, "--config-nfc devname flow-type tcp4 action foo" },
	{ 1, "-N devname flow-type foo" },
	{ 1, "--config-ntuple devname flow-type" },
	{ 1, "-U devname foo" },
	{ 1, "-N" },
	{ 1, "-U" },
	{ 0, "-T devname" },
	{ 0, "--show-time-stamping devname" },
	{ 1, "-T" },
	{ 0, "-x devname" },
	{ 0, "--show-rxfh-indir devname" },
	{ 0, "--show-rxfh devname" },
	{ 1, "-x" },
	/* Argument parsing for -X is specialised */
	{ 0, "-X devname equal 2" },
	{ 0, "--set-rxfh-indir devname equal 256" },
	{ 1, "-X devname equal 0" },
	{ 1, "--set-rxfh-indir devname equal foo" },
	{ 1, "-X devname equal" },
	{ 1, "-X devname start" },
	{ 1, "-X devname start 3" },
	{ 0, "-X devname start 4 equal 2" },
	{ 0, "--set-rxfh-indir devname weight 1 2 3 4" },
	{ 0, "--set-rxfh-indir devname start 4 weight 1 2 3 4" },
	{ 0, "--rxfh devname hkey 48:15:6e:bb:d8:bd:6f:b1:a4:c6:7a:c4:76:1c:29:98:da:e1:ae:6c:2e:12:2f:c0:b9:be:61:3d:00:54:35:9e:09:05:c7:d7:93:72:4a:ee" },
	{ 0, "-X devname hkey 48:15:6e:bb:d8:bd:6f:b1:a4:c6:7a:c4:76:1c:29:98:da:e1:ae:6c:2e:12:2f:c0:b9:be:61:3d:00:54:35:9e:09:05:c7:d7:93:72:4a:ee" },
#if 0
	/* XXX These won't fail as expected because we don't parse the
	 * hash key until after the first send_ioctl().  That needs to
	 * be changed before we enable them.
	 */
	{ 1, "--rxfh devname hkey foo" },
	{ 1, "-X devname hkey foo" },
#endif
	{ 0, "--rxfh devname hkey 48:15:6e:bb:d8:bd:6f:b1:a4:c6:7a:c4:76:1c:29:98:da:e1:ae:6c:2e:12:2f:c0:b9:be:61:3d:00:54:35:9e:09:05:c7:d7:93:72:4a:ee weight 1 2 3 4" },
	{ 0, "-X devname weight 1 2 3 4 hkey 48:15:6e:bb:d8:bd:6f:b1:a4:c6:7a:c4:76:1c:29:98:da:e1:ae:6c:2e:12:2f:c0:b9:be:61:3d:00:54:35:9e:09:05:c7:d7:93:72:4a:ee" },
	{ 0, "--rxfh devname hkey 48:15:6e:bb:d8:bd:6f:b1:a4:c6:7a:c4:76:1c:29:98:da:e1:ae:6c:2e:12:2f:c0:b9:be:61:3d:00:54:35:9e:09:05:c7:d7:93:72:4a:ee equal 2" },
	{ 0, "-X devname equal 2 hkey 48:15:6e:bb:d8:bd:6f:b1:a4:c6:7a:c4:76:1c:29:98:da:e1:ae:6c:2e:12:2f:c0:b9:be:61:3d:00:54:35:9e:09:05:c7:d7:93:72:4a:ee" },
	{ 1, "--rxfh devname weight 1 2 3 4 equal 8" },
	{ 1, "-X devname weight 1 2 3 4 equal 8" },
	{ 1, "-X devname foo" },
	{ 1, "-X" },
	{ 0, "-P devname" },
	{ 0, "--show-permaddr devname" },
	{ 1, "-P" },
	{ 0, "-w devname" },
	{ 0, "--get-dump devname data filename" },
	{ 0, "-w devname data filename" },
	{ 1, "--get-dump devname data" },
	{ 1, "-w devname foo" },
	{ 1, "-w" },
	{ 0, "-W devname 1" },
	{ 0, "--set-dump devname 2" },
	{ 1, "-W devname 1 foo" },
	{ 1, "-W devname foo" },
	{ 1, "-W" },
	{ 0, "-l devname" },
	{ 0, "--show-channels devname" },
	{ 1, "-l" },
	{ 0, "-L devname rx 1 tx 2 other 3 combined 4" },
	{ 0, "--set-channels devname rx 1 tx 2 other 3 combined 4" },
	{ 1, "-L devname rx foo" },
	{ 1, "--set-channels devname rx" },
	{ 0, "-L devname" },
	{ 1, "-L" },
	{ 0, "--show-priv-flags devname" },
	{ 1, "--show-priv-flags devname foo" },
	{ 1, "--show-priv-flags" },
	{ 1, "-m" },
	{ 0, "-m devname" },
	{ 1, "--dump-module-eeprom" },
	{ 0, "--dump-module-eeprom devname" },
	{ 1, "--module-info" },
	{ 0, "--module-info devname" },
	{ 0, "-m devname raw on" },
	{ 0, "-m devname raw off" },
	{ 0, "-m devname hex on" },
	{ 0, "-m devname hex off" },
	{ 1, "-m devname hex on raw on" },
	{ 0, "-m devname offset 4 length 6" },
	{ 1, "--show-eee" },
	{ 0, "--show-eee devname" },
	{ 1, "--show-eee devname foo" },
	{ 1, "--set-eee" },
	{ 1, "--set-eee devname" },
	{ 1, "--set-eee devname foo" },
	{ 0, "--set-eee devname eee on" },
	{ 0, "--set-eee devname eee off" },
	{ 1, "--set-eee devname eee foo" },
	{ 0, "--set-eee devname tx-lpi on" },
	{ 0, "--set-eee devname tx-lpi off" },
	{ 1, "--set-eee devname tx-lpi foo" },
	{ 0, "--set-eee devname tx-timer 42 advertise 0x4321" },
	{ 1, "--set-eee devname tx-timer foo" },
	{ 1, "--set-eee devname advertise foo" },
	{ 1, "--set-fec devname" },
	{ 0, "--set-fec devname encoding auto" },
	{ 0, "--set-fec devname encoding off" },
	{ 0, "--set-fec devname encoding baser rs" },
	{ 0, "--set-fec devname encoding auto auto" },
	/* encoding names are validated by kernel with netlink */
	{ !IS_NL, "--set-fec devname encoding foo" },
	{ !IS_NL, "--set-fec devname encoding auto foo" },
	{ !IS_NL, "--set-fec devname encoding none" },
	{ 1, "--set-fec devname auto" },
	/* can't test --set-priv-flags yet */
	{ 0, "-h" },
	{ 0, "--help" },
	{ 0, "--version" },
	{ 1, "--foo" },
	{ 1, "-foo" },
	{ 1, "-0" },
};

int send_ioctl(struct cmd_context *ctx __maybe_unused, void *cmd __maybe_unused)
{
	/* If we get this far then parsing succeeded */
	test_exit(0);
}

#ifdef ETHTOOL_ENABLE_NETLINK
struct nl_socket;
struct nl_msg_buff;

ssize_t nlsock_sendmsg(struct nl_socket *nlsk __maybe_unused,
		       struct nl_msg_buff *altbuff __maybe_unused)
{
	/* If we get this far then parsing succeeded */
	test_exit(0);
}
#endif

int main(void)
{
	struct test_case *tc;
	int test_rc;
	int rc = 0;

	for (tc = test_cases; tc < test_cases + ARRAY_SIZE(test_cases); tc++) {
		if (getenv("ETHTOOL_TEST_VERBOSE"))
			printf("I: Test command line: ethtool %s\n", tc->args);
		test_rc = test_cmdline(tc->args);
		if (test_rc != tc->rc) {
			fprintf(stderr, "E: ethtool %s returns %d\n",
				tc->args, test_rc);
			rc = 1;
		}
	}

	return rc;
}