1
0

test_e2e_room_keys.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. # Copyright 2017 New Vector Ltd
  4. # Copyright 2019 Matrix.org Foundation C.I.C.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import copy
  18. import mock
  19. from twisted.internet import defer
  20. import synapse.api.errors
  21. import synapse.handlers.e2e_room_keys
  22. import synapse.storage
  23. from synapse.api import errors
  24. from tests import unittest, utils
  25. # sample room_key data for use in the tests
  26. room_keys = {
  27. "rooms": {
  28. "!abc:matrix.org": {
  29. "sessions": {
  30. "c0ff33": {
  31. "first_message_index": 1,
  32. "forwarded_count": 1,
  33. "is_verified": False,
  34. "session_data": "SSBBTSBBIEZJU0gK",
  35. }
  36. }
  37. }
  38. }
  39. }
  40. class E2eRoomKeysHandlerTestCase(unittest.TestCase):
  41. def __init__(self, *args, **kwargs):
  42. super(E2eRoomKeysHandlerTestCase, self).__init__(*args, **kwargs)
  43. self.hs = None # type: synapse.server.HomeServer
  44. self.handler = None # type: synapse.handlers.e2e_keys.E2eRoomKeysHandler
  45. @defer.inlineCallbacks
  46. def setUp(self):
  47. self.hs = yield utils.setup_test_homeserver(
  48. self.addCleanup, handlers=None, replication_layer=mock.Mock()
  49. )
  50. self.handler = synapse.handlers.e2e_room_keys.E2eRoomKeysHandler(self.hs)
  51. self.local_user = "@boris:" + self.hs.hostname
  52. @defer.inlineCallbacks
  53. def test_get_missing_current_version_info(self):
  54. """Check that we get a 404 if we ask for info about the current version
  55. if there is no version.
  56. """
  57. res = None
  58. try:
  59. yield self.handler.get_version_info(self.local_user)
  60. except errors.SynapseError as e:
  61. res = e.code
  62. self.assertEqual(res, 404)
  63. @defer.inlineCallbacks
  64. def test_get_missing_version_info(self):
  65. """Check that we get a 404 if we ask for info about a specific version
  66. if it doesn't exist.
  67. """
  68. res = None
  69. try:
  70. yield self.handler.get_version_info(self.local_user, "bogus_version")
  71. except errors.SynapseError as e:
  72. res = e.code
  73. self.assertEqual(res, 404)
  74. @defer.inlineCallbacks
  75. def test_create_version(self):
  76. """Check that we can create and then retrieve versions.
  77. """
  78. res = yield self.handler.create_version(
  79. self.local_user,
  80. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  81. )
  82. self.assertEqual(res, "1")
  83. # check we can retrieve it as the current version
  84. res = yield self.handler.get_version_info(self.local_user)
  85. version_etag = res["etag"]
  86. del res["etag"]
  87. self.assertDictEqual(
  88. res,
  89. {
  90. "version": "1",
  91. "algorithm": "m.megolm_backup.v1",
  92. "auth_data": "first_version_auth_data",
  93. "count": 0,
  94. },
  95. )
  96. # check we can retrieve it as a specific version
  97. res = yield self.handler.get_version_info(self.local_user, "1")
  98. self.assertEqual(res["etag"], version_etag)
  99. del res["etag"]
  100. self.assertDictEqual(
  101. res,
  102. {
  103. "version": "1",
  104. "algorithm": "m.megolm_backup.v1",
  105. "auth_data": "first_version_auth_data",
  106. "count": 0,
  107. },
  108. )
  109. # upload a new one...
  110. res = yield self.handler.create_version(
  111. self.local_user,
  112. {
  113. "algorithm": "m.megolm_backup.v1",
  114. "auth_data": "second_version_auth_data",
  115. },
  116. )
  117. self.assertEqual(res, "2")
  118. # check we can retrieve it as the current version
  119. res = yield self.handler.get_version_info(self.local_user)
  120. del res["etag"]
  121. self.assertDictEqual(
  122. res,
  123. {
  124. "version": "2",
  125. "algorithm": "m.megolm_backup.v1",
  126. "auth_data": "second_version_auth_data",
  127. "count": 0,
  128. },
  129. )
  130. @defer.inlineCallbacks
  131. def test_update_version(self):
  132. """Check that we can update versions.
  133. """
  134. version = yield self.handler.create_version(
  135. self.local_user,
  136. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  137. )
  138. self.assertEqual(version, "1")
  139. res = yield self.handler.update_version(
  140. self.local_user,
  141. version,
  142. {
  143. "algorithm": "m.megolm_backup.v1",
  144. "auth_data": "revised_first_version_auth_data",
  145. "version": version,
  146. },
  147. )
  148. self.assertDictEqual(res, {})
  149. # check we can retrieve it as the current version
  150. res = yield self.handler.get_version_info(self.local_user)
  151. del res["etag"]
  152. self.assertDictEqual(
  153. res,
  154. {
  155. "algorithm": "m.megolm_backup.v1",
  156. "auth_data": "revised_first_version_auth_data",
  157. "version": version,
  158. "count": 0,
  159. },
  160. )
  161. @defer.inlineCallbacks
  162. def test_update_missing_version(self):
  163. """Check that we get a 404 on updating nonexistent versions
  164. """
  165. res = None
  166. try:
  167. yield self.handler.update_version(
  168. self.local_user,
  169. "1",
  170. {
  171. "algorithm": "m.megolm_backup.v1",
  172. "auth_data": "revised_first_version_auth_data",
  173. "version": "1",
  174. },
  175. )
  176. except errors.SynapseError as e:
  177. res = e.code
  178. self.assertEqual(res, 404)
  179. @defer.inlineCallbacks
  180. def test_update_omitted_version(self):
  181. """Check that the update succeeds if the version is missing from the body
  182. """
  183. version = yield self.handler.create_version(
  184. self.local_user,
  185. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  186. )
  187. self.assertEqual(version, "1")
  188. yield self.handler.update_version(
  189. self.local_user,
  190. version,
  191. {
  192. "algorithm": "m.megolm_backup.v1",
  193. "auth_data": "revised_first_version_auth_data",
  194. },
  195. )
  196. # check we can retrieve it as the current version
  197. res = yield self.handler.get_version_info(self.local_user)
  198. del res["etag"] # etag is opaque, so don't test its contents
  199. self.assertDictEqual(
  200. res,
  201. {
  202. "algorithm": "m.megolm_backup.v1",
  203. "auth_data": "revised_first_version_auth_data",
  204. "version": version,
  205. "count": 0,
  206. },
  207. )
  208. @defer.inlineCallbacks
  209. def test_update_bad_version(self):
  210. """Check that we get a 400 if the version in the body doesn't match
  211. """
  212. version = yield self.handler.create_version(
  213. self.local_user,
  214. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  215. )
  216. self.assertEqual(version, "1")
  217. res = None
  218. try:
  219. yield self.handler.update_version(
  220. self.local_user,
  221. version,
  222. {
  223. "algorithm": "m.megolm_backup.v1",
  224. "auth_data": "revised_first_version_auth_data",
  225. "version": "incorrect",
  226. },
  227. )
  228. except errors.SynapseError as e:
  229. res = e.code
  230. self.assertEqual(res, 400)
  231. @defer.inlineCallbacks
  232. def test_delete_missing_version(self):
  233. """Check that we get a 404 on deleting nonexistent versions
  234. """
  235. res = None
  236. try:
  237. yield self.handler.delete_version(self.local_user, "1")
  238. except errors.SynapseError as e:
  239. res = e.code
  240. self.assertEqual(res, 404)
  241. @defer.inlineCallbacks
  242. def test_delete_missing_current_version(self):
  243. """Check that we get a 404 on deleting nonexistent current version
  244. """
  245. res = None
  246. try:
  247. yield self.handler.delete_version(self.local_user)
  248. except errors.SynapseError as e:
  249. res = e.code
  250. self.assertEqual(res, 404)
  251. @defer.inlineCallbacks
  252. def test_delete_version(self):
  253. """Check that we can create and then delete versions.
  254. """
  255. res = yield self.handler.create_version(
  256. self.local_user,
  257. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  258. )
  259. self.assertEqual(res, "1")
  260. # check we can delete it
  261. yield self.handler.delete_version(self.local_user, "1")
  262. # check that it's gone
  263. res = None
  264. try:
  265. yield self.handler.get_version_info(self.local_user, "1")
  266. except errors.SynapseError as e:
  267. res = e.code
  268. self.assertEqual(res, 404)
  269. @defer.inlineCallbacks
  270. def test_get_missing_backup(self):
  271. """Check that we get a 404 on querying missing backup
  272. """
  273. res = None
  274. try:
  275. yield self.handler.get_room_keys(self.local_user, "bogus_version")
  276. except errors.SynapseError as e:
  277. res = e.code
  278. self.assertEqual(res, 404)
  279. @defer.inlineCallbacks
  280. def test_get_missing_room_keys(self):
  281. """Check we get an empty response from an empty backup
  282. """
  283. version = yield self.handler.create_version(
  284. self.local_user,
  285. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  286. )
  287. self.assertEqual(version, "1")
  288. res = yield self.handler.get_room_keys(self.local_user, version)
  289. self.assertDictEqual(res, {"rooms": {}})
  290. # TODO: test the locking semantics when uploading room_keys,
  291. # although this is probably best done in sytest
  292. @defer.inlineCallbacks
  293. def test_upload_room_keys_no_versions(self):
  294. """Check that we get a 404 on uploading keys when no versions are defined
  295. """
  296. res = None
  297. try:
  298. yield self.handler.upload_room_keys(
  299. self.local_user, "no_version", room_keys
  300. )
  301. except errors.SynapseError as e:
  302. res = e.code
  303. self.assertEqual(res, 404)
  304. @defer.inlineCallbacks
  305. def test_upload_room_keys_bogus_version(self):
  306. """Check that we get a 404 on uploading keys when an nonexistent version
  307. is specified
  308. """
  309. version = yield self.handler.create_version(
  310. self.local_user,
  311. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  312. )
  313. self.assertEqual(version, "1")
  314. res = None
  315. try:
  316. yield self.handler.upload_room_keys(
  317. self.local_user, "bogus_version", room_keys
  318. )
  319. except errors.SynapseError as e:
  320. res = e.code
  321. self.assertEqual(res, 404)
  322. @defer.inlineCallbacks
  323. def test_upload_room_keys_wrong_version(self):
  324. """Check that we get a 403 on uploading keys for an old version
  325. """
  326. version = yield self.handler.create_version(
  327. self.local_user,
  328. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  329. )
  330. self.assertEqual(version, "1")
  331. version = yield self.handler.create_version(
  332. self.local_user,
  333. {
  334. "algorithm": "m.megolm_backup.v1",
  335. "auth_data": "second_version_auth_data",
  336. },
  337. )
  338. self.assertEqual(version, "2")
  339. res = None
  340. try:
  341. yield self.handler.upload_room_keys(self.local_user, "1", room_keys)
  342. except errors.SynapseError as e:
  343. res = e.code
  344. self.assertEqual(res, 403)
  345. @defer.inlineCallbacks
  346. def test_upload_room_keys_insert(self):
  347. """Check that we can insert and retrieve keys for a session
  348. """
  349. version = yield self.handler.create_version(
  350. self.local_user,
  351. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  352. )
  353. self.assertEqual(version, "1")
  354. yield self.handler.upload_room_keys(self.local_user, version, room_keys)
  355. res = yield self.handler.get_room_keys(self.local_user, version)
  356. self.assertDictEqual(res, room_keys)
  357. # check getting room_keys for a given room
  358. res = yield self.handler.get_room_keys(
  359. self.local_user, version, room_id="!abc:matrix.org"
  360. )
  361. self.assertDictEqual(res, room_keys)
  362. # check getting room_keys for a given session_id
  363. res = yield self.handler.get_room_keys(
  364. self.local_user, version, room_id="!abc:matrix.org", session_id="c0ff33"
  365. )
  366. self.assertDictEqual(res, room_keys)
  367. @defer.inlineCallbacks
  368. def test_upload_room_keys_merge(self):
  369. """Check that we can upload a new room_key for an existing session and
  370. have it correctly merged"""
  371. version = yield self.handler.create_version(
  372. self.local_user,
  373. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  374. )
  375. self.assertEqual(version, "1")
  376. yield self.handler.upload_room_keys(self.local_user, version, room_keys)
  377. # get the etag to compare to future versions
  378. res = yield self.handler.get_version_info(self.local_user)
  379. backup_etag = res["etag"]
  380. self.assertEqual(res["count"], 1)
  381. new_room_keys = copy.deepcopy(room_keys)
  382. new_room_key = new_room_keys["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]
  383. # test that increasing the message_index doesn't replace the existing session
  384. new_room_key["first_message_index"] = 2
  385. new_room_key["session_data"] = "new"
  386. yield self.handler.upload_room_keys(self.local_user, version, new_room_keys)
  387. res = yield self.handler.get_room_keys(self.local_user, version)
  388. self.assertEqual(
  389. res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"],
  390. "SSBBTSBBIEZJU0gK",
  391. )
  392. # the etag should be the same since the session did not change
  393. res = yield self.handler.get_version_info(self.local_user)
  394. self.assertEqual(res["etag"], backup_etag)
  395. # test that marking the session as verified however /does/ replace it
  396. new_room_key["is_verified"] = True
  397. yield self.handler.upload_room_keys(self.local_user, version, new_room_keys)
  398. res = yield self.handler.get_room_keys(self.local_user, version)
  399. self.assertEqual(
  400. res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"], "new"
  401. )
  402. # the etag should NOT be equal now, since the key changed
  403. res = yield self.handler.get_version_info(self.local_user)
  404. self.assertNotEqual(res["etag"], backup_etag)
  405. backup_etag = res["etag"]
  406. # test that a session with a higher forwarded_count doesn't replace one
  407. # with a lower forwarding count
  408. new_room_key["forwarded_count"] = 2
  409. new_room_key["session_data"] = "other"
  410. yield self.handler.upload_room_keys(self.local_user, version, new_room_keys)
  411. res = yield self.handler.get_room_keys(self.local_user, version)
  412. self.assertEqual(
  413. res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"], "new"
  414. )
  415. # the etag should be the same since the session did not change
  416. res = yield self.handler.get_version_info(self.local_user)
  417. self.assertEqual(res["etag"], backup_etag)
  418. # TODO: check edge cases as well as the common variations here
  419. @defer.inlineCallbacks
  420. def test_delete_room_keys(self):
  421. """Check that we can insert and delete keys for a session
  422. """
  423. version = yield self.handler.create_version(
  424. self.local_user,
  425. {"algorithm": "m.megolm_backup.v1", "auth_data": "first_version_auth_data"},
  426. )
  427. self.assertEqual(version, "1")
  428. # check for bulk-delete
  429. yield self.handler.upload_room_keys(self.local_user, version, room_keys)
  430. yield self.handler.delete_room_keys(self.local_user, version)
  431. res = yield self.handler.get_room_keys(
  432. self.local_user, version, room_id="!abc:matrix.org", session_id="c0ff33"
  433. )
  434. self.assertDictEqual(res, {"rooms": {}})
  435. # check for bulk-delete per room
  436. yield self.handler.upload_room_keys(self.local_user, version, room_keys)
  437. yield self.handler.delete_room_keys(
  438. self.local_user, version, room_id="!abc:matrix.org"
  439. )
  440. res = yield self.handler.get_room_keys(
  441. self.local_user, version, room_id="!abc:matrix.org", session_id="c0ff33"
  442. )
  443. self.assertDictEqual(res, {"rooms": {}})
  444. # check for bulk-delete per session
  445. yield self.handler.upload_room_keys(self.local_user, version, room_keys)
  446. yield self.handler.delete_room_keys(
  447. self.local_user, version, room_id="!abc:matrix.org", session_id="c0ff33"
  448. )
  449. res = yield self.handler.get_room_keys(
  450. self.local_user, version, room_id="!abc:matrix.org", session_id="c0ff33"
  451. )
  452. self.assertDictEqual(res, {"rooms": {}})