Skip to content

Commit 1306e2a

Browse files
committed
Add tests related to ENSIP-10 implementation:
- Test the new ``ens_encode_name()`` from the ``ens.utils`` module. - Test the new ``ENS.parent()`` method to extract a parent from an ENS name. Unrelated to ENSIP-10 but served as a helpful abstraction to better implement it: - Test the new ``Contract.decodeABI()`` method from the
1 parent f3b7f32 commit 1306e2a

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

tests/ens/test_utils.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,100 @@
1+
import pytest
2+
3+
from eth_utils import (
4+
to_bytes,
5+
)
16

27
from ens.utils import (
8+
ens_encode_name,
39
init_web3,
410
)
11+
from web3.exceptions import (
12+
ValidationError,
13+
)
514

615

716
def test_init_adds_middlewares():
817
w3 = init_web3()
918
middlewares = map(str, w3.manager.middleware_onion)
1019
assert 'stalecheck_middleware' in next(middlewares)
20+
21+
22+
@pytest.mark.parametrize(
23+
'name,expected',
24+
(
25+
# test some allowed cases
26+
('tester.eth', b'\x06tester\x03eth\x00'),
27+
(
28+
'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p',
29+
b'\x01a\x01b\x01c\x01d\x01e\x01f\x01g\x01h\x01i\x01j\x01k\x01l\x01m\x01n\x01o\x01p\x00'
30+
),
31+
('1.2.3.4.5.6.7.8.9.10', b'\x011\x012\x013\x014\x015\x016\x017\x018\x019\x0210\x00'),
32+
('abc.123.def-456.eth', b'\x03abc\x03123\x07def-456\x03eth\x00'),
33+
('abc.123.def-456.eth', b'\x03abc\x03123\x07def-456\x03eth\x00'),
34+
('nhéééééé.eth', b'\x0enh\xc3\xa9\xc3\xa9\xc3\xa9\xc3\xa9\xc3\xa9\xc3\xa9\x03eth\x00'),
35+
('🌈rainbow.eth', b'\x0b\xf0\x9f\x8c\x88rainbow\x03eth\x00'),
36+
('🐔🐔.tk', b'\x08\xf0\x9f\x90\x94\xf0\x9f\x90\x94\x02tk\x00'),
37+
38+
# test that label length may be less than 64
39+
(f"{'a' * 63}.b", b'?' + (b'a' * 63) + b'\x01b\x00'),
40+
(f"a.{'b'* 63}", b'\x01a' + b'?' + (b'b' * 63) + b'\x00'),
41+
(f"abc-123.{'b'* 63}", b'\x07abc-123' + b'?' + b'b' * 63 + b'\x00'),
42+
)
43+
)
44+
def test_ens_encode_name(name, expected):
45+
assert ens_encode_name(name) == expected
46+
47+
48+
@pytest.mark.parametrize(
49+
'name,expected',
50+
(
51+
(
52+
f"{'a' * 63}.{'b' * 63}.{'c' * 63}.{'d' * 63}.{'e' * 63}.{'f' * 63}.{'g' * 63}",
53+
b''.join([b'?' + to_bytes(text=label) * 63 for label in 'abcdefg']) + b'\x00'
54+
),
55+
(
56+
f"{'a-1' * 21}.{'b-2' * 21}.{'c-3' * 21}.{'d-4' * 21}.{'e-5' * 21}.{'f-6' * 21}",
57+
b''.join([
58+
b'?' + to_bytes(text=label) * 21 for label in [
59+
'a-1', 'b-2', 'c-3', 'd-4', 'e-5', 'f-6',
60+
]
61+
]) + b'\x00'
62+
),
63+
)
64+
)
65+
def test_ens_encode_name_validating_total_encoded_name_size(name, expected):
66+
# This test is important because dns encoding technically limits the total encoded domain name
67+
# size to 255. ENSIP-10 expects the name to be DNS encoded with one of the exceptions
68+
# being that the total encoded size can be any length.
69+
ens_encoded = ens_encode_name(name)
70+
assert len(ens_encoded) > 255
71+
assert ens_encoded == expected
72+
73+
74+
@pytest.mark.parametrize('empty_name', ('', '.'))
75+
def test_ens_encode_name_returns_single_zero_byte_for_empty_name(empty_name):
76+
assert ens_encode_name(empty_name) == b'\00'
77+
78+
79+
@pytest.mark.parametrize(
80+
'name,invalid_label_index',
81+
(
82+
('a' * 64, 0),
83+
(f"{'a' * 64}.b", 0),
84+
(f"a.{'b-1' * 21}x", 1),
85+
(f"{'a' * 64}.{'1' * 63}.{'b' * 63}", 0),
86+
(f"{'a' * 63}.{'1' * 64}.{'b' * 63}", 1),
87+
(f"{'a' * 63}.{'1' * 63}.{'b' * 64}", 2),
88+
)
89+
)
90+
def test_ens_encode_name_raises_ValidationError_on_label_lengths_over_63(name, invalid_label_index):
91+
with pytest.raises(ValidationError, match=f'Label at position {invalid_label_index} too long'):
92+
ens_encode_name(name)
93+
94+
95+
def test_ens_encode_name_normalizes_name_before_encoding():
96+
assert ens_encode_name('Öbb.at') == ens_encode_name('öbb.at')
97+
assert ens_encode_name('nhÉéÉéÉé.eth') == ens_encode_name('nhéééééé.eth')
98+
assert ens_encode_name('TESTER.eth') == ens_encode_name('tester.eth')
99+
assert ens_encode_name('test\u200btest.com') == ens_encode_name('testtest.com')
100+
assert ens_encode_name("O\u0308bb.at") == ens_encode_name("öbb.at")

0 commit comments

Comments
 (0)