Blame lang/python/tests/t-data.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
Packit Service 672cf4
# Copyright (C) 2016 g10 Code GmbH
Packit Service 672cf4
#
Packit Service 672cf4
# This file is part of GPGME.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is free software; you can redistribute it and/or modify it
Packit Service 672cf4
# under the terms of the GNU General Public License as published by
Packit Service 672cf4
# the Free Software Foundation; either version 2 of the License, or
Packit Service 672cf4
# (at your option) any later version.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is distributed in the hope that it will be useful, but WITHOUT
Packit Service 672cf4
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit Service 672cf4
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
Packit Service 672cf4
# Public License for more details.
Packit Service 672cf4
#
Packit Service 672cf4
# You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service 672cf4
Packit Service 672cf4
from __future__ import absolute_import, print_function, unicode_literals
Packit Service 6c01f9
del absolute_import, print_function, unicode_literals
Packit Service 672cf4
Packit Service 672cf4
import io
Packit Service 672cf4
import os
Packit Service 672cf4
import tempfile
Packit Service 672cf4
import gpg
Packit Service 672cf4
import support
Packit Service 6c01f9
_ = support # to appease pyflakes.
Packit Service 672cf4
Packit Service 672cf4
data = gpg.Data('Hello world!')
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
assert data.read() == b''
Packit Service 672cf4
Packit Service 672cf4
data.seek(0, os.SEEK_SET)
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
assert data.read() == b''
Packit Service 672cf4
Packit Service 672cf4
data = gpg.Data(b'Hello world!')
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
Packit Service 672cf4
data = gpg.Data(b'Hello world!', copy=False)
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
Packit Service 672cf4
data = gpg.Data()
Packit Service 672cf4
data.write('Hello world!')
Packit Service 672cf4
data.seek(0, os.SEEK_SET)
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
Packit Service 672cf4
data = gpg.Data()
Packit Service 672cf4
data.write(b'Hello world!')
Packit Service 672cf4
data.seek(0, os.SEEK_SET)
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
Packit Service 672cf4
data = gpg.Data()
Packit Service 672cf4
data.write(b'Hello world!')
Packit Service 672cf4
# We expect the second argument to default to SEEK_SET
Packit Service 672cf4
data.seek(0)
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
Packit Service 672cf4
binjunk = bytes(range(256))
Packit Service 672cf4
data = gpg.Data()
Packit Service 672cf4
data.write(binjunk)
Packit Service 672cf4
data.seek(0, os.SEEK_SET)
Packit Service 672cf4
assert data.read() == binjunk
Packit Service 672cf4
Packit Service 672cf4
data = gpg.Data()
Packit Service 672cf4
data.set_file_name("foobar")
Packit Service 672cf4
assert data.get_file_name() == "foobar"
Packit Service 672cf4
Packit Service 672cf4
# Test reading from an existing file.
Packit Service 672cf4
with tempfile.NamedTemporaryFile() as tmp:
Packit Service 672cf4
    tmp.write(binjunk)
Packit Service 672cf4
    tmp.flush()
Packit Service 672cf4
    tmp.seek(0)
Packit Service 672cf4
Packit Service 672cf4
    # Open using name.
Packit Service 672cf4
    data = gpg.Data(file=tmp.name)
Packit Service 672cf4
    assert data.read() == binjunk
Packit Service 672cf4
Packit Service 672cf4
    # Open using name, without copying.
Packit Service 672cf4
    if False:
Packit Service 672cf4
        # delayed reads are not yet supported
Packit Service 672cf4
        data = gpg.Data(file=tmp.name, copy=False)
Packit Service 672cf4
        assert data.read() == binjunk
Packit Service 672cf4
Packit Service 672cf4
    # Open using stream.
Packit Service 672cf4
    tmp.seek(0)
Packit Service 672cf4
    data = gpg.Data(file=tmp)
Packit Service 672cf4
    assert data.read() == binjunk
Packit Service 672cf4
Packit Service 672cf4
    # Open using stream, offset, and length.
Packit Service 672cf4
    data = gpg.Data(file=tmp, offset=0, length=42)
Packit Service 672cf4
    assert data.read() == binjunk[:42]
Packit Service 672cf4
Packit Service 672cf4
    # Open using name, offset, and length.
Packit Service 672cf4
    data = gpg.Data(file=tmp.name, offset=23, length=42)
Packit Service 6c01f9
    assert data.read() == binjunk[23:23+42]
Packit Service 672cf4
Packit Service 672cf4
# Test callbacks.
Packit Service 672cf4
class DataObject(object):
Packit Service 672cf4
    def __init__(self):
Packit Service 672cf4
        self.buffer = io.BytesIO()
Packit Service 672cf4
        self.released = False
Packit Service 672cf4
Packit Service 672cf4
    def read(self, amount, hook=None):
Packit Service 672cf4
        assert not self.released
Packit Service 672cf4
        return self.buffer.read(amount)
Packit Service 672cf4
Packit Service 672cf4
    def write(self, data, hook=None):
Packit Service 672cf4
        assert not self.released
Packit Service 672cf4
        return self.buffer.write(data)
Packit Service 672cf4
Packit Service 672cf4
    def seek(self, offset, whence, hook=None):
Packit Service 672cf4
        assert not self.released
Packit Service 672cf4
        return self.buffer.seek(offset, whence)
Packit Service 672cf4
Packit Service 672cf4
    def release(self, hook=None):
Packit Service 672cf4
        assert not self.released
Packit Service 672cf4
        self.released = True
Packit Service 672cf4
Packit Service 672cf4
do = DataObject()
Packit Service 672cf4
cookie = object()
Packit Service 672cf4
data = gpg.Data(cbs=(do.read, do.write, do.seek, do.release, cookie))
Packit Service 672cf4
data.write('Hello world!')
Packit Service 672cf4
data.seek(0, os.SEEK_SET)
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
del data
Packit Service 672cf4
assert do.released
Packit Service 672cf4
Packit Service 672cf4
# Again, without the cookie.
Packit Service 672cf4
do = DataObject()
Packit Service 672cf4
data = gpg.Data(cbs=(do.read, do.write, do.seek, do.release))
Packit Service 672cf4
data.write('Hello world!')
Packit Service 672cf4
data.seek(0, os.SEEK_SET)
Packit Service 672cf4
assert data.read() == b'Hello world!'
Packit Service 672cf4
del data
Packit Service 672cf4
assert do.released