test_e2e_room_keys.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.storage.databases.main.e2e_room_keys import RoomKey
  15. from tests import unittest
  16. # sample room_key data for use in the tests
  17. room_key: RoomKey = {
  18. "first_message_index": 1,
  19. "forwarded_count": 1,
  20. "is_verified": False,
  21. "session_data": "SSBBTSBBIEZJU0gK",
  22. }
  23. class E2eRoomKeysHandlerTestCase(unittest.HomeserverTestCase):
  24. def make_homeserver(self, reactor, clock):
  25. hs = self.setup_test_homeserver("server", federation_http_client=None)
  26. self.store = hs.get_datastores().main
  27. return hs
  28. def test_room_keys_version_delete(self):
  29. # test that deleting a room key backup deletes the keys
  30. version1 = self.get_success(
  31. self.store.create_e2e_room_keys_version(
  32. "user_id", {"algorithm": "rot13", "auth_data": {}}
  33. )
  34. )
  35. self.get_success(
  36. self.store.add_e2e_room_keys(
  37. "user_id", version1, [("room", "session", room_key)]
  38. )
  39. )
  40. version2 = self.get_success(
  41. self.store.create_e2e_room_keys_version(
  42. "user_id", {"algorithm": "rot13", "auth_data": {}}
  43. )
  44. )
  45. self.get_success(
  46. self.store.add_e2e_room_keys(
  47. "user_id", version2, [("room", "session", room_key)]
  48. )
  49. )
  50. # make sure the keys were stored properly
  51. keys = self.get_success(self.store.get_e2e_room_keys("user_id", version1))
  52. self.assertEqual(len(keys["rooms"]), 1)
  53. keys = self.get_success(self.store.get_e2e_room_keys("user_id", version2))
  54. self.assertEqual(len(keys["rooms"]), 1)
  55. # delete version1
  56. self.get_success(self.store.delete_e2e_room_keys_version("user_id", version1))
  57. # make sure the key from version1 is gone, and the key from version2 is
  58. # still there
  59. keys = self.get_success(self.store.get_e2e_room_keys("user_id", version1))
  60. self.assertEqual(len(keys["rooms"]), 0)
  61. keys = self.get_success(self.store.get_e2e_room_keys("user_id", version2))
  62. self.assertEqual(len(keys["rooms"]), 1)