-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbasic_bitstring.rs
143 lines (125 loc) · 2.91 KB
/
basic_bitstring.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#![recursion_limit = "512"]
mod test_utils;
use asn1rs::descriptor::bitstring::BitVec;
use test_utils::*;
asn_to_rust!(
r"BasicBitString DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
Unconstrained ::= SEQUENCE {
abc BIT STRING
}
BasicConstrained ::= SEQUENCE {
abc BIT STRING (SIZE(8))
}
BasicConstrainedSmall ::= SEQUENCE {
abc BIT STRING (SIZE(4..6))
}
BasicConstrainedExtensible ::= SEQUENCE {
abc BIT STRING (SIZE(4..6,...))
}
SomeContainer ::= SEQUENCE {
some-value BIT STRING {
very-important-flag (0),
not-so-important-flag(1)
} (SIZE(2))
}
END"
);
#[test]
fn test_some_container_flag_set() {
let mut c = SomeContainer {
some_value: BitVec::with_len(2),
};
c.some_value
.set_bit(SomeContainer::SOME_VALUE_VERY_IMPORTANT_FLAG);
serialize_and_deserialize_uper(2, &[0x80], &c);
}
#[test]
fn test_unconstrained_6_bits() {
// from playground
serialize_and_deserialize_uper(
14,
&[0x06, 0xAC],
&Unconstrained {
abc: BitVec::from_bytes(vec![0b1010_1100], 6),
},
);
}
#[test]
fn test_unconstrained_5_bytes() {
// from playground
serialize_and_deserialize_uper(
48,
&[0x28, 0x12, 0x34, 0x56, 0x78, 0x90],
&Unconstrained {
abc: BitVec::from_all_bytes(vec![0x12, 0x34, 0x56, 0x78, 0x90]),
},
);
}
#[test]
fn test_fixed_size() {
// from playground
serialize_and_deserialize_uper(
8,
&[0x12],
&BasicConstrained {
abc: BitVec::from_all_bytes(vec![0x12]),
},
);
}
#[test]
#[should_panic(expected = "SizeNotInRange(8, 4, 6)")]
fn test_too_large() {
// from playground
serialize_and_deserialize_uper(
8,
&[0x12],
&BasicConstrainedSmall {
abc: BitVec::from_all_bytes(vec![0x12]),
},
);
}
#[test]
fn test_small_max() {
// from playground
serialize_and_deserialize_uper(
8,
&[0xBF],
&BasicConstrainedSmall {
abc: BitVec::from_bytes(vec![0xff], 6),
},
);
}
#[test]
fn test_extensible_small() {
// from playground
serialize_and_deserialize_uper(
9,
&[0x55, 0x80],
&BasicConstrainedExtensible {
abc: BitVec::from_bytes(vec![0xaf], 6),
},
);
}
#[test]
fn test_extensible_extended_1() {
// from playground
serialize_and_deserialize_uper(
16,
&[0x83, 0xD6],
&BasicConstrainedExtensible {
abc: BitVec::from_bytes(vec![0b1010_1100], 7),
},
);
}
#[test]
fn test_extensible_extended_7() {
// from playground
serialize_and_deserialize_uper(
23,
&[0x87, 0x56, 0xAC],
&BasicConstrainedExtensible {
abc: BitVec::from_bytes(vec![0b1010_1101, 0b0101_1000], 14),
},
);
}