• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2014-2019 Oracle and/or its affiliates. All Rights Reserved.
4# Copyright (c) International Business Machines Corp., 2001
5# Author:       Manoj Iyer, manjo@mail.utexas.edu
6#
7# Description:  Test basic functionality of ip command in route2 package
8
9TST_CNT=6
10TST_SETUP="init"
11TST_TESTFUNC="test"
12TST_CLEANUP="cleanup"
13TST_NEEDS_TMPDIR=1
14TST_NEEDS_ROOT=1
15TST_NEEDS_CMDS="cat awk diff"
16TST_NEEDS_DRIVERS="dummy"
17
18. tst_net.sh
19
20rm_dummy=
21
22init()
23{
24	tst_res TINFO "inititalizing tests"
25
26	iface=ltp_dummy
27	lsmod | grep -q dummy || rm_dummy=1
28
29	ROD ip link add $iface type dummy
30
31	ip4_addr=${IPV4_NET16_UNUSED}.6.6
32	ROD ip addr add ${ip4_addr}/24 dev $iface
33
34	cat > tst_ip02.exp <<-EOF
35	1:
36	link/loopback
37	2:
38	link/ether
39	3:
40	link/ether
41	EOF
42
43	if [ $? -ne 0 ]; then
44		tst_brk TBROK "can't create expected output for test02"
45	fi
46}
47
48cleanup()
49{
50	[ -n "$iface" -a -d /sys/class/net/$iface ] && ip link del $iface
51
52	[ "$rm_dummy" ] && modprobe -r dummy
53
54	# test #5
55	[ "$ip4_addr" ] && ip route show | grep -q $ip4_addr && ip route del $ip4_addr
56}
57
58test1()
59{
60	tst_res TINFO "test 'ip link set' command"
61	tst_res TINFO "changing mtu size of $iface device"
62
63	MTUSZ_BAK=$(cat /sys/class/net/${iface}/mtu)
64	ip link set ${iface} mtu 1281
65	if [ $? -ne 0 ]; then
66		tst_res TFAIL "ip command failed"
67		return
68	fi
69
70	MTUSZ=$(cat /sys/class/net/${iface}/mtu)
71	if [ $MTUSZ -eq 1281 ]; then
72		tst_res TPASS "successfully changed mtu size"
73		ip link set $iface mtu $MTUSZ_BAK
74	else
75		tst_res TFAIL "MTU value set to $MTUSZ, but expected 1281"
76	fi
77}
78
79test2()
80{
81	tst_res TINFO "test 'ip link show' command (list device attributes)"
82
83	ip link show $iface | grep $iface > /dev/null
84	if [ $? -ne 0 ]; then
85		tst_res TFAIL "'ip link show $iface' command failed"
86		return
87	fi
88
89	tst_res TPASS "$iface correctly listed"
90}
91
92test3()
93{
94	tst_res TINFO "test 'ip addr' command with loopback dev"
95	tst_res TINFO "add a new protocol address to the device"
96
97	ip addr add 127.6.6.6/24 dev lo
98	if [ $? -ne 0 ]; then
99		tst_res TFAIL "'ip addr add' command failed"
100		return
101	fi
102
103	tst_res TINFO "show protocol address"
104	ip addr show dev lo | grep 127.6.6.6 > /dev/null
105	if [ $? -ne 0 ]; then
106		tst_res TFAIL "'ip addr show' command failed"
107		return
108	fi
109
110	tst_res TINFO "delete protocol address"
111	ip addr del 127.6.6.6/24 dev lo
112	if [ $? -ne 0 ]; then
113		tst_res TFAIL "'ip addr del' command failed"
114		return
115	fi
116
117	ip addr show dev lo | grep 127.6.6.6 > /dev/null
118	if [ $? -eq 0 ]; then
119		tst_res TFAIL "ip addr del command failed"
120		return
121	fi
122
123	tst_res TPASS "'ip addr' command successfully tested"
124}
125
126test4()
127{
128	local taddr="$(tst_ipaddr_un)"
129	local tdev="$(tst_iface)"
130	tst_res TINFO "test 'ip neigh' command"
131	tst_res TINFO "add a new neighbor (or replace existed)"
132	ip neigh replace $taddr lladdr 00:00:00:00:00:00 dev $tdev nud reachable
133	if [ $? -ne 0 ]; then
134		tst_res TFAIL "'ip neigh replace' command failed"
135		return
136	fi
137
138	tst_res TINFO "show all neighbor entries in arp tables"
139	echo "$taddr dev $tdev lladdr 00:00:00:00:00:00 REACHABLE" > tst_ip.exp
140
141	ip neigh show $taddr | head -n1 > tst_ip.out 2>&1
142	if [ $? -ne 0 ]; then
143		tst_res TFAIL "'ip neigh show' command failed"
144		return
145	fi
146
147	diff -iwB tst_ip.out tst_ip.exp
148	if [ $? -ne 0 ]; then
149		tst_res TFAIL "expected output differs from actual output"
150		return
151	fi
152
153	tst_res TINFO "delete neighbor from the arp table"
154
155	ip neigh del $taddr dev $tdev
156	if [ $? -ne 0 ]; then
157		tst_res TFAIL "'ip neigh del' command failed"
158		return
159	fi
160
161	ip neigh show | grep $taddr | grep -v ' FAILED$' > /dev/null
162	if [ $? -eq 0 ]; then
163		tst_res TFAIL "$taddr still listed in arp"
164		return
165	fi
166
167	tst_res TPASS "'ip neigh' command successfully tested"
168}
169
170test5()
171{
172	tst_res TINFO "test 'ip route add/del' commands"
173
174	ROD ip route add $ip4_addr via 127.0.0.1
175
176	tst_res TINFO "show all route entries in route table"
177
178	# create expected output file.
179	cat > tst_ip.exp <<-EOF
180$ip4_addr via 127.0.0.1 dev lo
181	EOF
182
183	ip route show | grep "$ip4_addr via 127.0.0.1 dev lo" > tst_ip.out 2>&1
184	if [ $? -ne 0 ]; then
185		tst_res TFAIL "'ip route show' command failed"
186		return
187	fi
188
189	diff -iwB tst_ip.out tst_ip.exp
190	if [ $? -ne 0 ]; then
191		tst_res TFAIL "'ip route show' did not list new route"
192		return
193	fi
194
195	tst_res TINFO "delete route from the route table"
196
197	ROD ip route del $ip4_addr via 127.0.0.1
198
199	ip route show | grep 127.0.0.1 > /dev/null
200	if [ $? -eq 0 ]; then
201		tst_res TFAIL "route not deleted"
202		return
203	fi
204
205	tst_res TPASS "'ip route' command successfully tested"
206}
207
208test6()
209{
210	tst_res TINFO "test 'ip maddr add/del' commands"
211	tst_res TINFO "adding a new multicast addr"
212
213	ip maddr add 66:66:00:00:00:66 dev $iface
214	if [ $? -ne 0 ]; then
215		tst_res TFAIL "ip maddr add command failed"
216		return
217	fi
218
219	tst_res TINFO "show all multicast addr entries"
220
221	cat > tst_ip.exp <<-EOF
222        link  66:66:00:00:00:66 static
223	EOF
224
225	ip maddr show | grep "66:66:00:00:00:66" > tst_ip.out 2>&1
226	if [ $? -ne 0 ]; then
227		tst_res TFAIL "'ip maddr show' command failed"
228		return
229	fi
230
231	diff -iwB tst_ip.out tst_ip.exp
232	if [ $? -ne 0 ]; then
233		tst_res TFAIL "multicast addr not added to $iface"
234		return
235	fi
236
237	tst_res TINFO "delete multicast address"
238
239	ip maddr del 66:66:00:00:00:66 dev $iface
240	if [ $? -ne 0 ]; then
241		tst_res TFAIL "'ip maddr del' command failed"
242		return
243	fi
244
245	ip maddr show | grep "66:66:00:00:00:66" > /dev/null
246	if [ $? -eq 0 ]; then
247		tst_res TFAIL "66:66:00:00:00:66 is not deleted"
248		return
249	fi
250
251	tst_res TPASS "'ip maddr' command successfully tested"
252}
253
254tst_run
255