ip_tests.sh 5.4 KB

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