Skip to content

Commit

Permalink
dev-test: back to using fastrand crate (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 authored Sep 12, 2024
1 parent 59c9ea2 commit c0ebc5c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ socket2 = { version = "0.5.5", features = ["all"] } # socket APIs

[dev-dependencies]
env_logger = { version = "= 0.10.2", default-features = false, features= ["humantime"] }
fastrand = "2.1"
humantime = "2.1"
rand ="0.8"
test-log = "= 0.2.14"
test-log-macros = "= 0.2.14"
14 changes: 5 additions & 9 deletions src/dns_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,8 @@ const fn get_expiration_time(created: u64, ttl: u32, percent: u32) -> u64 {

#[cfg(test)]
mod tests {
use std::iter::repeat_with;

use super::{
current_time_millis, get_expiration_time, DnsIncoming, DnsNSec, DnsOutgoing, DnsPointer,
DnsRecordExt, DnsSrv, DnsTxt, CLASS_CACHE_FLUSH, CLASS_IN, FLAGS_QR_QUERY,
Expand Down Expand Up @@ -1770,24 +1772,18 @@ mod tests {

#[test]
fn test_rr_rand_data_error() {
use rand::prelude::*;

const DATA_LEN_MAX: usize = 2048;
const TEST_TIMES: usize = 100000;

let mut rng = rand::thread_rng();
let mut rand_data: Vec<u8> = Vec::with_capacity(DATA_LEN_MAX);

for _ in 0..TEST_TIMES {
// Generate a random length of data
let data_len: usize = rng.gen_range(0..DATA_LEN_MAX);
rand_data.resize(data_len, 0);
let data_len = fastrand::usize(0..DATA_LEN_MAX);

// Generate random data
rng.fill(rand_data.as_mut_slice());
let rand_data: Vec<u8> = repeat_with(|| fastrand::u8(..)).take(data_len).collect();

// Decode rand data, it should not panic
let _ = DnsIncoming::new(rand_data.clone());
let _ = DnsIncoming::new(rand_data);
}
}

Expand Down
4 changes: 1 addition & 3 deletions tests/mdns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 @@ -958,8 +957,7 @@ fn instance_name_two_dots() {

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

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

0 comments on commit c0ebc5c

Please sign in to comment.