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_parses(self): serialised_message = build_byte_string("0000000e40010101400200400304c0a80021080a") message = BgpMessageParser().parse(BgpMessage.UPDATE_MESSAGE, serialised_message) self.assertEqual(message.nlri[0], IP4Prefix.from_string("10.0.0.0/8")) self.assertEqual(message.path_attributes["next_hop"], IP4Address.from_string("192.168.0.33")) self.assertEqual(message.path_attributes["origin"], "EGP") self.assertEqual(message.path_attributes["as_path"], "")
def test_update_message_new_routes_parses_as4(self): serialised_message = build_byte_string("000000274001010040020802035ba0fe08fdebc0110e020300bc614e0000fe080000fdeb400304ac1900042009090909") message = BgpMessageParser().parse(BgpMessage.UPDATE_MESSAGE, serialised_message) self.assertEqual(message.nlri, [IP4Prefix.from_string("9.9.9.9/32")]) self.assertEqual(message.path_attributes["next_hop"], IP4Address.from_string("172.25.0.4")) self.assertEqual(message.path_attributes["origin"], "IGP") self.assertEqual(message.path_attributes["as_path"], "23456 65032 65003") self.assertEqual(message.path_attributes["as4_path"], "12345678 65032 65003")
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_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_parses_as4_new(self): serialised_message = build_byte_string("0000001c4001010040020e020300bc614e0000fe080001b2e5400304ac1900042009090909") parser = BgpMessageParser() parser.capabilities = { "fourbyteas": 12345 } message = parser.parse(BgpMessage.UPDATE_MESSAGE, serialised_message) self.assertEqual(message.nlri, [IP4Prefix.from_string("9.9.9.9/32")]) self.assertEqual(message.path_attributes["next_hop"], IP4Address.from_string("172.25.0.4")) self.assertEqual(message.path_attributes["origin"], "IGP") self.assertEqual(message.path_attributes["as_path"], "12345678 65032 111333")
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_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_keepalive_message_sends_all_routes(self): self.tick += 3600 self.state_machine.routes_to_advertise = [ RouteAddition( IP4Prefix.from_string("10.0.0.0/8"), IP4Address.from_string("192.168.1.33"), "", "IGP" ), RouteAddition( IP4Prefix.from_string("192.168.64.0/23"), IP4Address.from_string("192.168.1.33"), "", "IGP" ), RouteAddition( IP4Prefix.from_string("192.168.128.0/23"), IP4Address.from_string("192.168.1.34"), "", "IGP" ) ] message = BgpKeepaliveMessage() self.state_machine.event(EventMessageReceived(message), self.tick) self.assertEqual(self.state_machine.state, "established") self.assertTrue(self.state_machine.timers["hold"].running()) self.assertFalse(self.state_machine.timers["hold"].expired(self.tick)) self.assertEqual(self.state_machine.output_messages.qsize(), 2) first_update = self.state_machine.output_messages.get() second_update = self.state_machine.output_messages.get() self.assertTrue(isinstance(first_update, BgpUpdateMessage)) self.assertTrue(isinstance(second_update, BgpUpdateMessage)) self.assertEqual(first_update.path_attributes["next_hop"], IP4Address.from_string("192.168.1.33")) self.assertEqual(first_update.nlri, [ IP4Prefix.from_string("10.0.0.0/8"), IP4Prefix.from_string("192.168.64.0/23") ]) self.assertEqual(second_update.path_attributes["next_hop"], IP4Address.from_string("192.168.1.34")) self.assertEqual(second_update.nlri, [ IP4Prefix.from_string("192.168.128.0/23") ])
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_withdrawn_routes_parses(self): serialised_message = build_byte_string("0004180a01010000") message = BgpMessageParser().parse(BgpMessage.UPDATE_MESSAGE, serialised_message) self.assertEqual(message.withdrawn_routes[0], IP4Prefix.from_string("10.1.1.0/24"))
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")))