# Copyright (C) 2008 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """Tests for the wedav plugin.""" from cStringIO import StringIO import stat from bzrlib import ( errors, tests, ) from bzrlib.plugins.webdav import webdav def _get_list_dir_apache2_depth_1_prop(): return """ /19016477731212686926.835527/ HTTP/1.1 200 OK /19016477731212686926.835527/a HTTP/1.1 200 OK /19016477731212686926.835527/b HTTP/1.1 200 OK /19016477731212686926.835527/c/ HTTP/1.1 200 OK """ def _get_list_dir_apache2_depth_1_allprop(): return """ / 2008-06-08T10:50:38Z Sun, 08 Jun 2008 10:50:38 GMT "da7f5a-cc-7722db80" HTTP/1.1 200 OK /executable 2008-06-08T09:50:15Z 14 Sun, 08 Jun 2008 09:50:11 GMT "da9f81-0-9ef33ac0" T HTTP/1.1 200 OK /read-only 2008-06-08T09:50:11Z 42 Sun, 08 Jun 2008 09:50:11 GMT "da9f80-0-9ef33ac0" F HTTP/1.1 200 OK /titi 2008-06-08T09:49:53Z 6 Sun, 08 Jun 2008 09:49:53 GMT "da8cbc-6-9de09240" F HTTP/1.1 200 OK /toto/ 2008-06-06T08:07:07Z Fri, 06 Jun 2008 08:07:07 GMT "da8cb9-44-f2ac20c0" HTTP/1.1 200 OK """ class TestDavSaxParser(tests.TestCase): def _extract_dir_content_from_str(self, str): return webdav._extract_dir_content( 'http://localhost/blah', StringIO(str)) def _extract_stat_from_str(self, str): return webdav._extract_stat_info( 'http://localhost/blah', StringIO(str)) def test_unkown_format_response(self): # Valid but unrelated xml example = """""" self.assertRaises(errors.InvalidHttpResponse, self._extract_dir_content_from_str, example) def test_list_dir_malformed_response(self): # Invalid xml, neither multistatus nor response are properly closed example = """ http://localhost/""" self.assertRaises(errors.InvalidHttpResponse, self._extract_dir_content_from_str, example) def test_list_dir_incomplete_format_response(self): # The information we need is not present example = """ http://localhost/ http://localhost/titi http://localhost/toto """ self.assertRaises(errors.NotADirectory, self._extract_dir_content_from_str, example) def test_list_dir_apache2_example(self): example = _get_list_dir_apache2_depth_1_prop() self.assertRaises(errors.NotADirectory, self._extract_dir_content_from_str, example) def test_list_dir_lighttpd_example(self): example = """ http://localhost/ http://localhost/titi http://localhost/toto """ self.assertRaises(errors.NotADirectory, self._extract_dir_content_from_str, example) def test_list_dir_apache2_dir_depth_1_example(self): example = _get_list_dir_apache2_depth_1_allprop() self.assertEquals([('executable', False, 14, True), ('read-only', False, 42, False), ('titi', False, 6, False), ('toto', True, -1, False)], self._extract_dir_content_from_str(example)) def test_stat_malformed_response(self): # Invalid xml, neither multistatus nor response are properly closed example = """ http://localhost/""" self.assertRaises(errors.InvalidHttpResponse, self._extract_stat_from_str, example) def test_stat_incomplete_format_response(self): # The minimal information is present but doesn't conform to RFC 2518 # (well, as I understand it since the reference servers disagree on # more than details). # The href below is not enclosed in a response element and is # therefore ignored. example = """ http://localhost/toto """ self.assertRaises(errors.InvalidHttpResponse, self._extract_stat_from_str, example) def test_stat_apache2_file_example(self): example = """ /executable 2008-06-08T09:50:15Z 12 Sun, 08 Jun 2008 09:50:11 GMT "da9f81-0-9ef33ac0" T HTTP/1.1 200 OK """ st = self._extract_stat_from_str(example) self.assertEquals(12, st.st_size) self.assertFalse(stat.S_ISDIR(st.st_mode)) self.assertTrue(stat.S_ISREG(st.st_mode)) self.assertTrue(st.st_mode & stat.S_IXUSR) def test_stat_apache2_dir_depth_1_example(self): example = _get_list_dir_apache2_depth_1_allprop() self.assertRaises(errors.InvalidHttpResponse, self._extract_stat_from_str, example) def test_stat_apache2_dir_depth_0_example(self): example = """ / 2008-06-08T10:50:38Z Sun, 08 Jun 2008 10:50:38 GMT "da7f5a-cc-7722db80" HTTP/1.1 200 OK """ st = self._extract_stat_from_str(example) self.assertEquals(-1, st.st_size) self.assertTrue(stat.S_ISDIR(st.st_mode)) self.assertTrue(st.st_mode & stat.S_IXUSR)