예제 #1
0
  def test_withdraw_message(self):
    """Check the format of the output WITHDRAW messages."""

    withdraw = Withdraw(EmulatedRIB())
    information = RouteInformation(64497, None, None)
    assert withdraw.message(withdraw4, information) == expected_regular_withdraw4

    information = HijackInformation(64497, 666, None, None)
    assert withdraw.message(withdraw4, information) == expected_hijack_withdraw4
예제 #2
0
  def test_regular_withdraw(self):
    """Check that a WITHDRAW is correctly processed."""

    rib = EmulatedRIB()

    # Process an UPDATE
    route = Route(rib)
    route.process(update4)

    # Process a WITHDRAW
    withdraw = Withdraw(rib)
    route_messages, hijack_messages = withdraw.process(withdraw4)

    assert route_messages == [ expected_regular_withdraw4 ]
    assert len(hijack_messages) == 0
    assert len(rib.nodes()) == 0
예제 #3
0
  def test_withdraw_different_peers(self):
    """Check that a WITHDRAW only applies to a peer."""

    rib = EmulatedRIB()

    # Process an UPDATE
    route = Route(rib)
    route.process(update4)
    route.process(update4_2)

    # Process a WITHDRAW
    withdraw = Withdraw(rib)
    route_messages, hijack_messages = withdraw.process(withdraw4)

    assert route_messages == [ expected_regular_withdraw4 ]
    assert len(hijack_messages) == 0
    assert len(rib.nodes()) == 1
예제 #4
0
  def test_useless_withdraw(self):
    """Check that a useless WITHDRAW does nothing."""

    rib = EmulatedRIB()

    # Process an UPDATE
    route = Route(rib)
    route.process(update4)

    # Process a hijack
    hijack = Hijack(rib, 0)
    hijack.process(hijack4_specific)

    # Process a uselss WITHDRAW
    withdraw = Withdraw(rib)
    route_messages, hijack_messages = withdraw.process(useless_withdraw4)

    assert len(route_messages + hijack_messages) == 0
예제 #5
0
  def test_route_and_hijack_withdraw(self):
    """Check that a WITHDRAW concerning a route and a hijack is correctly processed."""

    rib = EmulatedRIB()

    # Process an UPDATE
    route = Route(rib)
    route.process(update4)

    # Process the hijack
    hijack = Hijack(rib, "F")
    hijack.process(hijack4)

    # Process a WITHDRAW
    withdraw = Withdraw(rib)
    route_messages, hijack_messages = withdraw.process(withdraw4)

    assert route_messages == [ expected_regular_withdraw4 ]
    assert hijack_messages == [ expected_hijack_withdraw4 ]
    assert len(rib.nodes()) == 0
예제 #6
0
  def test_hijack_withdraw(self):
    """Check that a WITHDRAW concerning a hijack is correctly processed."""

    rib = EmulatedRIB()
    rib.set_access_time(0)

    # Process an UPDATE
    route = Route(rib)
    route.process(update4)

    # Process the hijack
    hijack = Hijack(rib, "F")
    hijack.process(hijack4_specific)

    # Process a WITHDRAW
    withdraw = Withdraw(rib)
    route_messages, hijack_messages = withdraw.process(withdraw4_2)

    assert len(route_messages) == 0
    assert hijack_messages == [ expected_specific_hijack_withdraw4 ]

    ri = RouteInformation(origin_asn=64497, peer_as=64496, peer_ip='127.0.0.1')
    nodes_data = map(lambda x: (x.prefix, x.data), rib.nodes())
    assert nodes_data == [ ("1.2.3.0/24", { "routes_information": { ri: 0 } }) ]
