|
1 |
| -# Copyright 2010-2021 The pygit2 contributors |
| 1 | +# Copyright 2010-2022 The pygit2 contributors |
2 | 2 | #
|
3 | 3 | # This file is free software; you can redistribute it and/or modify
|
4 | 4 | # it under the terms of the GNU General Public License, version 2,
|
|
23 | 23 | # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
24 | 24 | # Boston, MA 02110-1301, USA.
|
25 | 25 |
|
26 |
| -import pygit2 |
27 |
| -import pytest |
| 26 | +from pygit2 import GIT_OBJ_COMMIT, Oid, Signature |
28 | 27 |
|
29 |
| -from . import utils |
| 28 | +content = """\ |
| 29 | +tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 30 | +parent 8496071c1b46c854b31185ea97743be6a8774479 |
| 31 | +author Ben Burkert <[email protected]> 1358451456 -0800 |
| 32 | +committer Ben Burkert <[email protected]> 1358451456 -0800 |
30 | 33 |
|
| 34 | +a simple commit which works\ |
| 35 | +""" |
31 | 36 |
|
32 |
| -@pytest.fixture |
33 |
| -def repo(tmp_path): |
34 |
| - with utils.TemporaryRepository('gpgsigned.zip', tmp_path) as path: |
35 |
| - yield pygit2.Repository(path) |
| 37 | +gpgsig = """\ |
| 38 | +-----BEGIN PGP SIGNATURE----- |
| 39 | +Version: GnuPG v1.4.12 (Darwin) |
36 | 40 |
|
| 41 | +iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al |
| 42 | +o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8 |
| 43 | +JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq |
| 44 | +AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq |
| 45 | +SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW |
| 46 | +who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok |
| 47 | +6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG |
| 48 | +cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu |
| 49 | +c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9 |
| 50 | +ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J |
| 51 | +7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc |
| 52 | +cpxtDQQMGYFpXK/71stq |
| 53 | +=ozeK |
| 54 | +-----END PGP SIGNATURE-----\ |
| 55 | +""" |
37 | 56 |
|
38 |
| -def test_get_gpg_signature_when_signed(repo): |
39 |
| - signed_hash = 'a00b212d5455ad8c4c1779f778c7d2a81bb5da23' |
40 |
| - expected_signature = ( |
41 |
| - '-----BEGIN PGP SIGNATURE-----\n\n' |
42 |
| - 'iQFGBAABCgAwFiEEQZu9JtePgJbDk7VC0+mlK74z13oFAlpzXykSHG1hcmtAbWFy\n' |
43 |
| - 'a2FkYW1zLm1lAAoJENPppSu+M9d6FRoIAJXeQRRT1V47nnHITiel6426loYkeij7\n' |
44 |
| - '66doGNIyll95H92SwH4LAjPyEEByIG1VsA6NztzUoNgnEvAXI0iAz3LyI7N16M4b\n' |
45 |
| - 'dPDkC72pp8tu280H5Qt5b2V5hmlKKSgtOS5iNhdU/FbWVS8MlHsqzQTZfoTdi6ch\n' |
46 |
| - 'KWUsjzudVd3F/H/AU+1Jsxt8Iz/oK4T/puUQLnJZKjKlljGP994FA3JIpnZpZmbG\n' |
47 |
| - 'FybYJEDXnng7uhx3Fz/Mo3KBJoQfAExTtaToY0n0hSjOe6GN9rEsRSMK3mWdysf2\n' |
48 |
| - 'wOdtYMMcT16hG5tAwnD/myZ4rIIpyZJ/9mjymdUsj6UKf7D+vJuqfsI=\n=IyYy\n' |
49 |
| - '-----END PGP SIGNATURE-----' |
50 |
| - ).encode('ascii') |
| 57 | +gpgsig_content = """\ |
| 58 | +tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 59 | +parent 8496071c1b46c854b31185ea97743be6a8774479 |
| 60 | +author Ben Burkert <[email protected]> 1358451456 -0800 |
| 61 | +committer Ben Burkert <[email protected]> 1358451456 -0800 |
| 62 | +gpgsig -----BEGIN PGP SIGNATURE----- |
| 63 | + Version: GnuPG v1.4.12 (Darwin) |
| 64 | + |
| 65 | + iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al |
| 66 | + o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8 |
| 67 | + JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq |
| 68 | + AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq |
| 69 | + SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW |
| 70 | + who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok |
| 71 | + 6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG |
| 72 | + cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu |
| 73 | + c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9 |
| 74 | + ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J |
| 75 | + 7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc |
| 76 | + cpxtDQQMGYFpXK/71stq |
| 77 | + =ozeK |
| 78 | + -----END PGP SIGNATURE----- |
51 | 79 |
|
52 |
| - expected_payload = ( |
53 |
| - 'tree c36c20831e43e5984c672a714661870b67ab1d95\nauthor Mark Adams ' |
54 |
| - '<[email protected]> 1517510299 -0600\ncommitter Mark Adams <ma' |
55 |
| - '[email protected]> 1517510441 -0600\n\nMaking a GPG signed commi' |
56 |
| - 't\n' |
57 |
| - ).encode('ascii') |
| 80 | +a simple commit which works\ |
| 81 | +""" |
| 82 | +# NOTE: ^^^ mind the gap (space must exist after GnuPG header) ^^^ |
| 83 | +# XXX: seems macos wants the space while linux does not |
58 | 84 |
|
59 |
| - commit = repo.get(signed_hash) |
| 85 | + |
| 86 | +def test_commit_signing(gpgsigned): |
| 87 | + repo = gpgsigned |
| 88 | + message = "a simple commit which works" |
| 89 | + author = Signature( |
| 90 | + name="Ben Burkert", |
| 91 | + |
| 92 | + time=1358451456, |
| 93 | + offset=-480, |
| 94 | + ) |
| 95 | + committer = Signature( |
| 96 | + name="Ben Burkert", |
| 97 | + |
| 98 | + time=1358451456, |
| 99 | + offset=-480, |
| 100 | + ) |
| 101 | + tree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" |
| 102 | + parents = ["8496071c1b46c854b31185ea97743be6a8774479"] |
| 103 | + |
| 104 | + # create commit string |
| 105 | + commit_string = repo.create_commit_string( |
| 106 | + author, committer, message, tree, parents |
| 107 | + ) |
| 108 | + assert commit_string == content |
| 109 | + |
| 110 | + # create/retrieve signed commit |
| 111 | + oid = repo.create_commit_with_signature(content, gpgsig) |
| 112 | + commit = repo.get(oid) |
60 | 113 | signature, payload = commit.gpg_signature
|
61 | 114 |
|
62 |
| - assert signature == expected_signature |
63 |
| - assert payload == expected_payload |
| 115 | + # validate signed commit |
| 116 | + assert content == payload.decode("utf-8") |
| 117 | + assert gpgsig == signature.decode("utf-8") |
| 118 | + assert gpgsig_content == commit.read_raw().decode("utf-8") |
| 119 | + |
| 120 | + # perform sanity checks |
| 121 | + assert GIT_OBJ_COMMIT == commit.type |
| 122 | + assert "6569fdf71dbd99081891154641869c537784a3ba" == commit.hex |
| 123 | + assert commit.message_encoding is None |
| 124 | + assert message == commit.message |
| 125 | + assert 1358451456 == commit.commit_time |
| 126 | + assert committer == commit.committer |
| 127 | + assert author == commit.author |
| 128 | + assert tree == commit.tree.hex |
| 129 | + assert Oid(hex=tree) == commit.tree_id |
| 130 | + assert 1 == len(commit.parents) |
| 131 | + assert parents[0] == commit.parents[0].hex |
| 132 | + assert Oid(hex=parents[0]) == commit.parent_ids[0] |
| 133 | + |
64 | 134 |
|
| 135 | +def test_get_gpg_signature_when_unsigned(gpgsigned): |
| 136 | + unhash = "5b5b025afb0b4c913b4c338a42934a3863bf3644" |
65 | 137 |
|
66 |
| -def test_get_gpg_signature_when_unsigned(repo): |
67 |
| - unsigned_hash = 'a84938d1d885e80dae24b86b06621cec47ff6edd' |
68 |
| - commit = repo.get(unsigned_hash) |
| 138 | + repo = gpgsigned |
| 139 | + commit = repo.get(unhash) |
69 | 140 | signature, payload = commit.gpg_signature
|
70 | 141 |
|
71 | 142 | assert signature is None
|
|
0 commit comments