# -*- encoding: utf-8 -*-
#
# Copyright (c) 2011 Ralf Schlatterbeck, rsc@runtux.com.
# This module is free software, and you may redistribute it and/or modify
# under the same terms as Python, so long as this copyright message and
# disclaimer are retained in their original form.
#
# This module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

import shutil, os
try:
    import gpg, gpg.core
except ImportError:
    gpg = None

pgp_test_key = """
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: GnuPG v1.4.10 (GNU/Linux)

lQOYBE6NqtsBCADG3UUMYxjwUOpDDVvr0Y8qkvKsgdF79en1zfHtRYlmZc+EJxg8
53CCFGReQWJwOjyP3/SLJwJqfiPR7MAYAqJsm/4U2lxF7sIlEnlrRpFuvB625KOQ
oedCkI4nLa+4QAXHxVX2qLx7es3r2JAoitZLX7ZtUB7qGSRh98DmdAgCY3CFN7iZ
w6xpvIU+LNbsHSo1sf8VP6z7NHQFacgrVvLyRJ4C5lTPU42iM5E6HKxYFExNV3Rn
+2G0bsuiifHV6nJQD73onjwcC6tU97W779dllHlhG3SSP0KlnwmCCvPMlQvROk0A
rLyzKWcUpZwK1aLRYByjFMH9WYXRkhf08bkDABEBAAEAB/9dcmSb6YUyiBNM5t4m
9hZcXykBvw79PRVvmBLy+BYUtArLgsN0+xx3Q7XWRMtJCVSkFw0GxpHwEM4sOyAZ
KEPC3ZqLmgB6LDO2z/OWYVa9vlCAiPgDYtEVCnCCIInN/ue4dBZtDeVj8NUK2n0D
UBpa2OMUgu3D+4SJNK7EnAmXdOaP6yfe6SXwcQfti8UoSFMJRkQkbY1rm/6iPfON
t2RBAc7jW4eRzdciWCfvJfMSj9cqxTBQWz5vVadeY9Bm/IKw1HiKNBrJratq2v+D
VGr0EkE9oOa5zbgZt2CFvknE4YhGmv81xFdK5GXr8L7nluZrePMblWbkI2ICTbV0
RKLhBADYLvyDFX3cCoFzWmCl5L32G6LLfTt0yU0eUHcAzXd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YAQIACQUCTo3C8AIbDAAKCRC/z7qg+FujnDzYA/9EU6Pv
Ci1+DCtxjnq7IOvOjqExhFNGvN9Dw17Tl8HcyW3if9v5RxeSWYKl0DhzVdzMQgH/
78q4F4W1q2IkB7SCpXizHLIc3eh8iZkbWZE+CGPvTpqyF03Yi16qhxpAbkGs2Yhq
jTx5oJ4CL5fybBOZLg+BTlK4HIee6xEcbNoq+A==
=ZKBW
-----END PGP PRIVATE KEY BLOCK-----
"""

ownertrust = """
723762CD5A5FECB76DC72DF85ADCFD5631C38741:6:
2940C247A1FBAD508A1AF24BBFCFBAA0F85BA39C:6:
"""

pgphome = 'pgp-test-home'
def setUpPGP():
    # prevent test from failing if left over from earlier run:
    if os.path.exists(pgphome):
        shutil.rmtree(pgphome)
    os.mkdir(pgphome)
    os.environ['GNUPGHOME'] = pgphome
    # gpgme_check_version() must have been called once in a programm
    # to initialise some subsystems of gpgme. See roundup/roundupdb.py.
    ctx = gpg.core.Context()
    key = gpg.core.Data(pgp_test_key)
    ctx.op_import(key)
    key = gpg.core.Data(john_doe_key)
    ctx.op_import(key)
    # trust-modelling with pyme isn't working in 0.8.1
    # based on libgpgme11 1.2.0, also tried in C -- same thing.
    otrust = os.popen ('gpg --import-ownertrust 2> /dev/null', 'w')
    otrust.write(ownertrust)
    otrust.close()

def tearDownPGP():
    if os.path.exists(pgphome):
        shutil.rmtree(pgphome, True) # ignore errors


# vim: set filetype=python sts=4 sw=4 et si :
