Blame lang/python/tests/t-protocol-assuan.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 gpg
Packit Service 672cf4
import support
Packit Service 6c01f9
_ = support # to appease pyflakes.
Packit Service 672cf4
Packit Service 672cf4
with gpg.Context(protocol=gpg.constants.protocol.ASSUAN) as c:
Packit Service 672cf4
    # Do nothing.
Packit Service 672cf4
    err = c.assuan_transact('nop')
Packit Service 6c01f9
    assert err == None
Packit Service 672cf4
    err = c.assuan_transact(b'NOP')
Packit Service 6c01f9
    assert err == None
Packit Service 672cf4
    err = c.assuan_transact(['NOP'])
Packit Service 6c01f9
    assert err == None
Packit Service 672cf4
Packit Service 672cf4
    err = c.assuan_transact('idontexist')
Packit Service 672cf4
    assert err.getsource() == gpg.errors.SOURCE_GPGAGENT
Packit Service 672cf4
    assert err.getcode() == gpg.errors.ASS_UNKNOWN_CMD
Packit Service 672cf4
Packit Service 672cf4
    # Invoke the pinentry to get a confirmation.
Packit Service 672cf4
    c.assuan_transact(['GET_CONFIRMATION', 'Hello there'])
Packit Service 672cf4
Packit Service 672cf4
    data = []
Packit Service 672cf4
    def data_cb(line):
Packit Service 672cf4
        data.append(line)
Packit Service 672cf4
Packit Service 672cf4
    err = c.assuan_transact(['GETINFO', 'version'], data_cb=data_cb)
Packit Service 672cf4
    assert not err
Packit Service 672cf4
    assert len(data) == 1
Packit Service 672cf4
Packit Service 672cf4
    data = []
Packit Service 672cf4
    err = c.assuan_transact(['GETINFO', 's2k_count'], data_cb=data_cb)
Packit Service 672cf4
    if not err:
Packit Service 672cf4
        assert len(data) == 1
Packit Service 672cf4
        assert int(data[0]) > 0
Packit Service 672cf4
Packit Service 672cf4
    # XXX HELP sends status lines if we could use ASSUAN_CONVEY_COMMENTS.
Packit Service 672cf4
Packit Service 672cf4
    status = []
Packit Service 672cf4
    def status_cb(line, args):
Packit Service 672cf4
        status.append((line, args))
Packit Service 672cf4
Packit Service 672cf4
    alphas_grip = '76F7E2B35832976B50A27A282D9B87E44577EB66'
Packit Service 672cf4
    err = c.assuan_transact(['KEYINFO', alphas_grip], status_cb=status_cb)
Packit Service 672cf4
    if not err:
Packit Service 672cf4
        assert len(status) == 1
Packit Service 672cf4
        line, args = status[0]
Packit Service 672cf4
        assert line.startswith('KEYINFO')
Packit Service 672cf4
        assert args.startswith(alphas_grip)
Packit Service 672cf4
Packit Service 672cf4
    # XXX: test these callbacks, e.g. using PRESET_PASSPHRASE
Packit Service 672cf4
    # XXX: once issue2428 is resolved
Packit Service 672cf4
    def inq_cb(name, args):
Packit Service 672cf4
        print("inq_cb", name, args)