Skip to content

Commit

Permalink
add a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 committed Feb 9, 2025
1 parent b18c748 commit 1951160
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,14 @@ impl TxtProperties {
self.get(key).map(|x| x.val_str())
}

/// Returns properties as a hashmap, where the keys are the properties keys.
/// Consumes properties and returns a hashmap, where the keys are the properties keys.
///
/// If a property value is empty, return an empty string (because RFC 6763 allows empty values).
/// If a property value is non-empty but not valid UTF-8, skip the property and log a message.
pub fn into_property_map_str(self) -> HashMap<String, String> {
self.properties
.into_iter()
.filter_map(|property| {
// If the value is empty, return an empty string (because RFC 6763 allows empty values).
// If the value is non-empty but not valid UTF-8, skip the property and log a message.
let val_string = property.val.map_or(Some(String::new()), |val| {
String::from_utf8(val)
.map_err(|e| {
Expand Down Expand Up @@ -700,6 +701,12 @@ where
}
}

impl IntoTxtProperties for Vec<TxtProperty> {
fn into_txt_properties(self) -> TxtProperties {
TxtProperties { properties: self }
}
}

// Convert from properties key/value pairs to DNS TXT record content
fn encode_txt<'a>(properties: impl Iterator<Item = &'a TxtProperty>) -> Vec<u8> {
let mut bytes = Vec::new();
Expand Down
25 changes: 24 additions & 1 deletion tests/mdns_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use if_addrs::{IfAddr, Interface};
use mdns_sd::{
DaemonEvent, DaemonStatus, HostnameResolutionEvent, IfKind, IntoTxtProperties, ServiceDaemon,
ServiceEvent, ServiceInfo, UnregisterStatus,
ServiceEvent, ServiceInfo, TxtProperty, UnregisterStatus,
};
use std::collections::{HashMap, HashSet};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
Expand Down Expand Up @@ -458,6 +458,29 @@ fn service_txt_properties_key_ascii() {
assert!(my_service.is_ok());
}

#[test]
fn test_txt_properties_into_hashmap_str() {
// Test valid UTF-8 properties
let properties = vec![("key1", "val1"), ("key2", "val2")].into_txt_properties();
let property_map = properties.into_property_map_str();
println!("property_map: {:?}", property_map);
assert_eq!(property_map.len(), 2);
assert_eq!(property_map.get("key1"), Some(&"val1".to_string()));
assert_eq!(property_map.get("key2"), Some(&"val2".to_string()));

// Test property with no value and property with invalid UTF-8
let invalid_vec: Vec<u8> = vec![200, 200]; // Invalid UTF-8 bytes
let prop1 = TxtProperty::from("key1");
let prop2 = TxtProperty::from(("key2", invalid_vec.as_slice()));
let properties = vec![prop1, prop2].into_txt_properties();
let property_map = properties.into_property_map_str();

// Property with no value should map to empty string
// Property with invalid UTF-8 should be skipped
assert_eq!(property_map.get("key1"), Some(&"".to_string()));
assert_eq!(property_map.len(), 1);
}

#[test]
fn test_into_txt_properties() {
// Verify (&str, String) tuple is supported.
Expand Down

0 comments on commit 1951160

Please sign in to comment.