Skip to content

Commit

Permalink
remove fastrand dependency from dev-test (#252)
Browse files Browse the repository at this point in the history
Also refactored a bit read_name without changing the logic.
  • Loading branch information
keepsimple1 authored Sep 6, 2024
1 parent dff1596 commit f0c4c27
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ socket2 = { version = "0.5.5", features = ["all"] } # socket APIs

[dev-dependencies]
env_logger = { version = "= 0.10.2", default-features = false, features= ["humantime"] }
fastrand = "1.8"
rand ="0.8"
test-log = "= 0.2.14"
test-log-macros = "= 0.2.14"
12 changes: 7 additions & 5 deletions src/dns_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub const FLAGS_TC: u16 = 0x0200;

pub(crate) type DnsRecordBox = Box<dyn DnsRecordExt>;

const U16_SIZE: usize = 2;

#[inline]
pub const fn ip_address_to_type(address: &IpAddr) -> u16 {
match address {
Expand Down Expand Up @@ -1420,7 +1422,6 @@ impl DnsIncoming {
}

fn read_u16(&mut self) -> Result<u16> {
const U16_SIZE: usize = 2;
let slice = &self.data[self.offset..];
if slice.len() < U16_SIZE {
return Err(Error::Msg(format!(
Expand Down Expand Up @@ -1560,13 +1561,14 @@ impl DnsIncoming {
0xC0 => {
// Message compression.
// See https://datatracker.ietf.org/doc/html/rfc1035#section-4.1.4
if data[offset..].len() < 2 {
let slice = &data[offset..];
if slice.len() < U16_SIZE {
return Err(Error::Msg(format!(
"read_u16: slice len is only {}",
data.len()
"read_name: u16 slice len is only {}",
slice.len()
)));
}
let pointer = (u16_from_be_slice(&data[offset..]) ^ 0xC000) as usize;
let pointer = (u16_from_be_slice(slice) ^ 0xC000) as usize;
if pointer >= offset {
return Err(Error::Msg(format!(
"Bad name with invalid message compression: pointer {} offset {} data (so far): {:x?}",
Expand Down
4 changes: 3 additions & 1 deletion tests/mdns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use mdns_sd::{
DaemonEvent, DaemonStatus, HostnameResolutionEvent, IfKind, IntoTxtProperties, ServiceDaemon,
ServiceEvent, ServiceInfo, UnregisterStatus,
};
use rand::Rng;
use std::collections::{HashMap, HashSet};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::thread::sleep;
Expand Down Expand Up @@ -957,7 +958,8 @@ fn instance_name_two_dots() {

fn my_ip_interfaces() -> Vec<Interface> {
// Use a random port for binding test.
let test_port = fastrand::u16(8000u16..9000u16);
let mut rng = rand::thread_rng();
let test_port = rng.gen_range(8000u16..9000u16);

if_addrs::get_if_addrs()
.unwrap_or_default()
Expand Down

0 comments on commit f0c4c27

Please sign in to comment.