test_end_to_end_keys.py 3.8 KB

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