Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 //! [Object Identifiers] are a standard of the [ITU] used to reference objects, things, and
2 //! concepts in a globally unique way. This crate provides for data structures and methods
3 //! to build, parse, and format OIDs.
4 //!
5 //!
6 //! ## Parsing OID String Representation
7 //! ```rust
8 //! use oid::prelude::*;
9 //!
10 //! fn main() -> Result<(), ObjectIdentifierError> {
11 //! let oid = ObjectIdentifier::try_from("0.1.2.3")?;
12 //! Ok(())
13 //! }
14 //! ```
15 //!
16 //! ## Parsing OID Binary Representation
17 //! ```rust
18 //! use oid::prelude::*;
19 //!
20 //! fn main() -> Result<(), ObjectIdentifierError> {
21 //! let oid = ObjectIdentifier::try_from(vec![0x00, 0x01, 0x02, 0x03])?;
22 //! Ok(())
23 //! }
24 //! ```
25 //!
26 //! ## Encoding OID as String Representation
27 //! ```rust
28 //! use oid::prelude::*;
29 //!
30 //! fn main() -> Result<(), ObjectIdentifierError> {
31 //! let oid = ObjectIdentifier::try_from("0.1.2.3")?;
32 //! let oid: String = oid.into();
33 //! assert_eq!(oid, "0.1.2.3");
34 //! Ok(())
35 //! }
36 //! ```
37 //!
38 //! ## Encoding OID as Binary Representation
39 //! ```rust
40 //! use oid::prelude::*;
41 //!
42 //! fn main() -> Result<(), ObjectIdentifierError> {
43 //! let oid = ObjectIdentifier::try_from(vec![0x00, 0x01, 0x02, 0x03])?;
44 //! let oid: Vec<u8> = oid.into();
45 //! assert_eq!(oid, vec![0x00, 0x01, 0x02, 0x03]);
46 //! Ok(())
47 //! }
48 //! ```
49 //!
50 //! [Object Identifiers]: https://en.wikipedia.org/wiki/Object_identifier
51 //! [ITU]: https://en.wikipedia.org/wiki/International_Telecommunications_Union
52 #![doc(
53 html_logo_url = "https://labs.unnecessary.engineering/logo.png",
54 html_favicon_url = "https://labs.unnecessary.engineering/favicon.ico",
55 issue_tracker_base_url = "https://github.com/UnnecessaryEngineering/oid/issues/"
56 )]
57 #![deny(missing_docs, unused_imports, missing_debug_implementations)]
58 #![cfg_attr(not(feature = "std"), no_std)]
59
60 #[cfg(not(feature = "std"))]
61 #[macro_use]
62 extern crate alloc;
63
64 #[cfg(not(feature = "std"))]
65 use alloc::{string::String, vec::Vec};
66
67 use core::convert::{TryFrom, TryInto};
68
69 // OID spec doesn't specify the maximum integer size of each node, so we default to usize
70 #[cfg(not(feature = "u32"))]
71 type Node = usize;
72
73 // Can also specifically set node size as u32, helpful for 8-bit embedded platforms
74 #[cfg(feature = "u32")]
75 type Node = u32;
76
77 /// Convenience module for quickly importing the public interface (e.g., `use oid::prelude::*`)
78 pub mod prelude {
79 pub use crate::ObjectIdentifier;
80 pub use crate::ObjectIdentifierError;
81 pub use core::convert::{TryFrom, TryInto};
82 }
83
84 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
85 enum ObjectIdentifierRoot {
86 ItuT = 0,
87 Iso = 1,
88 JointIsoItuT = 2,
89 }
90
91 impl Into<String> for ObjectIdentifierRoot {
92 fn into(self) -> String {
93 format!("{}", self as u8)
94 }
95 }
96
97 impl TryFrom<u8> for ObjectIdentifierRoot {
98 type Error = ObjectIdentifierError;
99 fn try_from(value: u8) -> Result<ObjectIdentifierRoot, Self::Error> {
100 match value {
101 0 => Ok(ObjectIdentifierRoot::ItuT),
102 1 => Ok(ObjectIdentifierRoot::Iso),
103 2 => Ok(ObjectIdentifierRoot::JointIsoItuT),
104 _ => Err(ObjectIdentifierError::IllegalRootNode),
105 }
106 }
107 }
108
109 /// Object Identifier Errors
110 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
111 pub enum ObjectIdentifierError {
112 /// Failed to parse OID due to illegal root node (must be 0-2 decimal)
113 IllegalRootNode,
114 /// Failed to parse OID due to illegal first node (must be 0-39 decimal)
115 IllegalFirstChildNode,
116 /// Failed to parse OID due to illegal child node value (except first node)
117 IllegalChildNodeValue,
118 }
119
120 /// Object Identifier (OID)
121 #[derive(Clone, Debug, Eq, PartialEq)]
122 pub struct ObjectIdentifier {
123 root: ObjectIdentifierRoot,
124 first_node: u8,
125 child_nodes: Vec<Node>,
126 }
127
128 fn parse_string_first_node(
129 nodes: &mut dyn Iterator<Item = &str>,
130 ) -> Result<u8, ObjectIdentifierError> {
131 if let Some(first_child_node) = nodes.next() {
132 let first_child_node: u8 = first_child_node
133 .parse()
134 .map_err(|_| ObjectIdentifierError::IllegalFirstChildNode)?;
135 if first_child_node > 39 {
136 return Err(ObjectIdentifierError::IllegalFirstChildNode);
137 }
138 Ok(first_child_node)
139 } else {
140 Err(ObjectIdentifierError::IllegalFirstChildNode)
141 }
142 }
143
144 fn parse_string_child_nodes(
145 nodes: &mut dyn Iterator<Item = &str>,
146 ) -> Result<Vec<Node>, ObjectIdentifierError> {
147 let mut result: Vec<Node> = vec![];
148 while let Some(node) = nodes.next() {
149 result.push(
150 node.parse()
151 .map_err(|_| ObjectIdentifierError::IllegalChildNodeValue)?,
152 );
153 }
154 Ok(result)
155 }
156
157 impl ObjectIdentifier {
158 fn from_string<S>(value: S) -> Result<ObjectIdentifier, ObjectIdentifierError>
159 where
160 S: Into<String>,
161 {
162 let value = value.into();
163 let mut nodes = value.split(".");
164 match &nodes.next() {
165 Some(root_node_value) => {
166 let root_node_value: Result<u8, _> = root_node_value.parse();
167 match root_node_value {
168 Ok(root_node) => {
169 let root_node: Result<ObjectIdentifierRoot, _> = root_node.try_into();
170 match root_node {
171 Ok(root) => {
172 let first_node = parse_string_first_node(&mut nodes)?;
173 Ok(ObjectIdentifier {
174 root,
175 first_node,
176 child_nodes: parse_string_child_nodes(&mut nodes)?,
177 })
178 }
179 Err(_err) => Err(ObjectIdentifierError::IllegalRootNode),
180 }
181 }
182 Err(_) => Err(ObjectIdentifierError::IllegalRootNode),
183 }
184 }
185 None => Err(ObjectIdentifierError::IllegalRootNode),
186 }
187 }
188 }
189
190 impl Into<String> for &ObjectIdentifier {
191 fn into(self) -> String {
192 let mut result: String = self.root.into();
193 result.push_str(&format!(".{}", self.first_node));
194 for node in &self.child_nodes {
195 result.push_str(&format!(".{}", node));
196 }
197 result
198 }
199 }
200
201 impl Into<String> for ObjectIdentifier {
202 fn into(self) -> String {
203 (&self).into()
204 }
205 }
206
207 impl Into<Vec<u8>> for &ObjectIdentifier {
208 fn into(self) -> Vec<u8> {
209 let mut result: Vec<u8> = vec![self.root as u8];
210 result[0] = result[0] * 40 + self.first_node;
211 for node in self.child_nodes.iter() {
212 // TODO bench against !*node &= 0x80, compiler may already optimize better
213 if *node <= 127 {
214 result.push(*node as u8);
215 } else {
216 let mut value = *node;
217 let mut mask: Node = 0;
218 let mut encoded: Vec<u8> = vec![];
219 while value > 0x80 {
220 encoded.insert(0, (value & 0x7f | mask) as u8);
221 value >>= 7;
222 mask = 0x80;
223 }
224 encoded.insert(0, (value | mask) as u8);
225 result.append(&mut encoded);
226 }
227 }
228 result
229 }
230 }
231
232 impl Into<Vec<u8>> for ObjectIdentifier {
233 fn into(self) -> Vec<u8> {
234 (&self).into()
235 }
236 }
237
238 impl TryFrom<&str> for ObjectIdentifier {
239 type Error = ObjectIdentifierError;
240 fn try_from(value: &str) -> Result<ObjectIdentifier, Self::Error> {
241 ObjectIdentifier::from_string(value)
242 }
243 }
244
245 impl TryFrom<String> for ObjectIdentifier {
246 type Error = ObjectIdentifierError;
247 fn try_from(value: String) -> Result<ObjectIdentifier, Self::Error> {
248 ObjectIdentifier::from_string(value)
249 }
250 }
251
252 impl TryFrom<&[u8]> for ObjectIdentifier {
253 type Error = ObjectIdentifierError;
254 fn try_from(value: &[u8]) -> Result<ObjectIdentifier, Self::Error> {
255 if value.len() < 1 {
256 return Err(ObjectIdentifierError::IllegalRootNode);
257 };
258 let root = ObjectIdentifierRoot::try_from(value[0] / 40)?;
259 let first_node = value[0] % 40;
260 let mut child_nodes = vec![];
261 let mut parsing_big_int = false;
262 let mut big_int: Node = 0;
263 for i in 1..value.len() {
264 if !parsing_big_int && value[i] < 128 {
265 child_nodes.push(value[i] as Node);
266 } else {
267 if big_int > 0 {
268 if big_int >= Node::max_value() >> 7 {
269 return Err(ObjectIdentifierError::IllegalChildNodeValue);
270 }
271 big_int <<= 7;
272 };
273 big_int |= (value[i] & !0x80) as Node;
274 parsing_big_int = value[i] & 0x80 != 0;
275 }
276 if big_int > 0 && !parsing_big_int {
277 child_nodes.push(big_int);
278 big_int = 0;
279 }
280 }
281 Ok(ObjectIdentifier {
282 root,
283 first_node,
284 child_nodes,
285 })
286 }
287 }
288
289 impl TryFrom<Vec<u8>> for ObjectIdentifier {
290 type Error = ObjectIdentifierError;
291 fn try_from(value: Vec<u8>) -> Result<ObjectIdentifier, Self::Error> {
292 value.as_slice().try_into()
293 }
294 }
295
296 #[cfg(feature = "serde_support")]
297 mod serde_support {
298 use super::*;
299 use core::fmt;
300 use serde::{de, ser};
301
302 struct OidVisitor;
303
304 impl<'de> de::Visitor<'de> for OidVisitor {
305 type Value = ObjectIdentifier;
306
307 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
308 formatter.write_str("a valid buffer representing an OID")
309 }
310
311 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
312 where
313 E: de::Error,
314 {
315 ObjectIdentifier::try_from(v).map_err(|err| {
316 E::invalid_value(
317 de::Unexpected::Other(match err {
318 ObjectIdentifierError::IllegalRootNode => "illegal root node",
319 ObjectIdentifierError::IllegalFirstChildNode => "illegal first child node",
320 ObjectIdentifierError::IllegalChildNodeValue => "illegal child node value",
321 }),
322 &"a valid buffer representing an OID",
323 )
324 })
325 }
326
327 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
328 where
329 E: de::Error,
330 {
331 ObjectIdentifier::try_from(v).map_err(|err| {
332 E::invalid_value(
333 de::Unexpected::Other(match err {
334 ObjectIdentifierError::IllegalRootNode => "illegal root node",
335 ObjectIdentifierError::IllegalFirstChildNode => "illegal first child node",
336 ObjectIdentifierError::IllegalChildNodeValue => "illegal child node value",
337 }),
338 &"a string representing an OID",
339 )
340 })
341 }
342 }
343
344 impl<'de> de::Deserialize<'de> for ObjectIdentifier {
345 fn deserialize<D>(deserializer: D) -> Result<ObjectIdentifier, D::Error>
346 where
347 D: de::Deserializer<'de>,
348 {
349 if deserializer.is_human_readable() {
350 deserializer.deserialize_str(OidVisitor)
351 } else {
352 deserializer.deserialize_bytes(OidVisitor)
353 }
354 }
355 }
356
357 impl ser::Serialize for ObjectIdentifier {
358 fn serialize<S>(
359 &self,
360 serializer: S,
361 ) -> Result<<S as ser::Serializer>::Ok, <S as ser::Serializer>::Error>
362 where
363 S: ser::Serializer,
364 {
365 if serializer.is_human_readable() {
366 let encoded: String = self.into();
367 serializer.serialize_str(&encoded)
368 } else {
369 let encoded: Vec<u8> = self.into();
370 serializer.serialize_bytes(&encoded)
371 }
372 }
373 }
374 }
375
376 #[cfg(test)]
377 mod tests {
378 use super::*;
379
380 #[cfg(feature = "serde_support")]
381 mod serde_support {
382 use super::*;
383 use serde_derive::{Deserialize, Serialize};
•384 //use serde_xml_rs;
385
386 #[test]
387 fn bincode_serde_serialize() {
388 let expected: Vec<u8> = vec![
389 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08,
390 0x0D, 0x15,
391 ];
392 let oid = ObjectIdentifier {
393 root: ObjectIdentifierRoot::ItuT,
394 first_node: 0x01,
395 child_nodes: vec![1, 2, 3, 5, 8, 13, 21],
396 };
397 let actual: Vec<u8> = bincode::serialize(&oid).unwrap();
398 assert_eq!(expected, actual);
399 }
400
401 #[test]
402 fn bincode_serde_deserialize() {
403 let expected = ObjectIdentifier {
404 root: ObjectIdentifierRoot::ItuT,
405 first_node: 0x01,
406 child_nodes: vec![1, 2, 3, 5, 8, 13, 21],
407 };
408 let actual: ObjectIdentifier = bincode::deserialize(&[
409 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08,
410 0x0D, 0x15,
411 ])
412 .unwrap();
413 assert_eq!(expected, actual);
414 }
415
416 #[derive(Debug, Deserialize, PartialEq, Serialize)]
417 struct MyStruct {
418 oid: ObjectIdentifier
419 }
420
•421 /*#[test]
422 fn xml_serde_serialize() {
423 let mydata = MyStruct {
424 oid: ObjectIdentifier::try_from("1.2.3.5.8.13.21").unwrap()
425 };
426 let expected = r#"<MyStruct><oid>1.2.3.5.8.13.21</oid></MyStruct>"#;
427 let actual = serde_xml_rs::to_string(&mydata).unwrap();
428 assert_eq!(expected, actual);
•429 }*/
430
•431 /*#[test]
432 fn xml_serde_deserialize_element() {
433 let src = r#"<mystruct><oid>1.2.3.5.8.13.21</oid></mystruct>"#;
434
435 let expected = MyStruct {
436 oid: ObjectIdentifier::try_from("1.2.3.5.8.13.21").unwrap()
437 };
438 let actual: MyStruct = serde_xml_rs::from_str(&src).unwrap();
439 assert_eq!(expected, actual);
•440 }*/
441
•442 /*#[test]
443 fn xml_serde_deserialize_attribute() {
444 let src = r#"<mystruct oid="1.2.3.5.8.13.21" />"#;
445
446 let expected = MyStruct {
447 oid: ObjectIdentifier::try_from("1.2.3.5.8.13.21").unwrap()
448 };
449 let actual: MyStruct = serde_xml_rs::from_str(&src).unwrap();
450 assert_eq!(expected, actual);
•451 }*/
452 }
453
454 #[test]
455 fn encode_binary_root_node_0() {
456 let expected: Vec<u8> = vec![0];
457 let oid = ObjectIdentifier {
458 root: ObjectIdentifierRoot::ItuT,
459 first_node: 0x00,
460 child_nodes: vec![],
461 };
462 let actual: Vec<u8> = (&oid).into();
463 assert_eq!(expected, actual);
464 }
465
466 #[test]
467 fn encode_binary_root_node_1() {
468 let expected: Vec<u8> = vec![40];
469 let oid = ObjectIdentifier {
470 root: ObjectIdentifierRoot::Iso,
471 first_node: 0x00,
472 child_nodes: vec![],
473 };
474 let actual: Vec<u8> = (&oid).into();
475 assert_eq!(expected, actual);
476 }
477
478 #[test]
479 fn encode_binary_root_node_2() {
480 let expected: Vec<u8> = vec![80];
481 let oid = ObjectIdentifier {
482 root: ObjectIdentifierRoot::JointIsoItuT,
483 first_node: 0x00,
484 child_nodes: vec![],
485 };
486 let actual: Vec<u8> = (&oid).into();
487 assert_eq!(expected, actual);
488 }
489
490 #[test]
491 fn encode_binary_example_1() {
492 let expected: Vec<u8> = vec![0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0D, 0x15];
493 let oid = ObjectIdentifier {
494 root: ObjectIdentifierRoot::ItuT,
495 first_node: 0x01,
496 child_nodes: vec![1, 2, 3, 5, 8, 13, 21],
497 };
498 let actual: Vec<u8> = (&oid).into();
499 assert_eq!(expected, actual);
500 }
501
502 #[test]
503 fn encode_binary_example_2() {
504 let expected: Vec<u8> = vec![
505 0x77, 0x2A, 0x93, 0x45, 0x83, 0xFF, 0x7F, 0x87, 0xFF, 0xFF, 0xFF, 0x7F, 0x89, 0x53,
506 0x92, 0x30,
507 ];
508 let oid = ObjectIdentifier {
509 root: ObjectIdentifierRoot::JointIsoItuT,
510 first_node: 39,
511 child_nodes: vec![42, 2501, 65535, 2147483647, 1235, 2352],
512 };
513 let actual: Vec<u8> = (&oid).into();
514 assert_eq!(expected, actual);
515 }
516
517 #[test]
518 fn encode_string_root_node_0() {
519 let expected = "0.0";
520 let oid = ObjectIdentifier {
521 root: ObjectIdentifierRoot::ItuT,
522 first_node: 0x00,
523 child_nodes: vec![],
524 };
525 let actual: String = (&oid).into();
526 assert_eq!(expected, actual);
527 }
528
529 #[test]
530 fn encode_string_root_node_1() {
531 let expected = "1.0";
532 let oid = ObjectIdentifier {
533 root: ObjectIdentifierRoot::Iso,
534 first_node: 0x00,
535 child_nodes: vec![],
536 };
537 let actual: String = (&oid).into();
538 assert_eq!(expected, actual);
539 }
540
541 #[test]
542 fn encode_string_root_node_2() {
543 let expected = "2.0";
544 let oid = ObjectIdentifier {
545 root: ObjectIdentifierRoot::JointIsoItuT,
546 first_node: 0x00,
547 child_nodes: vec![],
548 };
549 let actual: String = (&oid).into();
550 assert_eq!(expected, actual);
551 }
552
553 #[test]
554 fn encode_string_example_1() {
555 let expected = "0.1.1.2.3.5.8.13.21";
556 let oid = ObjectIdentifier {
557 root: ObjectIdentifierRoot::ItuT,
558 first_node: 0x01,
559 child_nodes: vec![1, 2, 3, 5, 8, 13, 21],
560 };
561 let actual: String = (&oid).into();
562 assert_eq!(expected, actual);
563 }
564
565 #[test]
566 fn encode_string_example_2() {
567 let expected = "2.39.42.2501.65535.2147483647.1235.2352";
568 let oid = ObjectIdentifier {
569 root: ObjectIdentifierRoot::JointIsoItuT,
570 first_node: 39,
571 child_nodes: vec![42, 2501, 65535, 2147483647, 1235, 2352],
572 };
573 let actual: String = (&oid).into();
574 assert_eq!(expected, actual);
575 }
576
577 #[test]
578 fn parse_binary_root_node_0() {
579 let expected = Ok(ObjectIdentifier {
580 root: ObjectIdentifierRoot::ItuT,
581 first_node: 0x00,
582 child_nodes: vec![],
583 });
584 let actual = vec![0x00].try_into();
585 assert_eq!(expected, actual);
586 }
587
588 #[test]
589 fn parse_binary_root_node_1() {
590 let expected = Ok(ObjectIdentifier {
591 root: ObjectIdentifierRoot::Iso,
592 first_node: 0x00,
593 child_nodes: vec![],
594 });
595 let actual = vec![40].try_into();
596 assert_eq!(expected, actual);
597 }
598
599 #[test]
600 fn parse_binary_root_node_2() {
601 let expected = Ok(ObjectIdentifier {
602 root: ObjectIdentifierRoot::JointIsoItuT,
603 first_node: 0x00,
604 child_nodes: vec![],
605 });
606 let actual = vec![80].try_into();
607 assert_eq!(expected, actual);
608 }
609
610 #[test]
611 fn parse_binary_example_1() {
612 let expected = Ok(ObjectIdentifier {
613 root: ObjectIdentifierRoot::ItuT,
614 first_node: 0x01,
615 child_nodes: vec![1, 2, 3, 5, 8, 13, 21],
616 });
617 let actual = vec![0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0D, 0x15].try_into();
618 assert_eq!(expected, actual);
619 }
620
621 #[test]
622 fn parse_binary_example_2() {
623 let expected = Ok(ObjectIdentifier {
624 root: ObjectIdentifierRoot::JointIsoItuT,
625 first_node: 39,
626 child_nodes: vec![42, 2501, 65535, 2147483647, 1235, 2352],
627 });
628 let actual = vec![
629 0x77, 0x2A, 0x93, 0x45, 0x83, 0xFF, 0x7F, 0x87, 0xFF, 0xFF, 0xFF, 0x7F, 0x89, 0x53,
630 0x92, 0x30,
631 ]
632 .try_into();
633 assert_eq!(expected, actual);
634 }
635
636 #[test]
637 fn parse_string_root_node_0() {
638 let expected = Ok(ObjectIdentifier {
639 root: ObjectIdentifierRoot::ItuT,
640 first_node: 0x00,
641 child_nodes: vec![],
642 });
643 let actual = "0.0".try_into();
644 assert_eq!(expected, actual);
645 }
646
647 #[test]
648 fn parse_string_root_node_1() {
649 let expected = Ok(ObjectIdentifier {
650 root: ObjectIdentifierRoot::Iso,
651 first_node: 0x00,
652 child_nodes: vec![],
653 });
654 let actual = "1.0".try_into();
655 assert_eq!(expected, actual);
656 }
657
658 #[test]
659 fn parse_string_root_node_2() {
660 let expected = Ok(ObjectIdentifier {
661 root: ObjectIdentifierRoot::JointIsoItuT,
662 first_node: 0x00,
663 child_nodes: vec![],
664 });
665 let actual = "2.0".try_into();
666 assert_eq!(expected, actual);
667 }
668
669 #[test]
670 fn parse_string_example_1() {
671 let expected = Ok(ObjectIdentifier {
672 root: ObjectIdentifierRoot::ItuT,
673 first_node: 0x01,
674 child_nodes: vec![1, 2, 3, 5, 8, 13, 21],
675 });
676 let actual = "0.1.1.2.3.5.8.13.21".try_into();
677 assert_eq!(expected, actual);
678 }
679
680 #[test]
681 fn parse_string_example_2() {
682 let expected = Ok(ObjectIdentifier {
683 root: ObjectIdentifierRoot::JointIsoItuT,
684 first_node: 39,
685 child_nodes: vec![42, 2501, 65535, 2147483647, 1235, 2352],
686 });
687 let actual = "2.39.42.2501.65535.2147483647.1235.2352".try_into();
688 assert_eq!(expected, actual);
689 }
690
691 #[test]
692 fn illegal_oid_root() {
693 let expected = Err(ObjectIdentifierError::IllegalRootNode);
694 for i in 3..core::u8::MAX {
695 let actual = ObjectIdentifierRoot::try_from(i);
696 assert_eq!(expected, actual);
697 }
698 }
699
700 #[test]
701 fn illegal_first_node_too_large() {
702 let expected = Err(ObjectIdentifierError::IllegalFirstChildNode);
703 for i in 40..core::u8::MAX {
704 let string_val = format!("{}.2.3.4", i);
705 let mut nodes_iter = string_val.split(".");
706 let actual = parse_string_first_node(&mut nodes_iter);
707 assert_eq!(expected, actual);
708 }
709 }
710
711 #[test]
712 fn illegal_first_node_empty() {
713 let expected = Err(ObjectIdentifierError::IllegalFirstChildNode);
714 let string_val = String::new();
715 let mut nodes_iter = string_val.split(".");
716 let actual = parse_string_first_node(&mut nodes_iter);
717 assert_eq!(expected, actual);
718 }
719
720 #[test]
721 fn illegal_first_node_none() {
722 let expected = Err(ObjectIdentifierError::IllegalFirstChildNode);
723 let string_val = String::new();
724 let mut nodes_iter = string_val.split(".");
725 let _ = nodes_iter.next();
726 let actual = parse_string_first_node(&mut nodes_iter);
727 assert_eq!(expected, actual);
728 }
729
730 #[test]
731 fn illegal_first_node_large() {
732 let expected = Err(ObjectIdentifierError::IllegalFirstChildNode);
733 let string_val = String::from("40");
734 let mut nodes_iter = string_val.split(".");
735 let actual = parse_string_first_node(&mut nodes_iter);
736 assert_eq!(expected, actual);
737 }
738
739 #[test]
740 fn parse_string_crap() {
741 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
742 Err(ObjectIdentifierError::IllegalRootNode);
743 let actual = "wtf".try_into();
744 assert_eq!(expected, actual);
745 }
746
747 #[test]
748 fn parse_string_empty() {
749 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
750 Err(ObjectIdentifierError::IllegalRootNode);
751 let actual = String::new().try_into();
752 assert_eq!(expected, actual);
753 }
754
755 #[test]
756 fn parse_binary_empty() {
757 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
758 Err(ObjectIdentifierError::IllegalRootNode);
759 let actual = vec![].try_into();
760 assert_eq!(expected, actual);
761 }
762
763 #[cfg(feature = "u32")]
764 #[test]
765 fn parse_binary_example_over_u32() {
766 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
767 Err(ObjectIdentifierError::IllegalChildNodeValue);
768 let actual = vec![0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00].try_into();
769 assert_eq!(expected, actual);
770 }
771
772 #[test]
773 fn parse_binary_example_over_u128() {
774 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
775 Err(ObjectIdentifierError::IllegalChildNodeValue);
776 let actual = vec![
777 0x00, 0x89, 0x97, 0xBF, 0xA3, 0xB8, 0xE8, 0xB3, 0xE6, 0xFB, 0xF2, 0xEA, 0xC3, 0xCA,
778 0xF2, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
779 ]
780 .try_into();
781 assert_eq!(expected, actual);
782 }
783 #[test]
784 fn parse_string_root_node_3plus() {
785 for i in 3..=core::u8::MAX {
786 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
787 Err(ObjectIdentifierError::IllegalRootNode);
788 let actual = format!("{}", i).try_into();
789 assert_eq!(expected, actual);
790 }
791 }
792
793 #[cfg(feature = "u32")]
794 #[test]
795 fn parse_string_example_over_u32() {
796 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
797 Err(ObjectIdentifierError::IllegalChildNodeValue);
798 let actual = "1.1.10000000000".try_into();
799 assert_eq!(expected, actual);
800 }
801
802 #[test]
803 fn parse_string_example_over_u128() {
804 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
805 Err(ObjectIdentifierError::IllegalChildNodeValue);
806 let actual = "1.1.349239782398732987223423423423423423423423423423434982342342342342342342324523453452345234523452345234523452345234537234987234".try_into();
807 assert_eq!(expected, actual);
808 }
809
810 #[test]
811 fn parse_string_example_first_node_over_39() {
812 let expected: Result<ObjectIdentifier, ObjectIdentifierError> =
813 Err(ObjectIdentifierError::IllegalFirstChildNode);
814 let actual = "1.40.1.2.3".try_into();
815 assert_eq!(expected, actual);
816 }
817
818 #[test]
819 fn encode_to_string() {
820 let expected = String::from("1.2.3.4");
821 let actual: String = ObjectIdentifier {
822 root: ObjectIdentifierRoot::Iso,
823 first_node: 2,
824 child_nodes: vec![3, 4],
825 }
826 .into();
827 assert_eq!(expected, actual);
828 }
829
830 #[test]
831 fn encode_to_bytes() {
832 let expected = vec![0x2A, 0x03, 0x04];
833 let actual: Vec<u8> = ObjectIdentifier {
834 root: ObjectIdentifierRoot::Iso,
835 first_node: 2,
836 child_nodes: vec![3, 4],
837 }
838 .into();
839 assert_eq!(expected, actual);
840 }
841 }