Files
synapse/tests/synapse_rust/test_unsigned.py
T
Erik Johnston e8e5a42180 Fix parsing events that have large integers (#19819)
Follow on from https://github.com/element-hq/synapse/pull/19701.

Unfortunately serde has a bug when using `#[serde(flatten)]` with
`arbitrary-precision` feature when handling integers that fit in a i128
when doing `serde_json::from_value`. See
https://github.com/serde-rs/serde/issues/2230.

The `depythonize` hits the same issue. To fix this we make it so we only
parse events from strings and not values.
2026-06-03 14:52:05 +01:00

60 lines
2.1 KiB
Python

#
# This file is licensed under the Affero General Public License (AGPL) version 3.
#
# Copyright (C) 2026 Element Creations Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# See the GNU Affero General Public License for more details:
# <https://www.gnu.org/licenses/agpl-3.0.html>.
from canonicaljson import encode_canonical_json
from synapse.synapse_rust.events import Unsigned
from synapse.types import JsonDict
from tests import unittest
def _make_unsigned(d: JsonDict) -> Unsigned:
return Unsigned(encode_canonical_json(d).decode("utf-8"))
class UnsignedTestCase(unittest.TestCase):
def test_prev_content(self) -> None:
"""Test that the prev_content field is correctly exposed as a JsonObject."""
unsigned = _make_unsigned({"prev_content": {"key1": "value1", "key2": 42}})
self.assert_dict(unsigned["prev_content"], {"key1": "value1", "key2": 42})
self.assert_dict(
unsigned.for_event(), {"prev_content": {"key1": "value1", "key2": 42}}
)
def test_large_age_ts(self) -> None:
"""Test that we can handle integers larger than 2^128, which is larger
than the maximum rust native integer size."""
large_int = 2**200
unsigned = _make_unsigned({"age_ts": large_int})
self.assertEqual(unsigned["age_ts"], large_int)
self.assert_dict(unsigned.for_event(), {"age_ts": large_int})
def test_large_integer_in_prev_content(self) -> None:
"""Test that we can handle integers larger than 2^128 in the
prev_content field, which is a JsonObject and thus can contain arbitrary
JSON."""
large_int = 2**200
unsigned = _make_unsigned({"prev_content": {"some_field": large_int}})
self.assertEqual(unsigned["prev_content"]["some_field"], large_int)
self.assert_dict(
unsigned.for_event(), {"prev_content": {"some_field": large_int}}
)