p≡p for Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.7 KiB

  1. """Message unit tests."""
  2. import os
  3. from . import constants
  4. def test_msg_enc_dec_roundtrip(tmpdir, alice_sec_key_data, bob_pub_key_data):
  5. os.environ["HOME"] = str(tmpdir)
  6. import pEp
  7. alice = pEp.Identity(
  8. constants.ALICE_ADDRESS, constants.ALICE_NAME,
  9. constants.ALICE_NAME_ADDR, constants.ALICE_FP, 0, ''
  10. )
  11. pEp.import_key(bob_pub_key_data)
  12. bob = pEp.Identity(
  13. constants.BOB_ADDRESS, constants.BOB_NAME, '',
  14. constants.BOB_FP, 56, ''
  15. )
  16. msg = pEp.Message(constants.OUTGOING_MSG, alice)
  17. msg.to = [bob]
  18. msg.shortmsg = constants.SUBJECT
  19. msg.longmsg = constants.BODY
  20. assert msg.enc_format == 0
  21. # XXX: No way to check MIME so far.
  22. # Test that creating the `Message` with `outgoing_message` is the same.
  23. msg2 = pEp.outgoing_message(alice)
  24. msg2.to = [bob]
  25. msg2.shortmsg = constants.SUBJECT
  26. msg2.longmsg = constants.BODY
  27. assert str(msg2) == str(msg)
  28. # Encrypt Message
  29. enc_msg = msg.encrypt()
  30. assert enc_msg.enc_format == 3
  31. assert str(enc_msg.from_) == constants.ALICE_NAME_ADDR
  32. assert str(enc_msg.to[0]) == constants.BOB_NAME_ADDR
  33. assert enc_msg.shortmsg == "p≡p"
  34. assert enc_msg.longmsg == \
  35. "this message was encrypted with p≡p https://pEp-project.org"
  36. # Decrypt message.
  37. dec_msg, key_list, rating, r = enc_msg.decrypt()
  38. assert r == 0
  39. # pEp version 2.2 throws this error:
  40. # AttributeError: module 'pEp' has no attribute 'PEP_rating'
  41. # assert rating == pEp.PEP_rating.PEP_rating_reliable
  42. # It seems to have changed to the following.
  43. assert rating == pEp.native_pEp.rating.reliable
  44. # The first 2 keys are Alice's ones, the last is Bob's one.
  45. assert key_list[0] == key_list[1] == constants.ALICE_FP
  46. assert key_list[-1] == constants.BOB_FP
  47. assert dec_msg.shortmsg == constants.SUBJECT
  48. assert dec_msg.longmsg.replace("\r", "") == msg.longmsg
  49. dec_lines = str(dec_msg).replace("\r", "").split("\n")
  50. # pEp version 2.2 seems to have fixed some of the replaced characters.
  51. # and changed also:
  52. # Content-Type: doesn't pring `; charset="utf-8"` anymore.
  53. # Content-Transfer-Encoding: doesn't print `quoted-printable` anymore.
  54. # Content-Disposition: is not present anymore.
  55. # `!` is not replaced by `=21` anymore.
  56. expected_dec_lines = """From: Alice Lovelace <alice@openpgp.example>
  57. To: Bob Babagge <bob@openpgp.example>
  58. Subject: This is a subject
  59. X-pEp-Version: 2.1
  60. X-EncStatus: reliable
  61. X-KeyList:
  62. X,X,D1A66E1A23B182C9980F788CFBFCC82A015E7330
  63. MIME-Version: 1.0
  64. Content-Type: text/plain
  65. Content-Transfer-Encoding: 7bit
  66. Hi world!
  67. """.split("\n")
  68. assert dec_lines[:5] == expected_dec_lines[:5]
  69. assert dec_lines[7:] == expected_dec_lines[7:]