|
| 1 | +import logging |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from libp2p.custom_types import TProtocol |
| 6 | +from libp2p.identity.identify.identify import ( |
| 7 | + AGENT_VERSION, |
| 8 | + ID, |
| 9 | + PROTOCOL_VERSION, |
| 10 | + _multiaddr_to_bytes, |
| 11 | + identify_handler_for, |
| 12 | + parse_identify_response, |
| 13 | +) |
| 14 | +from tests.utils.factories import host_pair_factory |
| 15 | + |
| 16 | +logger = logging.getLogger("libp2p.identity.identify-integration-test") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.trio |
| 20 | +async def test_identify_protocol_varint_format_integration(security_protocol): |
| 21 | + """Test identify protocol with varint format in real network scenario.""" |
| 22 | + async with host_pair_factory(security_protocol=security_protocol) as ( |
| 23 | + host_a, |
| 24 | + host_b, |
| 25 | + ): |
| 26 | + host_a.set_stream_handler( |
| 27 | + ID, identify_handler_for(host_a, use_varint_format=True) |
| 28 | + ) |
| 29 | + |
| 30 | + # Make identify request |
| 31 | + stream = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 32 | + response = await stream.read(8192) |
| 33 | + await stream.close() |
| 34 | + |
| 35 | + # Parse response |
| 36 | + result = parse_identify_response(response) |
| 37 | + |
| 38 | + # Verify response content |
| 39 | + assert result.agent_version == AGENT_VERSION |
| 40 | + assert result.protocol_version == PROTOCOL_VERSION |
| 41 | + assert result.public_key == host_a.get_public_key().serialize() |
| 42 | + assert result.listen_addrs == [ |
| 43 | + _multiaddr_to_bytes(addr) for addr in host_a.get_addrs() |
| 44 | + ] |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.trio |
| 48 | +async def test_identify_protocol_raw_format_integration(security_protocol): |
| 49 | + """Test identify protocol with raw format in real network scenario.""" |
| 50 | + async with host_pair_factory(security_protocol=security_protocol) as ( |
| 51 | + host_a, |
| 52 | + host_b, |
| 53 | + ): |
| 54 | + host_a.set_stream_handler( |
| 55 | + ID, identify_handler_for(host_a, use_varint_format=False) |
| 56 | + ) |
| 57 | + |
| 58 | + # Make identify request |
| 59 | + stream = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 60 | + response = await stream.read(8192) |
| 61 | + await stream.close() |
| 62 | + |
| 63 | + # Parse response |
| 64 | + result = parse_identify_response(response) |
| 65 | + |
| 66 | + # Verify response content |
| 67 | + assert result.agent_version == AGENT_VERSION |
| 68 | + assert result.protocol_version == PROTOCOL_VERSION |
| 69 | + assert result.public_key == host_a.get_public_key().serialize() |
| 70 | + assert result.listen_addrs == [ |
| 71 | + _multiaddr_to_bytes(addr) for addr in host_a.get_addrs() |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.trio |
| 76 | +async def test_identify_default_format_behavior(security_protocol): |
| 77 | + """Test identify protocol uses correct default format.""" |
| 78 | + async with host_pair_factory(security_protocol=security_protocol) as ( |
| 79 | + host_a, |
| 80 | + host_b, |
| 81 | + ): |
| 82 | + # Use default identify handler (should use varint format) |
| 83 | + host_a.set_stream_handler(ID, identify_handler_for(host_a)) |
| 84 | + |
| 85 | + # Make identify request |
| 86 | + stream = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 87 | + response = await stream.read(8192) |
| 88 | + await stream.close() |
| 89 | + |
| 90 | + # Parse response |
| 91 | + result = parse_identify_response(response) |
| 92 | + |
| 93 | + # Verify response content |
| 94 | + assert result.agent_version == AGENT_VERSION |
| 95 | + assert result.protocol_version == PROTOCOL_VERSION |
| 96 | + assert result.public_key == host_a.get_public_key().serialize() |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.trio |
| 100 | +async def test_identify_cross_format_compatibility_varint_to_raw(security_protocol): |
| 101 | + """Test varint dialer with raw listener compatibility.""" |
| 102 | + async with host_pair_factory(security_protocol=security_protocol) as ( |
| 103 | + host_a, |
| 104 | + host_b, |
| 105 | + ): |
| 106 | + # Host A uses raw format |
| 107 | + host_a.set_stream_handler( |
| 108 | + ID, identify_handler_for(host_a, use_varint_format=False) |
| 109 | + ) |
| 110 | + |
| 111 | + # Host B makes request (will automatically detect format) |
| 112 | + stream = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 113 | + response = await stream.read(8192) |
| 114 | + await stream.close() |
| 115 | + |
| 116 | + # Parse response (should work with automatic format detection) |
| 117 | + result = parse_identify_response(response) |
| 118 | + |
| 119 | + # Verify response content |
| 120 | + assert result.agent_version == AGENT_VERSION |
| 121 | + assert result.protocol_version == PROTOCOL_VERSION |
| 122 | + assert result.public_key == host_a.get_public_key().serialize() |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.trio |
| 126 | +async def test_identify_cross_format_compatibility_raw_to_varint(security_protocol): |
| 127 | + """Test raw dialer with varint listener compatibility.""" |
| 128 | + async with host_pair_factory(security_protocol=security_protocol) as ( |
| 129 | + host_a, |
| 130 | + host_b, |
| 131 | + ): |
| 132 | + # Host A uses varint format |
| 133 | + host_a.set_stream_handler( |
| 134 | + ID, identify_handler_for(host_a, use_varint_format=True) |
| 135 | + ) |
| 136 | + |
| 137 | + # Host B makes request (will automatically detect format) |
| 138 | + stream = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 139 | + response = await stream.read(8192) |
| 140 | + await stream.close() |
| 141 | + |
| 142 | + # Parse response (should work with automatic format detection) |
| 143 | + result = parse_identify_response(response) |
| 144 | + |
| 145 | + # Verify response content |
| 146 | + assert result.agent_version == AGENT_VERSION |
| 147 | + assert result.protocol_version == PROTOCOL_VERSION |
| 148 | + assert result.public_key == host_a.get_public_key().serialize() |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.trio |
| 152 | +async def test_identify_format_detection_robustness(security_protocol): |
| 153 | + """Test identify protocol format detection is robust with various message sizes.""" |
| 154 | + async with host_pair_factory(security_protocol=security_protocol) as ( |
| 155 | + host_a, |
| 156 | + host_b, |
| 157 | + ): |
| 158 | + # Test both formats with different message sizes |
| 159 | + for use_varint in [True, False]: |
| 160 | + host_a.set_stream_handler( |
| 161 | + ID, identify_handler_for(host_a, use_varint_format=use_varint) |
| 162 | + ) |
| 163 | + |
| 164 | + # Make identify request |
| 165 | + stream = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 166 | + response = await stream.read(8192) |
| 167 | + await stream.close() |
| 168 | + |
| 169 | + # Parse response |
| 170 | + result = parse_identify_response(response) |
| 171 | + |
| 172 | + # Verify response content |
| 173 | + assert result.agent_version == AGENT_VERSION |
| 174 | + assert result.protocol_version == PROTOCOL_VERSION |
| 175 | + assert result.public_key == host_a.get_public_key().serialize() |
| 176 | + |
| 177 | + |
| 178 | +@pytest.mark.trio |
| 179 | +async def test_identify_large_message_handling(security_protocol): |
| 180 | + """Test identify protocol handles large messages with many protocols.""" |
| 181 | + async with host_pair_factory(security_protocol=security_protocol) as ( |
| 182 | + host_a, |
| 183 | + host_b, |
| 184 | + ): |
| 185 | + # Add many protocols to make the message larger |
| 186 | + async def dummy_handler(stream): |
| 187 | + pass |
| 188 | + |
| 189 | + for i in range(10): |
| 190 | + host_a.set_stream_handler(TProtocol(f"/test/protocol/{i}"), dummy_handler) |
| 191 | + |
| 192 | + host_a.set_stream_handler( |
| 193 | + ID, identify_handler_for(host_a, use_varint_format=True) |
| 194 | + ) |
| 195 | + |
| 196 | + # Make identify request |
| 197 | + stream = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 198 | + response = await stream.read(8192) |
| 199 | + await stream.close() |
| 200 | + |
| 201 | + # Parse response |
| 202 | + result = parse_identify_response(response) |
| 203 | + |
| 204 | + # Verify response content |
| 205 | + assert result.agent_version == AGENT_VERSION |
| 206 | + assert result.protocol_version == PROTOCOL_VERSION |
| 207 | + assert result.public_key == host_a.get_public_key().serialize() |
| 208 | + |
| 209 | + |
| 210 | +@pytest.mark.trio |
| 211 | +async def test_identify_message_equivalence_real_network(security_protocol): |
| 212 | + """Test that both formats produce equivalent messages in real network.""" |
| 213 | + async with host_pair_factory(security_protocol=security_protocol) as ( |
| 214 | + host_a, |
| 215 | + host_b, |
| 216 | + ): |
| 217 | + # Test varint format |
| 218 | + host_a.set_stream_handler( |
| 219 | + ID, identify_handler_for(host_a, use_varint_format=True) |
| 220 | + ) |
| 221 | + stream_varint = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 222 | + response_varint = await stream_varint.read(8192) |
| 223 | + await stream_varint.close() |
| 224 | + |
| 225 | + # Test raw format |
| 226 | + host_a.set_stream_handler( |
| 227 | + ID, identify_handler_for(host_a, use_varint_format=False) |
| 228 | + ) |
| 229 | + stream_raw = await host_b.new_stream(host_a.get_id(), (ID,)) |
| 230 | + response_raw = await stream_raw.read(8192) |
| 231 | + await stream_raw.close() |
| 232 | + |
| 233 | + # Parse both responses |
| 234 | + result_varint = parse_identify_response(response_varint) |
| 235 | + result_raw = parse_identify_response(response_raw) |
| 236 | + |
| 237 | + # Both should produce identical parsed results |
| 238 | + assert result_varint.agent_version == result_raw.agent_version |
| 239 | + assert result_varint.protocol_version == result_raw.protocol_version |
| 240 | + assert result_varint.public_key == result_raw.public_key |
| 241 | + assert result_varint.listen_addrs == result_raw.listen_addrs |
0 commit comments