1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
import unittest
from nbxmpp.modules.security_labels import SecurityLabel
from nbxmpp.protocol import JID
from nbxmpp.simplexml import Node
from gajim.common import app
from gajim.common.settings import Settings
from gajim.common.storage.archive.const import ChatDirection
from gajim.common.storage.archive.const import MessageState
from gajim.common.storage.archive.const import MessageType
from gajim.common.storage.archive.storage import MessageArchiveStorage
from gajim.common.storage.archive.structs import DbInsertMessageRowData
from gajim.common.storage.archive.structs import DbUpsertSecurityLabelRowData
class SecurityLabelsTest(unittest.TestCase):
def setUp(self) -> None:
self._archive = MessageArchiveStorage(in_memory=True)
self._archive.init()
xml1 = '''
<securitylabel xmlns='urn:xmpp:sec-label:0'>
<displaymarking fgcolor='black' bgcolor='red'>SECRET</displaymarking>
<label>
<icismlabel xmlns='http://example.gov/IC-ISM/0' classification='S' ownerProducer='USA'/>
</label>
</securitylabel>
''' # noqa: E501
xml2 = '''
<securitylabel xmlns='urn:xmpp:sec-label:0'>
<displaymarking fgcolor='white' bgcolor='blue'>NOT SECRET</displaymarking>
<label>
<icismlabel xmlns='http://example.gov/IC-ISM/0' classification='S' ownerProducer='USA'/>
</label>
</securitylabel>
''' # noqa: E501
self._account = 'testacc1'
self._remote_jid = JID.from_string('remote@jid.org')
self._sec_label1 = SecurityLabel.from_node(Node(node=xml1)) # pyright: ignore # noqa: E501
self._sec_label2 = SecurityLabel.from_node(Node(node=xml2)) # pyright: ignore # noqa: E501
self._init_settings()
def _init_settings(self) -> None:
app.settings = Settings(in_memory=True)
app.settings.init()
app.settings.add_account('testacc1')
app.settings.set_account_setting('testacc1', 'name', 'user')
app.settings.set_account_setting('testacc1', 'hostname', 'domain.org')
def test_security_labels_join(self):
displaymarking = self._sec_label1.displaymarking
assert displaymarking is not None
sec_data = DbUpsertSecurityLabelRowData(
account=self._account,
remote_jid=self._remote_jid,
timestamp=0,
label_hash=self._sec_label1.get_label_hash(),
displaymarking=displaymarking.name,
fgcolor=displaymarking.fgcolor,
bgcolor=displaymarking.bgcolor,
)
fk_securitylabel_ek = self._archive.upsert_row(sec_data)
message_data = DbInsertMessageRowData(
account=self._account,
remote_jid=self._remote_jid,
m_type=MessageType.CHAT,
direction=ChatDirection.INCOMING,
timestamp=0,
state=MessageState.ACKNOWLEDGED,
resource='res',
message='Some Message',
message_id='1',
stanza_id='1a',
stable_id=True,
fk_occupant_ek=None,
user_delay_ts=None,
fk_securitylabel_ek=fk_securitylabel_ek,
fk_encryption_ek=None,
)
message_ek = self._archive.insert_row(message_data)
conversation_row = self._archive.get_message_with_entitykey(message_ek)
self.assertIsNotNone(conversation_row.securitylabel)
assert conversation_row.securitylabel is not None
self.assertEqual(
conversation_row.securitylabel.displaymarking, 'SECRET')
self.assertEqual(conversation_row.securitylabel.fgcolor, 'black')
self.assertEqual(conversation_row.securitylabel.bgcolor, 'red')
def test_security_labels_update(self):
displaymarking1 = self._sec_label1.displaymarking
assert displaymarking1 is not None
sec_data1 = DbUpsertSecurityLabelRowData(
account=self._account,
remote_jid=self._remote_jid,
timestamp=0,
label_hash=self._sec_label1.get_label_hash(),
displaymarking=displaymarking1.name,
fgcolor=displaymarking1.fgcolor,
bgcolor=displaymarking1.bgcolor,
)
displaymarking2 = self._sec_label2.displaymarking
assert displaymarking2 is not None
sec_data2 = DbUpsertSecurityLabelRowData(
account=self._account,
remote_jid=self._remote_jid,
timestamp=1,
label_hash=self._sec_label2.get_label_hash(),
displaymarking=displaymarking2.name,
fgcolor=displaymarking2.fgcolor,
bgcolor=displaymarking2.bgcolor,
)
fk_securitylabel_ek1 = self._archive.upsert_row(sec_data1)
fk_securitylabel_ek2 = self._archive.upsert_row(sec_data2)
self.assertEqual(fk_securitylabel_ek1, fk_securitylabel_ek2)
row = self._archive.get_row_with_entitykey(
'securitylabel', fk_securitylabel_ek2)
self.assertEqual(row.displaymarking, 'NOT SECRET')
self.assertEqual(row.fgcolor, 'white')
self.assertEqual(row.bgcolor, 'blue')
self.assertEqual(row.timestamp, 1)
if __name__ == '__main__':
unittest.main()
|