catch decompression errors

Change-Id: Ica380edc2364a5e18cefc26f70710e18ea329cfa
This commit is contained in:
David Goetz 2013-10-25 19:44:46 +00:00
parent 7ccde73974
commit d6c65c34aa
2 changed files with 15 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import tarfile
from urllib import quote, unquote from urllib import quote, unquote
from xml.sax import saxutils from xml.sax import saxutils
from time import time from time import time
import zlib
from swift.common.swob import Request, HTTPBadGateway, \ from swift.common.swob import Request, HTTPBadGateway, \
HTTPCreated, HTTPBadRequest, HTTPNotFound, HTTPUnauthorized, HTTPOk, \ HTTPCreated, HTTPBadRequest, HTTPNotFound, HTTPUnauthorized, HTTPOk, \
HTTPPreconditionFailed, HTTPRequestEntityTooLarge, HTTPNotAcceptable, \ HTTPPreconditionFailed, HTTPRequestEntityTooLarge, HTTPNotAcceptable, \
@ -500,7 +501,7 @@ class Bulk(object):
except HTTPException as err: except HTTPException as err:
resp_dict['Response Status'] = err.status resp_dict['Response Status'] = err.status
resp_dict['Response Body'] = err.body resp_dict['Response Body'] = err.body
except tarfile.TarError as tar_error: except (tarfile.TarError, zlib.error) as tar_error:
resp_dict['Response Status'] = HTTPBadRequest().status resp_dict['Response Status'] = HTTPBadRequest().status
resp_dict['Response Body'] = 'Invalid Tar File: %s' % tar_error resp_dict['Response Body'] = 'Invalid Tar File: %s' % tar_error
except Exception: except Exception:

View File

@ -17,6 +17,7 @@ import unittest
import os import os
import tarfile import tarfile
import urllib import urllib
import zlib
from shutil import rmtree from shutil import rmtree
from tempfile import mkdtemp from tempfile import mkdtemp
from StringIO import StringIO from StringIO import StringIO
@ -270,6 +271,16 @@ class TestUntar(unittest.TestCase):
resp_body = self.handle_extract_and_iter(req, '') resp_body = self.handle_extract_and_iter(req, '')
self.assertTrue('411 Length Required' in resp_body) self.assertTrue('411 Length Required' in resp_body)
def test_bad_tar(self):
req = Request.blank('/create_cont_fail/acc/cont', body='')
def bad_open(*args, **kwargs):
raise zlib.error('bad tar')
with patch.object(tarfile, 'open', bad_open):
resp_body = self.handle_extract_and_iter(req, '')
self.assertTrue('400 Bad Request' in resp_body)
def build_tar(self, dir_tree=None): def build_tar(self, dir_tree=None):
if not dir_tree: if not dir_tree:
dir_tree = [ dir_tree = [
@ -354,7 +365,8 @@ class TestUntar(unittest.TestCase):
resp_data = json.loads(resp_body) resp_data = json.loads(resp_body)
self.assertEquals(resp_data['Response Status'], '400 Bad Request') self.assertEquals(resp_data['Response Status'], '400 Bad Request')
self.assertEquals( self.assertEquals(
resp_data['Response Body'], 'Invalid Tar File: not a gzip file') resp_data['Response Body'].lower(),
'invalid tar file: not a gzip file')
def test_extract_tar_fail_max_failed_extractions(self): def test_extract_tar_fail_max_failed_extractions(self):
self.build_tar() self.build_tar()