From 051c29078188c01d5c451b60a003ba9ec00a2822 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Thu, 1 May 2014 17:02:59 +0900 Subject: [PATCH] Make the test more robust with an unreachable rule This ensures that even if the system has a default route, sends will return an error unless the socket has explicitly selected an interface. Change-Id: I3f0ddd88a1b679fa3fb47ab7b78616fe7bf3ca9f --- tests/net_test/iproute.py | 15 +++++++++++---- tests/net_test/mark_test.py | 9 +++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/net_test/iproute.py b/tests/net_test/iproute.py index f3560c6e..9468ff0d 100644 --- a/tests/net_test/iproute.py +++ b/tests/net_test/iproute.py @@ -293,8 +293,9 @@ class IPRoute(object): Args: version: An integer, 4 or 6. is_add: True to add a rule, False to delete it. - table: The table to add/delete the rule from. + table: If nonzero, rule looks up this table. If 0, it returns ENETUNREACH. match_nlattr: A blob of struct nlattrs that express the match condition. + If None, match everything. priority: An integer, the priority. Raises: @@ -303,11 +304,14 @@ class IPRoute(object): """ # Create a struct rtmsg specifying the table and the given match attributes. family = self._AddressFamily(version) + rule_type = RTN_UNICAST if table else RTN_UNREACHABLE rtmsg = RTMsg((family, 0, 0, 0, RT_TABLE_UNSPEC, - RTPROT_STATIC, RT_SCOPE_UNIVERSE, RTN_UNICAST, 0)).Pack() + RTPROT_STATIC, RT_SCOPE_UNIVERSE, rule_type, 0)).Pack() rtmsg += self._NlAttrU32(FRA_PRIORITY, priority) - rtmsg += match_nlattr - rtmsg += self._NlAttrU32(FRA_TABLE, table) + if match_nlattr: + rtmsg += match_nlattr + if table: + rtmsg += self._NlAttrU32(FRA_TABLE, table) # Create a netlink request containing the rtmsg. command = RTM_NEWRULE if is_add else RTM_DELRULE @@ -326,6 +330,9 @@ class IPRoute(object): self._NlAttrU32(EXPERIMENTAL_FRA_UID_END, end)) return self._Rule(version, is_add, table, nlattr, priority) + def UnreachableRule(self, version, is_add, priority): + return self._Rule(version, is_add, None, None, priority=priority) + def _GetRTMsg(self, data): """Parses a RTMsg into a header and a dictionary of attributes.""" # Parse the netlink and rtmsg headers. diff --git a/tests/net_test/mark_test.py b/tests/net_test/mark_test.py index e6751c50..fcdb16de 100755 --- a/tests/net_test/mark_test.py +++ b/tests/net_test/mark_test.py @@ -482,6 +482,9 @@ class MultiNetworkTest(net_test.NetworkTest): cls.SendRA(netid) cls._RunSetupCommands(netid, True) + for version in [4, 6]: + cls.iproute.UnreachableRule(version, True, 1000) + # Uncomment to look around at interface and rule configuration while # running in the background. (Once the test finishes running, all the # interfaces and rules are gone.) @@ -489,6 +492,12 @@ class MultiNetworkTest(net_test.NetworkTest): @classmethod def tearDownClass(cls): + for version in [4, 6]: + try: + cls.iproute.UnreachableRule(version, False, 1000) + except IOError: + pass + for netid in cls.tuns: cls._RunSetupCommands(netid, False) cls.tuns[netid].close() -- 2.11.0