test_gpg.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #! /usr/bin/env python3
  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), mode='rb') 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.assertFalse(result.weak_signature)
  35. self.assertEqual(result.primary_fingerprint, fpr_valid)
  36. self.assertEqual(result.contents, b"Valid: yes\n")
  37. self.assertEqual(result.signature_timestamp, datetime.datetime(2014, 9, 2, 21, 24, 10))
  38. def test_weak_sha1(self):
  39. result = verify('gpg/sha1.asc')
  40. self.assertTrue(result.valid)
  41. self.assertTrue(result.weak_signature)
  42. self.assertEqual(result.primary_fingerprint, fpr_valid)
  43. self.assertEqual(result.contents, b"Message generated with gpg --homedir gnupghome --digest-algo=sha1 --clearsign\n")
  44. self.assertEqual(result.signature_timestamp, datetime.datetime(2017, 2, 22, 18, 59, 59))
  45. def test_weak_ripemd160(self):
  46. result = verify('gpg/ripemd160.asc')
  47. self.assertTrue(result.valid)
  48. self.assertTrue(result.weak_signature)
  49. self.assertEqual(result.primary_fingerprint, fpr_valid)
  50. self.assertEqual(result.contents, b"Message generated with gpg --homedir gnupghome --digest-algo=ripemd160 --clearsign\n")
  51. self.assertEqual(result.signature_timestamp, datetime.datetime(2017, 2, 22, 19, 2, 54))
  52. def test_expired(self):
  53. result = verify('gpg/expired.asc', False)
  54. self.assertFalse(result.valid)
  55. self.assertEqual(result.primary_fingerprint, fpr_expired)
  56. self.assertEqual(result.contents, b"Valid: expired\n")
  57. self.assertEqual(result.signature_timestamp, datetime.datetime(2001, 2, 1, 0, 0, 0))
  58. def test_expired_assertion(self):
  59. with self.assertRaises(GpgException):
  60. verify('gpg/expired.asc')
  61. def test_expired_subkey(self):
  62. result = verify('gpg/expired-subkey.asc', False)
  63. self.assertFalse(result.valid)
  64. self.assertEqual(result.primary_fingerprint, fpr_expired_subkey)
  65. self.assertEqual(result.contents, b"Valid: expired-subkey\n")
  66. self.assertEqual(result.signature_timestamp, datetime.datetime(2014, 2, 1, 0, 0, 0))
  67. def test_expires_subkey_assertion(self):
  68. with self.assertRaises(GpgException):
  69. verify('gpg/expired-subkey.asc')
  70. def test_message_assertion(self):
  71. with self.assertRaises(GpgException):
  72. verify('gpg/message.asc')
  73. def test_plain_assertion(self):
  74. with self.assertRaises(GpgException):
  75. verify('gpg/plaintext.txt')
  76. def test_md5_assertion(self):
  77. with self.assertRaises(GpgException):
  78. verify('gpg/md5.asc')
  79. if __name__ == '__main__':
  80. unittest.main()