test_end_to_end_keys.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  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 twisted.internet import defer
  16. import tests.unittest
  17. import tests.utils
  18. class EndToEndKeyStoreTestCase(tests.unittest.TestCase):
  19. @defer.inlineCallbacks
  20. def setUp(self):
  21. hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
  22. self.store = hs.get_datastore()
  23. @defer.inlineCallbacks
  24. def test_key_without_device_name(self):
  25. now = 1470174257070
  26. json = {"key": "value"}
  27. yield defer.ensureDeferred(self.store.store_device("user", "device", None))
  28. yield defer.ensureDeferred(
  29. self.store.set_e2e_device_keys("user", "device", now, json)
  30. )
  31. res = yield defer.ensureDeferred(
  32. self.store.get_e2e_device_keys_for_cs_api((("user", "device"),))
  33. )
  34. self.assertIn("user", res)
  35. self.assertIn("device", res["user"])
  36. dev = res["user"]["device"]
  37. self.assertDictContainsSubset(json, dev)
  38. @defer.inlineCallbacks
  39. def test_reupload_key(self):
  40. now = 1470174257070
  41. json = {"key": "value"}
  42. yield defer.ensureDeferred(self.store.store_device("user", "device", None))
  43. changed = yield defer.ensureDeferred(
  44. self.store.set_e2e_device_keys("user", "device", now, json)
  45. )
  46. self.assertTrue(changed)
  47. # If we try to upload the same key then we should be told nothing
  48. # changed
  49. changed = yield defer.ensureDeferred(
  50. self.store.set_e2e_device_keys("user", "device", now, json)
  51. )
  52. self.assertFalse(changed)
  53. @defer.inlineCallbacks
  54. def test_get_key_with_device_name(self):
  55. now = 1470174257070
  56. json = {"key": "value"}
  57. yield defer.ensureDeferred(
  58. self.store.set_e2e_device_keys("user", "device", now, json)
  59. )
  60. yield defer.ensureDeferred(
  61. self.store.store_device("user", "device", "display_name")
  62. )
  63. res = yield defer.ensureDeferred(
  64. self.store.get_e2e_device_keys_for_cs_api((("user", "device"),))
  65. )
  66. self.assertIn("user", res)
  67. self.assertIn("device", res["user"])
  68. dev = res["user"]["device"]
  69. self.assertDictContainsSubset(
  70. {"key": "value", "unsigned": {"device_display_name": "display_name"}}, dev
  71. )
  72. @defer.inlineCallbacks
  73. def test_multiple_devices(self):
  74. now = 1470174257070
  75. yield defer.ensureDeferred(self.store.store_device("user1", "device1", None))
  76. yield defer.ensureDeferred(self.store.store_device("user1", "device2", None))
  77. yield defer.ensureDeferred(self.store.store_device("user2", "device1", None))
  78. yield defer.ensureDeferred(self.store.store_device("user2", "device2", None))
  79. yield defer.ensureDeferred(
  80. self.store.set_e2e_device_keys("user1", "device1", now, {"key": "json11"})
  81. )
  82. yield defer.ensureDeferred(
  83. self.store.set_e2e_device_keys("user1", "device2", now, {"key": "json12"})
  84. )
  85. yield defer.ensureDeferred(
  86. self.store.set_e2e_device_keys("user2", "device1", now, {"key": "json21"})
  87. )
  88. yield defer.ensureDeferred(
  89. self.store.set_e2e_device_keys("user2", "device2", now, {"key": "json22"})
  90. )
  91. res = yield defer.ensureDeferred(
  92. self.store.get_e2e_device_keys_for_cs_api(
  93. (("user1", "device1"), ("user2", "device2"))
  94. )
  95. )
  96. self.assertIn("user1", res)
  97. self.assertIn("device1", res["user1"])
  98. self.assertNotIn("device2", res["user1"])
  99. self.assertIn("user2", res)
  100. self.assertNotIn("device1", res["user2"])
  101. self.assertIn("device2", res["user2"])