<commit_before>import pytest
import os

import pgpy
from pgpy.pgpdump import PGPDumpFormat
from pgpy.errors import PGPError

keys = [
    "tests/testdata/testkeys.gpg",
    "tests/testdata/testkeys.sec.gpg",
    ["tests/testdata/testkeys.gpg", "tests/testdata/testkeys.sec.gpg"]
]
keyids = [
    "testkeys",
    "testkeys-sec",
    "testkeys-both",
]


@pytest.fixture(params=keys, ids=keyids)
def load_key(request):
    return request.param

akeys = [
    [ k for k in os.listdir("tests/testdata/pubkeys/") if k[-4:] == ".key" ],
    [ k for k in os.listdir("tests/testdata/seckeys/") if k[-8:] == ".sec.key" ],
]
akeys.append(keys[0] + keys[1])
akeyids = [
    "pubkeys",
    "seckeys",
    "both"
]


@pytest.fixture(params=akeys, ids=akeyids)
def load_akey(request):
    return request.param


class TestPGPKeyring:
    def test_load(self, load_key, pgpdump):
        k = pgpy.PGPKeyring(load_key)

        assert '\n'.join(PGPDumpFormat(k).out) + '\n' == pgpdump.decode()

    ##TODO: test keyring contents against ascii armored key contents
    # def test_load2(self, load_key, load_akey):
    #     pass

    def test_bytes(self, load_key):
        k = pgpy.PGPKeyring(load_key)
        fb = b''.join([open(f, 'rb').read() for f in load_key]) if type(load_key) is list else open(load_key, 'rb').read()

        assert k.__bytes__() == fb

    ##TODO: test bytes
    # def test_str(self, load_key):
    #     pass

    @pytest.mark.parametrize("sigf, sigsub",
                             [
                                 pytest.mark.xfail(
                                     ("tests/testdata/ubuntu-precise/Release.gpg", "tests/testdata/ubuntu-precise/Release")
                                 ),
                                 ("tests/testdata/debian-sid/Release.gpg", "tests/testdata/debian-sid/Release"),
                                 ("tests/testdata/aa-testing/Release.gpg", "tests/testdata/aa-testing/Release"),
                             ], ids=[
                                 "local-ubuntu",
                                 "local-debian",
                                 "local-aa-testing",
                             ])
    def test_verify(self, sigf, sigsub):
        k = pgpy.PGPKeyring(["tests/testdata/testkeys.gpg", "tests/testdata/testkeys.sec.gpg"])
<commit_msg>Test signing!<commit_after>import pytest
import os
from subprocess import check_output, STDOUT

import pgpy
from pgpy.pgpdump import PGPDumpFormat
from pgpy.errors import PGPError

keys = [
    "tests/testdata/testkeys.gpg",
    "tests/testdata/testkeys.sec.gpg",
    ["tests/testdata/testkeys.gpg", "tests/testdata/testkeys.sec.gpg"]
]
keyids = [
    "testkeys",
    "testkeys-sec",
    "testkeys-both",
]


@pytest.fixture(params=keys, ids=keyids)
def load_key(request):
    return request.param

akeys = [
    [ k for k in os.listdir("tests/testdata/pubkeys/") if k[-4:] == ".key" ],
    [ k for k in os.listdir("tests/testdata/seckeys/") if k[-8:] == ".sec.key" ],
]
akeys.append(keys[0] + keys[1])
akeyids = [
    "pubkeys",
    "seckeys",
    "both"
]


@pytest.fixture(params=akeys, ids=akeyids)
def load_akey(request):
    return request.param


