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.

173 lines
5.6 KiB

  1. """Message unit tests."""
  2. from . import constants
  3. def test_msg_enc_dec_roundtrip(create_alice_identity, create_bob_identity):
  4. import pEp
  5. alice = create_alice_identity
  6. bob = create_bob_identity
  7. msg = pEp.Message(constants.OUTGOING_MSG, alice)
  8. msg.to = [bob]
  9. msg.shortmsg = constants.SUBJECT
  10. msg.longmsg = constants.BODY
  11. assert msg.enc_format == 0
  12. # XXX: No way to check MIME so far.
  13. # Test that creating the `Message` with `outgoing_message` is the same.
  14. msg2 = pEp.outgoing_message(alice)
  15. msg2.to = [bob]
  16. msg2.shortmsg = constants.SUBJECT
  17. msg2.longmsg = constants.BODY
  18. assert str(msg2) == str(msg)
  19. # Encrypt Message
  20. enc_msg = msg.encrypt()
  21. assert enc_msg.enc_format == 3
  22. assert str(enc_msg.from_) == constants.ALICE_NAME_ADDR
  23. assert str(enc_msg.to[0]) == constants.BOB_NAME_ADDR
  24. assert enc_msg.shortmsg == "p≡p"
  25. assert enc_msg.longmsg == "this message was encrypted with p≡p https://pEp-project.org"
  26. # Decrypt message.
  27. dec_msg, key_list, rating, r = enc_msg.decrypt()
  28. assert r == 0
  29. # pEp version 2.2 throws this error:
  30. # AttributeError: module 'pEp' has no attribute 'PEP_rating'
  31. # assert rating == pEp.PEP_rating.PEP_rating_reliable
  32. # It seems to have changed to the following.
  33. assert rating == pEp._pEp.rating.reliable
  34. # The first 2 keys are Alice's ones, the last is Bob's one.
  35. assert key_list[0] == key_list[1] == constants.ALICE_FP
  36. assert key_list[-1] == constants.BOB_FP
  37. assert dec_msg.shortmsg == constants.SUBJECT
  38. assert dec_msg.longmsg.replace("\r", "") == msg.longmsg
  39. dec_lines = str(dec_msg).replace("\r", "").split("\n")
  40. # pEp version 2.2 seems to have fixed some of the replaced characters.
  41. # and changed also:
  42. # Content-Type: doesn't pring `; charset="utf-8"` anymore.
  43. # Content-Transfer-Encoding: doesn't print `quoted-printable` anymore.
  44. # Content-Disposition: is not present anymore.
  45. # `!` is not replaced by `=21` anymore.
  46. expected_dec_lines = \
  47. """From: Alice Lovelace <alice@openpgp.example>
  48. To: Bob Babagge <bob@openpgp.example>
  49. Subject: This is a subject
  50. X-pEp-Version: 2.1
  51. X-EncStatus: reliable
  52. X-KeyList:
  53. X,X,D1A66E1A23B182C9980F788CFBFCC82A015E7330
  54. MIME-Version: 1.0
  55. Content-Type: text/plain
  56. Content-Transfer-Encoding: 7bit
  57. Hi world!
  58. """.split("\n")
  59. assert dec_lines[:5] == expected_dec_lines[:5]
  60. assert dec_lines[7:] == expected_dec_lines[7:]
  61. def test_msg_len_changes(create_alice_identity, create_bob_identity):
  62. """Test that the original message is modified after encryption.
  63. Headers are added and therefore the modified unencrypted message length
  64. is different to the original.
  65. XXX: The original message should be left unchanged.
  66. There could be another method previous to `encrypt` that adds the
  67. extra headers and modify the subject returning a new message.
  68. """
  69. import pEp
  70. alice = create_alice_identity
  71. bob = create_bob_identity
  72. msg = pEp.outgoing_message(alice)
  73. msg.to = [bob]
  74. msg.shortmsg = constants.SUBJECT
  75. msg.longmsg = constants.BODY
  76. msg_len = len(str(msg))
  77. # Encrypt Message
  78. msg.encrypt()
  79. # After encryption, the original message is modified!!
  80. # It contains one more header and the alice's public key, if it's the first
  81. # msg to bob.
  82. # XXX: if/when this is fixed, change the following `!=` to `==`
  83. msg_after_encrypt_len = len(str(msg))
  84. assert msg.shortmsg != constants.SUBJECT
  85. assert msg.longmsg == constants.BODY
  86. assert msg_after_encrypt_len != msg_len
  87. def test_dec_msg_len(create_alice_identity, create_bob_identity):
  88. """
  89. Test that the decrypted message length is different from the original.
  90. Because it adds extra headers.
  91. """
  92. import pEp
  93. alice = create_alice_identity
  94. bob = create_bob_identity
  95. msg = pEp.outgoing_message(alice)
  96. msg.to = [bob]
  97. msg.shortmsg = constants.SUBJECT
  98. msg.longmsg = constants.BODY
  99. msg_len = len(str(msg))
  100. # Encrypt Message
  101. enc_msg = msg.encrypt()
  102. # Decrypt message.
  103. dec_msg, _key_list, _rating, _r = enc_msg.decrypt()
  104. dec_msg_len = len(str(dec_msg))
  105. assert dec_msg.longmsg.replace("\r", "") == constants.BODY # msg.longmsg
  106. expected_dec_msg = \
  107. """From: Alice Lovelace <alice@openpgp.example>\r
  108. To: Bob Babagge <bob@openpgp.example>\r
  109. Subject: This is a subject\r
  110. X-pEp-Version: 2.1\r
  111. X-EncStatus: reliable\r
  112. X-KeyList: \r
  113. EB85BB5FA33A75E15E944E63F231550C4F47E38E,EB85BB5FA33A75E15E944E63F231550C4F47E38E,D1A66E1A23B182C9980F788CFBFCC82A015E7330\r
  114. MIME-Version: 1.0\r
  115. Content-Type: text/plain\r
  116. Content-Transfer-Encoding: 7bit\r
  117. \r
  118. Hi world!\r
  119. """
  120. assert expected_dec_msg == str(dec_msg)
  121. # The decrypted message length should then be equal to the original message
  122. # minus the extra headers added.
  123. dec_lines = str(dec_msg).split("\n")
  124. extra_headers_lines = dec_lines[3:7]
  125. extra_headers = "\n".join(extra_headers_lines) + "\n"
  126. len_extra_headers = len(extra_headers)
  127. print("len_extra_headers", len_extra_headers)
  128. assert dec_msg_len - len_extra_headers == msg_len
  129. def test_null_char_rmed(create_alice_identity, create_bob_identity):
  130. """Test that null characters and anything after them is removed."""
  131. import pEp
  132. alice = create_alice_identity
  133. bob = create_bob_identity
  134. msg = pEp.outgoing_message(alice)
  135. msg.to = [bob]
  136. msg.shortmsg = constants.SUBJECT
  137. # Message with null chars, potentially for padding.
  138. body = "Hi Bob,\n" + "\0" * 255 + "\nBye,\nAlice."
  139. msg.longmsg = body
  140. # PYADAPT-91: The null characters and anything after them is removed.
  141. # If/when this is fixed, change the following assertion.
  142. assert msg.longmsg != body