test_gpg.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import datetime
  19. import unittest
  20. from base_test import DakTestCase, fixture
  21. from daklib.gpg import GpgException, SignedFile
  22. keyring = fixture('gpg/gnupghome/pubring.gpg')
  23. fpr_valid = '0ABB89079CB58F8F94F6F310CB9D5C5828606E84'
  24. fpr_expired = '05A558AE65B77B559BBE0C4D543B2BAEDA044F0B'
  25. fpr_expired_subkey = '8865D9EC71713394ADBD8F729F7A24B7F6388CE1'
  26. def verify(filename, require_signature=True):
  27. with open(fixture(filename)) as fh:
  28. data = fh.read()
  29. return SignedFile(data, [keyring], require_signature)
  30. class GpgTest(DakTestCase):
  31. def test_valid(self):
  32. result = verify('gpg/valid.asc')
  33. self.assertTrue(result.valid)
  34. self.assertEqual(result.primary_fingerprint, fpr_valid)
  35. self.assertEqual(result.contents, "Valid: yes\n")
  36. self.assertEqual(result.signature_timestamp, datetime.datetime(2014, 9, 2, 21, 24, 10))
  37. def test_expired(self):
  38. result = verify('gpg/expired.asc', False)
  39. self.assertFalse(result.valid)
  40. self.assertEqual(result.primary_fingerprint, fpr_expired)
  41. self.assertEqual(result.contents, "Valid: expired\n")
  42. self.assertEqual(result.signature_timestamp, datetime.datetime(2001, 2, 1, 0, 0, 0))
  43. def test_expired_assertion(self):
  44. with self.assertRaises(GpgException):
  45. verify('gpg/expired.asc')
  46. def test_expired_subkey(self):
  47. result = verify('gpg/expired-subkey.asc', False)
  48. self.assertFalse(result.valid)
  49. self.assertEqual(result.primary_fingerprint, fpr_expired_subkey)
  50. self.assertEqual(result.contents, "Valid: expired-subkey\n")
  51. self.assertEqual(result.signature_timestamp, datetime.datetime(2014, 2, 1, 0, 0, 0))
  52. def test_expires_subkey_assertion(self):
  53. with self.assertRaises(GpgException):
  54. verify('gpg/expired-subkey.asc')
  55. def test_message_assertion(self):
  56. with self.assertRaises(GpgException):
  57. verify('gpg/message.asc')
  58. def test_plain_assertion(self):
  59. with self.assertRaises(GpgException):
  60. verify('gpg/plaintext.txt')
  61. if __name__ == '__main__':
  62. unittest.main()