Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_tarfile.MiscReadTest Class Reference
Inheritance diagram for test.test_tarfile.MiscReadTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_tarfile.MiscReadTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_no_name_argument
def test_no_name_attribute
def test_empty_name_attribute
def test_fileobj_with_offset
def test_fail_comp
def test_v7_dirtype
def test_xstar_type
def test_check_members
def test_find_members
def test_extract_hardlink
def test_extractall
def test_extract_directory
def test_init_close_fobj
def test_empty_tarfile
def test_null_tarfile
def test_ignore_zeros
def setUp
def tearDown

Public Attributes

 mode
 tar

Static Public Attributes

 tarname = tarname

Detailed Description

Definition at line 222 of file test_tarfile.py.


Member Function Documentation

def test.test_tarfile.ReadTest.setUp (   self) [inherited]

Definition at line 43 of file test_tarfile.py.

00043 
00044     def setUp(self):
00045         self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")

Here is the caller graph for this function:

def test.test_tarfile.ReadTest.tearDown (   self) [inherited]

Definition at line 46 of file test_tarfile.py.

00046 
00047     def tearDown(self):
00048         self.tar.close()
00049 

Here is the caller graph for this function:

Definition at line 310 of file test_tarfile.py.

00310 
00311     def test_check_members(self):
00312         for tarinfo in self.tar:
00313             self.assertTrue(int(tarinfo.mtime) == 0o7606136617,
00314                     "wrong mtime for %s" % tarinfo.name)
00315             if not tarinfo.name.startswith("ustar/"):
00316                 continue
00317             self.assertTrue(tarinfo.uname == "tarfile",
00318                     "wrong uname for %s" % tarinfo.name)

Here is the call graph for this function:

Definition at line 237 of file test_tarfile.py.

00237 
00238     def test_empty_name_attribute(self):
00239         with open(self.tarname, "rb") as fobj:
00240             data = fobj.read()
00241         fobj = io.BytesIO(data)
00242         fobj.name = ""
00243         with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
00244             self.assertEqual(tar.name, None)

Here is the call graph for this function:

Definition at line 172 of file test_tarfile.py.

00172 
00173     def test_empty_tarfile(self):
00174         # Test for issue6123: Allow opening empty archives.
00175         # This test checks if tarfile.open() is able to open an empty tar
00176         # archive successfully. Note that an empty tar archive is not the
00177         # same as an empty file!
00178         with tarfile.open(tmpname, self.mode.replace("r", "w")):
00179             pass
00180         try:
00181             tar = tarfile.open(tmpname, self.mode)
00182             tar.getnames()
00183         except tarfile.ReadError:
00184             self.fail("tarfile.open() failed on empty archive")
00185         else:
00186             self.assertListEqual(tar.getmembers(), [])
00187         finally:
00188             tar.close()

Here is the call graph for this function:

Definition at line 383 of file test_tarfile.py.

00383 
00384     def test_extract_directory(self):
00385         dirtype = "ustar/dirtype"
00386         DIR = os.path.join(TEMPDIR, "extractdir")
00387         os.mkdir(DIR)
00388         try:
00389             with tarfile.open(tarname, encoding="iso8859-1") as tar:
00390                 tarinfo = tar.getmember(dirtype)
00391                 tar.extract(tarinfo, path=DIR)
00392                 extracted = os.path.join(DIR, dirtype)
00393                 self.assertEqual(os.path.getmtime(extracted), tarinfo.mtime)
00394                 if sys.platform != "win32":
00395                     self.assertEqual(os.stat(extracted).st_mode & 0o777, 0o755)
00396         finally:
00397             shutil.rmtree(DIR)

Here is the call graph for this function:

Definition at line 326 of file test_tarfile.py.

