test_e2e_room_keys.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 twisted.test.proto_helpers import MemoryReactor
  15. from synapse.server import HomeServer
  16. from synapse.storage.databases.main.e2e_room_keys import RoomKey
  17. from synapse.util import Clock
  18. from tests import unittest
  19. # sample room_key data for use in the tests
  20. room_key: RoomKey = {
  21. "first_message_index": 1,
  22. "forwarded_count": 1,
  23. "is_verified": False,
  24. "session_data": "SSBBTSBBIEZJU0gK",
  25. }
  26. class E2eRoomKeysHandlerTestCase(unittest.HomeserverTestCase):
  27. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  28. hs = self.setup_test_homeserver("server", federation_http_client=None)
  29. self.store = hs.get_datastores().main
  30. return hs
  31. def test_room_keys_version_delete(self) -> None:
  32. # test that deleting a room key backup deletes the keys
  33. version1 = self.get_success(
  34. self.store.create_e2e_room_keys_version(
  35. "user_id", {"algorithm": "rot13", "auth_data": {}}
  36. )
  37. )
  38. self.get_success(
  39. self.store.add_e2e_room_keys(
  40. "user_id", version1, [("room", "session", room_key)]
  41. )
  42. )
  43. version2 = self.get_success(
  44. self.store.create_e2e_room_keys_version(
  45. "user_id", {"algorithm": "rot13", "auth_data": {}}
  46. )
  47. )
  48. self.get_success(
  49. self.store.add_e2e_room_keys(
  50. "user_id", version2, [("room", "session", room_key)]
  51. )
  52. )
  53. # make sure the keys were stored properly
  54. keys = self.get_success(self.store.get_e2e_room_keys("user_id", version1))
  55. self.assertEqual(len(keys["rooms"]), 1)
  56. keys = self.get_success(self.store.get_e2e_room_keys("user_id", version2))
  57. self.assertEqual(len(keys["rooms"]), 1)
  58. # delete version1
  59. self.get_success(self.store.delete_e2e_room_keys_version("user_id", version1))
  60. # make sure the key from version1 is gone, and the key from version2 is
  61. # still there
  62. keys = self.get_success(self.store.get_e2e_room_keys("user_id", version1))
  63. self.assertEqual(len(keys["rooms"]), 0)
  64. keys = self.get_success(self.store.get_e2e_room_keys("user_id", version2))
  65. self.assertEqual(len(keys["rooms"]), 1)