예제 #7
0
  def test_hijacks_untracked(self):
    """Check if hijacks from an untracked AS are correctly processed."""

    # Setup the internal objects
    rib = EmulatedRIB()
    route = Route(rib)
    hijack = Hijack(rib, "U")
    withdraw = Withdraw(rib)

    # Process the /32 update
    untracked_update1 = InternalMessage(0, "collector", 0, "127.0.0.1",
                                              "2011:db8::/32", 10, "0 10")
    expected_untracked_update1 = collections.OrderedDict([ ("timestamp", 0),
                                         ("collector", "collector"),
                                         ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                         ("action", "A"),
                                         ("prefix", "2011:db8::/32"), 
                                         ("as_path", "0 10"), ("asn", 10)])
    messages = route.process(untracked_update1)
    assert messages == [ expected_untracked_update1 ]
    assert len(rib.nodes()) == 1

    # Process the /48 update
    untracked_update2 = InternalMessage(1, "collector", 0, "127.0.0.1",
                                              "2011:db8:10::/48", 11, "0 11")
    expected_untracked_hijack2 = collections.OrderedDict([ ("timestamp", 1),
                                         ("collector", "collector"),
                                         ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                         ("type", "U"),
                                         ("announce", collections.OrderedDict([ ("prefix", "2011:db8:10::/48"), 
                                             ("asn", 11), ("as_path", "0 11") ])),
                                         ("conflict_with", collections.OrderedDict([ ("prefix", "2011:db8::/32"),
                                             ("asn", 10) ])), ("asn", 10) ] )
    assert hijack.process(untracked_update2) == [ expected_untracked_hijack2 ]
    assert len(rib.nodes()) == 2

    # Process the /32 withdraw
    untracked_update3 = InternalMessage(2, "collector", 0, "127.0.0.1",
                                              "2011:db8::/32", 10, "0 10")
    expected_untracked_update3 = collections.OrderedDict([ ("timestamp", 2),
                                         ("collector", "collector"),
                                         ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                         ("action", "W"),
                                         ("prefix", "2011:db8::/32"), 
                                         ("asn", 10)])
    route_messages, hijack_messages = withdraw.process(untracked_update3)
    assert route_messages == [ expected_untracked_update3 ]
    assert hijack_messages == []
    assert len(rib.nodes()) == 1

    # Process the /48 withdraw
    untracked_update4 = InternalMessage(3, "collector", 0, "127.0.0.1",
                                        "2011:db8:10::/48", 11, "0 11")
    expected_untracked_update4 = collections.OrderedDict([ ("timestamp", 3),
                                         ("collector", "collector"),
                                         ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                         ("type", "W"),
                                         ("withdraw", collections.OrderedDict([("prefix", "2011:db8:10::/48"), ("asn", 11)])),
                                         ("asn", 10)])
    route_messages, hijack_messages = withdraw.process(untracked_update4)
    assert route_messages == []
    assert hijack_messages == [ expected_untracked_update4 ]
    assert len(rib.nodes()) == 0