00326 
00327     def test_extract_hardlink(self):
00328         # Test hardlink extraction (e.g. bug #857297).
00329         tar = tarfile.open(tarname, errorlevel=1, encoding="iso8859-1")
00330 
00331         try:
00332             tar.extract("ustar/regtype", TEMPDIR)
00333             try:
00334                 tar.extract("ustar/lnktype", TEMPDIR)
00335             except EnvironmentError as e:
00336                 if e.errno == errno.ENOENT:
00337                     self.fail("hardlink not extracted properly")
00338 
00339             with open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb") as f:
00340                 data = f.read()
00341             self.assertEqual(md5sum(data), md5_regtype)
00342 
00343             try:
00344                 tar.extract("ustar/symtype", TEMPDIR)
00345             except EnvironmentError as e:
00346                 if e.errno == errno.ENOENT:
00347                     self.fail("symlink not extracted properly")
00348 
00349             with open(os.path.join(TEMPDIR, "ustar/symtype"), "rb") as f:
00350                 data = f.read()
00351             self.assertEqual(md5sum(data), md5_regtype)
00352         finally:
00353             tar.close()

Here is the call graph for this function:

Definition at line 354 of file test_tarfile.py.

00354 
00355     def test_extractall(self):
00356         # Test if extractall() correctly restores directory permissions
00357         # and times (see issue1735).
00358         tar = tarfile.open(tarname, encoding="iso8859-1")
00359         DIR = os.path.join(TEMPDIR, "extractall")
00360         os.mkdir(DIR)
00361         try:
00362             directories = [t for t in tar if t.isdir()]
00363             tar.extractall(DIR, directories)
00364             for tarinfo in directories:
00365                 path = os.path.join(DIR, tarinfo.name)
00366                 if sys.platform != "win32":
00367                     # Win32 has no support for fine grained permissions.
00368                     self.assertEqual(tarinfo.mode & 0o777, os.stat(path).st_mode & 0o777)
00369                 def format_mtime(mtime):
00370                     if isinstance(mtime, float):
00371                         return "{} ({})".format(mtime, mtime.hex())
00372                     else:
00373                         return "{!r} (int)".format(mtime)
00374                 file_mtime = os.path.getmtime(path)
00375                 errmsg = "tar mtime {0} != file time {1} of path {2!a}".format(
00376                     format_mtime(tarinfo.mtime),
00377                     format_mtime(file_mtime),
00378                     path)
00379                 self.assertEqual(tarinfo.mtime, file_mtime, errmsg)
00380         finally:
00381             tar.close()
00382             shutil.rmtree(DIR)

Here is the call graph for this function:

Definition at line 284 of file test_tarfile.py.

00284 
00285     def test_fail_comp(self):
00286         # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
00287         if self.mode == "r:":
00288             return
00289         self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
00290         with open(tarname, "rb") as fobj:
00291             self.assertRaises(tarfile.ReadError, tarfile.open,
00292                               fileobj=fobj, mode=self.mode)

Definition at line 245 of file test_tarfile.py.

00245 
00246     def test_fileobj_with_offset(self):
00247         # Skip the first member and store values from the second member
00248         # of the testtar.
00249         tar = tarfile.open(self.tarname, mode=self.mode)
00250         try:
00251             tar.next()
00252             t = tar.next()
00253             name = t.name
00254             offset = t.offset
00255             f = tar.extractfile(t)
00256             data = f.read()
00257             f.close()
00258         finally:
00259             tar.close()
00260 
00261         # Open the testtar and seek to the offset of the second member.
00262         if self.mode.endswith(":gz"):
00263             _open = gzip.GzipFile
00264         elif self.mode.endswith(":bz2"):
00265             _open = bz2.BZ2File
00266         else:
00267             _open = open
00268         fobj = _open(self.tarname, "rb")
00269         try:
00270             fobj.seek(offset)
00271 
00272             # Test if the tarfile starts with the second member.
00273             tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
00274             t = tar.next()
00275             self.assertEqual(t.name, name)
00276             # Read to the end of fileobj and test if seeking back to the
00277             # beginning works.
00278             tar.getmembers()
00279             self.assertEqual(tar.extractfile(t).read(), data,
00280                     "seek back did not work")
00281             tar.close()
00282         finally:
00283             fobj.close()

Here is the call graph for this function:

Definition at line 319 of file test_tarfile.py.

00319 
00320     def test_find_members(self):
00321         self.assertTrue(self.tar.getmembers()[-1].name == "misc/eof",
00322                 "could not find all members")

Here is the call graph for this function:

Definition at line 198 of file test_tarfile.py.

