def test18(self): """test NewAsciiWriter's append method""" fname = self.fixture_file('foo') st = os.stat(fname) sio = StringIO() archive_writer = NewAsciiWriter(sio) archive_writer.append(fname) self.assertEqual(archive_writer._bytes_written, 128) fname = self.fixture_file('new_ascii_writer_foo_header') # we can only compare the sizes because the inode, dev_maj and dev_min # of the foo file has changed (because it was copied to a # tmpdir - that's how our testsuite works...) self.assertEqual(len(sio.getvalue()), len(open(fname, 'r').read())) # check manually... sio.seek(0, os.SEEK_SET) f = FileWrapper(fobj=sio) archive_reader = NewAsciiReader(f) hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') # self.assertEqual(hdr.ino, 1788176) self.assertEqual(hdr.mode, st.st_mode) self.assertEqual(hdr.uid, st.st_uid) self.assertEqual(hdr.gid, st.st_gid) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, int(st.st_mtime)) self.assertEqual(hdr.filesize, 9) # self.assertEqual(hdr.dev_maj, 8) # self.assertEqual(hdr.dev_min, 10) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 4) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'foo')
def test10(self): """test NewAsciiReader (unseekable file object)""" # identical to test8 but this time a unseekable fobj is used fname = self.fixture_file('new_ascii_reader2.cpio') sio = StringIO(open(fname, 'r').read()) sio.seek = None f = FileWrapper(fobj=sio) archive_reader = NewAsciiReader(f) hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') self.assertEqual(hdr.ino, 1788112) self.assertEqual(hdr.mode, 33188) self.assertEqual(hdr.uid, 1000) self.assertEqual(hdr.gid, 100) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, 1340459653) self.assertEqual(hdr.filesize, 28) self.assertEqual(hdr.dev_maj, 8) self.assertEqual(hdr.dev_min, 10) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 14) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'filewrapper_1') # self.assertTrue(hdr.is_regular_file()) # next header is trailer hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') self.assertEqual(hdr.ino, 0) self.assertEqual(hdr.mode, 0) self.assertEqual(hdr.uid, 0) self.assertEqual(hdr.gid, 0) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, 0) self.assertEqual(hdr.filesize, 0) self.assertEqual(hdr.dev_maj, 0) self.assertEqual(hdr.dev_min, 0) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 11) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'TRAILER!!!') # self.assertFalse(hdr.is_regular_file()) # no more headers available self.assertIsNone(archive_reader.next_header())
def test9(self): """test NewAsciiReader (no padding except for trailer)""" # nearly identical to test7 but the filename differ fname = self.fixture_file('new_ascii_reader2.cpio') f = FileWrapper(filename=fname) archive_reader = NewAsciiReader(f) hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') self.assertEqual(hdr.ino, 1788112) self.assertEqual(hdr.mode, 33188) self.assertEqual(hdr.uid, 1000) self.assertEqual(hdr.gid, 100) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, 1340459653) self.assertEqual(hdr.filesize, 28) self.assertEqual(hdr.dev_maj, 8) self.assertEqual(hdr.dev_min, 10) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 14) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'filewrapper_1') # self.assertTrue(hdr.is_regular_file()) # next header is trailer hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') self.assertEqual(hdr.ino, 0) self.assertEqual(hdr.mode, 0) self.assertEqual(hdr.uid, 0) self.assertEqual(hdr.gid, 0) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, 0) self.assertEqual(hdr.filesize, 0) self.assertEqual(hdr.dev_maj, 0) self.assertEqual(hdr.dev_min, 0) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 11) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'TRAILER!!!') # self.assertFalse(hdr.is_regular_file()) # no more headers available self.assertIsNone(archive_reader.next_header())
def test29(self): """test NewAsciiReader: modify FileWrapper while reading""" fname = self.fixture_file('new_ascii_reader1.cpio') f = FileWrapper(filename=fname) archive_reader = NewAsciiReader(f) self.assertIsNotNone(archive_reader.next_header()) # seeking _after_ the calculated next header position is not # allowed (even if the file is seekable) # next header position: 156 f.seek(158, os.SEEK_SET) self.assertRaises(CpioError, archive_reader.next_header)
def test28(self): """test NewAsciiReader: premature end of archive""" fname = self.fixture_file('new_ascii_reader1_premature_end.cpio') f = FileWrapper(filename=fname) archive_reader = NewAsciiReader(f) # first header is ok hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') self.assertEqual(hdr.ino, 1788112) self.assertEqual(hdr.mode, 33188) self.assertEqual(hdr.uid, 1000) self.assertEqual(hdr.gid, 100) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, 1340459653) self.assertEqual(hdr.filesize, 28) self.assertEqual(hdr.dev_maj, 8) self.assertEqual(hdr.dev_min, 10) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 17) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'filewrapper1.txt') # second header is incomplete self.assertRaises(CpioError, archive_reader.next_header)
def test11(self): """test NewAsciiReader (2 files)""" fname = self.fixture_file('new_ascii_reader3.cpio') f = FileWrapper(filename=fname) archive_reader = NewAsciiReader(f) # check file foo hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') self.assertEqual(hdr.ino, 1788176) self.assertEqual(hdr.mode, 33188) self.assertEqual(hdr.uid, 1000) self.assertEqual(hdr.gid, 100) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, 1340493596) self.assertEqual(hdr.filesize, 9) self.assertEqual(hdr.dev_maj, 8) self.assertEqual(hdr.dev_min, 10) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 4) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'foo') # self.assertTrue(hdr.is_regular_file()) # check file foobar hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') self.assertEqual(hdr.ino, 1788110) self.assertEqual(hdr.mode, 33188) self.assertEqual(hdr.uid, 1000) self.assertEqual(hdr.gid, 100) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, 1340493602) self.assertEqual(hdr.filesize, 18) self.assertEqual(hdr.dev_maj, 8) self.assertEqual(hdr.dev_min, 10) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 7) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'foobar') # self.assertTrue(hdr.is_regular_file()) # next header is trailer hdr = archive_reader.next_header() self.assertEqual(hdr.magic, '070701') self.assertEqual(hdr.ino, 0) self.assertEqual(hdr.mode, 0) self.assertEqual(hdr.uid, 0) self.assertEqual(hdr.gid, 0) self.assertEqual(hdr.nlink, 1) self.assertEqual(hdr.mtime, 0) self.assertEqual(hdr.filesize, 0) self.assertEqual(hdr.dev_maj, 0) self.assertEqual(hdr.dev_min, 0) self.assertEqual(hdr.rdev_maj, 0) self.assertEqual(hdr.rdev_min, 0) self.assertEqual(hdr.namesize, 11) self.assertEqual(hdr.chksum, 0) self.assertEqual(hdr.name, 'TRAILER!!!') # self.assertFalse(hdr.is_regular_file()) # no more headers available self.assertIsNone(archive_reader.next_header())