You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

114 lines
4.0 KiB

  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.assertLessEqual(json.items(), dev.items())
  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.assertLessEqual(
  59. {
  60. "key": "value",
  61. "unsigned": {"device_display_name": "display_name"},
  62. }.items(),
  63. dev.items(),
  64. )
  65. def test_multiple_devices(self) -> None:
  66. now = 1470174257070
  67. self.get_success(self.store.store_device("user1", "device1", None))
  68. self.get_success(self.store.store_device("user1", "device2", None))
  69. self.get_success(self.store.store_device("user2", "device1", None))
  70. self.get_success(self.store.store_device("user2", "device2", None))
  71. self.get_success(
  72. self.store.set_e2e_device_keys("user1", "device1", now, {"key": "json11"})
  73. )
  74. self.get_success(
  75. self.store.set_e2e_device_keys("user1", "device2", now, {"key": "json12"})
  76. )
  77. self.get_success(
  78. self.store.set_e2e_device_keys("user2", "device1", now, {"key": "json21"})
  79. )
  80. self.get_success(
  81. self.store.set_e2e_device_keys("user2", "device2", now, {"key": "json22"})
  82. )
  83. res = self.get_success(
  84. self.store.get_e2e_device_keys_for_cs_api(
  85. (("user1", "device1"), ("user2", "device2"))
  86. )
  87. )
  88. self.assertIn("user1", res)
  89. self.assertIn("device1", res["user1"])
  90. self.assertNotIn("device2", res["user1"])
  91. self.assertIn("user2", res)
  92. self.assertNotIn("device1", res["user2"])
  93. self.assertIn("device2", res["user2"])