Skip to content

Commit e243375

Browse files
committed
Add codec for ip6zone, and tests
1 parent 5d46d92 commit e243375

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

multiaddr/codecs/ip6zone.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("empty ip6zone")
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', 'ip6zone'),
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

0 commit comments

Comments
 (0)