|
18 | 18 | from zeroconf import DNSAddress, RecordUpdate, const |
19 | 19 | from zeroconf._protocol.outgoing import DNSOutgoing |
20 | 20 | from zeroconf._services import info |
21 | | -from zeroconf._services.info import ServiceInfo |
| 21 | +from zeroconf._services.info import ServiceInfo, _has_more_scope_info |
| 22 | +from zeroconf._utils.ipaddress import ZeroconfIPv4Address |
22 | 23 | from zeroconf._utils.net import IPVersion |
23 | 24 | from zeroconf.asyncio import AsyncZeroconf |
24 | 25 |
|
@@ -790,6 +791,222 @@ def test_scoped_addresses_from_cache(): |
790 | 791 | zeroconf.close() |
791 | 792 |
|
792 | 793 |
|
| 794 | +def test_scoped_address_preferred_when_unscoped_arrives_first_in_cache(): |
| 795 | + """A scoped AAAA in the cache wins over an earlier unscoped copy of the same address.""" |
| 796 | + type_ = "_http._tcp.local." |
| 797 | + registration_name = f"scoped-first.{type_}" |
| 798 | + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) |
| 799 | + host = "scoped-first.local." |
| 800 | + packed = socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6") |
| 801 | + |
| 802 | + zeroconf.cache.async_add_records( |
| 803 | + [ |
| 804 | + r.DNSPointer( |
| 805 | + type_, |
| 806 | + const._TYPE_PTR, |
| 807 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 808 | + 120, |
| 809 | + registration_name, |
| 810 | + ), |
| 811 | + r.DNSService( |
| 812 | + registration_name, |
| 813 | + const._TYPE_SRV, |
| 814 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 815 | + 120, |
| 816 | + 0, |
| 817 | + 0, |
| 818 | + 80, |
| 819 | + host, |
| 820 | + ), |
| 821 | + r.DNSAddress( |
| 822 | + host, |
| 823 | + const._TYPE_AAAA, |
| 824 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 825 | + 120, |
| 826 | + packed, |
| 827 | + scope_id=None, |
| 828 | + ), |
| 829 | + r.DNSAddress( |
| 830 | + host, |
| 831 | + const._TYPE_AAAA, |
| 832 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 833 | + 120, |
| 834 | + packed, |
| 835 | + scope_id=7, |
| 836 | + ), |
| 837 | + ] |
| 838 | + ) |
| 839 | + |
| 840 | + info = ServiceInfo(type_, registration_name) |
| 841 | + info.load_from_cache(zeroconf) |
| 842 | + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6%7"] |
| 843 | + assert info.ip_addresses_by_version(r.IPVersion.V6Only) == [ip_address("fe80::52e:c2f2:bc5f:e9c6%7")] |
| 844 | + zeroconf.close() |
| 845 | + |
| 846 | + |
| 847 | +@pytest.mark.asyncio |
| 848 | +async def test_scoped_address_replaces_unscoped_in_live_update(): |
| 849 | + """A late-arriving scoped AAAA replaces a previously-stored unscoped variant.""" |
| 850 | + type_ = "_http._tcp.local." |
| 851 | + registration_name = f"scoped-live.{type_}" |
| 852 | + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) |
| 853 | + host = "scoped-live.local." |
| 854 | + packed = socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6") |
| 855 | + |
| 856 | + info = ServiceInfo(type_, registration_name, server=host) |
| 857 | + now = r.current_time_millis() |
| 858 | + unscoped = r.DNSAddress( |
| 859 | + host, |
| 860 | + const._TYPE_AAAA, |
| 861 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 862 | + 120, |
| 863 | + packed, |
| 864 | + scope_id=None, |
| 865 | + ) |
| 866 | + scoped = r.DNSAddress( |
| 867 | + host, |
| 868 | + const._TYPE_AAAA, |
| 869 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 870 | + 120, |
| 871 | + packed, |
| 872 | + scope_id=9, |
| 873 | + ) |
| 874 | + info.async_update_records(aiozc.zeroconf, now, [RecordUpdate(unscoped, None)]) |
| 875 | + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6"] |
| 876 | + info.async_update_records(aiozc.zeroconf, now, [RecordUpdate(scoped, unscoped)]) |
| 877 | + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6%9"] |
| 878 | + await aiozc.async_close() |
| 879 | + |
| 880 | + |
| 881 | +def test_scoped_address_kept_when_unscoped_arrives_after_in_cache(): |
| 882 | + """Scoped AAAA seen first in iteration keeps its scope when an unscoped duplicate follows.""" |
| 883 | + type_ = "_http._tcp.local." |
| 884 | + registration_name = f"scoped-after.{type_}" |
| 885 | + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) |
| 886 | + host = "scoped-after.local." |
| 887 | + packed = socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6") |
| 888 | + |
| 889 | + zeroconf.cache.async_add_records( |
| 890 | + [ |
| 891 | + r.DNSPointer( |
| 892 | + type_, |
| 893 | + const._TYPE_PTR, |
| 894 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 895 | + 120, |
| 896 | + registration_name, |
| 897 | + ), |
| 898 | + r.DNSService( |
| 899 | + registration_name, |
| 900 | + const._TYPE_SRV, |
| 901 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 902 | + 120, |
| 903 | + 0, |
| 904 | + 0, |
| 905 | + 80, |
| 906 | + host, |
| 907 | + ), |
| 908 | + r.DNSAddress( |
| 909 | + host, |
| 910 | + const._TYPE_AAAA, |
| 911 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 912 | + 120, |
| 913 | + packed, |
| 914 | + scope_id=5, |
| 915 | + ), |
| 916 | + r.DNSAddress( |
| 917 | + host, |
| 918 | + const._TYPE_AAAA, |
| 919 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 920 | + 120, |
| 921 | + packed, |
| 922 | + scope_id=None, |
| 923 | + ), |
| 924 | + ] |
| 925 | + ) |
| 926 | + |
| 927 | + info = ServiceInfo(type_, registration_name) |
| 928 | + info.load_from_cache(zeroconf) |
| 929 | + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6%5"] |
| 930 | + assert info.ip_addresses_by_version(r.IPVersion.V6Only) == [ip_address("fe80::52e:c2f2:bc5f:e9c6%5")] |
| 931 | + zeroconf.close() |
| 932 | + |
| 933 | + |
| 934 | +def test_has_more_scope_info_returns_false_for_ipv4(): |
| 935 | + """The scope_id helper short-circuits for IPv4 since A records carry no scope.""" |
| 936 | + ip4 = ZeroconfIPv4Address("192.0.2.1") |
| 937 | + assert _has_more_scope_info(ip4, ip4) is False |
| 938 | + |
| 939 | + |
| 940 | +def test_scope_upgrade_preserves_lifo_recency_order(): |
| 941 | + """A scoped AAAA that upgrades an earlier entry becomes the most recent in LIFO order.""" |
| 942 | + type_ = "_http._tcp.local." |
| 943 | + registration_name = f"reorder.{type_}" |
| 944 | + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) |
| 945 | + host = "reorder.local." |
| 946 | + link_local = socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6") |
| 947 | + ula = socket.inet_pton(socket.AF_INET6, "fdc8:d776:7cca:46ed::2") |
| 948 | + |
| 949 | + zeroconf.cache.async_add_records( |
| 950 | + [ |
| 951 | + r.DNSPointer( |
| 952 | + type_, |
| 953 | + const._TYPE_PTR, |
| 954 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 955 | + 120, |
| 956 | + registration_name, |
| 957 | + ), |
| 958 | + r.DNSService( |
| 959 | + registration_name, |
| 960 | + const._TYPE_SRV, |
| 961 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 962 | + 120, |
| 963 | + 0, |
| 964 | + 0, |
| 965 | + 80, |
| 966 | + host, |
| 967 | + ), |
| 968 | + r.DNSAddress( |
| 969 | + host, |
| 970 | + const._TYPE_AAAA, |
| 971 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 972 | + 120, |
| 973 | + link_local, |
| 974 | + scope_id=None, |
| 975 | + ), |
| 976 | + r.DNSAddress( |
| 977 | + host, |
| 978 | + const._TYPE_AAAA, |
| 979 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 980 | + 120, |
| 981 | + ula, |
| 982 | + scope_id=None, |
| 983 | + ), |
| 984 | + r.DNSAddress( |
| 985 | + host, |
| 986 | + const._TYPE_AAAA, |
| 987 | + const._CLASS_IN | const._CLASS_UNIQUE, |
| 988 | + 120, |
| 989 | + link_local, |
| 990 | + scope_id=11, |
| 991 | + ), |
| 992 | + ] |
| 993 | + ) |
| 994 | + |
| 995 | + info = ServiceInfo(type_, registration_name) |
| 996 | + info.load_from_cache(zeroconf) |
| 997 | + # The scoped link-local upgrade is the most recent observation, so it |
| 998 | + # has to come first in LIFO order, ahead of the earlier unrelated ULA. |
| 999 | + assert info.ip_addresses_by_version(r.IPVersion.V6Only) == [ |
| 1000 | + ip_address("fe80::52e:c2f2:bc5f:e9c6%11"), |
| 1001 | + ip_address("fdc8:d776:7cca:46ed::2"), |
| 1002 | + ] |
| 1003 | + assert info.parsed_scoped_addresses() == [ |
| 1004 | + "fe80::52e:c2f2:bc5f:e9c6%11", |
| 1005 | + "fdc8:d776:7cca:46ed::2", |
| 1006 | + ] |
| 1007 | + zeroconf.close() |
| 1008 | + |
| 1009 | + |
793 | 1010 | # This test uses asyncio because it needs to access the cache directly |
794 | 1011 | # which is not threadsafe |
795 | 1012 | @pytest.mark.asyncio |
|
0 commit comments