|
| 1 | +import threading |
| 2 | + |
| 3 | +from src.device import Device |
| 4 | + |
| 5 | + |
| 6 | +def test_access_connection(switch): |
| 7 | + d1 = Device('a', '192.168.250.1', '255.255.255.0') |
| 8 | + d2 = Device('b', '192.168.250.2', '255.255.255.0') |
| 9 | + |
| 10 | + switch.connect_device_access(d1, 20) |
| 11 | + switch.connect_device_access(d2, 20) |
| 12 | + |
| 13 | + out = d1.run_from_namespace('ping -c 1 192.168.250.2') |
| 14 | + assert '1 packets transmitted, 1 received' in out |
| 15 | + |
| 16 | + out = d2.run_from_namespace('ping -c 1 192.168.250.1') |
| 17 | + assert '1 packets transmitted, 1 received' in out |
| 18 | + |
| 19 | + switch.disconnect_device(d1) |
| 20 | + switch.disconnect_device(d2) |
| 21 | + |
| 22 | + |
| 23 | +def test_trunk_connection(switch): |
| 24 | + d1 = Device('a', '192.168.250.1', '255.255.255.0') |
| 25 | + d2 = Device('b', '192.168.250.2', '255.255.255.0') |
| 26 | + |
| 27 | + switch.connect_device_trunk(d1, 20) |
| 28 | + switch.connect_device_trunk(d2, 20) |
| 29 | + |
| 30 | + out = d1.run_from_namespace('ping -c 1 192.168.250.2') |
| 31 | + assert '1 packets transmitted, 1 received' in out |
| 32 | + |
| 33 | + out = d2.run_from_namespace('ping -c 1 192.168.250.1') |
| 34 | + assert '1 packets transmitted, 1 received' in out |
| 35 | + |
| 36 | + switch.disconnect_device(d1) |
| 37 | + switch.disconnect_device(d2) |
| 38 | + |
| 39 | + |
| 40 | +def test_different_port_type_connection(switch): |
| 41 | + d1 = Device('a', '192.168.250.1', '255.255.255.0') |
| 42 | + d2 = Device('b', '192.168.250.2', '255.255.255.0') |
| 43 | + |
| 44 | + switch.connect_device_trunk(d1, 20) |
| 45 | + switch.connect_device_access(d2, 20) |
| 46 | + |
| 47 | + out = d1.run_from_namespace('ping -c 1 192.168.250.2') |
| 48 | + assert '1 packets transmitted, 1 received' in out |
| 49 | + |
| 50 | + out = d2.run_from_namespace('ping -c 1 192.168.250.1') |
| 51 | + assert '1 packets transmitted, 1 received' in out |
| 52 | + |
| 53 | + switch.disconnect_device(d1) |
| 54 | + switch.disconnect_device(d2) |
| 55 | + |
| 56 | + |
| 57 | +def test_vlans(switch): |
| 58 | + d1 = Device('a', '192.168.250.1', '255.255.255.0') |
| 59 | + d2 = Device('b', '192.168.250.2', '255.255.255.0') |
| 60 | + d3 = Device('c', '192.168.250.3', '255.255.255.0') |
| 61 | + |
| 62 | + switch.connect_device_trunk(d1, 20) |
| 63 | + switch.connect_device_access(d2, 20) |
| 64 | + |
| 65 | + # Note that d3 is in a different vlan |
| 66 | + switch.connect_device_access(d3, 10) |
| 67 | + |
| 68 | + out = d1.run_from_namespace('ping -c 1 192.168.250.2') |
| 69 | + assert '1 packets transmitted, 1 received' in out |
| 70 | + |
| 71 | + out = d2.run_from_namespace('ping -c 1 192.168.250.1') |
| 72 | + assert '1 packets transmitted, 1 received' in out |
| 73 | + |
| 74 | + # The following should fail as d3 is in a different vlan |
| 75 | + out = d3.run_from_namespace('ping -c 1 -W 2 192.168.250.1') |
| 76 | + assert '1 packets transmitted, 0 received' in out |
| 77 | + |
| 78 | + out = d3.run_from_namespace('ping -c 1 -W 2 192.168.250.2') |
| 79 | + assert '1 packets transmitted, 0 received' in out |
| 80 | + |
| 81 | + switch.disconnect_device(d1) |
| 82 | + switch.disconnect_device(d2) |
| 83 | + switch.disconnect_device(d3) |
| 84 | + |
| 85 | + |
| 86 | +def test_tcp_connection(switch): |
| 87 | + d1 = Device('a', '192.168.250.1', '255.255.255.0') |
| 88 | + d2 = Device('b', '192.168.250.2', '255.255.255.0') |
| 89 | + |
| 90 | + switch.connect_device_trunk(d1, 20) |
| 91 | + switch.connect_device_trunk(d2, 20) |
| 92 | + |
| 93 | + def _run_listening_nc(dev): |
| 94 | + out = dev.run_from_namespace('timeout 5s nc -l 0.0.0.0 4444') |
| 95 | + assert 'hello' == out |
| 96 | + |
| 97 | + t = threading.Thread(target=_run_listening_nc, args=(d1, )) |
| 98 | + t.start() |
| 99 | + |
| 100 | + out = d2.run_from_namespace('bash -c "echo hello | nc 192.168.250.1 4444 -w 3"') |
| 101 | + assert '' == out |
| 102 | + |
| 103 | + switch.disconnect_device(d1) |
| 104 | + switch.disconnect_device(d2) |
| 105 | + |
| 106 | + |
| 107 | +def test_udp_connection(switch): |
| 108 | + d1 = Device('a', '192.168.250.1', '255.255.255.0') |
| 109 | + d2 = Device('b', '192.168.250.2', '255.255.255.0') |
| 110 | + |
| 111 | + switch.connect_device_trunk(d1, 20) |
| 112 | + switch.connect_device_access(d2, 20) |
| 113 | + |
| 114 | + def _run_listening_nc(dev): |
| 115 | + out = dev.run_from_namespace('timeout 7s nc -lu 0.0.0.0 4444') |
| 116 | + assert 'hello' == out |
| 117 | + |
| 118 | + t = threading.Thread(target=_run_listening_nc, args=(d1, )) |
| 119 | + t.start() |
| 120 | + |
| 121 | + out = d2.run_from_namespace('bash -c "echo hello | nc -u 192.168.250.1 4444 -w 5"') |
| 122 | + t.join() |
| 123 | + |
| 124 | + assert '' == out |
| 125 | + |
| 126 | + switch.disconnect_device(d1) |
| 127 | + switch.disconnect_device(d2) |
0 commit comments