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.

166 lines
5.7 KiB

2 years ago
  1. # -*- coding: utf-8 -*-
  2. """Message unit tests."""
  3. from . import constants
  4. def test_msg_enc_dec_roundtrip(pEp, import_ident_alice_as_own_ident, import_ident_bob):
  5. alice = import_ident_alice_as_own_ident
  6. bob = import_ident_bob
  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_FPR
  36. assert key_list[-1] == constants.BOB_FPR
  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(pEp, import_ident_alice_as_own_ident, import_ident_bob):
  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. alice = import_ident_alice_as_own_ident
  70. bob = import_ident_bob
  71. msg = pEp.outgoing_message(alice)
  72. msg.to = [bob]
  73. msg.shortmsg = constants.SUBJECT
  74. msg.longmsg = constants.BODY
  75. msg_len = len(str(msg))
  76. # Encrypt Message
  77. msg.encrypt()
  78. # After encryption, the original message is modified!!
  79. # It contains one more header and the alice's public key, if it's the first
  80. # msg to bob.
  81. # XXX: if/when this is fixed, change the following `!=` to `==`
  82. msg_after_encrypt_len = len(str(msg))
  83. assert msg.shortmsg != constants.SUBJECT
  84. assert msg.longmsg == constants.BODY
  85. assert msg_after_encrypt_len != msg_len
  86. def test_dec_msg_len(pEp, import_ident_alice_as_own_ident, import_ident_bob):
  87. """
  88. Test that the decrypted message length is different from the original.
  89. Because it adds extra headers.
  90. """
  91. alice = import_ident_alice_as_own_ident
  92. bob = import_ident_bob
  93. msg = pEp.outgoing_message(alice)
  94. msg.to = [bob]
  95. msg.shortmsg = constants.SUBJECT
  96. msg.longmsg = constants.BODY
  97. msg_len = len(str(msg))
  98. # Encrypt Message
  99. enc_msg = msg.encrypt()
  100. # Decrypt message.
  101. dec_msg, _key_list, _rating, _r = enc_msg.decrypt()
  102. dec_msg_len = len(str(dec_msg))
  103. assert dec_msg.longmsg.replace("\r", "") == constants.BODY # msg.longmsg
  104. expected_dec_msg = \
  105. """From: Alice Lovelace <alice@openpgp.example>\r
  106. To: Bob Babagge <bob@openpgp.example>\r
  107. Subject: This is a subject\r
  108. X-pEp-Version: 2.1\r
  109. X-EncStatus: reliable\r
  110. X-KeyList: \r
  111. EB85BB5FA33A75E15E944E63F231550C4F47E38E,EB85BB5FA33A75E15E944E63F231550C4F47E38E,D1A66E1A23B182C9980F788CFBFCC82A015E7330\r
  112. MIME-Version: 1.0\r
  113. Content-Type: text/plain\r
  114. Content-Transfer-Encoding: 7bit\r
  115. \r
  116. Hi world!\r
  117. """
  118. assert expected_dec_msg == str(dec_msg)
  119. # The decrypted message length should then be equal to the original message
  120. # minus the extra headers added.
  121. dec_lines = str(dec_msg).split("\n")
  122. extra_headers_lines = dec_lines[3:7]
  123. extra_headers = "\n".join(extra_headers_lines) + "\n"
  124. len_extra_headers = len(extra_headers)
  125. print("len_extra_headers", len_extra_headers)
  126. assert dec_msg_len - len_extra_headers == msg_len
  127. def test_null_char_rmed(pEp, import_ident_alice_as_own_ident, import_ident_bob):
  128. """Test that null characters and anything after them is removed."""
  129. alice = import_ident_alice_as_own_ident
  130. bob = import_ident_bob
  131. msg = pEp.outgoing_message(alice)
  132. msg.to = [bob]
  133. msg.shortmsg = constants.SUBJECT
  134. # Message with null chars, potentially for padding.
  135. body = "Hi Bob,\n" + "\0" * 255 + "\nBye,\nAlice."
  136. msg.longmsg = body
  137. # PYADAPT-91: The null characters and anything after them is removed.
  138. # If/when this is fixed, change the following assertion.
  139. assert msg.longmsg != body