00198 
00199     def test_ignore_zeros(self):
00200         # Test TarFile's ignore_zeros option.
00201         if self.mode.endswith(":gz"):
00202             _open = gzip.GzipFile
00203         elif self.mode.endswith(":bz2"):
00204             _open = bz2.BZ2File
00205         else:
00206             _open = open
00207 
00208         for char in (b'\0', b'a'):
00209             # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
00210             # are ignored correctly.
00211             with _open(tmpname, "wb") as fobj:
00212                 fobj.write(char * 1024)
00213                 fobj.write(tarfile.TarInfo("foo").tobuf())
00214 
00215             tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
00216             try:
00217                 self.assertListEqual(tar.getnames(), ["foo"],
00218                     "ignore_zeros=True should have skipped the %r-blocks" % char)
00219             finally:
00220                 tar.close()
00221 

Here is the call graph for this function:

Definition at line 398 of file test_tarfile.py.

00398 
00399     def test_init_close_fobj(self):
00400         # Issue #7341: Close the internal file object in the TarFile
00401         # constructor in case of an error. For the test we rely on
00402         # the fact that opening an empty file raises a ReadError.
00403         empty = os.path.join(TEMPDIR, "empty")
00404         with open(empty, "wb") as fobj:
00405             fobj.write(b"")
00406 
00407         try:
00408             tar = object.__new__(tarfile.TarFile)
00409             try:
00410                 tar.__init__(empty)
00411             except tarfile.ReadError:
00412                 self.assertTrue(tar.fileobj.closed)
00413             else:
00414                 self.fail("ReadError not raised")
00415         finally:
00416             support.unlink(empty)
00417 

Here is the call graph for this function:

Definition at line 224 of file test_tarfile.py.

00224 
00225     def test_no_name_argument(self):
00226         with open(self.tarname, "rb") as fobj:
00227             tar = tarfile.open(fileobj=fobj, mode=self.mode)
00228             self.assertEqual(tar.name, os.path.abspath(fobj.name))

Here is the call graph for this function:

Definition at line 229 of file test_tarfile.py.

00229 
00230     def test_no_name_attribute(self):
00231         with open(self.tarname, "rb") as fobj:
00232             data = fobj.read()
00233         fobj = io.BytesIO(data)
00234         self.assertRaises(AttributeError, getattr, fobj, "name")
00235         tar = tarfile.open(fileobj=fobj, mode=self.mode)
00236         self.assertEqual(tar.name, None)

Here is the call graph for this function:

Definition at line 189 of file test_tarfile.py.

00189 
00190     def test_null_tarfile(self):
00191         # Test for issue6123: Allow opening empty archives.
00192         # This test guarantees that tarfile.open() does not treat an empty
00193         # file as an empty tar archive.
00194         with open(tmpname, "wb"):
00195             pass
00196         self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode)
00197         self.assertRaises(tarfile.ReadError, tarfile.open, tmpname)

Here is the call graph for this function:

Definition at line 293 of file test_tarfile.py.

00293 
00294     def test_v7_dirtype(self):
00295         # Test old style dirtype member (bug #1336623):
00296         # Old V7 tars create directory members using an AREGTYPE
00297         # header with a "/" appended to the filename field.
00298         tarinfo = self.tar.getmember("misc/dirtype-old-v7")
00299         self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
00300                 "v7 dirtype failed")

Here is the call graph for this function:

Definition at line 301 of file test_tarfile.py.

00301 
00302     def test_xstar_type(self):
00303         # The xstar format stores extra atime and ctime fields inside the
00304         # space reserved for the prefix field. The prefix field must be
00305         # ignored in this case, otherwise it will mess up the name.
00306         try:
00307             self.tar.getmember("misc/regtype-xstar")
00308         except KeyError:
00309             self.fail("failed to find misc/regtype-xstar (mangled prefix?)")

Here is the call graph for this function:


Member Data Documentation

Reimplemented from test.test_tarfile.ReadTest.

Reimplemented in test.test_tarfile.GzipMiscReadTest.

Definition at line 286 of file test_tarfile.py.

Reimplemented in test.test_tarfile.MemberReadTest.

Definition at line 44 of file test_tarfile.py.


The documentation for this class was generated from the following file: