Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/zeroconf/_services/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,15 @@ def _unpack_text_into_properties(self) -> None:
length = text[index]
index += 1
key_value = text[index : index + length]
key_sep_value = key_value.partition(b"=")
key = key_sep_value[0]
key, sep, value = key_value.partition(b"=")
if key not in properties:
properties[key] = key_sep_value[2] or None
# RFC 6763 section 6.4 distinguishes a key with no '=' (a
# boolean attribute: present, no value) from `key=` (present
# with an empty value). Testing the separator rather than the
# value keeps them apart; `value or None` collapsed both to
# None, so re-serialising a received `key=` emitted a bare
# `key`.
properties[key] = value if sep else None
index += length

self._properties = properties
Expand Down
63 changes: 63 additions & 0 deletions tests/services/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,69 @@ def test_service_info_duplicate_properties_txt_records(self):
assert info.properties[b"ci"] == b"2"
zc.close()

def test_service_info_empty_value_txt_record(self):
"""Verify `key=` stays distinct from a valueless `key`.

RFC 6763 section 6.4 defines a key with no '=' as a boolean attribute
(present, no value), and `key=` as present with an empty value. If both
decode to None they are indistinguishable, and re-encoding a received
`key=` emits a bare `key` -- a different record than the one received.
"""
zc = r.Zeroconf(interfaces=["127.0.0.1"])
service_name = "name._type._tcp.local."
service_type = "_type._tcp.local."
service_server = "ash-1.local."
text = b"\x03rm=\x02rs\x05ve=05"
info = ServiceInfo(
service_type,
service_name,
22,
0,
0,
{"path": "/~paulsm/"},
service_server,
addresses=[socket.inet_aton("10.0.1.2")],
)
info.async_update_records(
zc,
r.current_time_millis(),
[
r.RecordUpdate(
r.DNSText(
service_name,
const._TYPE_TXT,
const._CLASS_IN | const._CLASS_UNIQUE,
120,
text,
),
None,
)
],
)
assert info.properties[b"rm"] == b""
assert info.properties[b"rs"] is None
assert info.properties[b"ve"] == b"05"

# The string-facing API must preserve the distinction too.
assert info.decoded_properties["rm"] == ""
assert info.decoded_properties["rs"] is None
assert info.decoded_properties["ve"] == "05"

# Re-encoding the decoded properties must reproduce the received rdata.
assert (
ServiceInfo(
service_type,
service_name,
22,
0,
0,
info.properties,
service_server,
).text
== text
)
zc.close()


def test_multiple_addresses():
type_ = "_http._tcp.local."
Expand Down
Loading