-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathder_basic_boolean.rs
50 lines (39 loc) · 1.43 KB
/
der_basic_boolean.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use asn1rs::descriptor::boolean::NoConstraint;
use asn1rs::descriptor::{Boolean, ReadableType, WritableType};
use asn1rs::prelude::basic::DER;
#[test]
pub fn test_der_basic_boolean() {
for bool_value in [true, false] {
let mut buffer = Vec::new();
let mut writer = DER::writer(&mut buffer);
Boolean::<NoConstraint>::write_value(&mut writer, &bool_value).unwrap();
assert_eq!(
&[0x01, 0x01, if bool_value { 0x01 } else { 0x00 }],
&buffer[..]
);
let mut reader = DER::reader(&buffer[..]);
let result = Boolean::<NoConstraint>::read_value(&mut reader).unwrap();
assert_eq!(bool_value, result)
}
}
#[test]
pub fn test_der_basic_boolean_false_from_0x00() {
let mut reader = DER::reader(&[0x01, 0x01, 0xFF][..]);
let result = Boolean::<NoConstraint>::read_value(&mut reader).unwrap();
assert_eq!(true, result)
}
#[test]
pub fn test_der_basic_boolean_true_from_0xff() {
let mut reader = DER::reader(&[0x01, 0x01, 0xFF][..]);
let result = Boolean::<NoConstraint>::read_value(&mut reader).unwrap();
assert_eq!(true, result)
}
#[test]
pub fn test_der_basic_boolean_true_from_any_greater_zero() {
for value in 1..=u8::MAX {
let values = [0x01, 0x01, value];
let mut reader = DER::reader(&values[..]);
let result = Boolean::<NoConstraint>::read_value(&mut reader).unwrap();
assert_eq!(true, result)
}
}