|
19 | 19 | # [This file includes modifications made by New Vector Limited]
|
20 | 20 | #
|
21 | 21 | #
|
| 22 | +import time |
22 | 23 | from typing import Dict, Iterable
|
23 | 24 | from unittest import mock
|
24 | 25 |
|
@@ -1826,3 +1827,72 @@ def test_check_cross_signing_setup(self) -> None:
|
1826 | 1827 | )
|
1827 | 1828 | self.assertIs(exists, True)
|
1828 | 1829 | self.assertIs(replaceable_without_uia, False)
|
| 1830 | + |
| 1831 | + def test_delete_old_one_time_keys(self): |
| 1832 | + """Test the db migration that clears out old OTKs""" |
| 1833 | + |
| 1834 | + # We upload two sets of keys, one just over a week ago, and one just less than |
| 1835 | + # a week ago. Each batch contains some keys that match the deletion pattern |
| 1836 | + # (key IDs of 6 chars), and some that do not. |
| 1837 | + # |
| 1838 | + # Finally, set the scheduled task going, and check what gets deleted. |
| 1839 | + |
| 1840 | + user_id = "@user000:" + self.hs.hostname |
| 1841 | + device_id = "xyz" |
| 1842 | + |
| 1843 | + # The scheduled task should be for "now" in real, wallclock time, so |
| 1844 | + # set the test reactor to just over a week ago. |
| 1845 | + self.reactor.advance(time.time() - 7.5 * 24 * 3600) |
| 1846 | + |
| 1847 | + # Upload some keys |
| 1848 | + self.get_success( |
| 1849 | + self.handler.upload_keys_for_user( |
| 1850 | + user_id, |
| 1851 | + device_id, |
| 1852 | + { |
| 1853 | + "one_time_keys": { |
| 1854 | + # some keys to delete |
| 1855 | + "alg1:AAAAAA": "key1", |
| 1856 | + "alg2:AAAAAB": {"key": "key2", "signatures": {"k1": "sig1"}}, |
| 1857 | + # A key to *not* delete |
| 1858 | + "alg2:AAAAAAAAAA": {"key": "key3"}, |
| 1859 | + } |
| 1860 | + }, |
| 1861 | + ) |
| 1862 | + ) |
| 1863 | + |
| 1864 | + # A day passes |
| 1865 | + self.reactor.advance(24 * 3600) |
| 1866 | + |
| 1867 | + # Upload some more keys |
| 1868 | + self.get_success( |
| 1869 | + self.handler.upload_keys_for_user( |
| 1870 | + user_id, |
| 1871 | + device_id, |
| 1872 | + { |
| 1873 | + "one_time_keys": { |
| 1874 | + # some keys which match the pattern |
| 1875 | + "alg1:BAAAAA": "key1", |
| 1876 | + "alg2:BAAAAB": {"key": "key2", "signatures": {"k1": "sig1"}}, |
| 1877 | + # A key to *not* delete |
| 1878 | + "alg2:BAAAAAAAAA": {"key": "key3"}, |
| 1879 | + } |
| 1880 | + }, |
| 1881 | + ) |
| 1882 | + ) |
| 1883 | + |
| 1884 | + # The rest of the week passes, which should set the scheduled task going. |
| 1885 | + self.reactor.advance(6.5 * 24 * 3600) |
| 1886 | + |
| 1887 | + # Check what we're left with in the database |
| 1888 | + remaining_key_ids = { |
| 1889 | + row[0] |
| 1890 | + for row in self.get_success( |
| 1891 | + self.handler.store.db_pool.simple_select_list( |
| 1892 | + "e2e_one_time_keys_json", None, ["key_id"] |
| 1893 | + ) |
| 1894 | + ) |
| 1895 | + } |
| 1896 | + self.assertEqual( |
| 1897 | + remaining_key_ids, {"AAAAAAAAAA", "BAAAAA", "BAAAAB", "BAAAAAAAAA"} |
| 1898 | + ) |
0 commit comments