test_iproute2.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. import infra.basetest
  3. class TestIpRoute2(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_IPROUTE2=y
  7. BR2_TARGET_ROOTFS_CPIO=y
  8. # BR2_TARGET_ROOTFS_TAR is not set
  9. """
  10. def test_run(self):
  11. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  12. self.emulator.boot(arch="armv5",
  13. kernel="builtin",
  14. options=["-initrd", cpio_file])
  15. self.emulator.login()
  16. # Check the program can execute. This also check we are
  17. # actually using the version from the iproute2 package, rather
  18. # than the BusyBox version (which does not understand this
  19. # option).
  20. self.assertRunOk("ip -Version")
  21. # We run simple invocations of iproute2 tools.
  22. self.assertRunOk("ifstat")
  23. self.assertRunOk("ip link show dev lo")
  24. # Buildroot is supposed to have setup the loopback "lo"
  25. # interface. We should be able to ping any address in
  26. # the 127.0.0.0/8 subnet.
  27. addrs = ["127.0.0.1", "127.0.1.2", "127.1.2.3"]
  28. ping_cmd = "ping -c 3 -i 0.2"
  29. for addr in addrs:
  30. self.assertRunOk(f"{ping_cmd} {addr}")
  31. # We now change this 127.0.0.1/8 to a /16.
  32. self.assertRunOk("ip addr del 127.0.0.1/8 dev lo")
  33. self.assertRunOk("ip addr add 127.0.0.1/16 dev lo")
  34. # The IPs in the 127.0.0.0/16 subnet are still supposed to
  35. # ping...
  36. addrs = ["127.0.0.1", "127.0.1.2"]
  37. for addr in addrs:
  38. self.assertRunOk(f"{ping_cmd} {addr}")
  39. # ...but the IP outside is supposed to fail.
  40. _, ret = self.emulator.run(f"{ping_cmd} 127.1.2.3")
  41. self.assertNotEqual(ret, 0)
  42. # We add a prohibited route.
  43. self.assertRunOk("ip route add prohibit 127.0.1.0/24")
  44. # Now, only 127.0.0.1 is supposed to ping...
  45. self.assertRunOk(f"{ping_cmd} 127.0.0.1")
  46. # ...while the other IPs expected to fail.
  47. addrs = ["127.0.1.2", "127.1.2.3"]
  48. for addr in addrs:
  49. _, ret = self.emulator.run(f"{ping_cmd} {addr}")
  50. self.assertNotEqual(ret, 0)
  51. # We should be able to see our prohibited route.
  52. out, ret = self.emulator.run("ip route list")
  53. self.assertEqual(ret, 0)
  54. self.assertEqual(out[0].strip(), "prohibit 127.0.1.0/24")
  55. # We create a new network namespace, and create a new shell
  56. # process in it.
  57. self.assertRunOk("ip netns add br-test")
  58. self.assertRunOk("ip netns exec br-test /bin/sh")
  59. # Since we are in a new namespace, we should no longer see the
  60. # prohibited route. The route list output should be empty.
  61. out, ret = self.emulator.run("ip route list")
  62. self.assertEqual(ret, 0)
  63. self.assertEqual(len(out), 0)