| 1 | //! Tests for `ObjectIdentifier`. |
| 2 | |
| 3 | // TODO(tarcieri): test full set of OID encoding constraints specified here: |
| 4 | // <https://misc.daniel-marschall.de/asn.1/oid_facts.html> |
| 5 | |
| 6 | use const_oid::{Error, ObjectIdentifier}; |
| 7 | use hex_literal::hex; |
| 8 | use std::string::ToString; |
| 9 | |
| 10 | /// Example OID value with a root arc of `0` (and large arc). |
| 11 | const EXAMPLE_OID_0_STR: &str = "0.9.2342.19200300.100.1.1"; |
| 12 | const EXAMPLE_OID_0_BER: &[u8] = &hex!("0992268993F22C640101"); |
| 13 | const EXAMPLE_OID_0: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_0_STR); |
| 14 | |
| 15 | /// Example OID value with a root arc of `1`. |
| 16 | const EXAMPLE_OID_1_STR: &str = "1.2.840.10045.2.1"; |
| 17 | const EXAMPLE_OID_1_BER: &[u8] = &hex!("2A8648CE3D0201"); |
| 18 | const EXAMPLE_OID_1: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_1_STR); |
| 19 | |
| 20 | /// Example OID value with a root arc of `2`. |
| 21 | const EXAMPLE_OID_2_STR: &str = "2.16.840.1.101.3.4.1.42"; |
| 22 | const EXAMPLE_OID_2_BER: &[u8] = &hex!("60864801650304012A"); |
| 23 | const EXAMPLE_OID_2: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_2_STR); |
| 24 | |
| 25 | /// Example OID value with a large arc |
| 26 | const EXAMPLE_OID_LARGE_ARC_0_STR: &str = "1.2.16384"; |
| 27 | const EXAMPLE_OID_LARGE_ARC_0_BER: &[u8] = &hex!("2A818000"); |
| 28 | const EXAMPLE_OID_LARGE_ARC_0: ObjectIdentifier = |
| 29 | ObjectIdentifier::new_unwrap(crate::EXAMPLE_OID_LARGE_ARC_0_STR); |
| 30 | |
| 31 | /// Example OID value with a large arc |
| 32 | const EXAMPLE_OID_LARGE_ARC_1_STR: &str = "1.1.1.60817410.1"; |
| 33 | const EXAMPLE_OID_LARGE_ARC_1_BER: &[u8] = &hex!("29019D80800201"); |
| 34 | const EXAMPLE_OID_LARGE_ARC_1: ObjectIdentifier = |
| 35 | ObjectIdentifier::new_unwrap(EXAMPLE_OID_LARGE_ARC_1_STR); |
| 36 | |
| 37 | /// Example OID value with a large arc (namely `u32::MAX`, the edge case) |
| 38 | const EXAMPLE_OID_LARGE_ARC_2_STR: &str = "1.2.4294967295"; |
| 39 | const EXAMPLE_OID_LARGE_ARC_2_BER: &[u8] = &hex!("2A8FFFFFFF7F"); |
| 40 | const EXAMPLE_OID_LARGE_ARC_2: ObjectIdentifier = |
| 41 | ObjectIdentifier::new_unwrap(crate::EXAMPLE_OID_LARGE_ARC_2_STR); |
| 42 | |
| 43 | /// Create an OID from a string. |
| 44 | pub fn oid(s: &str) -> ObjectIdentifier { |
| 45 | ObjectIdentifier::new(s).unwrap() |
| 46 | } |
| 47 | |
| 48 | /// 0.9.2342.19200300.100.1.1 |
| 49 | #[test] |
| 50 | fn from_bytes_oid_0() { |
| 51 | let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_0_BER).unwrap(); |
| 52 | assert_eq!(oid, EXAMPLE_OID_0); |
| 53 | assert_eq!(oid.arc(0).unwrap(), 0); |
| 54 | assert_eq!(oid.arc(1).unwrap(), 9); |
| 55 | assert_eq!(oid.arc(2).unwrap(), 2342); |
| 56 | } |
| 57 | |
| 58 | /// 1.2.840.10045.2.1 |
| 59 | #[test] |
| 60 | fn from_bytes_oid_1() { |
| 61 | let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_1_BER).unwrap(); |
| 62 | assert_eq!(oid, EXAMPLE_OID_1); |
| 63 | assert_eq!(oid.arc(0).unwrap(), 1); |
| 64 | assert_eq!(oid.arc(1).unwrap(), 2); |
| 65 | assert_eq!(oid.arc(2).unwrap(), 840); |
| 66 | } |
| 67 | |
| 68 | /// 2.16.840.1.101.3.4.1.42 |
| 69 | #[test] |
| 70 | fn from_bytes_oid_2() { |
| 71 | let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_2_BER).unwrap(); |
| 72 | assert_eq!(oid, EXAMPLE_OID_2); |
| 73 | assert_eq!(oid.arc(0).unwrap(), 2); |
| 74 | assert_eq!(oid.arc(1).unwrap(), 16); |
| 75 | assert_eq!(oid.arc(2).unwrap(), 840); |
| 76 | } |
| 77 | |
| 78 | /// 1.2.16384 |
| 79 | #[test] |
| 80 | fn from_bytes_oid_largearc_0() { |
| 81 | let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_LARGE_ARC_0_BER).unwrap(); |
| 82 | assert_eq!(oid, EXAMPLE_OID_LARGE_ARC_0); |
| 83 | assert_eq!(oid.arc(0).unwrap(), 1); |
| 84 | assert_eq!(oid.arc(1).unwrap(), 2); |
| 85 | assert_eq!(oid.arc(2).unwrap(), 16384); |
| 86 | assert_eq!(oid.arc(3), None); |
| 87 | } |
| 88 | |
| 89 | /// 1.1.1.60817410.1 |
| 90 | #[test] |
| 91 | fn from_bytes_oid_largearc_1() { |
| 92 | let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_LARGE_ARC_1_BER).unwrap(); |
| 93 | assert_eq!(oid, EXAMPLE_OID_LARGE_ARC_1); |
| 94 | assert_eq!(oid.arc(0).unwrap(), 1); |
| 95 | assert_eq!(oid.arc(1).unwrap(), 1); |
| 96 | assert_eq!(oid.arc(2).unwrap(), 1); |
| 97 | assert_eq!(oid.arc(3).unwrap(), 60817410); |
| 98 | assert_eq!(oid.arc(4).unwrap(), 1); |
| 99 | assert_eq!(oid.arc(5), None); |
| 100 | } |
| 101 | |
| 102 | /// 1.2.4294967295 |
| 103 | #[test] |
| 104 | fn from_bytes_oid_largearc_2() { |
| 105 | let oid = ObjectIdentifier::from_bytes(EXAMPLE_OID_LARGE_ARC_2_BER).unwrap(); |
| 106 | assert_eq!(oid, EXAMPLE_OID_LARGE_ARC_2); |
| 107 | assert_eq!(oid.arc(0).unwrap(), 1); |
| 108 | assert_eq!(oid.arc(1).unwrap(), 2); |
| 109 | assert_eq!(oid.arc(2).unwrap(), 4294967295); |
| 110 | assert_eq!(oid.arc(3), None); |
| 111 | |
| 112 | // Empty |
| 113 | assert_eq!(ObjectIdentifier::from_bytes(&[]), Err(Error::Empty)); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn from_str() { |
| 118 | let oid0 = EXAMPLE_OID_0_STR.parse::<ObjectIdentifier>().unwrap(); |
| 119 | assert_eq!(oid0.arc(0).unwrap(), 0); |
| 120 | assert_eq!(oid0.arc(1).unwrap(), 9); |
| 121 | assert_eq!(oid0, EXAMPLE_OID_0); |
| 122 | |
| 123 | let oid1 = EXAMPLE_OID_1_STR.parse::<ObjectIdentifier>().unwrap(); |
| 124 | assert_eq!(oid1.arc(0).unwrap(), 1); |
| 125 | assert_eq!(oid1.arc(1).unwrap(), 2); |
| 126 | assert_eq!(oid1, EXAMPLE_OID_1); |
| 127 | |
| 128 | let oid2 = EXAMPLE_OID_2_STR.parse::<ObjectIdentifier>().unwrap(); |
| 129 | assert_eq!(oid2.arc(0).unwrap(), 2); |
| 130 | assert_eq!(oid2.arc(1).unwrap(), 16); |
| 131 | assert_eq!(oid2, EXAMPLE_OID_2); |
| 132 | |
| 133 | let oid_largearc0 = EXAMPLE_OID_LARGE_ARC_0_STR |
| 134 | .parse::<ObjectIdentifier>() |
| 135 | .unwrap(); |
| 136 | assert_eq!(oid_largearc0.arc(0).unwrap(), 1); |
| 137 | assert_eq!(oid_largearc0.arc(1).unwrap(), 2); |
| 138 | assert_eq!(oid_largearc0.arc(2).unwrap(), 16384); |
| 139 | assert_eq!(oid_largearc0, EXAMPLE_OID_LARGE_ARC_0); |
| 140 | |
| 141 | let oid_largearc1 = EXAMPLE_OID_LARGE_ARC_1_STR |
| 142 | .parse::<ObjectIdentifier>() |
| 143 | .unwrap(); |
| 144 | assert_eq!(oid_largearc1.arc(0).unwrap(), 1); |
| 145 | assert_eq!(oid_largearc1.arc(1).unwrap(), 1); |
| 146 | assert_eq!(oid_largearc1.arc(2).unwrap(), 1); |
| 147 | assert_eq!(oid_largearc1.arc(3).unwrap(), 60817410); |
| 148 | assert_eq!(oid_largearc1.arc(4).unwrap(), 1); |
| 149 | assert_eq!(oid_largearc1, EXAMPLE_OID_LARGE_ARC_1); |
| 150 | |
| 151 | let oid_largearc2 = EXAMPLE_OID_LARGE_ARC_2_STR |
| 152 | .parse::<ObjectIdentifier>() |
| 153 | .unwrap(); |
| 154 | assert_eq!(oid_largearc2.arc(0).unwrap(), 1); |
| 155 | assert_eq!(oid_largearc2.arc(1).unwrap(), 2); |
| 156 | assert_eq!(oid_largearc2.arc(2).unwrap(), 4294967295); |
| 157 | assert_eq!(oid_largearc2, EXAMPLE_OID_LARGE_ARC_2); |
| 158 | |
| 159 | // Truncated |
| 160 | assert_eq!( |
| 161 | "1.2.840.10045.2.".parse::<ObjectIdentifier>(), |
| 162 | Err(Error::TrailingDot) |
| 163 | ); |
| 164 | |
| 165 | // Invalid first arc |
| 166 | assert_eq!( |
| 167 | "3.2.840.10045.2.1".parse::<ObjectIdentifier>(), |
| 168 | Err(Error::ArcInvalid { arc: 3 }) |
| 169 | ); |
| 170 | |
| 171 | // Invalid second arc |
| 172 | assert_eq!( |
| 173 | "1.40.840.10045.2.1".parse::<ObjectIdentifier>(), |
| 174 | Err(Error::ArcInvalid { arc: 40 }) |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn display() { |
| 180 | assert_eq!(EXAMPLE_OID_0.to_string(), EXAMPLE_OID_0_STR); |
| 181 | assert_eq!(EXAMPLE_OID_1.to_string(), EXAMPLE_OID_1_STR); |
| 182 | assert_eq!(EXAMPLE_OID_2.to_string(), EXAMPLE_OID_2_STR); |
| 183 | assert_eq!( |
| 184 | EXAMPLE_OID_LARGE_ARC_0.to_string(), |
| 185 | EXAMPLE_OID_LARGE_ARC_0_STR |
| 186 | ); |
| 187 | assert_eq!( |
| 188 | EXAMPLE_OID_LARGE_ARC_1.to_string(), |
| 189 | EXAMPLE_OID_LARGE_ARC_1_STR |
| 190 | ); |
| 191 | assert_eq!( |
| 192 | EXAMPLE_OID_LARGE_ARC_2.to_string(), |
| 193 | EXAMPLE_OID_LARGE_ARC_2_STR |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn try_from_u32_slice() { |
| 199 | let oid1 = ObjectIdentifier::from_arcs([1, 2, 840, 10045, 2, 1]).unwrap(); |
| 200 | assert_eq!(oid1.arc(0).unwrap(), 1); |
| 201 | assert_eq!(oid1.arc(1).unwrap(), 2); |
| 202 | assert_eq!(EXAMPLE_OID_1, oid1); |
| 203 | |
| 204 | let oid2 = ObjectIdentifier::from_arcs([2, 16, 840, 1, 101, 3, 4, 1, 42]).unwrap(); |
| 205 | assert_eq!(oid2.arc(0).unwrap(), 2); |
| 206 | assert_eq!(oid2.arc(1).unwrap(), 16); |
| 207 | assert_eq!(EXAMPLE_OID_2, oid2); |
| 208 | |
| 209 | // Invalid first arc |
| 210 | assert_eq!( |
| 211 | ObjectIdentifier::from_arcs([3, 2, 840, 10045, 3, 1, 7]), |
| 212 | Err(Error::ArcInvalid { arc: 3 }) |
| 213 | ); |
| 214 | |
| 215 | // Invalid second arc |
| 216 | assert_eq!( |
| 217 | ObjectIdentifier::from_arcs([1, 40, 840, 10045, 3, 1, 7]), |
| 218 | Err(Error::ArcInvalid { arc: 40 }) |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn as_bytes() { |
| 224 | assert_eq!(EXAMPLE_OID_1.as_bytes(), EXAMPLE_OID_1_BER); |
| 225 | assert_eq!(EXAMPLE_OID_2.as_bytes(), EXAMPLE_OID_2_BER); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn as_oid_ref() { |
| 230 | assert_eq!( |
| 231 | EXAMPLE_OID_0.as_bytes(), |
| 232 | EXAMPLE_OID_0.as_oid_ref().as_bytes() |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn parse_empty() { |
| 238 | assert_eq!(ObjectIdentifier::new(""), Err(Error::Empty)); |
| 239 | } |
| 240 | |
| 241 | #[test] |
| 242 | fn parse_invalid_first_arc() { |
| 243 | assert_eq!( |
| 244 | ObjectIdentifier::new("3.2.840.10045.3.1.7"), |
| 245 | Err(Error::ArcInvalid { arc: 3 }) |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn parse_invalid_second_arc() { |
| 251 | assert_eq!( |
| 252 | ObjectIdentifier::new("1.40.840.10045.3.1.7"), |
| 253 | Err(Error::ArcInvalid { arc: 40 }) |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | fn parse_invalid_repeat_dots() { |
| 259 | assert_eq!(ObjectIdentifier::new("1.2..3.4"), Err(Error::RepeatedDot)) |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn parent() { |
| 264 | let child = oid("1.2.3.4"); |
| 265 | let parent = child.parent().unwrap(); |
| 266 | assert_eq!(parent, oid("1.2.3")); |
| 267 | |
| 268 | let parent = parent.parent().unwrap(); |
| 269 | assert_eq!(parent, oid("1.2")); |
| 270 | assert_eq!(parent.parent(), None); |
| 271 | } |
| 272 | |
| 273 | #[test] |
| 274 | fn push_arc() { |
| 275 | let parent = oid("1.2.3"); |
| 276 | assert_eq!(parent.push_arc(4).unwrap(), oid("1.2.3.4")); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn starts_with() { |
| 281 | let child = ObjectIdentifier::new("1.2.3.4.5").unwrap(); |
| 282 | assert!(child.starts_with(oid("1.2.3.4.5"))); |
| 283 | assert!(child.starts_with(oid("1.2.3.4"))); |
| 284 | assert!(child.starts_with(oid("1.2.3"))); |
| 285 | |
| 286 | assert!(!child.starts_with(oid("1.2.4"))); |
| 287 | assert!(!child.starts_with(oid("2.2.3"))); |
| 288 | assert!(!child.starts_with(oid("1.2.3.4.5.6"))); |
| 289 | } |