From 89c45af26deca41cba5dffcc4cae5e653fedc89a Mon Sep 17 00:00:00 2001 From: Chen Hanxiao Date: Mon, 31 Mar 2014 22:25:36 +0800 Subject: [PATCH] interface: check ip address format Signed-off-by: Chen Hanxiao --- tests/xmlparse-xml/interface-test-bridge-ip-out.xml | 2 +- tests/xmlparse.py | 2 +- virtinst/interface.py | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/xmlparse-xml/interface-test-bridge-ip-out.xml b/tests/xmlparse-xml/interface-test-bridge-ip-out.xml index d4cfba046..49e0510fe 100644 --- a/tests/xmlparse-xml/interface-test-bridge-ip-out.xml +++ b/tests/xmlparse-xml/interface-test-bridge-ip-out.xml @@ -15,7 +15,7 @@ - + diff --git a/tests/xmlparse.py b/tests/xmlparse.py index 3e78a5d6e..75189bce5 100644 --- a/tests/xmlparse.py +++ b/tests/xmlparse.py @@ -965,7 +965,7 @@ class XMLParseTest(unittest.TestCase): check("autoconf", True, False) check = self._make_checker(iface.protocols[1].ips[1]) - check("address", "fe80::215:58ff:fe6e:5", "foobar") + check("address", "fe80::215:58ff:fe6e:5", "2002::") check("prefix", 64, 38) # Remove a child interface, verify it's data remains intact diff --git a/virtinst/interface.py b/virtinst/interface.py index c1e0c88d9..4fca76aa2 100644 --- a/virtinst/interface.py +++ b/virtinst/interface.py @@ -23,6 +23,7 @@ Classes for building and installing libvirt interface xml import logging import libvirt +import ipaddr from virtinst import util from virtinst.xmlbuilder import XMLBuilder, XMLChildProperty, XMLProperty @@ -32,7 +33,15 @@ class _IPAddress(XMLBuilder): _XML_PROP_ORDER = ["address", "prefix"] _XML_ROOT_NAME = "ip" - address = XMLProperty("./@address") + ###################### + # Validation helpers # + ###################### + + def _validate_ipaddr(self, addr): + ipaddr.IPAddress(addr) + return addr + + address = XMLProperty("./@address", validate_cb=_validate_ipaddr) prefix = XMLProperty("./@prefix", is_int=True)