Skip to content

Commit ad51d44

Browse files
committed
added suggested test cases
1 parent d44f462 commit ad51d44

File tree

4 files changed

+114
-70
lines changed

4 files changed

+114
-70
lines changed

multiaddr/protocols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def __repr__(self) -> str:
167167
Protocol(P_P2P_CIRCUIT, "p2p-circuit", None),
168168
Protocol(P_WEBTRANSPORT, "webtransport", None),
169169
Protocol(P_UNIX, "unix", "fspath"),
170-
Protocol(P_MEMORY, "memory", None),
170+
Protocol(P_MEMORY, "memory", "memory"),
171171
]
172172

173173

tests/test_codec.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

tests/test_multiaddr.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
from multiaddr import protocols
34
from multiaddr.exceptions import (
45
BinaryParseError,
56
ProtocolLookupError,
@@ -802,3 +803,25 @@ def test_decapsulate_code():
802803
# No-op on empty
803804
ma3 = Multiaddr("")
804805
assert str(ma3.decapsulate_code(P_TCP)) == ""
806+
807+
808+
def test_memory_protocol_integration():
809+
ma = Multiaddr("/memory/12345")
810+
assert str(ma) == "/memory/12345"
811+
assert len(ma.protocols()) == 1
812+
assert ma.protocols()[0].name == "memory" # type: ignore
813+
assert ma.value_for_protocol(777) == "12345"
814+
815+
# Binary rountrip
816+
binary = ma.to_bytes()
817+
reconstructed = Multiaddr(binary)
818+
assert str(reconstructed) == str(ma)
819+
820+
821+
def test_memory_protocol_properties():
822+
proto = protocols.Protocol(protocols.P_MEMORY, "memory", "memory")
823+
assert proto.size == 64 # 8 bytes/ 64 bits
824+
assert not proto.path # Not a path protocol
825+
assert proto.code == 777
826+
assert proto.name == "memory"
827+
assert proto.codec == "memory"

tests/test_protocols.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import pytest
22
import varint
33

4-
from multiaddr import exceptions, protocols
4+
from multiaddr import Multiaddr, exceptions, protocols
5+
from multiaddr.codecs import memory
6+
from multiaddr.exceptions import BinaryParseError
57

68

79
def test_code_to_varint():
@@ -180,3 +182,90 @@ def test_add_protocol_lock(valid_params):
180182
def test_protocol_repr():
181183
proto = protocols.protocol_with_name("ip4")
182184
assert "Protocol(code=4, name='ip4', codec='ip4')" == repr(proto)
185+
186+
187+
def test_to_bytes_and_to_string_roundtrip():
188+
codec = memory.Codec()
189+
190+
# some valid values
191+
for val in [0, 1, 42, 2**32, 2**64 - 1]:
192+
s = str(val)
193+
b = codec.to_bytes(None, s)
194+
# must be exactly 8 bytes
195+
assert isinstance(b, bytes)
196+
assert len(b) == 8
197+
# roundtrip back to string
198+
out = codec.to_string(None, b)
199+
assert out == s
200+
201+
202+
def test_invalid_string_to_bytes():
203+
codec = memory.Codec()
204+
205+
# not a number
206+
with pytest.raises(ValueError):
207+
codec.to_bytes(None, "abc")
208+
209+
# negative number
210+
with pytest.raises(ValueError):
211+
codec.to_bytes(None, "-1")
212+
213+
# too large
214+
with pytest.raises(ValueError):
215+
codec.to_bytes(None, str(2**64))
216+
217+
218+
def test_invalid_bytes_to_string():
219+
codec = memory.Codec()
220+
221+
# too short
222+
with pytest.raises(BinaryParseError):
223+
codec.to_string(None, b"\x00\x01")
224+
225+
# too long
226+
with pytest.raises(BinaryParseError):
227+
codec.to_string(None, b"\x00" * 9)
228+
229+
230+
def test_specific_encoding():
231+
codec = memory.Codec()
232+
233+
# 42 encoded in big-endian
234+
expected_bytes = b"\x00\x00\x00\x00\x00\x00\x00*"
235+
assert codec.to_bytes(None, "42") == expected_bytes
236+
assert codec.to_string(None, expected_bytes) == "42"
237+
238+
239+
def test_memory_validate_function():
240+
# Directly test the helper
241+
codec = memory.Codec()
242+
243+
# Valid case
244+
codec.memory_validate(b"\x00" * 8) # should not raise
245+
246+
# Invalid length
247+
with pytest.raises(ValueError):
248+
codec.memory_validate(b"\x00" * 7)
249+
250+
251+
def test_memory_integration_edge_values():
252+
# Minimum (0)
253+
ma0 = Multiaddr("/memory/0")
254+
assert str(ma0) == "/memory/0"
255+
assert ma0.value_for_protocol(777) == "0"
256+
257+
# Maximum (2**64 - 1)
258+
max_val = str(2**64 - 1)
259+
mamax = Multiaddr(f"/memory/{max_val}")
260+
assert str(mamax) == f"/memory/{max_val}"
261+
assert mamax.value_for_protocol(777) == max_val
262+
263+
264+
def test_memory_integration_invalid_values():
265+
# Negative number
266+
with pytest.raises(ValueError):
267+
Multiaddr("/memory/-1")
268+
269+
# Too large (overflow > uint64)
270+
with pytest.raises(ValueError):
271+
Multiaddr(f"/memory/{2**64}")

0 commit comments

Comments
 (0)