diff --git a/schemars/tests/integration/std_types.rs b/schemars/tests/integration/std_types.rs index 5abdcafe..0ef2d7ab 100644 --- a/schemars/tests/integration/std_types.rs +++ b/schemars/tests/integration/std_types.rs @@ -1,8 +1,10 @@ use crate::prelude::*; use std::ffi::{CStr, CString, OsStr, OsString}; use std::marker::PhantomData; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use std::num::{NonZeroI64, NonZeroU64}; use std::ops::{Bound, Range, RangeInclusive}; +use std::path::{Path, PathBuf}; use std::time::{Duration, SystemTime}; #[test] @@ -23,11 +25,76 @@ fn result() { fn nonzero() { test!(NonZeroI64) .assert_allows_ser_roundtrip([NonZeroI64::MIN, NonZeroI64::MAX]) - .assert_rejects_de([Value::from(0)]); + .assert_rejects_de([Value::from(0)]) + .assert_matches_de_roundtrip(arbitrary_values_except( + |v| v.as_u64().is_some_and(|u| u > i64::MAX as u64), + "FIXME schema allows out-of-range positive integers", + )); test!(NonZeroU64) .assert_allows_ser_roundtrip([NonZeroU64::MIN, NonZeroU64::MAX]) - .assert_rejects_de([Value::from(0)]); + .assert_rejects_de([Value::from(0)]) + .assert_matches_de_roundtrip(arbitrary_values()); +} + +const IPV4_SAMPLES: [Ipv4Addr; 3] = [ + // Commented-out until https://github.com/Stranger6667/jsonschema-rs/issues/512 is fixed + // Ipv4Addr::UNSPECIFIED, + Ipv4Addr::LOCALHOST, + Ipv4Addr::BROADCAST, + Ipv4Addr::new(1, 2, 3, 4), +]; +const IPV6_SAMPLES: [Ipv6Addr; 4] = [ + Ipv6Addr::UNSPECIFIED, + Ipv6Addr::LOCALHOST, + Ipv4Addr::LOCALHOST.to_ipv6_mapped(), + Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), +]; + +#[test] +fn ip_addr() { + test!(Ipv4Addr) + .assert_allows_ser_roundtrip(IPV4_SAMPLES) + .assert_matches_de_roundtrip(arbitrary_values()); + + test!(Ipv6Addr) + .assert_allows_ser_roundtrip(IPV6_SAMPLES) + .assert_matches_de_roundtrip(arbitrary_values()); + + test!(IpAddr) + .assert_allows_ser_roundtrip(IPV4_SAMPLES.map(Into::into)) + .assert_allows_ser_roundtrip(IPV6_SAMPLES.map(Into::into)) + .assert_matches_de_roundtrip(arbitrary_values_except( + Value::is_string, + "Custom format 'ip', so arbitrary strings technically allowed by schema", + )); +} + +#[test] +fn socket_addr() { + let port = 12345; + + test!(SocketAddrV4) + .assert_allows_ser_roundtrip(IPV4_SAMPLES.map(|ip| SocketAddrV4::new(ip, port))) + .assert_matches_de_roundtrip(arbitrary_values_except( + Value::is_string, + "Arbitrary strings allowed by schema", + )); + + test!(SocketAddrV6) + .assert_allows_ser_roundtrip(IPV6_SAMPLES.map(|ip| SocketAddrV6::new(ip, port, 0, 0))) + .assert_matches_de_roundtrip(arbitrary_values_except( + Value::is_string, + "Arbitrary strings allowed by schema", + )); + + test!(SocketAddr) + .assert_allows_ser_roundtrip(IPV4_SAMPLES.map(|ip| (ip, port).into())) + .assert_allows_ser_roundtrip(IPV6_SAMPLES.map(|ip| (ip, port).into())) + .assert_matches_de_roundtrip(arbitrary_values_except( + Value::is_string, + "Arbitrary strings allowed by schema", + )); } #[test] @@ -79,6 +146,22 @@ fn os_strings() { .assert_matches_de_roundtrip(arbitrary_values()); } +#[test] +fn paths() { + let strings = [PathBuf::new(), PathBuf::from("test")]; + + test!(PathBuf) + .assert_identical::() + .assert_allows_ser_roundtrip(strings.clone()) + .assert_matches_de_roundtrip(arbitrary_values()); + + test!(Box) + .assert_identical::() + .assert_identical::() + .assert_allows_ser_roundtrip(strings.into_iter().map(Into::into)) + .assert_matches_de_roundtrip(arbitrary_values()); +} + #[test] fn bound() { test!(Bound).assert_allows_ser_roundtrip([