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.

169 lines
5.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # This file is under GNU Affero General Public License 3.0
  3. # see LICENSE.txt
  4. """Message unit tests."""
  5. from . import constants
  6. def test_msg_enc_dec_roundtrip(pEp, model, import_ident_alice_as_own_ident, import_ident_bob):
  7. alice = import_ident_alice_as_own_ident
  8. bob = import_ident_bob
  9. msg = pEp.Message(constants.OUTGOING_MSG, alice)
  10. msg.to = [bob]
  11. msg.shortmsg = constants.SUBJECT
  12. msg.longmsg = constants.BODY
  13. assert msg.enc_format == 0
  14. # XXX: No way to check MIME so far.
  15. # Test that creating the `Message` with `outgoing_message` is the same.
  16. msg2 = pEp.outgoing_message(alice)
  17. msg2.to = [bob]
  18. msg2.shortmsg = constants.SUBJECT
  19. msg2.longmsg = constants.BODY
  20. assert str(msg2) == str(msg)
  21. # Encrypt Message
  22. enc_msg = msg.encrypt()
  23. assert enc_msg.enc_format == 3
  24. assert str(enc_msg.from_) == str(model.alice)
  25. assert str(enc_msg.to[0]) == str(model.bob)
  26. assert enc_msg.shortmsg == "p≡p"
  27. assert enc_msg.longmsg == "this message was encrypted with p≡p https://pEp-project.org"
  28. # Decrypt message.
  29. dec_msg, key_list, rating, r = enc_msg.decrypt()
  30. assert r == 0
  31. # pEp version 2.2 throws this error:
  32. # AttributeError: module 'pEp' has no attribute 'PEP_rating'
  33. # assert rating == pEp.PEP_rating.PEP_rating_reliable
  34. # It seems to have changed to the following.
  35. assert rating == pEp._pEp.rating.reliable
  36. # The first 2 keys are Alice's ones, the last is Bob's one.
  37. assert key_list[0] == key_list[1] == model.alice.fpr
  38. assert key_list[-1] == model.bob.fpr
  39. assert dec_msg.shortmsg == constants.SUBJECT
  40. assert dec_msg.longmsg.replace("\r", "") == msg.longmsg
  41. dec_lines = str(dec_msg).replace("\r", "").split("\n")
  42. # pEp version 2.2 seems to have fixed some of the replaced characters.
  43. # and changed also:
  44. # Content-Type: doesn't pring `; charset="utf-8"` anymore.
  45. # Content-Transfer-Encoding: doesn't print `quoted-printable` anymore.
  46. # Content-Disposition: is not present anymore.
  47. # `!` is not replaced by `=21` anymore.
  48. expected_dec_lines = \
  49. """From: alice <alice@peptest.org>
  50. To: bob <bob@peptest.org>
  51. Subject: This is a subject
  52. X-pEp-Version: 2.1
  53. X-EncStatus: reliable
  54. X-KeyList:
  55. X,X,6A9835699EF1215F1558A496D9C1D4B0984094E5
  56. MIME-Version: 1.0
  57. Content-Type: text/plain
  58. Content-Transfer-Encoding: 7bit
  59. Hi world!
  60. """.split("\n")
  61. assert dec_lines[:5] == expected_dec_lines[:5]
  62. assert dec_lines[7:] == expected_dec_lines[7:]
  63. def test_msg_len_changes(pEp, import_ident_alice_as_own_ident, import_ident_bob):
  64. """Test that the original message is modified after encryption.
  65. Headers are added and therefore the modified unencrypted message length
  66. is different to the original.
  67. XXX: The original message should be left unchanged.
  68. There could be another method previous to `encrypt` that adds the
  69. extra headers and modify the subject returning a new message.
  70. """
  71. alice = import_ident_alice_as_own_ident
  72. bob = import_ident_bob
  73. msg = pEp.outgoing_message(alice)
  74. msg.to = [bob]
  75. msg.shortmsg = constants.SUBJECT
  76. msg.longmsg = constants.BODY
  77. msg_len = len(str(msg))
  78. # Encrypt Message
  79. msg.encrypt()
  80. # After encryption, the original message is modified!!
  81. # It contains one more header and the alice's public key, if it's the first
  82. # msg to bob.
  83. # XXX: if/when this is fixed, change the following `!=` to `==`
  84. msg_after_encrypt_len = len(str(msg))
  85. assert msg.shortmsg != constants.SUBJECT
  86. assert msg.longmsg == constants.BODY
  87. assert msg_after_encrypt_len != msg_len
  88. def test_dec_msg_len(pEp, import_ident_alice_as_own_ident, import_ident_bob):
  89. """
  90. Test that the decrypted message length is different from the original.
  91. Because it adds extra headers.
  92. """
  93. alice = import_ident_alice_as_own_ident
  94. bob = import_ident_bob
  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 <alice@peptest.org>\r
  108. To: bob <bob@peptest.org>\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. 2D35731B9C754564CBAD15D2D18F7444594F2283,2D35731B9C754564CBAD15D2D18F7444594F2283,6A9835699EF1215F1558A496D9C1D4B0984094E5\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. #@pytest.mark.skip(reason="PYADAPT-91")
  130. def test_null_char_rmed(pEp, import_ident_alice_as_own_ident, import_ident_bob):
  131. """Test that null characters and anything after them are not removed."""
  132. alice = import_ident_alice_as_own_ident
  133. bob = import_ident_bob
  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