test_e2e_room_keys.py 2.6 KB

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