-
Notifications
You must be signed in to change notification settings - Fork 33
Add codec unit tests #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
032bf99
d4b4ec7
6c4bcc7
526d0da
46e921b
312d74f
0b96d1e
53a0a8d
e82e907
80b9848
886fb1b
8e98369
d93a17d
ff1065a
fd28d34
1da6127
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import pytest | ||
|
|
||
| from multiaddr.codec import address_bytes_to_string | ||
| from multiaddr.codec import address_string_to_bytes | ||
| from multiaddr.codec import bytes_split | ||
| from multiaddr.codec import bytes_to_string | ||
| from multiaddr.codec import size_for_addr | ||
| from multiaddr.codec import string_to_bytes | ||
|
|
||
| from multiaddr.protocols import _names_to_protocols | ||
| from multiaddr.protocols import Protocol | ||
|
|
||
| # These test values were generated by running them | ||
| # through the go implementation of multiaddr. | ||
| # https://github.com/jbenet/multiaddr | ||
| ADDR_BYTES_MAP_STR_TEST_DATA = [ | ||
| (_names_to_protocols['ip4'], b'0a0b0c0d', '10.11.12.13'), | ||
| (_names_to_protocols['ip6'], b'1aa12bb23cc34dd45ee56ff67ab78ac8', | ||
| '1aa1:2bb2:3cc3:4dd4:5ee5:6ff6:7ab7:8ac8'), | ||
| (_names_to_protocols['tcp'], b'abcd', '43981'), | ||
| (_names_to_protocols['onion'], b'9a18087306369043091f04d2', | ||
| 'timaq4ygg2iegci7:1234'), | ||
| (_names_to_protocols['ipfs'], | ||
| b'221220d52ebb89d85b02a284948203a62ff28389c57c9f42beec4ec20db76a68911c0b', | ||
| 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'), | ||
| ] | ||
|
|
||
| BYTES_MAP_STR_TEST_DATA = [ | ||
| ("/ip4/127.0.0.1/udp/1234", b'047f0000011104d2'), | ||
| ("/ip4/127.0.0.1/tcp/4321", b'047f0000010610e1'), | ||
| ("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", | ||
| b'047f0000011104d2047f0000010610e1') | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("proto, buf, expected", [ | ||
| (_names_to_protocols['https'], b'\x01\x02\x03', 0), | ||
| (_names_to_protocols['ip4'], b'\x01\x02\x03', 4), | ||
| (_names_to_protocols['ipfs'], b'\x40\x50\x60\x51', 65), | ||
| ]) | ||
| def test_size_for_addr(proto, buf, expected): | ||
| assert size_for_addr(proto, buf) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("buf, expected", [ | ||
| # "/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321" | ||
| (b'047f0000011104d2047f0000010610e1', | ||
| [b'\x04\x7f\x00\x00\x01', | ||
| b'\x11\x04\xd2', | ||
| b'\x04\x7f\x00\x00\x01', | ||
| b'\x06\x10\xe1']), | ||
| ]) | ||
| def test_bytes_split(buf, expected): | ||
| assert bytes_split(buf) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("proto, buf, expected", ADDR_BYTES_MAP_STR_TEST_DATA) | ||
| def test_address_bytes_to_string(proto, buf, expected): | ||
| assert address_bytes_to_string(proto, buf) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("proto, expected, string", | ||
| ADDR_BYTES_MAP_STR_TEST_DATA) | ||
| def test_address_string_to_bytes(proto, string, expected): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think pytest will let you use a method to generate the parameters which will let you avoid duplicating these.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have any samples you can point me to? I didn't know this was possible.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just need to use a variable and pass that into the parametrize decorator. Duh. |
||
| assert address_string_to_bytes(proto, string) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("string, buf", BYTES_MAP_STR_TEST_DATA) | ||
| def test_string_to_bytes(string, buf): | ||
| assert string_to_bytes(string) == buf | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("string, buf", BYTES_MAP_STR_TEST_DATA) | ||
| def test_bytes_to_string(string, buf): | ||
| assert bytes_to_string(buf) == string | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("string", [ | ||
| 'test', | ||
| '/ip4/' | ||
| ]) | ||
| def test_string_to_bytes_value_error(string): | ||
| with pytest.raises(ValueError): | ||
| string_to_bytes(string) | ||
|
|
||
|
|
||
| class DummyProtocol(Protocol): | ||
| def __init__(self, code, size, name, vcode): | ||
| self.code = code | ||
| self.size = size | ||
| self.name = name | ||
| self.vcode = vcode | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("proto, address", [ | ||
| (DummyProtocol(234, 32, 'test', b'123'), '1.2.3.4'), | ||
| (_names_to_protocols['ip4'], '1124.2.3'), | ||
| (_names_to_protocols['ip6'], '123:31224444'), | ||
| (_names_to_protocols['tcp'], 'a'), | ||
| (_names_to_protocols['tcp'], '100000'), | ||
| (_names_to_protocols['onion'], '100000'), | ||
| (_names_to_protocols['onion'], '1234567890123456:0'), | ||
| (_names_to_protocols['onion'], 'timaq4ygg2iegci7:a'), | ||
| (_names_to_protocols['onion'], 'timaq4ygg2iegci7:0'), | ||
| (_names_to_protocols['onion'], 'timaq4ygg2iegci7:71234'), | ||
| (_names_to_protocols['ipfs'], '15230d52ebb89d85b02a284948203a'), | ||
| ]) | ||
| def test_address_string_to_bytes_value_error(proto, address): | ||
| with pytest.raises(ValueError): | ||
| address_string_to_bytes(proto, address) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("proto, buf", [ | ||
| (DummyProtocol(234, 32, 'test', b'123'), b'0a0b0c0d'), | ||
| (_names_to_protocols['ipfs'], b'15230d52ebb89d85b02a284948203a') | ||
| ]) | ||
| def test_address_bytes_to_string_value_error(proto, buf): | ||
| with pytest.raises(ValueError): | ||
| address_bytes_to_string(proto, buf) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another nit which flake8 will point out: imports should be sorted alphabetically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I don't get this warning from flake8. I don't think I've changed the default config.