def test_update_v6_message_adds_route(self): path_attributes = { "as_path" : "65032 65011 65002", "origin" : "EGP", "mp_reach_nlri" : { "next_hop" : [ IP6Address.from_string("2001:db8:1::242:ac11:2"), IP6Address.from_string("fe80::42:acff:fe11:2"), ], "nlri" : [ IP6Prefix.from_string("2001:db4::/127"), ] } } route_attributes = { "prefix" : IP6Prefix.from_string("2001:db4::/127"), "next_hop" : IP6Address.from_string("2001:db8:1::242:ac11:2"), "as_path" : "65032 65011 65002", "origin" : "EGP" } message = BgpUpdateMessage([], path_attributes, []) self.state_machine.event(EventMessageReceived(message), self.tick) self.assertEqual(self.state_machine.state, "established") self.assertEqual(self.state_machine.route_updates.qsize(), 1) self.assertEqual(self.state_machine.route_updates.get(), RouteAddition(**route_attributes))
def test_update_message_advances_to_idle(self): path_attributes = { "next_hop" : IP4Address.from_string("5.4.3.2"), "as_path" : "65032 65011 65002", "origin" : "EGP" } message = BgpUpdateMessage([], path_attributes, [IP4Prefix.from_string("192.168.0.0/16")]) with self.assertRaises(IdleError) as context: self.state_machine.event(EventMessageReceived(message), self.tick) self.assertEqual(self.state_machine.state, "idle")
def test_update_message_new_routes_packs_as4(self): expected_serialised_message = build_byte_string("000000274001010040020802035ba0fe08fdebc0110e020300bc614e0000fe080000fdeb400304ac1900042009090909") nlri = [ IP4Prefix.from_string("9.9.9.9/32"), ] path_attributes = { "next_hop": IP4Address.from_string("172.25.0.4"), "origin": "IGP", "as_path": "23456 65032 65003", "as4_path": "12345678 65032 65003" } message = BgpUpdateMessage([], path_attributes, nlri) serialised_message = BgpMessagePacker().pack(message) self.assertEqual(serialised_message[19:], expected_serialised_message)
def test_update_message_new_routes_packs(self): expected_serialised_message = build_byte_string("0000000e40010101400200400304c0a80021080a17c0a840") nlri = [ IP4Prefix.from_string("10.0.0.0/8"), IP4Prefix.from_string("192.168.64.0/23") ] path_attributes = { "next_hop": IP4Address.from_string("192.168.0.33"), "origin": "EGP", "as_path": "" } message = BgpUpdateMessage([], path_attributes, nlri) serialised_message = BgpMessagePacker().pack(message) self.assertEqual(serialised_message[19:], expected_serialised_message)
def test_update_message_new_routes_packs_as4_new(self): expected_serialised_message = build_byte_string("0000001c4001010040020e020300bc614e0000fe080001b2e5400304ac1900042009090909") nlri = [ IP4Prefix.from_string("9.9.9.9/32"), ] path_attributes = { "next_hop": IP4Address.from_string("172.25.0.4"), "origin": "IGP", "as_path": "12345678 65032 111333" } message = BgpUpdateMessage([], path_attributes, nlri) packer = BgpMessagePacker() packer.capabilities = {"fourbyteas": 12345} serialised_message = packer.pack(message) self.assertEqual(serialised_message[19:], expected_serialised_message)
def test_update_v6_message_withdrawn_routes_packs(self): expected_serialised_message = build_byte_string("0000002d800f2a0002017f20010db40000000000000000000000003020010db100003320010db20000002f20010db30000") path_attributes = { "mp_unreach_nlri": { "withdrawn_routes": [ IP6Prefix.from_string("2001:db4::/127"), IP6Prefix.from_string("2001:db1::/48"), IP6Prefix.from_string("2001:db2::/51"), IP6Prefix.from_string("2001:db3::/47") ] } } message = BgpUpdateMessage([], path_attributes, []) serialised_message = BgpMessagePacker().pack(message) self.assertEqual(serialised_message[19:], expected_serialised_message)
def test_update_message_adds_route(self): path_attributes = { "next_hop" : IP4Address.from_string("5.4.3.2"), "as_path" : "65032 65011 65002", "origin" : "EGP" } route_attributes = { "prefix" : IP4Prefix.from_string("192.168.0.0/16"), "next_hop" : IP4Address.from_string("5.4.3.2"), "as_path" : "65032 65011 65002", "origin" : "EGP" } message = BgpUpdateMessage([], path_attributes, [IP4Prefix.from_string("192.168.0.0/16")]) self.state_machine.event(EventMessageReceived(message), self.tick) self.assertEqual(self.state_machine.state, "established") self.assertEqual(self.state_machine.route_updates.qsize(), 1) self.assertEqual(self.state_machine.route_updates.get(), RouteAddition(**route_attributes))
def test_update_v6_message_new_routes_packs(self): expected_serialised_message = build_byte_string("0000004740010100400200800e3d0002012020010db80001000000000242ac110002fe800000000000000042acfffe110002007f20010db40000000000000000000000002f20010db30000") path_attributes = { "origin": "IGP", "as_path": "", "mp_reach_nlri": { "next_hop": [ IP6Address.from_string("2001:db8:1::242:ac11:2"), IP6Address.from_string("fe80::42:acff:fe11:2") ], "nlri": [ IP6Prefix.from_string("2001:db4::/127"), IP6Prefix.from_string("2001:db3::/47") ] } } message = BgpUpdateMessage([], path_attributes, []) serialised_message = BgpMessagePacker().pack(message) self.assertEqual(serialised_message[19:], expected_serialised_message)
def test_update_message_withdrawn_routes_packs(self): expected_serialised_message = build_byte_string("0004180a01010000") message = BgpUpdateMessage([IP4Prefix.from_string("10.1.1.0/24")], {}, []) serialised_message = BgpMessagePacker().pack(message) self.assertEqual(serialised_message[19:], expected_serialised_message)
def test_update_message_removes_route(self): message = BgpUpdateMessage([IP4Prefix.from_string("192.168.0.0/16")], [], []) self.state_machine.event(EventMessageReceived(message), self.tick) self.assertEqual(self.state_machine.state, "established") self.assertEqual(self.state_machine.route_updates.qsize(), 1) self.assertEqual(self.state_machine.route_updates.get(), RouteRemoval(IP4Prefix.from_string("192.168.0.0/16")))