test_end_to_end_keys.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2016-2021 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.util import Clock
  17. from tests.unittest import HomeserverTestCase
  18. class EndToEndKeyStoreTestCase(HomeserverTestCase):
  19. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  20. self.store = hs.get_datastores().main
  21. def test_key_without_device_name(self) -> None:
  22. now = 1470174257070
  23. json = {"key": "value"}
  24. self.get_success(self.store.store_device("user", "device", None))
  25. self.get_success(self.store.set_e2e_device_keys("user", "device", now, json))
  26. res = self.get_success(
  27. self.store.get_e2e_device_keys_for_cs_api((("user", "device"),))
  28. )
  29. self.assertIn("user", res)
  30. self.assertIn("device", res["user"])
  31. dev = res["user"]["device"]
  32. self.assertDictContainsSubset(json, dev)
  33. def test_reupload_key(self) -> None:
  34. now = 1470174257070
  35. json = {"key": "value"}
  36. self.get_success(self.store.store_device("user", "device", None))
  37. changed = self.get_success(
  38. self.store.set_e2e_device_keys("user", "device", now, json)
  39. )
  40. self.assertTrue(changed)
  41. # If we try to upload the same key then we should be told nothing
  42. # changed
  43. changed = self.get_success(
  44. self.store.set_e2e_device_keys("user", "device", now, json)
  45. )
  46. self.assertFalse(changed)
  47. def test_get_key_with_device_name(self) -> None:
  48. now = 1470174257070
  49. json = {"key": "value"}
  50. self.get_success(self.store.set_e2e_device_keys("user", "device", now, json))
  51. self.get_success(self.store.store_device("user", "device", "display_name"))
  52. res = self.get_success(
  53. self.store.get_e2e_device_keys_for_cs_api((("user", "device"),))
  54. )
  55. self.assertIn("user", res)
  56. self.assertIn("device", res["user"])
  57. dev = res["user"]["device"]
  58. self.assertDictContainsSubset(
  59. {"key": "value", "unsigned": {"device_display_name": "display_name"}}, dev
  60. )
  61. def test_multiple_devices(self) -> None:
  62. now = 1470174257070
  63. self.get_success(self.store.store_device("user1", "device1", None))
  64. self.get_success(self.store.store_device("user1", "device2", None))
  65. self.get_success(self.store.store_device("user2", "device1", None))
  66. self.get_success(self.store.store_device("user2", "device2", None))
  67. self.get_success(
  68. self.store.set_e2e_device_keys("user1", "device1", now, {"key": "json11"})
  69. )
  70. self.get_success(
  71. self.store.set_e2e_device_keys("user1", "device2", now, {"key": "json12"})
  72. )
  73. self.get_success(
  74. self.store.set_e2e_device_keys("user2", "device1", now, {"key": "json21"})
  75. )
  76. self.get_success(
  77. self.store.set_e2e_device_keys("user2", "device2", now, {"key": "json22"})
  78. )
  79. res = self.get_success(
  80. self.store.get_e2e_device_keys_for_cs_api(
  81. (("user1", "device1"), ("user2", "device2"))
  82. )
  83. )
  84. self.assertIn("user1", res)
  85. self.assertIn("device1", res["user1"])
  86. self.assertNotIn("device2", res["user1"])
  87. self.assertIn("user2", res)
  88. self.assertNotIn("device1", res["user2"])
  89. self.assertIn("device2", res["user2"])