test_e2e_room_keys.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from tests import unittest
  16. # sample room_key data for use in the tests
  17. room_key = {
  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_datastore()
  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)