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