Mercurial > libervia-backend
annotate tests/unit/test_plugin_xep_0420.py @ 3881:2e4a0f6050bd
component AP gateway: better verbose logging:
gather data to show according to verbosity and log it all at once with a small `<<<` or
`>>>` indicating the direction. With a verbosity of 3, headers are also shown.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 31 Aug 2022 17:07:03 +0200 |
parents | 00212260f659 |
children | 8289ac1b34f4 |
rev | line source |
---|---|
3877
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
2 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
3 # Tests for Libervia's Stanza Content Encryption plugin |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
4 # Copyright (C) 2022-2022 Tim Henkes (me@syndace.dev) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
5 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
9 # (at your option) any later version. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
10 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
15 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
18 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
19 # Type-check with `mypy --strict --disable-error-code no-untyped-call` |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
20 # Lint with `pylint` |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
21 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
22 from datetime import datetime, timezone |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
23 from typing import Callable, Iterator, Optional, cast |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
24 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
25 import pytest |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
26 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
27 from sat.plugins.plugin_xep_0334 import NS_HINTS |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
28 from sat.plugins.plugin_xep_0420 import ( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
29 NS_SCE, XEP_0420, AffixVerificationFailed, ProfileRequirementsNotMet, SCEAffixPolicy, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
30 SCECustomAffix, SCEProfile |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
31 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
32 from sat.tools.xml_tools import ElementParser |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
33 from twisted.words.xish import domish |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
34 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
35 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
36 __all__ = [ # pylint: disable=unused-variable |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
37 "test_unpack_matches_original", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
38 "test_affixes_included", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
39 "test_all_affixes_verified", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
40 "test_incomplete_affixes", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
41 "test_rpad_affix", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
42 "test_time_affix", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
43 "test_to_affix", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
44 "test_from_affix", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
45 "test_custom_affixes", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
46 "test_namespace_conversion", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
47 "test_non_encryptable_elements", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
48 "test_schema_validation" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
49 ] |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
50 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
51 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
52 string_to_domish = cast(Callable[[str], domish.Element], ElementParser()) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
53 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
54 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
55 class CustomAffixImpl(SCECustomAffix): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
56 """ |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
57 A simple custom affix implementation for testing purposes. Verifies the full JIDs of |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
58 both sender and recipient. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
59 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
60 @warning: This is just an example, an affix element like this might not make sense due |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
61 to potentially allowed modifications of recipient/sender full JIDs (I don't know |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
62 enough about XMPP routing to know whether full JIDs are always left untouched by |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
63 the server). |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
64 """ |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
65 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
66 @property |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
67 def element_name(self) -> str: |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
68 return "full-jids" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
69 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
70 @property |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
71 def element_schema(self) -> str: |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
72 return """<xs:element name="full-jids"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
73 <xs:complexType> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
74 <xs:attribute name="recipient" type="xs:string"/> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
75 <xs:attribute name="sender" type="xs:string"/> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
76 </xs:complexType> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
77 </xs:element>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
78 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
79 def create(self, stanza: domish.Element) -> domish.Element: |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
80 recipient = cast(Optional[str], stanza.getAttribute("to", None)) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
81 sender = cast(Optional[str], stanza.getAttribute("from", None)) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
82 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
83 if recipient is None or sender is None: |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
84 raise ValueError( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
85 "Stanza doesn't have ``to`` and ``from`` attributes required by the" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
86 " full-jids custom affix." |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
87 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
88 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
89 element = domish.Element((NS_SCE, "full-jids")) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
90 element["recipient"] = recipient |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
91 element["sender"] = sender |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
92 return element |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
93 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
94 def verify(self, stanza: domish.Element, element: domish.Element) -> None: |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
95 recipient_target = element["recipient"] |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
96 recipient_actual = stanza.getAttribute("to") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
97 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
98 sender_target = element["sender"] |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
99 sender_actual = stanza.getAttribute("from") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
100 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
101 if recipient_actual != recipient_target or sender_actual != sender_target: |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
102 raise AffixVerificationFailed( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
103 f"Full JIDs differ. Recipient: actual={recipient_actual} vs." |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
104 f" target={recipient_target}; Sender: actual={sender_actual} vs." |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
105 f" target={sender_target}" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
106 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
107 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
108 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
109 def test_unpack_matches_original() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
110 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
111 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
112 SCEAffixPolicy.OPTIONAL, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
113 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
114 SCEAffixPolicy.OPTIONAL, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
115 custom_policies={ CustomAffixImpl(): SCEAffixPolicy.NOT_NEEDED } |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
116 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
117 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
118 stanza_string = ( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
119 '<message from="foo@example.com" to="bar@example.com"><body>Test with both a body' |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
120 ' and some other custom element.</body><custom xmlns="urn:xmpp:example:0"' |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
121 ' test="matches-original">some more content</custom></message>' |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
122 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
123 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
124 stanza = string_to_domish(stanza_string) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
125 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
126 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
127 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
128 # The stanza should not have child elements any more |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
129 assert len(list(stanza.elements())) == 0 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
130 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
131 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
132 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
133 # domish.Element doesn't override __eq__, thus we compare the .toXml() strings here in |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
134 # the hope that serialization for an example as small as this is unique enough to be |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
135 # compared that way. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
136 assert stanza.toXml() == string_to_domish(stanza_string).toXml() |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
137 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
138 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
139 def test_affixes_included() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
140 custom_affix = CustomAffixImpl() |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
141 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
142 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
143 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
144 SCEAffixPolicy.OPTIONAL, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
145 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
146 SCEAffixPolicy.OPTIONAL, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
147 custom_policies={ custom_affix: SCEAffixPolicy.OPTIONAL } |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
148 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
149 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
150 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
151 <body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
152 Make sure that both the REQUIRED and the OPTIONAL affixes are included. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
153 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
154 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
155 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
156 affix_values = XEP_0420.unpack_stanza( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
157 profile, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
158 stanza, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
159 XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
160 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
161 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
162 assert affix_values.rpad is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
163 assert affix_values.timestamp is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
164 assert affix_values.recipient is None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
165 assert affix_values.sender is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
166 assert custom_affix in affix_values.custom |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
167 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
168 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
169 def test_all_affixes_verified() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
170 packing_profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
171 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
172 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
173 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
174 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
175 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
176 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
177 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
178 unpacking_profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
179 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
180 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
181 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
182 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
183 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
184 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
185 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
186 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
187 <body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
188 When unpacking, all affixes are loaded, even those marked as NOT_NEEDED. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
189 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
190 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
191 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
192 envelope_serialized = XEP_0420.pack_stanza(packing_profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
193 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
194 affix_values = XEP_0420.unpack_stanza(unpacking_profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
195 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
196 assert affix_values.rpad is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
197 assert affix_values.timestamp is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
198 assert affix_values.recipient is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
199 assert affix_values.sender is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
200 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
201 # When unpacking, all affixes are verified, even if they are NOT_NEEDED by the profile |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
202 stanza = string_to_domish( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
203 """<message from="fooo@example.com" to="baz@example.com"></message>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
204 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
205 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
206 with pytest.raises(AffixVerificationFailed): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
207 XEP_0420.unpack_stanza(unpacking_profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
208 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
209 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
210 def test_incomplete_affixes() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
211 packing_profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
212 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
213 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
214 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
215 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
216 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
217 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
218 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
219 unpacking_profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
220 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
221 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
222 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
223 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
224 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
225 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
226 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
227 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
228 <body>Check that all affixes REQUIRED by the profile are present.</body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
229 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
230 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
231 with pytest.raises(ProfileRequirementsNotMet): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
232 XEP_0420.unpack_stanza( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
233 unpacking_profile, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
234 stanza, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
235 XEP_0420.pack_stanza(packing_profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
236 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
237 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
238 # Do the same but with a custom affix missing |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
239 custom_affix = CustomAffixImpl() |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
240 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
241 packing_profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
242 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
243 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
244 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
245 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
246 custom_policies={ custom_affix: SCEAffixPolicy.NOT_NEEDED } |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
247 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
248 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
249 unpacking_profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
250 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
251 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
252 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
253 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
254 custom_policies={ custom_affix: SCEAffixPolicy.REQUIRED } |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
255 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
256 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
257 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
258 <body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
259 Check that all affixes REQUIRED by the profile are present, including custom |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
260 affixes. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
261 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
262 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
263 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
264 with pytest.raises(ProfileRequirementsNotMet): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
265 XEP_0420.unpack_stanza( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
266 unpacking_profile, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
267 stanza, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
268 XEP_0420.pack_stanza(packing_profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
269 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
270 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
271 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
272 def test_rpad_affix() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
273 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
274 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
275 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
276 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
277 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
278 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
279 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
280 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
281 for _ in range(100): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
282 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
283 <body>OK</body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
284 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
285 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
286 affix_values = XEP_0420.unpack_stanza( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
287 profile, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
288 stanza, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
289 XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
290 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
291 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
292 # Test that the rpad exists and that the content elements are always padded to at |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
293 # least 53 characters |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
294 assert affix_values.rpad is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
295 assert len(affix_values.rpad) >= 53 - len("<body xmlns='jabber:client'>OK</body>") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
296 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
297 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
298 def test_time_affix() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
299 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
300 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
301 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
302 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
303 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
304 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
305 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
306 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
307 stanza = string_to_domish( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
308 """<message from="foo@example.com" to="bar@example.com"></message>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
309 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
310 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
311 envelope_serialized = f"""<envelope xmlns="{NS_SCE}"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
312 <content> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
313 <body xmlns="jabber:client"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
314 The time affix is only parsed and not otherwise verified. Not much to test |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
315 here. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
316 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
317 </content> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
318 <time stamp="1969-07-21T02:56:15Z"/> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
319 </envelope>""".encode("utf-8") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
320 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
321 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
322 assert affix_values.timestamp == datetime(1969, 7, 21, 2, 56, 15, tzinfo=timezone.utc) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
323 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
324 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
325 def test_to_affix() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
326 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
327 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
328 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
329 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
330 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
331 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
332 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
333 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
334 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
335 <body>Check that the ``to`` affix is correctly added.</body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
336 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
337 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
338 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
339 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
340 assert affix_values.recipient is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
341 assert affix_values.recipient.userhost() == "bar@example.com" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
342 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
343 # Check that a mismatch in recipient bare JID causes an exception to be raised |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
344 stanza = string_to_domish( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
345 """<message from="foo@example.com" to="baz@example.com"></message>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
346 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
347 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
348 with pytest.raises(AffixVerificationFailed): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
349 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
350 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
351 # Check that only the bare JID matters |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
352 stanza = string_to_domish( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
353 """<message from="foo@example.com" to="bar@example.com/device"></message>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
354 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
355 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
356 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
357 assert affix_values.recipient is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
358 assert affix_values.recipient.userhost() == "bar@example.com" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
359 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
360 stanza = string_to_domish("""<message from="foo@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
361 <body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
362 Check that a missing "to" attribute on the stanza fails stanza packing. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
363 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
364 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
365 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
366 with pytest.raises(ValueError): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
367 XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
368 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
369 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
370 def test_from_affix() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
371 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
372 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
373 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
374 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
375 SCEAffixPolicy.REQUIRED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
376 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
377 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
378 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
379 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
380 <body>Check that the ``from`` affix is correctly added.</body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
381 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
382 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
383 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
384 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
385 assert affix_values.sender is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
386 assert affix_values.sender.userhost() == "foo@example.com" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
387 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
388 # Check that a mismatch in sender bare JID causes an exception to be raised |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
389 stanza = string_to_domish( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
390 """<message from="fooo@example.com" to="bar@example.com"></message>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
391 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
392 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
393 with pytest.raises(AffixVerificationFailed): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
394 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
395 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
396 # Check that only the bare JID matters |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
397 stanza = string_to_domish( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
398 """<message from="foo@example.com/device" to="bar@example.com"></message>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
399 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
400 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
401 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
402 assert affix_values.sender is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
403 assert affix_values.sender.userhost() == "foo@example.com" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
404 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
405 stanza = string_to_domish("""<message to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
406 <body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
407 Check that a missing "from" attribute on the stanza fails stanza packing. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
408 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
409 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
410 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
411 with pytest.raises(ValueError): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
412 XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
413 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
414 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
415 def test_custom_affixes() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
416 custom_affix = CustomAffixImpl() |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
417 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
418 packing_profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
419 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
420 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
421 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
422 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
423 custom_policies={ custom_affix: SCEAffixPolicy.REQUIRED } |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
424 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
425 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
426 unpacking_profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
427 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
428 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
429 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
430 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
431 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
432 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
433 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
434 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
435 <body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
436 If a custom affix is included in the envelope, but not excpected by the |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
437 recipient, the schema validation should fail. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
438 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
439 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
440 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
441 with pytest.raises(ValueError): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
442 XEP_0420.unpack_stanza( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
443 unpacking_profile, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
444 stanza, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
445 XEP_0420.pack_stanza(packing_profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
446 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
447 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
448 profile = packing_profile |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
449 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
450 stanza = string_to_domish("""<message from="foo@example.com/device0" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
451 to="bar@example.com/Libervia.123"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
452 <body>The affix element should be returned as part of the affix values.</body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
453 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
454 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
455 affix_values = XEP_0420.unpack_stanza( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
456 profile, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
457 stanza, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
458 XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
459 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
460 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
461 assert custom_affix in affix_values.custom |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
462 assert affix_values.custom[custom_affix].getAttribute("recipient") == \ |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
463 "bar@example.com/Libervia.123" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
464 assert affix_values.custom[custom_affix].getAttribute("sender") == \ |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
465 "foo@example.com/device0" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
466 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
467 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
468 def test_namespace_conversion() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
469 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
470 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
471 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
472 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
473 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
474 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
475 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
476 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
477 stanza = domish.Element((None, "message")) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
478 stanza["from"] = "foo@example.com" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
479 stanza["to"] = "bar@example.com" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
480 stanza.addElement( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
481 "body", |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
482 content=( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
483 "This body element has namespace ``None``, which has to be replaced with" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
484 " jabber:client." |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
485 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
486 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
487 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
488 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
489 envelope = string_to_domish(envelope_serialized.decode("utf-8")) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
490 content = next(cast(Iterator[domish.Element], envelope.elements(NS_SCE, "content"))) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
491 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
492 # The body should have been assigned ``jabber:client`` as its namespace |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
493 assert next( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
494 cast(Iterator[domish.Element], content.elements("jabber:client", "body")), |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
495 None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
496 ) is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
497 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
498 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
499 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
500 # The body should still have ``jabber:client`` after unpacking |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
501 assert next( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
502 cast(Iterator[domish.Element], stanza.elements("jabber:client", "body")), |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
503 None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
504 ) is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
505 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
506 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
507 def test_non_encryptable_elements() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
508 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
509 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
510 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
511 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
512 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
513 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
514 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
515 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
516 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
517 <body>This stanza includes a store hint which must not be encrypted.</body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
518 <store xmlns="urn:xmpp:hints"/> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
519 </message>""") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
520 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
521 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
522 envelope = string_to_domish(envelope_serialized.decode("utf-8")) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
523 content = next(cast(Iterator[domish.Element], envelope.elements(NS_SCE, "content"))) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
524 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
525 # The store hint must not have been moved to the content element |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
526 assert next( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
527 cast(Iterator[domish.Element], stanza.elements(NS_HINTS, "store")), |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
528 None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
529 ) is not None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
530 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
531 assert next( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
532 cast(Iterator[domish.Element], content.elements(NS_HINTS, "store")), |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
533 None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
534 ) is None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
535 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
536 stanza = string_to_domish( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
537 """<message from="foo@example.com" to="bar@example.com"></message>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
538 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
539 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
540 envelope_serialized = f"""<envelope xmlns="{NS_SCE}"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
541 <content> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
542 <body xmlns="jabber:client"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
543 The store hint must not be moved to the stanza. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
544 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
545 <store xmlns="urn:xmpp:hints"/> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
546 </content> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
547 </envelope>""".encode("utf-8") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
548 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
549 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
550 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
551 assert next( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
552 cast(Iterator[domish.Element], stanza.elements(NS_HINTS, "store")), |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
553 None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
554 ) is None |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
555 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
556 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
557 def test_schema_validation() -> None: # pylint: disable=missing-function-docstring |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
558 profile = SCEProfile( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
559 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
560 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
561 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
562 SCEAffixPolicy.NOT_NEEDED, |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
563 custom_policies={} |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
564 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
565 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
566 stanza = string_to_domish( |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
567 """<message from="foo@example.com" to="bar@example.com"></message>""" |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
568 ) |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
569 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
570 envelope_serialized = f"""<envelope xmlns="{NS_SCE}"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
571 <content> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
572 <body xmlns="jabber:client"> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
573 An unknwon affix should cause a schema validation error. |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
574 </body> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
575 <store xmlns="urn:xmpp:hints"/> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
576 </content> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
577 <unknown-affix unknown-attr="unknown"/> |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
578 </envelope>""".encode("utf-8") |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
579 |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
580 with pytest.raises(ValueError): |
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
diff
changeset
|
581 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |