|
1 | 1 | use std::sync::Once; |
2 | 2 |
|
3 | | -use asn1_parser::{Asn1, Asn1Decoder, Asn1Encoder, Asn1Type, MetaInfo, ObjectIdentifier, Taggable}; |
| 3 | +use asn1_parser::{Asn1, Asn1Decoder, Asn1Encoder, Asn1Type, BitString, MetaInfo, ObjectIdentifier, Tag, Taggable}; |
4 | 4 | use prop_strategies::any_asn1_type; |
5 | 5 | use proptest::proptest; |
6 | 6 |
|
@@ -305,3 +305,52 @@ fn test_2() { |
305 | 305 | let asn1 = Asn1::decode_buff(raw).unwrap(); |
306 | 306 | println!("{:?}", asn1); |
307 | 307 | } |
| 308 | + |
| 309 | +#[test] |
| 310 | +fn empty_bitstring() { |
| 311 | + init_logging(); |
| 312 | + |
| 313 | + let raw = [0x03, 0x01, 0x00]; |
| 314 | + let asn1 = Asn1::decode_buff(&raw).expect("Failed to decode empty BitString"); |
| 315 | + |
| 316 | + assert_eq!(asn1.inner_asn1().tag(), Tag::from(3), "Tag should be 0x03"); |
| 317 | + |
| 318 | + if let Asn1Type::BitString(bitstring) = asn1.inner_asn1() { |
| 319 | + assert_eq!(bitstring.raw_bits(), &[0], "Raw bits should be [0]"); |
| 320 | + assert_eq!(bitstring.bits_amount(), 0, "Bits amount should be 0"); |
| 321 | + assert!(bitstring.inner().is_none(), "Inner should be None"); |
| 322 | + } else { |
| 323 | + panic!("Expected BitString type"); |
| 324 | + } |
| 325 | + |
| 326 | + // Test encoding back |
| 327 | + let mut encoded = vec![0; asn1.needed_buf_size()]; |
| 328 | + asn1.encode_buff(&mut encoded) |
| 329 | + .expect("Failed to encode empty BitString"); |
| 330 | + assert_eq!(encoded, raw, "Encoded bytes should match original"); |
| 331 | + |
| 332 | + // Test creating an empty BitString directly |
| 333 | + let empty_bits = BitString::from_raw_vec(0, vec![]).expect("Failed to create empty BitString"); |
| 334 | + assert_eq!(empty_bits.raw_bits(), &[0], "Created raw bits should be [0]"); |
| 335 | + assert_eq!(empty_bits.bits_amount(), 0, "Created bits amount should be 0"); |
| 336 | + assert!(empty_bits.inner().is_none(), "Created inner should be None"); |
| 337 | + |
| 338 | + // Test invalid decoding: BitString with length 0 (invalid in DER) |
| 339 | + let invalid_raw = [0x03, 0x00]; |
| 340 | + assert!( |
| 341 | + Asn1::decode_buff(&invalid_raw).is_err(), |
| 342 | + "Decoding length 0 should fail" |
| 343 | + ); |
| 344 | + |
| 345 | + // Test invalid creation: too many bits |
| 346 | + assert!( |
| 347 | + BitString::from_raw_vec(1, vec![]).is_err(), |
| 348 | + "Creating with too many bits should fail" |
| 349 | + ); |
| 350 | + |
| 351 | + // Test invalid creation: too many unused bits |
| 352 | + assert!( |
| 353 | + BitString::from_raw_vec(0, vec![0]).is_err(), |
| 354 | + "Creating with excess unused bits should fail" |
| 355 | + ); |
| 356 | +} |
0 commit comments