# -*-mode: doctest; tab-width: 0; py-indent-offset: 4; coding: utf-8-unix -*-

== Keyutils ==

This is a Python doctest file that is executable documentation.

/mnt/linuxPen19/usr/local/src/python-keyutils/README.rst

    >>> import keyutils

    # NOTE: only pass `bytes` to the keyutils API:
    name = b'foo'
    value = b'bar'
    ring = keyutils.KEY_SPEC_PROCESS_KEYRING

    key_id = keyutils.add_key(name, value, ring)

    assert keyutils.request_key(name, ring) == key_id
    assert keyutils.read_key(key_id) == value

    # set timeout to 5 seconds, wait and then... it's gone:
    keyutils.set_timeout(key_id, 5)
    from time import sleep
    sleep(6)
    assert keyutils.request_key(name, ring) == None

=== Keyutils ===

    >>> import keyutils
    >>> dir(keyutils)
    ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

=== Kernel Keyring ===

    >>> from keyutils.backend import KeyutilsKeyringBackend
    >>> dir(KeyutilsKeyringBackend)
    ['__abstractmethods__', ... 'delete_password', 'get_credential', 'get_password', 'get_viable_backends', 'name', 'priority', 'set_password', 'set_properties_from_env', 'viable']


    >>> from keyutils import keys
    >>> dir (keys)
    ['Key', 'KeyEntity', 'Keyring', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'errno', 'group_keyring', 'process_keyring', 'raw', 'session_keyring', 'thread_keyring', 'user_keyring', 'user_session_keyring']

    >>> from keyutils.keys import session_keyring
    >>> dir( session_keyring)
    [...'add_key', 'create', 'from_serial', 'invalidate', 'join_session_keyring', 'revoke', 'search', 'serial']
    >>> session_keyring.serial
    -...

    >>> from keyutils.keys import group_keyring
    >>> from keyutils.keys import process_keyring

    >>> service = b'ser\xffvice'
    >>> username = b'user\xdcname'
    >>> password = b'\xdc\0\xff\0pass\x7f'

test_backend.py

    >>> parent = process_keyring
    >>> oKeyring = KeyutilsKeyringBackend(keyring_name='foobar@p', parent_keyring=parent, payload_encoding=None)

    >>> assert oKeyring.get_password(service, username) is None

    >>> oKeyring.set_password(service, username, password)
    >>> actual_password = oKeyring.get_password(service, username)
    >>> assert password == actual_password

    >>> from keyutils.keys import user_session_keyring
    >>> dir( user_session_keyring)
    [... 'add_key', 'create', 'from_serial', 'invalidate', 'join_session_keyring', 'revoke', 'search', 'serial']
    >>> from keyutils.keys import user_keyring

=== user_keyring ===

    >>> parent = user_keyring
    >>> oKeyring = KeyutilsKeyringBackend(keyring_name='foobar@u', parent_keyring=parent, payload_encoding=None)

    >>> assert oKeyring.get_password(service, username) is None

    >>> oKeyring.set_password(service, username, password)
    >>> actual_password = oKeyring.get_password(service, username)
    >>> assert password == actual_password

=== user_session_keyring ===

    >>> parent = user_session_keyring
    >>> oKeyring = KeyutilsKeyringBackend(keyring_name='foobar@us', parent_keyring=parent, payload_encoding=None)


Zap is possible.

    >>> plen = len(actual_password)
    >>> for i in range(plen):
    ...     actual_password[i] = 0
    >>> bytes(plen) == actual_password
    True

    >>> for elt in ['group_keyring', 'process_keyring', 'session_keyring', 'thread_keyring', 'user_keyring', 'user_session_keyring']:
    ...	  assert __import__('keyutils.keys' , elt)

===  parent = process_keyring ===

    >>> parent = process_keyring
    >>> oKeyring = KeyutilsKeyringBackend(keyring_name='foobar', parent_keyring=parent, payload_encoding=None)

    >>> assert oKeyring.get_password(service, username) is None

    >>> oKeyring.set_password(service, username, password)
    >>> actual_password = oKeyring.get_password(service, username)
    >>> assert password == actual_password

== /usr/local/src/python-keyutils/test/ ==

    >>> os.chdir('/usr/local/src/python-keyutils/test/')
    >>> os.system('python3.sh keyutils_test.py')

marcus@linux:~/python-keyring-keyutils> keyctl print 22790318
secret
