Skip to content

Commit 17c268c

Browse files
Add tests for tag-only protocols in Multiaddr
Implemented unit tests to ensure tag-only protocols reject invalid value syntax (both /tag/value and /tag=value) and validate correct combinations. Enhanced error message clarity for invalid inputs.
1 parent afd7420 commit 17c268c

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

tests/test_multiaddr.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,3 +1022,86 @@ def test_http_path_raw_value_access():
10221022
from urllib.parse import quote
10231023

10241024
assert quote(raw_value, safe="") == encoded_value
1025+
1026+
1027+
def test_tag_only_protocol_rejects_value_slash_syntax():
1028+
"""Test that tag-only protocols reject values using /tag/value syntax"""
1029+
tag_only_protocols = [
1030+
"webrtc",
1031+
"webrtc-direct",
1032+
"noise",
1033+
"quic",
1034+
"quic-v1",
1035+
"tls",
1036+
"http",
1037+
"https",
1038+
"ws",
1039+
"wss",
1040+
"p2p-circuit",
1041+
"webtransport",
1042+
]
1043+
1044+
for proto_name in tag_only_protocols:
1045+
# Should fail with clear error message
1046+
with pytest.raises(StringParseError) as exc_info:
1047+
Multiaddr(f"/{proto_name}/value")
1048+
assert "does not take an argument" in str(exc_info.value)
1049+
assert proto_name in str(exc_info.value)
1050+
1051+
1052+
def test_tag_only_protocol_rejects_value_equals_syntax():
1053+
"""Test that tag-only protocols reject values using /tag=value syntax"""
1054+
tag_only_protocols = [
1055+
"webrtc",
1056+
"webrtc-direct",
1057+
"noise",
1058+
"quic",
1059+
"tls",
1060+
"http",
1061+
]
1062+
1063+
for proto_name in tag_only_protocols:
1064+
# Should fail with clear error message
1065+
with pytest.raises(StringParseError) as exc_info:
1066+
Multiaddr(f"/{proto_name}=value")
1067+
assert "does not take an argument" in str(exc_info.value)
1068+
assert proto_name in str(exc_info.value)
1069+
1070+
1071+
def test_tag_only_protocol_allows_valid_combinations():
1072+
"""Test that tag-only protocols work correctly in valid combinations"""
1073+
# Single tag protocol
1074+
assert str(Multiaddr("/webrtc")) == "/webrtc"
1075+
assert str(Multiaddr("/webrtc-direct")) == "/webrtc-direct"
1076+
1077+
# Multiple tag protocols chained
1078+
assert str(Multiaddr("/webrtc/noise")) == "/webrtc/noise"
1079+
assert str(Multiaddr("/webrtc-direct/webrtc")) == "/webrtc-direct/webrtc"
1080+
1081+
# Tag protocol followed by value protocol
1082+
assert (
1083+
str(Multiaddr("/webrtc-direct/ip4/127.0.0.1"))
1084+
== "/webrtc-direct/ip4/127.0.0.1"
1085+
)
1086+
1087+
# Complex valid address
1088+
addr = "/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g"
1089+
assert str(Multiaddr(addr)) == addr
1090+
1091+
1092+
def test_tag_only_protocol_error_message_format():
1093+
"""Test that error messages for tag-only protocols are clear and helpful"""
1094+
# Test /tag/value syntax
1095+
with pytest.raises(StringParseError) as exc_info:
1096+
Multiaddr("/webrtc-direct/invalidvalue")
1097+
error_msg = str(exc_info.value)
1098+
assert "does not take an argument" in error_msg
1099+
assert "webrtc-direct" in error_msg
1100+
assert "invalidvalue" not in error_msg # Should not mention the invalid value
1101+
1102+
# Test /tag=value syntax
1103+
with pytest.raises(StringParseError) as exc_info:
1104+
Multiaddr("/webrtc=somevalue")
1105+
error_msg = str(exc_info.value)
1106+
assert "does not take an argument" in error_msg
1107+
assert "webrtc" in error_msg

0 commit comments

Comments
 (0)