Skip to content

Commit eacf455

Browse files
authored
Merge pull request #43 from mhchia/fix/ip6zone
Add codec for ip6zone, and tests
2 parents 5d46d92 + 2c391e0 commit eacf455

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

multiaddr/codecs/utf8.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from __future__ import absolute_import
2+
3+
from . import LENGTH_PREFIXED_VAR_SIZE
4+
5+
6+
SIZE = LENGTH_PREFIXED_VAR_SIZE
7+
IS_PATH = False
8+
9+
10+
def to_bytes(proto, string):
11+
if len(string) == 0:
12+
raise ValueError("{0} value must not be empty".format(proto.name))
13+
return string.encode('utf-8')
14+
15+
16+
def to_string(proto, buf):
17+
if len(buf) == 0:
18+
raise ValueError("invalid length (should be > 0)")
19+
return buf.decode('utf-8')

multiaddr/protocols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __repr__(self):
124124
Protocol(P_UDP, 'udp', 'uint16be'),
125125
Protocol(P_DCCP, 'dccp', 'uint16be'),
126126
Protocol(P_IP6, 'ip6', 'ip6'),
127-
Protocol(P_IP6ZONE, 'ip6zone', '?'),
127+
Protocol(P_IP6ZONE, 'ip6zone', 'utf8'),
128128
Protocol(P_DNS, 'dns', 'idna'),
129129
Protocol(P_DNS4, 'dns4', 'idna'),
130130
Protocol(P_DNS6, 'dns6', 'idna'),

tests/test_multiaddr.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"/ip4/::1",
3030
"/ip4/fdpsofodsajfdoisa",
3131
"/ip6",
32+
"/ip6zone",
33+
"/ip6zone/",
34+
"/ip6zone//ip6/fe80::1",
3235
"/udp",
3336
"/tcp",
3437
"/sctp",
@@ -67,6 +70,9 @@ def test_invalid(addr_str):
6770
"/ip4/0.0.0.0",
6871
"/ip6/::1",
6972
"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21",
73+
"/ip6zone/x/ip6/fe80::1",
74+
"/ip6zone/x%y/ip6/fe80::1",
75+
"/ip6zone/x%y/ip6/::",
7076
"/onion/timaq4ygg2iegci7:1234",
7177
"/onion/timaq4ygg2iegci7:80/http",
7278
"/udp/0",
@@ -330,3 +336,14 @@ def test_decapsulate():
330336
def test__repr():
331337
a = Multiaddr("/ip4/127.0.0.1/udp/1234")
332338
assert(repr(a) == "<Multiaddr %s>" % str(a))
339+
340+
341+
def test_zone():
342+
ip6_string = "/ip6zone/eth0/ip6/::1"
343+
ip6_bytes = b"\x2a\x04eth0\x29\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"
344+
345+
maddr_from_str = Multiaddr(ip6_string)
346+
assert maddr_from_str.to_bytes() == ip6_bytes
347+
348+
maddr_from_bytes = Multiaddr(ip6_bytes)
349+
assert str(maddr_from_bytes) == ip6_string

tests/test_transforms.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# These test values were generated by running them
2222
# through the go implementation of multiaddr.
23-
# https://github.com/jbenet/multiaddr
23+
# https://github.com/multiformats/go-multiaddr
2424
ADDR_BYTES_MAP_STR_TEST_DATA = [
2525
(_names_to_protocols['ip4'], b'\x0a\x0b\x0c\x0d', '10.11.12.13'),
2626
(_names_to_protocols['ip6'],
@@ -148,6 +148,7 @@ def test_bytes_to_string_value_error(protocol_extension, bytes):
148148
(_names_to_protocols['onion'], 'timaq4ygg2iegci7:0'),
149149
(_names_to_protocols['onion'], 'timaq4ygg2iegci7:71234'),
150150
(_names_to_protocols['p2p'], '15230d52ebb89d85b02a284948203a'),
151+
(_names_to_protocols['ip6zone'], ""),
151152
])
152153
def test_codec_to_bytes_value_error(proto, address):
153154
# Codecs themselves may raise any exception type – it will then be converted
@@ -157,7 +158,8 @@ def test_codec_to_bytes_value_error(proto, address):
157158

158159

159160
@pytest.mark.parametrize("proto, buf", [
160-
(_names_to_protocols['tcp'], b'\xff\xff\xff\xff')
161+
(_names_to_protocols['tcp'], b'\xff\xff\xff\xff'),
162+
(_names_to_protocols['ip6zone'], b""),
161163
])
162164
def test_codec_to_string_value_error(proto, buf):
163165
# Codecs themselves may raise any exception type – it will then be converted

0 commit comments

Comments
 (0)