use minicbor::bytes::ByteSlice; use minicbor::Encoder; use seedle::infallible_encoder::InfallibleEncoder; use seedle_macros::seedle; use std::ffi::c_void; #[seedle(file = "examples/test.cddl", language = "c")] pub mod c {} #[seedle(file = "examples/test.cddl", language = "ts")] pub mod ts {} fn make_byte_str(s: &str) -> [u8; N] { let mut ret = [0; N]; let min = std::cmp::min(s.len(), N); ret[0..min].copy_from_slice(&s.as_bytes()[0..min]); ret[min..].fill(0); ret } fn make_netw() -> c::network { c::network { dhcp: true, ip: make_byte_str("192.168.168.1"), sn: make_byte_str("255.255.255.0"), gw: make_byte_str("192.168.168.0"), mac: [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF], ids: [1, 2], ..Default::default() } } #[test] fn test_enum() { assert_eq!(c::KEY::NETWORK as u8, 0); assert_eq!(c::KEY::PORT as u8, 1); assert_eq!(c::KEY::THING as u8, 2); } #[test] fn test_literals() { assert_eq!(c::GROUPA_LITERAL_CHAR, 'C'); assert_eq!(c::GROUPA_LITERAL_THREE, 3); assert_eq!(c::GROUPB_LITERAL_FOUR, 4); } #[test] fn test_encode_decode() { let mut buff = [0; 4096]; let mut decoded = std::mem::MaybeUninit::::uninit(); let netw = make_netw(); let cap = c::len(c::KEY::NETWORK, &netw as *const c::network as *const c_void); let ret_encode = c::encode( buff.as_mut_ptr(), cap as u32, c::KEY::NETWORK, &netw as *const c::network as *const c_void, ); let mut encoder = Encoder::new(InfallibleEncoder::new(cap as usize)); encoder.encode(&netw).unwrap(); let ret_decode = c::decode( decoded.as_mut_ptr() as *mut c_void, c::KEY::NETWORK, buff.as_ptr(), cap, ); let decoded = unsafe { decoded.assume_init_mut() }; assert_eq!(cap as i32, ret_encode); assert_eq!(cap as i32, ret_decode); assert_eq!(encoder.into_writer().into_inner(), buff[0..cap as usize]); assert_eq!(*decoded, netw); } #[test] fn test_encode_decode_array() { let mut buff = [0; 4096]; let netw = [make_netw(), make_netw()]; let mut decoded = std::mem::MaybeUninit::<[c::network; 2]>::uninit(); let cap = c::array_len(c::KEY::NETWORK, netw.as_ptr() as *const c_void, 2); let ret_encode = c::encode_array( buff.as_mut_ptr(), cap as u32, c::KEY::NETWORK, netw.as_ptr() as *const c_void, 2, ); let mut encoder = Encoder::new(InfallibleEncoder::new(cap as usize)); encoder.encode(&netw).unwrap(); let ret_decode = c::decode_array( decoded.as_mut_ptr() as *mut c_void, 2, c::KEY::NETWORK, buff.as_ptr(), cap, ); let decoded = unsafe { decoded.assume_init_mut() }; assert_eq!(cap as i32, ret_encode); assert_eq!(cap as i32, ret_decode); assert_eq!(encoder.into_writer().into_inner(), buff[0..cap as usize]); assert_eq!(*decoded, netw); } #[test] fn test_mcbor_encode_num() { let mut actual: [u8; 1] = [0; 1]; let mut expect: [u8; 1] = [0; 1]; let ret = c::cbor_encode_num(actual.as_mut_ptr(), actual.len() as u32, 2); Encoder::new(expect.as_mut()).i64(2).unwrap(); assert_eq!(1, ret); assert_eq!(expect, actual); } #[test] fn test_mcbor_encode_bytes() { let dat = vec![0, 1, 2, 3]; let enclen = c::cbor_len_bytes(dat.as_ptr(), dat.len() as u32); let mut actual = vec![0; enclen as usize]; let ret = c::cbor_encode_bytes( actual.as_mut_ptr(), actual.len() as u32, dat.as_ptr(), dat.len() as u32, ); assert_eq!(enclen, ret as u32); assert_eq!( vec![0, 1, 2, 3], minicbor::decode::<&ByteSlice>(actual.as_ref()) .unwrap() .to_vec() ); } #[test] fn test_mcbor_encode_str() { let dat = vec![b'h', b'e', b'l', b'l', b'o', b'\0']; let enclen = c::cbor_len_str(dat.as_ptr() as *const i8); let mut actual = vec![0; enclen as usize]; let ret = c::cbor_encode_str( actual.as_mut_ptr(), actual.len() as u32, dat.as_ptr() as *const i8, ); assert_eq!(enclen, ret as i32); assert_eq!("hello", minicbor::decode::<&str>(actual.as_ref()).unwrap()); } #[test] fn test_mcbor_encode_fixed_array() { let mut actual: [u8; 3] = [0; 3]; let ret = c::cbor_encode_fixed_array(actual.as_mut_ptr(), 3, 2); assert_eq!(1, ret); let ret = c::cbor_encode_num(actual[1..].as_mut_ptr(), 2, 4); assert_eq!(1, ret); let ret = c::cbor_encode_num(actual[2..].as_mut_ptr(), 1, 2); assert_eq!(1, ret); assert_eq!([4, 2], minicbor::decode::<[u8; 2]>(&actual).unwrap()); } #[test] fn test_mcbor_encode_map() { let mut actual: [u8; 3] = [0; 3]; let ret = c::cbor_encode_map(actual.as_mut_ptr(), 3, 2); assert_eq!(1, ret); let ret = c::cbor_encode_num(actual[1..].as_mut_ptr(), 2, 4); assert_eq!(1, ret); let ret = c::cbor_encode_num(actual[2..].as_mut_ptr(), 1, 2); assert_eq!(1, ret); let mut decoder = minicbor::decode::Decoder::new(&actual); assert!(decoder.map().is_ok()); assert_eq!(4, decoder.u8().unwrap()); assert_eq!(2, decoder.u8().unwrap()); } #[test] fn test_mcbor_decode_bool() { let mut buf = [0; 1]; let mut uut = false; let ret = c::cbor_encode_bool(buf.as_mut_ptr(), 1, true); assert_eq!(1, ret); let ret = c::cbor_decode_bool(&mut uut as *mut bool, buf.as_mut_ptr(), 1); assert_eq!(1, ret); assert_eq!(true, uut); let ret = c::cbor_encode_bool(buf.as_mut_ptr(), 1, false); assert_eq!(1, ret); let ret = c::cbor_decode_bool(&mut uut as *mut bool, buf.as_mut_ptr(), 1); assert_eq!(1, ret); assert_eq!(false, uut); } #[test] fn test_mcbor_decode_map() { let mut buf = [0; 1]; let ret = c::cbor_encode_map(buf.as_mut_ptr(), 1, 2); assert_eq!(1, ret); let ret = c::cbor_decode_map(buf.as_mut_ptr(), 1); assert_eq!(2, ret); } #[test] fn test_mcbor_decode_fixed_array() { let mut buf = [0; 1]; let ret = c::cbor_encode_fixed_array(buf.as_mut_ptr(), 1, 2); assert_eq!(1, ret); let ret = c::cbor_decode_fixed_array(buf.as_mut_ptr(), 1); assert_eq!(2, ret); } #[test] fn test_mcbor_decode_bytes() { let mut actual = [0; 3]; let mut data = [0; 4]; let mut encoder = Encoder::new(data.as_mut()); encoder.bytes(&[0, 1, 2]).unwrap(); let ret = c::cbor_decode_bytes(actual.as_mut_ptr(), 3, data.as_ptr(), 4); assert_eq!(3, ret); assert_eq!([0, 1, 2], actual); } #[test] fn test_mcbor_decode_str() { let mut actual = [0; 3]; let mut data = [0; 4]; let mut encoder = Encoder::new(data.as_mut()); encoder.str("hii").unwrap(); let ret = c::cbor_decode_str(actual.as_mut_ptr(), 3, data.as_ptr(), 4); assert_eq!(3, ret); assert_eq!("hii".as_bytes(), actual); } #[test] fn test_mcbor_encode_str_ts() { let hello = ts::cbor_encode_str("hello".to_owned()); assert_eq!("hello", ts::cbor_decode_str(&hello).unwrap()); } #[test] fn test_mcbor_encode_num_ts() { let hello = ts::cbor_encode_num(42); assert_eq!(42, ts::cbor_decode_num(&hello).unwrap()); }