test_OpenPGP.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import nose
  2. import os.path
  3. import OpenPGP
  4. class TestASCIIArmor:
  5. def readLocalFile(self, filename):
  6. return open(os.path.dirname(__file__) + filename, 'r').read()
  7. def test_unarmor_one(self):
  8. armored = self.readLocalFile('/data/helloKey.asc')
  9. _, unarmored = OpenPGP.unarmor(armored)[0]
  10. message = OpenPGP.Message.parse(unarmored)
  11. nose.tools.assert_equal(message[0].fingerprint(), '421F28FEAAD222F856C8FFD5D4D54EA16F87040E')
  12. def test_enarmor_one(self):
  13. expected = self.readLocalFile('/data/helloKey.asc')
  14. messages = OpenPGP.unarmor(expected) # [(header, data), ...]
  15. header, data = messages[0]
  16. actual = OpenPGP.enarmor(data, headers = [keyValue.split(': ', 1) for keyValue in header.split('\n')])
  17. nose.tools.assert_equal(actual, expected)
  18. class TestSerialization:
  19. def one_serialization(self, path):
  20. inm = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/' + path, 'rb').read())
  21. mid = inm.to_bytes()
  22. out = OpenPGP.Message.parse(mid)
  23. nose.tools.assert_equal(inm, out)
  24. def test000001006public_key(self):
  25. self.one_serialization("000001-006.public_key")
  26. def test000002013user_id(self):
  27. self.one_serialization("000002-013.user_id")
  28. def test000003002sig(self):
  29. self.one_serialization("000003-002.sig")
  30. def test000004012ring_trust(self):
  31. self.one_serialization("000004-012.ring_trust")
  32. def test000005002sig(self):
  33. self.one_serialization("000005-002.sig")
  34. def test000006012ring_trust(self):
  35. self.one_serialization("000006-012.ring_trust")
  36. def test000007002sig(self):
  37. self.one_serialization("000007-002.sig")
  38. def test000008012ring_trust(self):
  39. self.one_serialization("000008-012.ring_trust")
  40. def test000009002sig(self):
  41. self.one_serialization("000009-002.sig")
  42. def test000010012ring_trust(self):
  43. self.one_serialization("000010-012.ring_trust")
  44. def test000011002sig(self):
  45. self.one_serialization("000011-002.sig")
  46. def test000012012ring_trust(self):
  47. self.one_serialization("000012-012.ring_trust")
  48. def test000013014public_subkey(self):
  49. self.one_serialization("000013-014.public_subkey")
  50. def test000014002sig(self):
  51. self.one_serialization("000014-002.sig")
  52. def test000015012ring_trust(self):
  53. self.one_serialization("000015-012.ring_trust")
  54. def test000016006public_key(self):
  55. self.one_serialization("000016-006.public_key")
  56. def test000017002sig(self):
  57. self.one_serialization("000017-002.sig")
  58. def test000018012ring_trust(self):
  59. self.one_serialization("000018-012.ring_trust")
  60. def test000019013user_id(self):
  61. self.one_serialization("000019-013.user_id")
  62. def test000020002sig(self):
  63. self.one_serialization("000020-002.sig")
  64. def test000021012ring_trust(self):
  65. self.one_serialization("000021-012.ring_trust")
  66. def test000022002sig(self):
  67. self.one_serialization("000022-002.sig")
  68. def test000023012ring_trust(self):
  69. self.one_serialization("000023-012.ring_trust")
  70. def test000024014public_subkey(self):
  71. self.one_serialization("000024-014.public_subkey")
  72. def test000025002sig(self):
  73. self.one_serialization("000025-002.sig")
  74. def test000026012ring_trust(self):
  75. self.one_serialization("000026-012.ring_trust")
  76. def test000027006public_key(self):
  77. self.one_serialization("000027-006.public_key")
  78. def test000028002sig(self):
  79. self.one_serialization("000028-002.sig")
  80. def test000029012ring_trust(self):
  81. self.one_serialization("000029-012.ring_trust")
  82. def test000030013user_id(self):
  83. self.one_serialization("000030-013.user_id")
  84. def test000031002sig(self):
  85. self.one_serialization("000031-002.sig")
  86. def test000032012ring_trust(self):
  87. self.one_serialization("000032-012.ring_trust")
  88. def test000033002sig(self):
  89. self.one_serialization("000033-002.sig")
  90. def test000034012ring_trust(self):
  91. self.one_serialization("000034-012.ring_trust")
  92. def test000035006public_key(self):
  93. self.one_serialization("000035-006.public_key")
  94. def test000036013user_id(self):
  95. self.one_serialization("000036-013.user_id")
  96. def test000037002sig(self):
  97. self.one_serialization("000037-002.sig")
  98. def test000038012ring_trust(self):
  99. self.one_serialization("000038-012.ring_trust")
  100. def test000039002sig(self):
  101. self.one_serialization("000039-002.sig")
  102. def test000040012ring_trust(self):
  103. self.one_serialization("000040-012.ring_trust")
  104. def test000041017attribute(self):
  105. self.one_serialization("000041-017.attribute")
  106. def test000042002sig(self):
  107. self.one_serialization("000042-002.sig")
  108. def test000043012ring_trust(self):
  109. self.one_serialization("000043-012.ring_trust")
  110. def test000044014public_subkey(self):
  111. self.one_serialization("000044-014.public_subkey")
  112. def test000045002sig(self):
  113. self.one_serialization("000045-002.sig")
  114. def test000046012ring_trust(self):
  115. self.one_serialization("000046-012.ring_trust")
  116. def test000047005secret_key(self):
  117. self.one_serialization("000047-005.secret_key")
  118. def test000048013user_id(self):
  119. self.one_serialization("000048-013.user_id")
  120. def test000049002sig(self):
  121. self.one_serialization("000049-002.sig")
  122. def test000050012ring_trust(self):
  123. self.one_serialization("000050-012.ring_trust")
  124. def test000051007secret_subkey(self):
  125. self.one_serialization("000051-007.secret_subkey")
  126. def test000052002sig(self):
  127. self.one_serialization("000052-002.sig")
  128. def test000053012ring_trust(self):
  129. self.one_serialization("000053-012.ring_trust")
  130. def test000054005secret_key(self):
  131. self.one_serialization("000054-005.secret_key")
  132. def test000055002sig(self):
  133. self.one_serialization("000055-002.sig")
  134. def test000056012ring_trust(self):
  135. self.one_serialization("000056-012.ring_trust")
  136. def test000057013user_id(self):
  137. self.one_serialization("000057-013.user_id")
  138. def test000058002sig(self):
  139. self.one_serialization("000058-002.sig")
  140. def test000059012ring_trust(self):
  141. self.one_serialization("000059-012.ring_trust")
  142. def test000060007secret_subkey(self):
  143. self.one_serialization("000060-007.secret_subkey")
  144. def test000061002sig(self):
  145. self.one_serialization("000061-002.sig")
  146. def test000062012ring_trust(self):
  147. self.one_serialization("000062-012.ring_trust")
  148. def test000063005secret_key(self):
  149. self.one_serialization("000063-005.secret_key")
  150. def test000064002sig(self):
  151. self.one_serialization("000064-002.sig")
  152. def test000065012ring_trust(self):
  153. self.one_serialization("000065-012.ring_trust")
  154. def test000066013user_id(self):
  155. self.one_serialization("000066-013.user_id")
  156. def test000067002sig(self):
  157. self.one_serialization("000067-002.sig")
  158. def test000068012ring_trust(self):
  159. self.one_serialization("000068-012.ring_trust")
  160. def test000069005secret_key(self):
  161. self.one_serialization("000069-005.secret_key")
  162. def test000070013user_id(self):
  163. self.one_serialization("000070-013.user_id")
  164. def test000071002sig(self):
  165. self.one_serialization("000071-002.sig")
  166. def test000072012ring_trust(self):
  167. self.one_serialization("000072-012.ring_trust")
  168. def test000073017attribute(self):
  169. self.one_serialization("000073-017.attribute")
  170. def test000074002sig(self):
  171. self.one_serialization("000074-002.sig")
  172. def test000075012ring_trust(self):
  173. self.one_serialization("000075-012.ring_trust")
  174. def test000076007secret_subkey(self):
  175. self.one_serialization("000076-007.secret_subkey")
  176. def test000077002sig(self):
  177. self.one_serialization("000077-002.sig")
  178. def test000078012ring_trust(self):
  179. self.one_serialization("000078-012.ring_trust")
  180. def test002182002sig(self):
  181. self.one_serialization("002182-002.sig")
  182. def testpubringgpg(self):
  183. self.one_serialization("pubring.gpg")
  184. def testsecringgpg(self):
  185. self.one_serialization("secring.gpg")
  186. def testcompressedsiggpg(self):
  187. self.one_serialization("compressedsig.gpg")
  188. def testcompressedsigzlibgpg(self):
  189. self.one_serialization("compressedsig-zlib.gpg")
  190. def testcompressedsigbzip2gpg(self):
  191. self.one_serialization("compressedsig-bzip2.gpg")
  192. def testonepass_sig(self):
  193. self.one_serialization("onepass_sig")
  194. def testsymmetrically_encrypted(self):
  195. self.one_serialization("symmetrically_encrypted")
  196. def testuncompressedopsdsagpg(self):
  197. self.one_serialization("uncompressed-ops-dsa.gpg")
  198. def testuncompressedopsdsasha384txtgpg(self):
  199. self.one_serialization("uncompressed-ops-dsa-sha384.txt.gpg")
  200. def testuncompressedopsrsagpg(self):
  201. self.one_serialization("uncompressed-ops-rsa.gpg")
  202. def testSymmetricAES(self):
  203. self.one_serialization("symmetric-aes.gpg")
  204. def testSymmetricNoMDC(self):
  205. self.one_serialization("symmetric-no-mdc.gpg")
  206. class TestFingerprint:
  207. def one_fingerprint(self, path, kf):
  208. m = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/' + path, 'rb').read())
  209. nose.tools.assert_equal(m[0].fingerprint(), kf)
  210. def test000001006public_key(self):
  211. self.one_fingerprint("000001-006.public_key", "421F28FEAAD222F856C8FFD5D4D54EA16F87040E")
  212. def test000016006public_key(self):
  213. self.one_fingerprint("000016-006.public_key", "AF95E4D7BAC521EE9740BED75E9F1523413262DC")
  214. def test000027006public_key(self):
  215. self.one_fingerprint("000027-006.public_key", "1EB20B2F5A5CC3BEAFD6E5CB7732CF988A63EA86")
  216. def test000035006public_key(self):
  217. self.one_fingerprint("000035-006.public_key", "CB7933459F59C70DF1C3FBEEDEDC3ECF689AF56D")
  218. class TestStreaming:
  219. def test_partial_results(self):
  220. m = OpenPGP.Message.parse(OpenPGP.Message([OpenPGP.UserIDPacket('My name <e@example.com>'), OpenPGP.UserIDPacket('Your name <y@example.com>')]).to_bytes())
  221. m[0] # Just the first one
  222. nose.tools.assert_equal(len(m.force()), 2)
  223. def test_file_stream(self):
  224. m = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/pubring.gpg', 'rb'))
  225. nose.tools.assert_equal(len(m.force()), 1944)