|
1 | 1 | import pytest |
2 | 2 | import varint |
3 | 3 |
|
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 |
5 | 7 |
|
6 | 8 |
|
7 | 9 | def test_code_to_varint(): |
@@ -180,3 +182,90 @@ def test_add_protocol_lock(valid_params): |
180 | 182 | def test_protocol_repr(): |
181 | 183 | proto = protocols.protocol_with_name("ip4") |
182 | 184 | 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