*************** *** 76,78 **** --- 76,142 ---- else: raise TestFailed("expected creation of readable ZipFile without\n" " a file to raise an IOError.") + + + # This test checks that ZIP decryption works. Since the library does not + # support encryption at the moment, we use a pre-generated encrypted + # ZIP file + + test = ( + 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' + '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' + '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' + 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' + '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' + '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' + '\x00\x00L\x00\x00\x00\x00\x00' ) + + plain = 'zipfile.py encryption test' + + try: + fp = open(zipname, "wb") + fp.write(test) + fp.close() + + zip = zipfile.ZipFile(zipname, "r") + + # Be sure the file is closed before propagating + # the exceptions + try: + + # Reading the encrypted file without password + # must generate a RunTime exception + try: + zip.read("test.txt") + except RuntimeError: + pass + except zipfile.BadZipfile: + raise TestFailed, "zipfile.BadZipfile reported on password error" + else: + raise TestFailed, "Encrypted file was read without decryption" + + zip.setpassword("perl") + try: + zip.read("test.txt") + except RuntimeError: + pass + except zipfile.BadZipfile: + raise TestFailed, "zipfile.BadZipfile reported on password error" + else: + raise TestFailed, "Encrypted file was read even with wrong pwd" + + zip.setpassword("python") + try: + if zip.read("test.txt") != plain: + raise TestFailed, "Decrypted data is not the same of original data" + except RuntimeError: + raise TestFailed, "Decryption failed" + except zipfile.BadZipfile: + raise TestFailed, "Decryption failed" + + finally: + zip.close() + + finally: + if os.path.isfile(zipname): + os.unlink(zipname)

AltStyle によって変換されたページ (->オリジナル) /