Exemplo n.º 1
0
    def test_decryptorfile(self):
        # create a plaintext blob bigger than IO_BLOCK_SIZE
        plaintext1 = b"rvdmfki6iudmx8bb25tx1sozex3f4u0nm7uba4eibscgda0ckledcydz089qw1p1"
        repeat = int(1.5 * IO_BLOCK_SIZE / len(plaintext1))
        plaintext = repeat * plaintext1
        encryptor = Encryptor(CONSTANT_TEST_RSA_PUBLIC_KEY)
        ciphertext = encryptor.update(plaintext) + encryptor.finalize()
        plain_fp = tempfile.TemporaryFile(prefix="test-pghoard.", mode="r+b")
        plain_fp.write(ciphertext)
        plain_fp.seek(0)
        fp = DecryptorFile(plain_fp, CONSTANT_TEST_RSA_PRIVATE_KEY)  # pylint: disable=redefined-variable-type
        assert fp.fileno() == plain_fp.fileno()
        assert fp.readable() is True
        assert fp.writable() is False
        fp.flush()
        result = fp.read()
        assert plaintext == result

        assert fp.seekable() is True
        with pytest.raises(ValueError):
            fp.seek(-1)
        fp.seek(0, os.SEEK_SET)
        with pytest.raises(io.UnsupportedOperation):
            fp.seek(1, os.SEEK_CUR)
        with pytest.raises(io.UnsupportedOperation):
            fp.seek(1, os.SEEK_END)
        with pytest.raises(ValueError):
            fp.seek(1, 0xff)
        assert fp.seek(0, os.SEEK_END) == len(plaintext)
        assert fp.seek(0, os.SEEK_CUR) == len(plaintext)

        fp.seek(0)
        result = fp.read()
        assert plaintext == result
        assert fp.read(1234) == b""
        assert fp.read() == b""

        fp.seek(0)
        result = fp.read(8192)
        assert result == plaintext[:8192]
        result = fp.read(8192)
        assert result == plaintext[8192:8192 * 2]
        result = fp.read(IO_BLOCK_SIZE * 2)
        assert plaintext[8192 * 2:] == result
        assert fp.seek(IO_BLOCK_SIZE // 2) == IO_BLOCK_SIZE // 2
        result = fp.read()
        assert len(result) == len(plaintext) - IO_BLOCK_SIZE // 2
        assert plaintext[IO_BLOCK_SIZE // 2:] == result

        fp.seek(2)
        result = fp.read(1)
        assert plaintext[2:3] == result
        assert fp.tell() == 3
        with pytest.raises(io.UnsupportedOperation):
            fp.truncate()
        # close the file (this can be safely called multiple times), other ops should fail after that
        fp.close()
        fp.close()
        with pytest.raises(ValueError):
            fp.truncate()
Exemplo n.º 2
0
def test_decryptorfile(tmpdir):
    # create a plaintext blob bigger than IO_BLOCK_SIZE
    plaintext1 = b"rvdmfki6iudmx8bb25tx1sozex3f4u0nm7uba4eibscgda0ckledcydz089qw1p1"
    repeat = int(1.5 * IO_BLOCK_SIZE / len(plaintext1))
    plaintext = repeat * plaintext1
    encryptor = Encryptor(CONSTANT_TEST_RSA_PUBLIC_KEY)
    ciphertext = encryptor.update(plaintext) + encryptor.finalize()
    plain_fp = open(tmpdir.join("plain").strpath, mode="w+b")
    plain_fp.write(ciphertext)
    plain_fp.seek(0)
    fp = DecryptorFile(plain_fp, CONSTANT_TEST_RSA_PRIVATE_KEY)  # pylint: disable=redefined-variable-type
    assert fp.fileno() == plain_fp.fileno()
    assert fp.readable() is True
    assert fp.writable() is False
    fp.flush()
    result = fp.read()
    assert plaintext == result

    assert fp.seekable() is True
    with pytest.raises(ValueError):
        fp.seek(-1)
    fp.seek(0, os.SEEK_SET)
    with pytest.raises(io.UnsupportedOperation):
        fp.seek(1, os.SEEK_CUR)
    with pytest.raises(io.UnsupportedOperation):
        fp.seek(1, os.SEEK_END)
    with pytest.raises(ValueError):
        fp.seek(1, 0xff)
    assert fp.seek(0, os.SEEK_END) == len(plaintext)
    assert fp.seek(0, os.SEEK_CUR) == len(plaintext)

    fp.seek(0)
    result = fp.read()
    assert plaintext == result
    assert fp.read(1234) == b""
    assert fp.read() == b""

    fp.seek(0)
    result = fp.read(8192)
    assert result == plaintext[:8192]
    result = fp.read(8192)
    assert result == plaintext[8192:8192 * 2]
    result = fp.read(IO_BLOCK_SIZE * 2)
    assert plaintext[8192 * 2:] == result
    assert fp.seek(IO_BLOCK_SIZE // 2) == IO_BLOCK_SIZE // 2
    result = fp.read()
    assert len(result) == len(plaintext) - IO_BLOCK_SIZE // 2
    assert plaintext[IO_BLOCK_SIZE // 2:] == result

    fp.seek(2)
    result = fp.read(1)
    assert plaintext[2:3] == result
    assert fp.tell() == 3
    with pytest.raises(io.UnsupportedOperation):
        fp.truncate()
    # close the file (this can be safely called multiple times), other ops should fail after that
    fp.close()
    fp.close()
    with pytest.raises(ValueError):
        fp.truncate()