[CalendarServer-changes] [10694] CalendarServer/trunk/twext/who

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 11 16:37:11 PST 2013


Revision: 10694
          http://trac.calendarserver.org//changeset/10694
Author:   wsanchez at apple.com
Date:     2013-02-11 16:37:11 -0800 (Mon, 11 Feb 2013)
Log Message:
-----------
Test parse errors.

Modified Paths:
--------------
    CalendarServer/trunk/twext/who/test/test_xml.py
    CalendarServer/trunk/twext/who/xml.py

Modified: CalendarServer/trunk/twext/who/test/test_xml.py
===================================================================
--- CalendarServer/trunk/twext/who/test/test_xml.py	2013-02-12 00:19:29 UTC (rev 10693)
+++ CalendarServer/trunk/twext/who/test/test_xml.py	2013-02-12 00:37:11 UTC (rev 10694)
@@ -24,6 +24,7 @@
 from twisted.internet.defer import inlineCallbacks
 
 from twext.who.idirectory import NoSuchRecordError
+from twext.who.xml import ParseError
 from twext.who.xml import DirectoryService, DirectoryRecord
 
 from twext.who.test import test_directory
@@ -185,9 +186,11 @@
 
     def test_reloadInterval(self):
         service = self._testService()
+
         service.loadRecords(stat=False)
         lastRefresh = service._lastRefresh
         self.assertTrue(service._lastRefresh)
+
         sleep(1)
         service.loadRecords(stat=False)
         self.assertEquals(lastRefresh, service._lastRefresh)
@@ -195,14 +198,56 @@
 
     def test_reloadStat(self):
         service = self._testService()
+
         service.loadRecords(loadNow=True)
         lastRefresh = service._lastRefresh
         self.assertTrue(service._lastRefresh)
+
         sleep(1)
         service.loadRecords(loadNow=True)
         self.assertEquals(lastRefresh, service._lastRefresh)
 
 
+    def test_badXML(self):
+        service = self._testService(xmlData="Hello")
+
+        self.assertRaises(ParseError, service.loadRecords)
+
+
+    def test_badRootElement(self):
+        service = self._testService(xmlData=
+"""<?xml version="1.0" encoding="utf-8"?>
+
+<frobnitz />
+"""
+        )
+
+        self.assertRaises(ParseError, service.loadRecords)
+        try:
+            service.loadRecords()
+        except ParseError as e:
+            self.assertTrue(str(e).startswith("Incorrect root element"), e)
+        else:
+            raise AssertionError
+
+
+    def test_noRealmName(self):
+        service = self._testService(xmlData=
+"""<?xml version="1.0" encoding="utf-8"?>
+
+<directory />
+"""
+        )
+
+        self.assertRaises(ParseError, service.loadRecords)
+        try:
+            service.loadRecords()
+        except ParseError as e:
+            self.assertTrue(str(e).startswith("No realm name"), e)
+        else:
+            raise AssertionError
+
+
     @inlineCallbacks
     def test_recordWithUID(self):
         service = self._testService()

Modified: CalendarServer/trunk/twext/who/xml.py
===================================================================
--- CalendarServer/trunk/twext/who/xml.py	2013-02-12 00:19:29 UTC (rev 10693)
+++ CalendarServer/trunk/twext/who/xml.py	2013-02-12 00:37:11 UTC (rev 10694)
@@ -21,6 +21,7 @@
 """
 
 __all__ = [
+    "ParseError",
     "DirectoryService",
     "DirectoryRecord",
 ]
@@ -47,6 +48,17 @@
 
 
 ##
+# Exceptions
+##
+
+class ParseError(DirectoryServiceError):
+    """
+    Parse error.
+    """
+
+
+
+##
 # Data type extentions
 ##
 
@@ -225,7 +237,7 @@
             try:
                 etree = parseXML(fh)
             except XMLParseError as e:
-                raise DirectoryServiceError(e.getMessage())
+                raise ParseError(e)
         finally:
             fh.close()
 
@@ -234,12 +246,12 @@
         #
         directoryNode = etree.getroot()
         if directoryNode.tag != self.element.directory.value:
-            raise DirectoryServiceError("Incorrect root element: %s" % (directoryNode.tag,))
+            raise ParseError("Incorrect root element: %s" % (directoryNode.tag,))
 
         realmName = directoryNode.get(self.attribute.realm.value, "").encode("utf-8")
 
         if not realmName:
-            raise DirectoryServiceError("No realm name.")
+            raise ParseError("No realm name.")
 
         unknownRecordTypes   = set()
         unknownFieldElements = set()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130211/4565c38d/attachment-0001.html>


More information about the calendarserver-changes mailing list