]> git.decadent.org.uk Git - dak.git/blob - tests/test_gpg.py
Reorganize code a bit.
[dak.git] / tests / test_gpg.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2014, Ansgar Burchardt <ansgar@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import datetime
20 import unittest
21 from base_test import DakTestCase, fixture
22 from daklib.gpg import GpgException, SignedFile
23
24 keyring = fixture('gpg/gnupghome/pubring.gpg')
25 fpr_valid = '0ABB89079CB58F8F94F6F310CB9D5C5828606E84'
26 fpr_expired = '05A558AE65B77B559BBE0C4D543B2BAEDA044F0B'
27 fpr_expired_subkey = '8865D9EC71713394ADBD8F729F7A24B7F6388CE1'
28
29 def verify(filename, require_signature=True):
30     with open(fixture(filename)) as fh:
31         data = fh.read()
32     return SignedFile(data, [keyring], require_signature)
33
34 class GpgTest(DakTestCase):
35     def test_valid(self):
36         result = verify('gpg/valid.asc')
37         self.assertTrue(result.valid)
38         self.assertEqual(result.primary_fingerprint, fpr_valid)
39         self.assertEqual(result.contents, "Valid: yes\n")
40         self.assertEqual(result.signature_timestamp, datetime.datetime(2014, 9, 2, 21, 24, 10))
41
42     def test_expired(self):
43         result = verify('gpg/expired.asc', False)
44         self.assertFalse(result.valid)
45         self.assertEqual(result.primary_fingerprint, fpr_expired)
46         self.assertEqual(result.contents, "Valid: expired\n")
47         self.assertEqual(result.signature_timestamp, datetime.datetime(2001, 2, 1, 0, 0, 0))
48
49     def test_expired_assertion(self):
50         with self.assertRaises(GpgException):
51             verify('gpg/expired.asc')
52
53     def test_expired_subkey(self):
54         result = verify('gpg/expired-subkey.asc', False)
55         self.assertFalse(result.valid)
56         self.assertEqual(result.primary_fingerprint, fpr_expired_subkey)
57         self.assertEqual(result.contents, "Valid: expired-subkey\n")
58         self.assertEqual(result.signature_timestamp, datetime.datetime(2014, 2, 1, 0, 0, 0))
59
60     def test_expires_subkey_assertion(self):
61         with self.assertRaises(GpgException):
62             verify('gpg/expired-subkey.asc')
63
64     def test_message_assertion(self):
65         with self.assertRaises(GpgException):
66             verify('gpg/message.asc')
67
68     def test_plain_assertion(self):
69         with self.assertRaises(GpgException):
70             verify('gpg/plaintext.txt')
71
72 if __name__ == '__main__':
73     unittest.main()