Vendor dependencies

This commit is contained in:
2026-08-01 16:11:49 +03:00
parent 7f139a0241
commit 6b5e7f0f8b
29706 changed files with 9575646 additions and 0 deletions
+289
View File
@@ -0,0 +1,289 @@
//! Tests for `ObjectIdentifier`.
// TODO(tarcieri): test full set of OID encoding constraints specified here:
// <https://misc.daniel-marschall.de/asn.1/oid_facts.html>
use const_oid::{Error, ObjectIdentifier};
use hex_literal::hex;
use std::string::ToString;
/// Example OID value with a root arc of `0` (and large arc).
const EXAMPLE_OID_0_STR: &str = "0.9.2342.19200300.100.1.1";
const EXAMPLE_OID_0_BER: &[u8] = &hex!("0992268993F22C640101");
const EXAMPLE_OID_0: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_0_STR);
/// Example OID value with a root arc of `1`.
const EXAMPLE_OID_1_STR: &str = "1.2.840.10045.2.1";
const EXAMPLE_OID_1_BER: &[u8] = &hex!("2A8648CE3D0201");
const EXAMPLE_OID_1: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_1_STR);
/// Example OID value with a root arc of `2`.
const EXAMPLE_OID_2_STR: &str = "2.16.840.1.101.3.4.1.42";
const EXAMPLE_OID_2_BER: &[u8] = &hex!("60864801650304012A");
const EXAMPLE_OID_2: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_2_STR);
/// Example OID value with a large arc
const EXAMPLE_OID_LARGE_ARC_0_STR: &str = "1.2.16384";
const EXAMPLE_OID_LARGE_ARC_0_BER: &[u8] = &hex!("2A818000");
const EXAMPLE_OID_LARGE_ARC_0: ObjectIdentifier =
ObjectIdentifier::new_unwrap(crate::EXAMPLE_OID_LARGE_ARC_0_STR);
/// Example OID value with a large arc
const EXAMPLE_OID_LARGE_ARC_1_STR: &str = "1.1.1.60817410.1";
const EXAMPLE_OID_LARGE_ARC_1_BER: &[u8] = &hex!("29019D80800201");
const EXAMPLE_OID_LARGE_ARC_1: ObjectIdentifier =
ObjectIdentifier::new_unwrap(EXAMPLE_OID_LARGE_ARC_1_STR);
/// Example OID value with a large arc (namely `u32::MAX`, the edge case)
const EXAMPLE_OID_LARGE_ARC_2_STR: &str = "1.2.4294967295";
const EXAMPLE_OID_LARGE_ARC_2_BER: &[u8] = &hex!("2A8FFFFFFF7F");
const EXAMPLE_OID_LARGE_ARC_2: ObjectIdentifier =
ObjectIdentifier::new_unwrap(crate::EXAMPLE_OID_LARGE_ARC_2_STR);
/// Create an OID from a string.
pub fn oid(s: &str) -> ObjectIdentifier {
ObjectIdentifier::new(s).unwrap()
}
/// 0.9.2342.19200300.100.1.1
#[test]
fn from_bytes_oid_0() {
let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_0_BER).unwrap();
assert_eq!(oid, EXAMPLE_OID_0);
assert_eq!(oid.arc(0).unwrap(), 0);
assert_eq!(oid.arc(1).unwrap(), 9);
assert_eq!(oid.arc(2).unwrap(), 2342);
}
/// 1.2.840.10045.2.1
#[test]
fn from_bytes_oid_1() {
let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_1_BER).unwrap();
assert_eq!(oid, EXAMPLE_OID_1);
assert_eq!(oid.arc(0).unwrap(), 1);
assert_eq!(oid.arc(1).unwrap(), 2);
assert_eq!(oid.arc(2).unwrap(), 840);
}
/// 2.16.840.1.101.3.4.1.42
#[test]
fn from_bytes_oid_2() {
let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_2_BER).unwrap();
assert_eq!(oid, EXAMPLE_OID_2);
assert_eq!(oid.arc(0).unwrap(), 2);
assert_eq!(oid.arc(1).unwrap(), 16);
assert_eq!(oid.arc(2).unwrap(), 840);
}
/// 1.2.16384
#[test]
fn from_bytes_oid_largearc_0() {
let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_LARGE_ARC_0_BER).unwrap();
assert_eq!(oid, EXAMPLE_OID_LARGE_ARC_0);
assert_eq!(oid.arc(0).unwrap(), 1);
assert_eq!(oid.arc(1).unwrap(), 2);
assert_eq!(oid.arc(2).unwrap(), 16384);
assert_eq!(oid.arc(3), None);
}
/// 1.1.1.60817410.1
#[test]
fn from_bytes_oid_largearc_1() {
let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_LARGE_ARC_1_BER).unwrap();
assert_eq!(oid, EXAMPLE_OID_LARGE_ARC_1);
assert_eq!(oid.arc(0).unwrap(), 1);
assert_eq!(oid.arc(1).unwrap(), 1);
assert_eq!(oid.arc(2).unwrap(), 1);
assert_eq!(oid.arc(3).unwrap(), 60817410);
assert_eq!(oid.arc(4).unwrap(), 1);
assert_eq!(oid.arc(5), None);
}
/// 1.2.4294967295
#[test]
fn from_bytes_oid_largearc_2() {
let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_LARGE_ARC_2_BER).unwrap();
assert_eq!(oid, EXAMPLE_OID_LARGE_ARC_2);
assert_eq!(oid.arc(0).unwrap(), 1);
assert_eq!(oid.arc(1).unwrap(), 2);
assert_eq!(oid.arc(2).unwrap(), 4294967295);
assert_eq!(oid.arc(3), None);
// Empty
assert_eq!(ObjectIdentifier::from_bytes(&[]), Err(Error::Empty));
}
#[test]
fn from_str() {
let oid0 = EXAMPLE_OID_0_STR.parse::<ObjectIdentifier>().unwrap();
assert_eq!(oid0.arc(0).unwrap(), 0);
assert_eq!(oid0.arc(1).unwrap(), 9);
assert_eq!(oid0, EXAMPLE_OID_0);
let oid1 = EXAMPLE_OID_1_STR.parse::<ObjectIdentifier>().unwrap();
assert_eq!(oid1.arc(0).unwrap(), 1);
assert_eq!(oid1.arc(1).unwrap(), 2);
assert_eq!(oid1, EXAMPLE_OID_1);
let oid2 = EXAMPLE_OID_2_STR.parse::<ObjectIdentifier>().unwrap();
assert_eq!(oid2.arc(0).unwrap(), 2);
assert_eq!(oid2.arc(1).unwrap(), 16);
assert_eq!(oid2, EXAMPLE_OID_2);
let oid_largearc0 = EXAMPLE_OID_LARGE_ARC_0_STR
.parse::<ObjectIdentifier>()
.unwrap();
assert_eq!(oid_largearc0.arc(0).unwrap(), 1);
assert_eq!(oid_largearc0.arc(1).unwrap(), 2);
assert_eq!(oid_largearc0.arc(2).unwrap(), 16384);
assert_eq!(oid_largearc0, EXAMPLE_OID_LARGE_ARC_0);
let oid_largearc1 = EXAMPLE_OID_LARGE_ARC_1_STR
.parse::<ObjectIdentifier>()
.unwrap();
assert_eq!(oid_largearc1.arc(0).unwrap(), 1);
assert_eq!(oid_largearc1.arc(1).unwrap(), 1);
assert_eq!(oid_largearc1.arc(2).unwrap(), 1);
assert_eq!(oid_largearc1.arc(3).unwrap(), 60817410);
assert_eq!(oid_largearc1.arc(4).unwrap(), 1);
assert_eq!(oid_largearc1, EXAMPLE_OID_LARGE_ARC_1);
let oid_largearc2 = EXAMPLE_OID_LARGE_ARC_2_STR
.parse::<ObjectIdentifier>()
.unwrap();
assert_eq!(oid_largearc2.arc(0).unwrap(), 1);
assert_eq!(oid_largearc2.arc(1).unwrap(), 2);
assert_eq!(oid_largearc2.arc(2).unwrap(), 4294967295);
assert_eq!(oid_largearc2, EXAMPLE_OID_LARGE_ARC_2);
// Truncated
assert_eq!(
"1.2.840.10045.2.".parse::<ObjectIdentifier>(),
Err(Error::TrailingDot)
);
// Invalid first arc
assert_eq!(
"3.2.840.10045.2.1".parse::<ObjectIdentifier>(),
Err(Error::ArcInvalid { arc: 3 })
);
// Invalid second arc
assert_eq!(
"1.40.840.10045.2.1".parse::<ObjectIdentifier>(),
Err(Error::ArcInvalid { arc: 40 })
);
}
#[test]
fn display() {
assert_eq!(EXAMPLE_OID_0.to_string(), EXAMPLE_OID_0_STR);
assert_eq!(EXAMPLE_OID_1.to_string(), EXAMPLE_OID_1_STR);
assert_eq!(EXAMPLE_OID_2.to_string(), EXAMPLE_OID_2_STR);
assert_eq!(
EXAMPLE_OID_LARGE_ARC_0.to_string(),
EXAMPLE_OID_LARGE_ARC_0_STR
);
assert_eq!(
EXAMPLE_OID_LARGE_ARC_1.to_string(),
EXAMPLE_OID_LARGE_ARC_1_STR
);
assert_eq!(
EXAMPLE_OID_LARGE_ARC_2.to_string(),
EXAMPLE_OID_LARGE_ARC_2_STR
);
}
#[test]
fn try_from_u32_slice() {
let oid1 = ObjectIdentifier::from_arcs([1, 2, 840, 10045, 2, 1]).unwrap();
assert_eq!(oid1.arc(0).unwrap(), 1);
assert_eq!(oid1.arc(1).unwrap(), 2);
assert_eq!(EXAMPLE_OID_1, oid1);
let oid2 = ObjectIdentifier::from_arcs([2, 16, 840, 1, 101, 3, 4, 1, 42]).unwrap();
assert_eq!(oid2.arc(0).unwrap(), 2);
assert_eq!(oid2.arc(1).unwrap(), 16);
assert_eq!(EXAMPLE_OID_2, oid2);
// Invalid first arc
assert_eq!(
ObjectIdentifier::from_arcs([3, 2, 840, 10045, 3, 1, 7]),
Err(Error::ArcInvalid { arc: 3 })
);
// Invalid second arc
assert_eq!(
ObjectIdentifier::from_arcs([1, 40, 840, 10045, 3, 1, 7]),
Err(Error::ArcInvalid { arc: 40 })
);
}
#[test]
fn as_bytes() {
assert_eq!(EXAMPLE_OID_1.as_bytes(), EXAMPLE_OID_1_BER);
assert_eq!(EXAMPLE_OID_2.as_bytes(), EXAMPLE_OID_2_BER);
}
#[test]
fn as_oid_ref() {
assert_eq!(
EXAMPLE_OID_0.as_bytes(),
EXAMPLE_OID_0.as_oid_ref().as_bytes()
);
}
#[test]
fn parse_empty() {
assert_eq!(ObjectIdentifier::new(""), Err(Error::Empty));
}
#[test]
fn parse_invalid_first_arc() {
assert_eq!(
ObjectIdentifier::new("3.2.840.10045.3.1.7"),
Err(Error::ArcInvalid { arc: 3 })
);
}
#[test]
fn parse_invalid_second_arc() {
assert_eq!(
ObjectIdentifier::new("1.40.840.10045.3.1.7"),
Err(Error::ArcInvalid { arc: 40 })
);
}
#[test]
fn parse_invalid_repeat_dots() {
assert_eq!(ObjectIdentifier::new("1.2..3.4"), Err(Error::RepeatedDot))
}
#[test]
fn parent() {
let child = oid("1.2.3.4");
let parent = child.parent().unwrap();
assert_eq!(parent, oid("1.2.3"));
let parent = parent.parent().unwrap();
assert_eq!(parent, oid("1.2"));
assert_eq!(parent.parent(), None);
}
#[test]
fn push_arc() {
let parent = oid("1.2.3");
assert_eq!(parent.push_arc(4).unwrap(), oid("1.2.3.4"));
}
#[test]
fn starts_with() {
let child = ObjectIdentifier::new("1.2.3.4.5").unwrap();
assert!(child.starts_with(oid("1.2.3.4.5")));
assert!(child.starts_with(oid("1.2.3.4")));
assert!(child.starts_with(oid("1.2.3")));
assert!(!child.starts_with(oid("1.2.4")));
assert!(!child.starts_with(oid("2.2.3")));
assert!(!child.starts_with(oid("1.2.3.4.5.6")));
}
+71
View File
@@ -0,0 +1,71 @@
//! Tests for `ObjectIdentifierRef`.
use const_oid::{Error, ObjectIdentifier, ObjectIdentifierRef};
use hex_literal::hex;
/// Example OID value with a root arc of `0` (and large arc).
const EXAMPLE_OID_0_STR: &str = "0.9.2342.19200300.100.1.1";
const EXAMPLE_OID_0_BER: &[u8] = &hex!("0992268993F22C640101");
const EXAMPLE_OID_0: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_0_STR);
/// Example OID value with a root arc of `1`.
const EXAMPLE_OID_1_STR: &str = "1.2.840.10045.2.1";
const EXAMPLE_OID_1_BER: &[u8] = &hex!("2A8648CE3D0201");
const EXAMPLE_OID_1: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_1_STR);
/// Example OID value with a root arc of `2`.
const EXAMPLE_OID_2_STR: &str = "2.16.840.1.101.3.4.1.42";
const EXAMPLE_OID_2_BER: &[u8] = &hex!("60864801650304012A");
const EXAMPLE_OID_2: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_2_STR);
/// Example OID value with a large arc
const EXAMPLE_OID_LARGE_ARC_STR: &str = "0.9.2342.19200300.100.1.1";
const EXAMPLE_OID_LARGE_ARC_BER: &[u8] = &hex!("0992268993F22C640101");
const EXAMPLE_OID_LARGE_ARC: ObjectIdentifier =
ObjectIdentifier::new_unwrap("0.9.2342.19200300.100.1.1");
#[test]
fn from_bytes() {
let oid0 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_0_BER).unwrap();
assert_eq!(oid0.arc(0).unwrap(), 0);
assert_eq!(oid0.arc(1).unwrap(), 9);
assert_eq!(oid0, &EXAMPLE_OID_0);
let oid1 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_1_BER).unwrap();
assert_eq!(oid1.arc(0).unwrap(), 1);
assert_eq!(oid1.arc(1).unwrap(), 2);
assert_eq!(oid1, &EXAMPLE_OID_1);
let oid2 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_2_BER).unwrap();
assert_eq!(oid2.arc(0).unwrap(), 2);
assert_eq!(oid2.arc(1).unwrap(), 16);
assert_eq!(oid2, &EXAMPLE_OID_2);
let oid3 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_LARGE_ARC_BER).unwrap();
assert_eq!(oid3.arc(0).unwrap(), 0);
assert_eq!(oid3.arc(1).unwrap(), 9);
assert_eq!(oid3.arc(2).unwrap(), 2342);
assert_eq!(oid3.arc(3).unwrap(), 19200300);
assert_eq!(oid3.arc(4).unwrap(), 100);
assert_eq!(oid3.arc(5).unwrap(), 1);
assert_eq!(oid3.arc(6).unwrap(), 1);
assert_eq!(oid3, &EXAMPLE_OID_LARGE_ARC);
// Empty
assert_eq!(ObjectIdentifierRef::from_bytes(&[]), Err(Error::Empty));
}
#[test]
fn display() {
let oid0 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_0_BER).unwrap();
assert_eq!(oid0.to_string(), EXAMPLE_OID_0_STR);
let oid1 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_1_BER).unwrap();
assert_eq!(oid1.to_string(), EXAMPLE_OID_1_STR);
let oid2 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_2_BER).unwrap();
assert_eq!(oid2.to_string(), EXAMPLE_OID_2_STR);
let oid3 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_LARGE_ARC_BER).unwrap();
assert_eq!(oid3.to_string(), EXAMPLE_OID_LARGE_ARC_STR);
}
@@ -0,0 +1,14 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 1663923d2fb0c804c5b850d10dd0ded1cbfc06dddf3f88faa4abf149b8430831 # shrinks to s = ""
cc 829ba8833ee42816bc33d308b7a186452e36617f0fa0e771edea08fd07d78718 # shrinks to s = "0.40"
cc bc88f232a7e2e45d2b1325d4e02c09742aca7ad31903326fedb36c047533696c # shrinks to s = "0"
cc d90305406041ea5e4cf4d9e7849cad03391db1869d0b1329f60ccbf1fabaee91 # shrinks to s = "0..0"
cc 8ed8dde35d12a2c8e10cdde6d591a8f17f0cd6d6fdf90f1582536401364623bf # shrinks to s = "0.00"
cc ba5e3e3dc1a64870477e82054bbf6d8272f8b0d0c9094115bf7e8b5ff59f3c63 # shrinks to s = "00.1.1"
cc d211e943da9a0e3d0ee5097899b2435f784ca2b3d2f8d4790aae3744823a268a # shrinks to s = "1.1.1.60817410.1"
cc 61bdeaa6cfc6707a0c4f3e9c6165d99d28042e78acb29b0ca7f747c169e83e74 # shrinks to s = "1.1.1.270000000"
+56
View File
@@ -0,0 +1,56 @@
//! `proptest`-powered property-based tests.
use const_oid::{Error, ObjectIdentifier};
use proptest::prelude::*;
use regex::Regex;
prop_compose! {
/// Produce a string of digits and dots, i.e. the component parts of OIDs.
///
/// Note that this can be any permutation of digits-and-dots and does not necessarily
/// represent a valid OID.
fn oid_like_string()(bytes in any::<Vec<u8>>()) -> String {
// Create a digit or dot from a byte input
fn byte_to_char(byte: u8) -> char {
match byte % 11 {
n @ 0..=9 => (b'0' + n) as char,
10 => '.',
_ => unreachable!()
}
}
let mut ret = String::with_capacity(bytes.len());
for byte in bytes {
ret.push(byte_to_char(byte));
}
ret
}
}
proptest! {
#[test]
fn round_trip(s in oid_like_string()) {
match ObjectIdentifier::new(&s) {
Ok(oid) => {
// Leading zeros won't round trip, so ignore that case
// TODO(tarcieri): disallow leading zeros?
if !s.starts_with("0") && !s.contains(".0") {
let oid_string = oid.to_string();
prop_assert_eq!(s, oid_string);
}
},
Err(Error::ArcInvalid { .. }) | Err(Error::ArcTooBig) => (),
Err(e) => {
let re = Regex::new("^([0-2])\\.([0-3]?[0-9])((\\.0)|(\\.[1-9][0-9]*))+$").unwrap();
prop_assert!(
re.find(&s).is_none(),
"regex asserts OID `{}` is valid, but `const-oid`failed: {}",
&s,
&e
);
}
}
}
}