class TestPGPKeyring:
    def test_load(self, load_key, pgpdump):
        k = pgpy.PGPKeyring(load_key)

        assert '\n'.join(PGPDumpFormat(k).out) + '\n' == pgpdump.decode()

    ##TODO: test keyring contents against ascii armored key contents
    # def test_load2(self, load_key, load_akey):
    #     pass

    def test_bytes(self, load_key):
        k = pgpy.PGPKeyring(load_key)
        fb = b''.join([open(f, 'rb').read() for f in load_key]) if type(load_key) is list else open(load_key, 'rb').read()

        assert k.__bytes__() == fb

    ##TODO: test str
    # def test_str(self, load_key):
    #     pass

    @pytest.mark.parametrize("keyid",
                             ["<KEY>", "<KEY>",
                              "<KEY>",
                              "ADC1 4E18 DAD7 52BB F78D  4422 6D30 F666 <KEY>"],
                             ids=["half-key id", "key id", "fp-no-spaces", "fingerprint"])
    def test_key_selection(self, keyid):
        k = pgpy.PGPKeyring(["tests/testdata/testkeys.gpg", "tests/testdata/testkeys.sec.gpg"])

        with k.key(keyid):
            assert k.using == "<KEY>"

    def test_sign_with_rsa(self):
        k = pgpy.PGPKeyring(["tests/testdata/testkeys.gpg", "tests/testdata/testkeys.sec.gpg"])

        with k.key("6C368E85"):
            sig = k.sign("tests/testdata/unsigned_message")

            # verify the signature ourselves first
            assert k.verify("tests/testdata/unsigned_message", str(sig).encode())

        # now write out to a file and test with gpg
        with open('tests/testdata/unsigned_message.asc', 'w') as sigf:
            sigf.write(str(sig))

        assert b'Good signature from "TestRSAKey (TESTING-USE-ONLY) <<EMAIL>>"' in \
            check_output(['gpg',
                          '--no-default-keyring',
                          '--keyring', 'tests/testdata/testkeys.gpg',
                          '--secret-keyring', 'tests/testdata/testkeys.sec.gpg',
                          '-vv',
                          '--verify', 'tests/testdata/unsigned_message.asc',
                          'tests/testdata/unsigned_message'], stderr=STDOUT)

        # and finally, remove the file
        os.remove('tests/testdata/unsigned_message.asc')

    def test_sign_with_encrypted_rsa(self):
        k = pgpy.PGPKeyring(["tests/testdata/testkeys.gpg", "tests/testdata/testkeys.sec.gpg"])

        with k.key("AD66AAD5"):
            # first, make sure an exception is raised if we try to sign with it before decrypting
            with pytest.raises(PGPError):
                k.sign("tests/testdata/unsigned_message")

            # now decrypt the key with the right passphrase and *then* sign
            k.unlock("QwertyUiop")
            sig = k.sign("tests/testdata/unsigned_message")

            # verify the signature ourselves first
            assert k.verify("tests/testdata/unsigned_message", str(sig).encode())

        # now write out to a file and test with gpg
        with open('tests/testdata/unsigned_message.asc', 'w') as sigf:
            sigf.write(str(sig))

        assert b'Good signature from "KeyTestDecryption-RSA (Passphrase: <PASSWORD>) <<EMAIL>>"' in \
            check_output(['gpg',
                          '--no-default-keyring',
                          '--keyring', 'tests/testdata/testkeys.gpg',
                          '--secret-keyring', 'tests/testdata/testkeys.sec.gpg',
                          '-vv',
                          '--verify', 'tests/testdata/unsigned_message.asc',
                          'tests/testdata/unsigned_message'], stderr=STDOUT)

        # and finally, remove the file
        os.remove('tests/testdata/unsigned_message.asc')

    @pytest.mark.parametrize("sigf, sigsub",
                             [
                                 pytest.mark.xfail(
                                     ("tests/testdata/ubuntu-precise/Release.gpg", "tests/testdata/ubuntu-precise/Release")
                                 ),
                                 ("tests/testdata/debian-sid/Release.gpg", "tests/testdata/debian-sid/Release"),
                                 ("tests/testdata/aa-testing/Release.gpg", "tests/testdata/aa-testing/Release"),
                                 ("tests/testdata/signed_message.asc", "tests/testdata/signed_message"),
                             ], ids=[
                                 "local-ubuntu",
                                 "local-debian",
                                 "local-aa-testing",
                                 "signed_message",
                             ])
    def test_verify_signature(self, sigf, sigsub):
        k = pgpy.PGPKeyring(["tests/testdata/testkeys.gpg", "tests/testdata/testkeys.sec.gpg"])

{'fraction_non_alphanumeric': 0.13541930580187483, 'fraction_numerical': 0.004813782619711173, 'mean_word_length': 1.8898243045387995, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4087792', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2471, 'n_words': 453}