test_keys.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright 2017 Vector Creations Ltd
  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 signedjson.key
  15. import signedjson.types
  16. import unpaddedbase64
  17. from twisted.internet.defer import Deferred
  18. from synapse.storage.keys import FetchKeyResult
  19. import tests.unittest
  20. def decode_verify_key_base64(
  21. key_id: str, key_base64: str
  22. ) -> signedjson.types.VerifyKey:
  23. key_bytes = unpaddedbase64.decode_base64(key_base64)
  24. return signedjson.key.decode_verify_key_bytes(key_id, key_bytes)
  25. KEY_1 = decode_verify_key_base64(
  26. "ed25519:key1", "fP5l4JzpZPq/zdbBg5xx6lQGAAOM9/3w94cqiJ5jPrw"
  27. )
  28. KEY_2 = decode_verify_key_base64(
  29. "ed25519:key2", "Noi6WqcDj0QmPxCNQqgezwTlBKrfqehY1u2FyWP9uYw"
  30. )
  31. class KeyStoreTestCase(tests.unittest.HomeserverTestCase):
  32. def test_get_server_verify_keys(self) -> None:
  33. store = self.hs.get_datastores().main
  34. key_id_1 = "ed25519:key1"
  35. key_id_2 = "ed25519:KEY_ID_2"
  36. d = store.store_server_verify_keys(
  37. "from_server",
  38. 10,
  39. [
  40. ("server1", key_id_1, FetchKeyResult(KEY_1, 100)),
  41. ("server1", key_id_2, FetchKeyResult(KEY_2, 200)),
  42. ],
  43. )
  44. self.get_success(d)
  45. d = store.get_server_verify_keys(
  46. [("server1", key_id_1), ("server1", key_id_2), ("server1", "ed25519:key3")]
  47. )
  48. res = self.get_success(d)
  49. self.assertEqual(len(res.keys()), 3)
  50. res1 = res[("server1", key_id_1)]
  51. self.assertEqual(res1.verify_key, KEY_1)
  52. self.assertEqual(res1.verify_key.version, "key1")
  53. self.assertEqual(res1.valid_until_ts, 100)
  54. res2 = res[("server1", key_id_2)]
  55. self.assertEqual(res2.verify_key, KEY_2)
  56. # version comes from the ID it was stored with
  57. self.assertEqual(res2.verify_key.version, "KEY_ID_2")
  58. self.assertEqual(res2.valid_until_ts, 200)
  59. # non-existent result gives None
  60. self.assertIsNone(res[("server1", "ed25519:key3")])
  61. def test_cache(self) -> None:
  62. """Check that updates correctly invalidate the cache."""
  63. store = self.hs.get_datastores().main
  64. key_id_1 = "ed25519:key1"
  65. key_id_2 = "ed25519:key2"
  66. d = store.store_server_verify_keys(
  67. "from_server",
  68. 0,
  69. [
  70. ("srv1", key_id_1, FetchKeyResult(KEY_1, 100)),
  71. ("srv1", key_id_2, FetchKeyResult(KEY_2, 200)),
  72. ],
  73. )
  74. self.get_success(d)
  75. d = store.get_server_verify_keys([("srv1", key_id_1), ("srv1", key_id_2)])
  76. res = self.get_success(d)
  77. self.assertEqual(len(res.keys()), 2)
  78. res1 = res[("srv1", key_id_1)]
  79. self.assertEqual(res1.verify_key, KEY_1)
  80. self.assertEqual(res1.valid_until_ts, 100)
  81. res2 = res[("srv1", key_id_2)]
  82. self.assertEqual(res2.verify_key, KEY_2)
  83. self.assertEqual(res2.valid_until_ts, 200)
  84. # we should be able to look up the same thing again without a db hit
  85. res = store.get_server_verify_keys([("srv1", key_id_1)])
  86. if isinstance(res, Deferred):
  87. res = self.successResultOf(res)
  88. self.assertEqual(len(res.keys()), 1)
  89. self.assertEqual(res[("srv1", key_id_1)].verify_key, KEY_1)
  90. new_key_2 = signedjson.key.get_verify_key(
  91. signedjson.key.generate_signing_key("key2")
  92. )
  93. d = store.store_server_verify_keys(
  94. "from_server", 10, [("srv1", key_id_2, FetchKeyResult(new_key_2, 300))]
  95. )
  96. self.get_success(d)
  97. d = store.get_server_verify_keys([("srv1", key_id_1), ("srv1", key_id_2)])
  98. res = self.get_success(d)
  99. self.assertEqual(len(res.keys()), 2)
  100. res1 = res[("srv1", key_id_1)]
  101. self.assertEqual(res1.verify_key, KEY_1)
  102. self.assertEqual(res1.valid_until_ts, 100)
  103. res2 = res[("srv1", key_id_2)]
  104. self.assertEqual(res2.verify_key, new_key_2)
  105. self.assertEqual(res2.valid_until_ts, 300)