| 1 | //! Tests for `ObjectIdentifierRef`. |
| 2 | |
| 3 | use const_oid::{Error, ObjectIdentifier, ObjectIdentifierRef}; |
| 4 | use hex_literal::hex; |
| 5 | |
| 6 | /// Example OID value with a root arc of `0` (and large arc). |
| 7 | const EXAMPLE_OID_0_STR: &str = "0.9.2342.19200300.100.1.1"; |
| 8 | const EXAMPLE_OID_0_BER: &[u8] = &hex!("0992268993F22C640101"); |
| 9 | const EXAMPLE_OID_0: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_0_STR); |
| 10 | |
| 11 | /// Example OID value with a root arc of `1`. |
| 12 | const EXAMPLE_OID_1_STR: &str = "1.2.840.10045.2.1"; |
| 13 | const EXAMPLE_OID_1_BER: &[u8] = &hex!("2A8648CE3D0201"); |
| 14 | const EXAMPLE_OID_1: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_1_STR); |
| 15 | |
| 16 | /// Example OID value with a root arc of `2`. |
| 17 | const EXAMPLE_OID_2_STR: &str = "2.16.840.1.101.3.4.1.42"; |
| 18 | const EXAMPLE_OID_2_BER: &[u8] = &hex!("60864801650304012A"); |
| 19 | const EXAMPLE_OID_2: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_2_STR); |
| 20 | |
| 21 | /// Example OID value with a large arc |
| 22 | const EXAMPLE_OID_LARGE_ARC_STR: &str = "0.9.2342.19200300.100.1.1"; |
| 23 | const EXAMPLE_OID_LARGE_ARC_BER: &[u8] = &hex!("0992268993F22C640101"); |
| 24 | const EXAMPLE_OID_LARGE_ARC: ObjectIdentifier = |
| 25 | ObjectIdentifier::new_unwrap("0.9.2342.19200300.100.1.1"); |
| 26 | |
| 27 | #[test] |
| 28 | fn from_bytes() { |
| 29 | let oid0 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_0_BER).unwrap(); |
| 30 | assert_eq!(oid0.arc(0).unwrap(), 0); |
| 31 | assert_eq!(oid0.arc(1).unwrap(), 9); |
| 32 | assert_eq!(oid0, &EXAMPLE_OID_0); |
| 33 | |
| 34 | let oid1 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_1_BER).unwrap(); |
| 35 | assert_eq!(oid1.arc(0).unwrap(), 1); |
| 36 | assert_eq!(oid1.arc(1).unwrap(), 2); |
| 37 | assert_eq!(oid1, &EXAMPLE_OID_1); |
| 38 | |
| 39 | let oid2 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_2_BER).unwrap(); |
| 40 | assert_eq!(oid2.arc(0).unwrap(), 2); |
| 41 | assert_eq!(oid2.arc(1).unwrap(), 16); |
| 42 | assert_eq!(oid2, &EXAMPLE_OID_2); |
| 43 | |
| 44 | let oid3 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_LARGE_ARC_BER).unwrap(); |
| 45 | assert_eq!(oid3.arc(0).unwrap(), 0); |
| 46 | assert_eq!(oid3.arc(1).unwrap(), 9); |
| 47 | assert_eq!(oid3.arc(2).unwrap(), 2342); |
| 48 | assert_eq!(oid3.arc(3).unwrap(), 19200300); |
| 49 | assert_eq!(oid3.arc(4).unwrap(), 100); |
| 50 | assert_eq!(oid3.arc(5).unwrap(), 1); |
| 51 | assert_eq!(oid3.arc(6).unwrap(), 1); |
| 52 | assert_eq!(oid3, &EXAMPLE_OID_LARGE_ARC); |
| 53 | |
| 54 | // Empty |
| 55 | assert_eq!(ObjectIdentifierRef::from_bytes(&[]), Err(Error::Empty)); |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn display() { |
| 60 | let oid0 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_0_BER).unwrap(); |
| 61 | assert_eq!(oid0.to_string(), EXAMPLE_OID_0_STR); |
| 62 | |
| 63 | let oid1 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_1_BER).unwrap(); |
| 64 | assert_eq!(oid1.to_string(), EXAMPLE_OID_1_STR); |
| 65 | |
| 66 | let oid2 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_2_BER).unwrap(); |
| 67 | assert_eq!(oid2.to_string(), EXAMPLE_OID_2_STR); |
| 68 | |
| 69 | let oid3 = ObjectIdentifierRef::from_bytes(EXAMPLE_OID_LARGE_ARC_BER).unwrap(); |
| 70 | assert_eq!(oid3.to_string(), EXAMPLE_OID_LARGE_ARC_STR); |
| 71 | } |