test_keys.py 4.3 KB

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