예제 #8
0
  def test_hijacks_different_peers(self):
    """Check if hijacks from different peers are correctly processed."""

    # Setup the internal objects
    rib = EmulatedRIB()
    route = Route(rib)
    hijack = Hijack(rib, "U")
    withdraw = Withdraw(rib)

    # A prefix is seen from peer_as 0
    different_peers_update1 = InternalMessage(0, "collector", 0, "127.0.0.1",
                                              "2011:db8::/32", 10, "0 10")
    expected_different_peers_update1 = collections.OrderedDict([ ("timestamp", 0),
                                         ("collector", "collector"),
                                         ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                         ("action", "A"),
                                         ("prefix", "2011:db8::/32"), 
                                         ("as_path", "0 10"), ("asn", 10)])
    messages = route.process(different_peers_update1)
    assert messages == [ expected_different_peers_update1 ]

    # The same prefix is seen from peer_as 1
    different_peers_update2 = InternalMessage(0, "collector", 1, "127.0.0.2",
                                              "2011:db8::/32", 10, "1 10")
    expected_different_peers_update2 = collections.OrderedDict([ ("timestamp", 0),
                                         ("collector", "collector"),
                                         ("peer_as", 1), ("peer_ip", "127.0.0.2"),
                                         ("action", "A"),
                                         ("prefix", "2011:db8::/32"), 
                                         ("as_path", "1 10"), ("asn", 10)])
    messages = route.process(different_peers_update2)
    assert messages == [ expected_different_peers_update2 ]

    # A more specific prefix is seen from peer_as 0
    different_peers_update3 = InternalMessage(1, "collector", 0, "127.0.0.1",
                                              "2011:db8:10::/48", 11, "0 11")
    expected_different_peers_update3 = collections.OrderedDict([ ("timestamp", 1),
                                         ("collector", "collector"),
                                         ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                         ("action", "A"),
                                         ("prefix", "2011:db8:10::/48"), 
                                         ("as_path", "0 11"), ("asn", 11)])
    expected_different_peers_hijack3 = collections.OrderedDict([ ("timestamp", 1),
                                         ("collector", "collector"),
                                         ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                         ("type", "U"),
                                         ("announce", collections.OrderedDict([ ("prefix", "2011:db8:10::/48"), 
                                             ("asn", 11), ("as_path", "0 11") ])),
                                         ("conflict_with", collections.OrderedDict([ ("prefix", "2011:db8::/32"),
                                             ("asn", 10) ])), ("asn", 10) ] )
    messages = route.process(different_peers_update3)
    assert messages == [ expected_different_peers_update3 ]
    assert hijack.process(different_peers_update3) == [ expected_different_peers_hijack3 ]

    # A more specific prefix is seen from peer_as 1
    different_peers_update4 = InternalMessage(1, "collector", 1, "127.0.0.2",
                                              "2011:db8:10::/48", 11, "1 11")
    expected_different_peers_update4 = collections.OrderedDict([ ("timestamp", 1),
                                         ("collector", "collector"),
                                         ("peer_as", 1), ("peer_ip", "127.0.0.2"),
                                         ("action", "A"),
                                         ("prefix", "2011:db8:10::/48"), 
                                         ("as_path", "1 11"), ("asn", 11)])
    expected_different_peers_hijack4 = collections.OrderedDict([ ("timestamp", 1),
                                         ("collector", "collector"),
                                         ("peer_as", 1), ("peer_ip", "127.0.0.2"),
                                         ("type", "U"),
                                         ("announce", collections.OrderedDict([ ("prefix", "2011:db8:10::/48"), 
                                             ("asn", 11), ("as_path", "1 11") ])),
                                         ("conflict_with", collections.OrderedDict([ ("prefix", "2011:db8::/32"),
                                             ("asn", 10) ])), ("asn", 10) ] )
    messages = route.process(different_peers_update4)
    assert messages == [ expected_different_peers_update4 ]
    assert hijack.process(different_peers_update4) == [ expected_different_peers_hijack4 ]


    # A withdraw is received from the /32 on peer_as 0
    different_peers_withdraw5 = InternalMessage(2, "collector", 0, "127.0.0.1",
                                                "2011:db8::/32", 10, "0 10")
    expected_different_peers_withdraw5 = collections.OrderedDict([("timestamp", 2),
                                           ("collector", "collector"),
                                           ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                           ("action", "W"),
                                           ("prefix", "2011:db8::/32"), ("asn", 10)])
    route_messages, hijack_messages =  withdraw.process(different_peers_withdraw5)
    assert route_messages == [ expected_different_peers_withdraw5 ]
    assert hijack_messages == []

    # A withdraw is received from the /32 on peer_as 1
    different_peers_withdraw6 = InternalMessage(2, "collector", 1, "127.0.0.2",
                                                "2011:db8::/32", 10, "1 10")
    expected_different_peers_withdraw6 = collections.OrderedDict([("timestamp", 2),
                                           ("collector", "collector"),
                                           ("peer_as", 1), ("peer_ip", "127.0.0.2"),
                                           ("action", "W"),
                                           ("prefix", "2011:db8::/32"), ("asn", 10)])
    route_messages, hijack_messages =  withdraw.process(different_peers_withdraw6)
    assert route_messages == [ expected_different_peers_withdraw6 ]
    assert hijack_messages == []

    # A withdraw is received from the /48 on peer_as 0
    different_peers_withdraw6= InternalMessage(3, "collector", 0, "127.0.0.1",
                                                 "2011:db8:10::/48", 11, "0 11")
    expected_different_peers_withdraw6 = collections.OrderedDict([("timestamp", 3),
                                          ("collector", "collector"),
                                          ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                          ("action", "W"),
                                          ("prefix", "2011:db8:10::/48"), ("asn", 11)])
    expected_different_peers_withdraw6_hijack = collections.OrderedDict([("timestamp", 3),
                                                  ("collector", "collector"),
                                                  ("peer_as", 0), ("peer_ip", "127.0.0.1"),
                                                  ("type", "W"),
                                                  ("withdraw", collections.OrderedDict([("prefix", "2011:db8:10::/48"), ("asn", 11)])),
                                                  ("asn", 10)])
    route_messages, hijack_messages = withdraw.process(different_peers_withdraw6)
    assert route_messages == [ expected_different_peers_withdraw6 ]
    assert hijack_messages == [ expected_different_peers_withdraw6_hijack]

    # A withdraw is received from the /48 on peer_as 1
    different_peers_withdraw7= InternalMessage(3, "collector", 1, "127.0.0.2",
                                                 "2011:db8:10::/48", 11, "1 11")
    expected_different_peers_withdraw7 = collections.OrderedDict([("timestamp", 3),
                                          ("collector", "collector"),
                                          ("peer_as", 1), ("peer_ip", "127.0.0.2"),
                                          ("action", "W"),
                                          ("prefix", "2011:db8:10::/48"), ("asn", 11)])
    expected_different_peers_withdraw7_hijack = collections.OrderedDict([("timestamp", 3),
                                                  ("collector", "collector"),
                                                  ("peer_as", 1), ("peer_ip", "127.0.0.2"),
                                                  ("type", "W"),
                                                  ("withdraw", collections.OrderedDict([("prefix", "2011:db8:10::/48"), ("asn", 11)])),
                                                  ("asn", 10)])
    route_messages, hijack_messages = withdraw.process(different_peers_withdraw7)
    assert route_messages == [ expected_different_peers_withdraw7 ]
    assert hijack_messages == [ expected_different_peers_withdraw7_hijack]

    assert len(rib.nodes()) == 0