Coverage for tests/common/monero_utils/test_serialization.py: 100%

43 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1from __future__ import annotations 

2 

3import logging 

4from typing import Any 

5 

6import pytest 

7from monero import MoneroUtils 

8 

9from .base_test_monero_utils import BaseTestMoneroUtils 

10 

11logger: logging.Logger = logging.getLogger("TestMoneroUtilsSerialization") 

12 

13 

14@pytest.mark.unit 

15class TestMoneroUtilsSerialization(BaseTestMoneroUtils): 

16 """Monero utilities serialization unit tests.""" 

17 

18 # Can serialize heights with small numbers 

19 def test_serialize_heights_small(self) -> None: 

20 json_map: dict[Any, Any] = { 

21 "heights": [111, 222, 333] 

22 } 

23 

24 binary: bytes = MoneroUtils.dict_to_binary(json_map) 

25 assert len(binary) > 0 

26 

27 json_map2: dict[Any, Any] = MoneroUtils.binary_to_dict(binary) 

28 assert json_map == json_map2 

29 

30 # Can serialize heights with big numbers 

31 def test_serialize_heights_big(self) -> None: 

32 json_map: dict[Any, Any] = { 

33 "heights": [123456, 1234567, 870987] 

34 } 

35 

36 binary: bytes = MoneroUtils.dict_to_binary(json_map) 

37 assert len(binary) > 0 

38 

39 json_map2: dict[Any, Any] = MoneroUtils.binary_to_dict(binary) 

40 assert json_map == json_map2 

41 

42 # can serialize height with large unsigned values 

43 def test_serialize_large_unsigned_values(self) -> None: 

44 big_value: int = 18446744073709551615 # UINT64_MAX 

45 json_map: dict[Any, Any] = { 

46 "heights": [big_value] 

47 } 

48 binary: bytes = MoneroUtils.dict_to_binary(json_map) 

49 assert len(binary) > 0 

50 

51 json_map2: dict[Any, Any] = MoneroUtils.binary_to_dict(binary) 

52 assert json_map == json_map2 

53 assert json_map2["heights"][0] > 0, "uint64 > INT64_MAX must not serialize as negative" 

54 

55 # Can serialize jsonMap with text 

56 def test_serialize_text_short(self, config: BaseTestMoneroUtils.Config) -> None: 

57 assert config.serialization_msg is not None and config.serialization_msg != "" 

58 json_map: dict[Any, Any] = { 

59 "msg": config.serialization_msg 

60 } 

61 

62 binary: bytes = MoneroUtils.dict_to_binary(json_map) 

63 assert len(binary) > 0 

64 

65 json_map2: dict[Any, Any] = MoneroUtils.binary_to_dict(binary) 

66 assert json_map == json_map2 

67 

68 # Can serialize json with long text 

69 def test_serialize_text_long(self, config: BaseTestMoneroUtils.Config) -> None: 

70 msg: str = config.serialization_msg 

71 json_map: dict[str, str] = { 

72 "msg": f"{msg}\n" + 

73 f"{msg}\n" + 

74 f"{msg}\n" + 

75 f"{msg}\n" + 

76 f"{msg}\n" + 

77 f"{msg}\n" + 

78 f"{msg}\n" + 

79 f"{msg}\n" + 

80 f"{msg}\n" + 

81 f"{msg}\n" + 

82 f"{msg}\n" + 

83 f"{msg}\n" + 

84 f"{msg}\n" + 

85 f"{msg}\n" 

86 } 

87 

88 binary: bytes = MoneroUtils.dict_to_binary(json_map) 

89 assert len(binary) > 0 

90 

91 json_map2: dict[Any, Any] = MoneroUtils.binary_to_dict(binary) 

92 assert json_map == json_map2