test_devices.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. import synapse.api.errors
  15. from synapse.api.constants import EduTypes
  16. from tests.unittest import HomeserverTestCase
  17. class DeviceStoreTestCase(HomeserverTestCase):
  18. def prepare(self, reactor, clock, hs):
  19. self.store = hs.get_datastores().main
  20. def add_device_change(self, user_id, device_ids, host):
  21. """Add a device list change for the given device to
  22. `device_lists_outbound_pokes` table.
  23. """
  24. for device_id in device_ids:
  25. stream_id = self.get_success(
  26. self.store.add_device_change_to_streams(
  27. user_id, [device_id], ["!some:room"]
  28. )
  29. )
  30. self.get_success(
  31. self.store.add_device_list_outbound_pokes(
  32. user_id=user_id,
  33. device_id=device_id,
  34. room_id="!some:room",
  35. stream_id=stream_id,
  36. hosts=[host],
  37. context={},
  38. )
  39. )
  40. def test_store_new_device(self):
  41. self.get_success(
  42. self.store.store_device("user_id", "device_id", "display_name")
  43. )
  44. res = self.get_success(self.store.get_device("user_id", "device_id"))
  45. self.assertDictContainsSubset(
  46. {
  47. "user_id": "user_id",
  48. "device_id": "device_id",
  49. "display_name": "display_name",
  50. },
  51. res,
  52. )
  53. def test_get_devices_by_user(self):
  54. self.get_success(
  55. self.store.store_device("user_id", "device1", "display_name 1")
  56. )
  57. self.get_success(
  58. self.store.store_device("user_id", "device2", "display_name 2")
  59. )
  60. self.get_success(
  61. self.store.store_device("user_id2", "device3", "display_name 3")
  62. )
  63. res = self.get_success(self.store.get_devices_by_user("user_id"))
  64. self.assertEqual(2, len(res.keys()))
  65. self.assertDictContainsSubset(
  66. {
  67. "user_id": "user_id",
  68. "device_id": "device1",
  69. "display_name": "display_name 1",
  70. },
  71. res["device1"],
  72. )
  73. self.assertDictContainsSubset(
  74. {
  75. "user_id": "user_id",
  76. "device_id": "device2",
  77. "display_name": "display_name 2",
  78. },
  79. res["device2"],
  80. )
  81. def test_count_devices_by_users(self):
  82. self.get_success(
  83. self.store.store_device("user_id", "device1", "display_name 1")
  84. )
  85. self.get_success(
  86. self.store.store_device("user_id", "device2", "display_name 2")
  87. )
  88. self.get_success(
  89. self.store.store_device("user_id2", "device3", "display_name 3")
  90. )
  91. res = self.get_success(self.store.count_devices_by_users())
  92. self.assertEqual(0, res)
  93. res = self.get_success(self.store.count_devices_by_users(["unknown"]))
  94. self.assertEqual(0, res)
  95. res = self.get_success(self.store.count_devices_by_users(["user_id"]))
  96. self.assertEqual(2, res)
  97. res = self.get_success(
  98. self.store.count_devices_by_users(["user_id", "user_id2"])
  99. )
  100. self.assertEqual(3, res)
  101. def test_get_device_updates_by_remote(self):
  102. device_ids = ["device_id1", "device_id2"]
  103. # Add two device updates with sequential `stream_id`s
  104. self.add_device_change("@user_id:test", device_ids, "somehost")
  105. # Get all device updates ever meant for this remote
  106. now_stream_id, device_updates = self.get_success(
  107. self.store.get_device_updates_by_remote("somehost", -1, limit=100)
  108. )
  109. # Check original device_ids are contained within these updates
  110. self._check_devices_in_updates(device_ids, device_updates)
  111. def test_get_device_updates_by_remote_can_limit_properly(self):
  112. """
  113. Tests that `get_device_updates_by_remote` returns an appropriate
  114. stream_id to resume fetching from (without skipping any results).
  115. """
  116. # Add some device updates with sequential `stream_id`s
  117. device_ids = [
  118. "device_id1",
  119. "device_id2",
  120. "device_id3",
  121. "device_id4",
  122. "device_id5",
  123. ]
  124. self.add_device_change("@user_id:test", device_ids, "somehost")
  125. # Get device updates meant for this remote
  126. next_stream_id, device_updates = self.get_success(
  127. self.store.get_device_updates_by_remote("somehost", -1, limit=3)
  128. )
  129. # Check the first three original device_ids are contained within these updates
  130. self._check_devices_in_updates(device_ids[:3], device_updates)
  131. # Get the next batch of device updates
  132. next_stream_id, device_updates = self.get_success(
  133. self.store.get_device_updates_by_remote("somehost", next_stream_id, limit=3)
  134. )
  135. # Check the last two original device_ids are contained within these updates
  136. self._check_devices_in_updates(device_ids[3:], device_updates)
  137. # Add some more device updates to ensure it still resumes properly
  138. device_ids = ["device_id6", "device_id7"]
  139. self.add_device_change("@user_id:test", device_ids, "somehost")
  140. # Get the next batch of device updates
  141. next_stream_id, device_updates = self.get_success(
  142. self.store.get_device_updates_by_remote("somehost", next_stream_id, limit=3)
  143. )
  144. # Check the newly-added device_ids are contained within these updates
  145. self._check_devices_in_updates(device_ids, device_updates)
  146. # Check there are no more device updates left.
  147. _, device_updates = self.get_success(
  148. self.store.get_device_updates_by_remote("somehost", next_stream_id, limit=3)
  149. )
  150. self.assertEqual(device_updates, [])
  151. def test_get_device_updates_by_remote_cross_signing_key_updates(
  152. self,
  153. ) -> None:
  154. """
  155. Tests that `get_device_updates_by_remote` limits the length of the return value
  156. properly when cross-signing key updates are present.
  157. Current behaviour is that the cross-signing key updates will always come in pairs,
  158. even if that means leaving an earlier batch one EDU short of the limit.
  159. """
  160. assert self.hs.is_mine_id(
  161. "@user_id:test"
  162. ), "Test not valid: this MXID should be considered local"
  163. self.get_success(
  164. self.store.set_e2e_cross_signing_key(
  165. "@user_id:test",
  166. "master",
  167. {
  168. "keys": {
  169. "ed25519:fakeMaster": "aaafakefakefake1AAAAAAAAAAAAAAAAAAAAAAAAAAA="
  170. },
  171. "signatures": {
  172. "@user_id:test": {
  173. "ed25519:fake2": "aaafakefakefake2AAAAAAAAAAAAAAAAAAAAAAAAAAA="
  174. }
  175. },
  176. },
  177. )
  178. )
  179. self.get_success(
  180. self.store.set_e2e_cross_signing_key(
  181. "@user_id:test",
  182. "self_signing",
  183. {
  184. "keys": {
  185. "ed25519:fakeSelfSigning": "aaafakefakefake3AAAAAAAAAAAAAAAAAAAAAAAAAAA="
  186. },
  187. "signatures": {
  188. "@user_id:test": {
  189. "ed25519:fake4": "aaafakefakefake4AAAAAAAAAAAAAAAAAAAAAAAAAAA="
  190. }
  191. },
  192. },
  193. )
  194. )
  195. # Add some device updates with sequential `stream_id`s
  196. # Note that the public cross-signing keys occupy the same space as device IDs,
  197. # so also notify that those have updated.
  198. device_ids = [
  199. "device_id1",
  200. "device_id2",
  201. "fakeMaster",
  202. "fakeSelfSigning",
  203. ]
  204. self.add_device_change("@user_id:test", device_ids, "somehost")
  205. # Get device updates meant for this remote
  206. next_stream_id, device_updates = self.get_success(
  207. self.store.get_device_updates_by_remote("somehost", -1, limit=3)
  208. )
  209. # Here we expect the device updates for `device_id1` and `device_id2`.
  210. # That means we only receive 2 updates this time around.
  211. # If we had a higher limit, we would expect to see the pair of
  212. # (unstable-prefixed & unprefixed) signing key updates for the device
  213. # represented by `fakeMaster` and `fakeSelfSigning`.
  214. # Our implementation only sends these two variants together, so we get
  215. # a short batch.
  216. self.assertEqual(len(device_updates), 2, device_updates)
  217. # Check the first two devices (device_id1, device_id2) came out.
  218. self._check_devices_in_updates(device_ids[:2], device_updates)
  219. # Get more device updates meant for this remote
  220. next_stream_id, device_updates = self.get_success(
  221. self.store.get_device_updates_by_remote("somehost", next_stream_id, limit=3)
  222. )
  223. # The next 2 updates should be a cross-signing key update
  224. # (the master key update and the self-signing key update are combined into
  225. # one 'signing key update', but the cross-signing key update is emitted
  226. # twice, once with an unprefixed type and once again with an unstable-prefixed type)
  227. # (This is a temporary arrangement for backwards compatibility!)
  228. self.assertEqual(len(device_updates), 2, device_updates)
  229. self.assertEqual(
  230. device_updates[0][0], EduTypes.SIGNING_KEY_UPDATE, device_updates[0]
  231. )
  232. self.assertEqual(
  233. device_updates[1][0],
  234. EduTypes.UNSTABLE_SIGNING_KEY_UPDATE,
  235. device_updates[1],
  236. )
  237. # Check there are no more device updates left.
  238. _, device_updates = self.get_success(
  239. self.store.get_device_updates_by_remote("somehost", next_stream_id, limit=3)
  240. )
  241. self.assertEqual(device_updates, [])
  242. def _check_devices_in_updates(self, expected_device_ids, device_updates):
  243. """Check that an specific device ids exist in a list of device update EDUs"""
  244. self.assertEqual(len(device_updates), len(expected_device_ids))
  245. received_device_ids = {
  246. update["device_id"] for edu_type, update in device_updates
  247. }
  248. self.assertEqual(received_device_ids, set(expected_device_ids))
  249. def test_update_device(self):
  250. self.get_success(
  251. self.store.store_device("user_id", "device_id", "display_name 1")
  252. )
  253. res = self.get_success(self.store.get_device("user_id", "device_id"))
  254. self.assertEqual("display_name 1", res["display_name"])
  255. # do a no-op first
  256. self.get_success(self.store.update_device("user_id", "device_id"))
  257. res = self.get_success(self.store.get_device("user_id", "device_id"))
  258. self.assertEqual("display_name 1", res["display_name"])
  259. # do the update
  260. self.get_success(
  261. self.store.update_device(
  262. "user_id", "device_id", new_display_name="display_name 2"
  263. )
  264. )
  265. # check it worked
  266. res = self.get_success(self.store.get_device("user_id", "device_id"))
  267. self.assertEqual("display_name 2", res["display_name"])
  268. def test_update_unknown_device(self):
  269. exc = self.get_failure(
  270. self.store.update_device(
  271. "user_id", "unknown_device_id", new_display_name="display_name 2"
  272. ),
  273. synapse.api.errors.StoreError,
  274. )
  275. self.assertEqual(404, exc.value.code)