serial.rs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. use std::{
  2. borrow::Cow,
  3. io,
  4. io::{Cursor, Read, Write},
  5. mem,
  6. net::{IpAddr, SocketAddr},
  7. };
  8. use num_bigint::BigUint;
  9. use crate::{
  10. endian,
  11. error::{Error, Result},
  12. };
  13. /// Encode an object into a vector
  14. pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
  15. let mut encoder = Vec::new();
  16. let len = data.encode(&mut encoder).unwrap();
  17. assert_eq!(len, encoder.len());
  18. encoder
  19. }
  20. /// Encode an object into a hex-encoded string
  21. pub fn serialize_hex<T: Encodable + ?Sized>(data: &T) -> String {
  22. hex::encode(serialize(data))
  23. }
  24. /// Deserialize an object from a vector, will error if said deserialization
  25. /// doesn't consume the entire vector.
  26. pub fn deserialize<T: Decodable>(data: &[u8]) -> Result<T> {
  27. let (rv, consumed) = deserialize_partial(data)?;
  28. // Fail if data are not consumed entirely.
  29. if consumed == data.len() {
  30. Ok(rv)
  31. } else {
  32. Err(Error::ParseFailed("data not consumed entirely when explicitly deserializing"))
  33. }
  34. }
  35. /// Deserialize an object from a vector, but will not report an error if said
  36. /// deserialization doesn't consume the entire vector.
  37. pub fn deserialize_partial<T: Decodable>(data: &[u8]) -> Result<(T, usize)> {
  38. let mut decoder = Cursor::new(data);
  39. let rv = Decodable::decode(&mut decoder)?;
  40. let consumed = decoder.position() as usize;
  41. Ok((rv, consumed))
  42. }
  43. /// Extensions of `Write` to encode data as per Bitcoin consensus
  44. pub trait WriteExt {
  45. /// Output a 64-bit uint
  46. fn write_u64(&mut self, v: u64) -> Result<()>;
  47. /// Output a 32-bit uint
  48. fn write_u32(&mut self, v: u32) -> Result<()>;
  49. /// Output a 16-bit uint
  50. fn write_u16(&mut self, v: u16) -> Result<()>;
  51. /// Output a 8-bit uint
  52. fn write_u8(&mut self, v: u8) -> Result<()>;
  53. /// Output a 64-bit int
  54. fn write_i64(&mut self, v: i64) -> Result<()>;
  55. /// Output a 32-bit int
  56. fn write_i32(&mut self, v: i32) -> Result<()>;
  57. /// Output a 16-bit int
  58. fn write_i16(&mut self, v: i16) -> Result<()>;
  59. /// Output a 8-bit int
  60. fn write_i8(&mut self, v: i8) -> Result<()>;
  61. /// Output a boolean
  62. fn write_bool(&mut self, v: bool) -> Result<()>;
  63. /// Output a byte slice
  64. fn write_slice(&mut self, v: &[u8]) -> Result<()>;
  65. }
  66. /// Extensions of `Read` to decode data as per Bitcoin consensus
  67. pub trait ReadExt {
  68. /// Read a 64-bit uint
  69. fn read_u64(&mut self) -> Result<u64>;
  70. /// Read a 32-bit uint
  71. fn read_u32(&mut self) -> Result<u32>;
  72. /// Read a 16-bit uint
  73. fn read_u16(&mut self) -> Result<u16>;
  74. /// Read a 8-bit uint
  75. fn read_u8(&mut self) -> Result<u8>;
  76. /// Read a 64-bit int
  77. fn read_i64(&mut self) -> Result<i64>;
  78. /// Read a 32-bit int
  79. fn read_i32(&mut self) -> Result<i32>;
  80. /// Read a 16-bit int
  81. fn read_i16(&mut self) -> Result<i16>;
  82. /// Read a 8-bit int
  83. fn read_i8(&mut self) -> Result<i8>;
  84. /// Read a boolean
  85. fn read_bool(&mut self) -> Result<bool>;
  86. /// Read a byte slice
  87. fn read_slice(&mut self, slice: &mut [u8]) -> Result<()>;
  88. }
  89. macro_rules! encoder_fn {
  90. ($name:ident, $val_type:ty, $writefn:ident) => {
  91. #[inline]
  92. fn $name(&mut self, v: $val_type) -> Result<()> {
  93. self.write_all(&endian::$writefn(v)).map_err(|e| Error::Io(e.kind()))
  94. }
  95. };
  96. }
  97. macro_rules! decoder_fn {
  98. ($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => {
  99. #[inline]
  100. fn $name(&mut self) -> Result<$val_type> {
  101. assert_eq!(::std::mem::size_of::<$val_type>(), $byte_len); // size_of isn't a constfn in 1.22
  102. let mut val = [0; $byte_len];
  103. self.read_exact(&mut val[..]).map_err(|e| Error::Io(e.kind()))?;
  104. Ok(endian::$readfn(&val))
  105. }
  106. };
  107. }
  108. impl<W: Write> WriteExt for W {
  109. encoder_fn!(write_u64, u64, u64_to_array_le);
  110. encoder_fn!(write_u32, u32, u32_to_array_le);
  111. encoder_fn!(write_u16, u16, u16_to_array_le);
  112. encoder_fn!(write_i64, i64, i64_to_array_le);
  113. encoder_fn!(write_i32, i32, i32_to_array_le);
  114. encoder_fn!(write_i16, i16, i16_to_array_le);
  115. #[inline]
  116. fn write_i8(&mut self, v: i8) -> Result<()> {
  117. self.write_all(&[v as u8]).map_err(|e| Error::Io(e.kind()))
  118. }
  119. #[inline]
  120. fn write_u8(&mut self, v: u8) -> Result<()> {
  121. self.write_all(&[v]).map_err(|e| Error::Io(e.kind()))
  122. }
  123. #[inline]
  124. fn write_bool(&mut self, v: bool) -> Result<()> {
  125. self.write_all(&[v as u8]).map_err(|e| Error::Io(e.kind()))
  126. }
  127. #[inline]
  128. fn write_slice(&mut self, v: &[u8]) -> Result<()> {
  129. self.write_all(v).map_err(|e| Error::Io(e.kind()))
  130. }
  131. }
  132. impl<R: Read> ReadExt for R {
  133. decoder_fn!(read_u64, u64, slice_to_u64_le, 8);
  134. decoder_fn!(read_u32, u32, slice_to_u32_le, 4);
  135. decoder_fn!(read_u16, u16, slice_to_u16_le, 2);
  136. decoder_fn!(read_i64, i64, slice_to_i64_le, 8);
  137. decoder_fn!(read_i32, i32, slice_to_i32_le, 4);
  138. decoder_fn!(read_i16, i16, slice_to_i16_le, 2);
  139. #[inline]
  140. fn read_u8(&mut self) -> Result<u8> {
  141. let mut slice = [0u8; 1];
  142. self.read_exact(&mut slice)?;
  143. Ok(slice[0])
  144. }
  145. #[inline]
  146. fn read_i8(&mut self) -> Result<i8> {
  147. let mut slice = [0u8; 1];
  148. self.read_exact(&mut slice)?;
  149. Ok(slice[0] as i8)
  150. }
  151. #[inline]
  152. fn read_bool(&mut self) -> Result<bool> {
  153. ReadExt::read_i8(self).map(|bit| bit != 0)
  154. }
  155. #[inline]
  156. fn read_slice(&mut self, slice: &mut [u8]) -> Result<()> {
  157. self.read_exact(slice).map_err(|e| Error::Io(e.kind()))
  158. }
  159. }
  160. /// Data which can be encoded in a consensus-consistent way
  161. pub trait Encodable {
  162. /// Encode an object with a well-defined format, should only ever error if
  163. /// the underlying `Write` errors. Returns the number of bytes written on
  164. /// success
  165. fn encode<W: io::Write>(&self, e: W) -> Result<usize>;
  166. }
  167. /// Data which can be encoded in a consensus-consistent way
  168. pub trait Decodable: Sized {
  169. /// Decode an object with a well-defined format
  170. fn decode<D: io::Read>(d: D) -> Result<Self>;
  171. }
  172. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
  173. pub struct VarInt(pub u64);
  174. // Primitive types
  175. macro_rules! impl_int_encodable {
  176. ($ty:ident, $meth_dec:ident, $meth_enc:ident) => {
  177. impl Decodable for $ty {
  178. #[inline]
  179. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  180. ReadExt::$meth_dec(&mut d).map($ty::from_le)
  181. }
  182. }
  183. impl Encodable for $ty {
  184. #[inline]
  185. fn encode<S: WriteExt>(&self, mut s: S) -> Result<usize> {
  186. s.$meth_enc(self.to_le())?;
  187. Ok(mem::size_of::<$ty>())
  188. }
  189. }
  190. };
  191. }
  192. impl_int_encodable!(u8, read_u8, write_u8);
  193. impl_int_encodable!(u16, read_u16, write_u16);
  194. impl_int_encodable!(u32, read_u32, write_u32);
  195. impl_int_encodable!(u64, read_u64, write_u64);
  196. impl_int_encodable!(i8, read_i8, write_i8);
  197. impl_int_encodable!(i16, read_i16, write_i16);
  198. impl_int_encodable!(i32, read_i32, write_i32);
  199. impl_int_encodable!(i64, read_i64, write_i64);
  200. impl VarInt {
  201. /// Gets the length of this VarInt when encoded.
  202. /// Returns 1 for 0...0xFC, 3 for 0xFD...(2^16-1), 5 for 0x10000...(2^32-1),
  203. /// and 9 otherwise.
  204. #[inline]
  205. pub fn length(&self) -> usize {
  206. match self.0 {
  207. 0..=0xFC => 1,
  208. 0xFD..=0xFFFF => 3,
  209. 0x10000..=0xFFFFFFFF => 5,
  210. _ => 9,
  211. }
  212. }
  213. }
  214. impl Encodable for VarInt {
  215. #[inline]
  216. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  217. match self.0 {
  218. 0..=0xFC => {
  219. (self.0 as u8).encode(s)?;
  220. Ok(1)
  221. }
  222. 0xFD..=0xFFFF => {
  223. s.write_u8(0xFD)?;
  224. (self.0 as u16).encode(s)?;
  225. Ok(3)
  226. }
  227. 0x10000..=0xFFFFFFFF => {
  228. s.write_u8(0xFE)?;
  229. (self.0 as u32).encode(s)?;
  230. Ok(5)
  231. }
  232. _ => {
  233. s.write_u8(0xFF)?;
  234. (self.0 as u64).encode(s)?;
  235. Ok(9)
  236. }
  237. }
  238. }
  239. }
  240. impl Decodable for VarInt {
  241. #[inline]
  242. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  243. let n = ReadExt::read_u8(&mut d)?;
  244. match n {
  245. 0xFF => {
  246. let x = ReadExt::read_u64(&mut d)?;
  247. if x < 0x100000000 {
  248. Err(self::Error::NonMinimalVarInt)
  249. } else {
  250. Ok(VarInt(x))
  251. }
  252. }
  253. 0xFE => {
  254. let x = ReadExt::read_u32(&mut d)?;
  255. if x < 0x10000 {
  256. Err(self::Error::NonMinimalVarInt)
  257. } else {
  258. Ok(VarInt(x as u64))
  259. }
  260. }
  261. 0xFD => {
  262. let x = ReadExt::read_u16(&mut d)?;
  263. if x < 0xFD {
  264. Err(self::Error::NonMinimalVarInt)
  265. } else {
  266. Ok(VarInt(x as u64))
  267. }
  268. }
  269. n => Ok(VarInt(n as u64)),
  270. }
  271. }
  272. }
  273. // Booleans
  274. impl Encodable for bool {
  275. #[inline]
  276. fn encode<S: WriteExt>(&self, mut s: S) -> Result<usize> {
  277. s.write_bool(*self)?;
  278. Ok(1)
  279. }
  280. }
  281. impl Decodable for bool {
  282. #[inline]
  283. fn decode<D: io::Read>(mut d: D) -> Result<bool> {
  284. ReadExt::read_bool(&mut d)
  285. }
  286. }
  287. // Strings
  288. impl Encodable for String {
  289. #[inline]
  290. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  291. let b = self.as_bytes();
  292. let vi_len = VarInt(b.len() as u64).encode(&mut s)?;
  293. s.write_slice(b)?;
  294. Ok(vi_len + b.len())
  295. }
  296. }
  297. impl Decodable for String {
  298. #[inline]
  299. fn decode<D: io::Read>(d: D) -> Result<String> {
  300. String::from_utf8(Decodable::decode(d)?)
  301. .map_err(|_| self::Error::ParseFailed("String was not valid UTF8"))
  302. }
  303. }
  304. // Cow<'static, str>
  305. impl Encodable for Cow<'static, str> {
  306. #[inline]
  307. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  308. let b = self.as_bytes();
  309. let vi_len = VarInt(b.len() as u64).encode(&mut s)?;
  310. s.write_slice(b)?;
  311. Ok(vi_len + b.len())
  312. }
  313. }
  314. impl Decodable for Cow<'static, str> {
  315. #[inline]
  316. fn decode<D: io::Read>(d: D) -> Result<Cow<'static, str>> {
  317. String::from_utf8(Decodable::decode(d)?)
  318. .map_err(|_| self::Error::ParseFailed("String was not valid UTF8"))
  319. .map(Cow::Owned)
  320. }
  321. }
  322. impl<const N: usize> Encodable for [u8; N] {
  323. #[inline]
  324. fn encode<S: WriteExt>(&self, mut s: S) -> Result<usize> {
  325. s.write_slice(&self[..])?;
  326. Ok(self.len())
  327. }
  328. }
  329. impl<const N: usize> Decodable for [u8; N] {
  330. #[inline]
  331. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  332. let mut ret = [0; N];
  333. d.read_slice(&mut ret)?;
  334. Ok(ret)
  335. }
  336. }
  337. // Options
  338. impl<T: Encodable> Encodable for Option<T> {
  339. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  340. let mut len = 0;
  341. if let Some(v) = self {
  342. len += true.encode(&mut s)?;
  343. len += v.encode(&mut s)?;
  344. } else {
  345. len += false.encode(&mut s)?;
  346. }
  347. Ok(len)
  348. }
  349. }
  350. impl<T: Decodable> Decodable for Option<T> {
  351. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  352. let valid: bool = Decodable::decode(&mut d)?;
  353. let mut val: Option<T> = None;
  354. if valid {
  355. val = Some(Decodable::decode(&mut d)?);
  356. }
  357. Ok(val)
  358. }
  359. }
  360. impl<T: Encodable> Encodable for Vec<Option<T>> {
  361. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  362. let mut len = 0;
  363. len += VarInt(self.len() as u64).encode(&mut s)?;
  364. for val in self {
  365. len += val.encode(&mut s)?;
  366. }
  367. Ok(len)
  368. }
  369. }
  370. impl<T: Decodable> Decodable for Vec<Option<T>> {
  371. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  372. let len = VarInt::decode(&mut d)?.0;
  373. let mut ret = Vec::with_capacity(len as usize);
  374. for _ in 0..len {
  375. ret.push(Decodable::decode(&mut d)?);
  376. }
  377. Ok(ret)
  378. }
  379. }
  380. // Vectors
  381. #[macro_export]
  382. macro_rules! impl_vec {
  383. ($type: ty) => {
  384. impl Encodable for Vec<$type> {
  385. #[inline]
  386. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  387. let mut len = 0;
  388. len += VarInt(self.len() as u64).encode(&mut s)?;
  389. for c in self.iter() {
  390. len += c.encode(&mut s)?;
  391. }
  392. Ok(len)
  393. }
  394. }
  395. impl Decodable for Vec<$type> {
  396. #[inline]
  397. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  398. let len = VarInt::decode(&mut d)?.0;
  399. let mut ret = Vec::with_capacity(len as usize);
  400. for _ in 0..len {
  401. ret.push(Decodable::decode(&mut d)?);
  402. }
  403. Ok(ret)
  404. }
  405. }
  406. };
  407. }
  408. impl_vec!(SocketAddr);
  409. impl_vec!([u8; 32]);
  410. impl Encodable for IpAddr {
  411. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  412. let mut len = 0;
  413. match self {
  414. IpAddr::V4(ip) => {
  415. let version: u8 = 4;
  416. len += version.encode(&mut s)?;
  417. len += ip.octets().encode(s)?;
  418. }
  419. IpAddr::V6(ip) => {
  420. let version: u8 = 6;
  421. len += version.encode(&mut s)?;
  422. len += ip.octets().encode(s)?;
  423. }
  424. }
  425. Ok(len)
  426. }
  427. }
  428. impl Decodable for IpAddr {
  429. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  430. let version: u8 = Decodable::decode(&mut d)?;
  431. match version {
  432. 4 => {
  433. let addr: [u8; 4] = Decodable::decode(&mut d)?;
  434. Ok(IpAddr::from(addr))
  435. }
  436. 6 => {
  437. let addr: [u8; 16] = Decodable::decode(&mut d)?;
  438. Ok(IpAddr::from(addr))
  439. }
  440. _ => Err(Error::ParseFailed("couldn't decode IpAddr")),
  441. }
  442. }
  443. }
  444. impl Encodable for SocketAddr {
  445. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  446. let mut len = 0;
  447. len += self.ip().encode(&mut s)?;
  448. len += self.port().encode(s)?;
  449. Ok(len)
  450. }
  451. }
  452. impl Decodable for SocketAddr {
  453. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  454. let ip = Decodable::decode(&mut d)?;
  455. let port: u16 = Decodable::decode(d)?;
  456. Ok(SocketAddr::new(ip, port))
  457. }
  458. }
  459. pub fn encode_with_size<S: io::Write>(data: &[u8], mut s: S) -> Result<usize> {
  460. let vi_len = VarInt(data.len() as u64).encode(&mut s)?;
  461. s.write_slice(data)?;
  462. Ok(vi_len + data.len())
  463. }
  464. impl Encodable for Vec<u8> {
  465. #[inline]
  466. fn encode<S: io::Write>(&self, s: S) -> Result<usize> {
  467. encode_with_size(self, s)
  468. }
  469. }
  470. impl Decodable for Vec<u8> {
  471. #[inline]
  472. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  473. let len = VarInt::decode(&mut d)?.0 as usize;
  474. let mut ret = vec![0u8; len];
  475. d.read_slice(&mut ret)?;
  476. Ok(ret)
  477. }
  478. }
  479. impl Encodable for Box<[u8]> {
  480. #[inline]
  481. fn encode<S: io::Write>(&self, s: S) -> Result<usize> {
  482. encode_with_size(self, s)
  483. }
  484. }
  485. impl Decodable for Box<[u8]> {
  486. #[inline]
  487. fn decode<D: io::Read>(d: D) -> Result<Self> {
  488. <Vec<u8>>::decode(d).map(From::from)
  489. }
  490. }
  491. impl Encodable for BigUint {
  492. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  493. let bytes = self.to_bytes_le();
  494. s.write_slice(&bytes)?;
  495. Ok(bytes.len())
  496. }
  497. }
  498. impl Decodable for BigUint {
  499. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  500. let mut bytes = vec![];
  501. d.read_slice(&mut bytes)?;
  502. Ok(BigUint::from_bytes_le(&bytes))
  503. }
  504. }
  505. // Tuples
  506. macro_rules! tuple_encode {
  507. ($($x:ident),*) => (
  508. impl <$($x: Encodable),*> Encodable for ($($x),*) {
  509. #[inline]
  510. #[allow(non_snake_case)]
  511. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  512. let &($(ref $x),*) = self;
  513. let mut len = 0;
  514. $(len += $x.encode(&mut s)?;)*
  515. Ok(len)
  516. }
  517. }
  518. impl<$($x: Decodable),*> Decodable for ($($x),*) {
  519. #[inline]
  520. #[allow(non_snake_case)]
  521. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  522. Ok(($({let $x = Decodable::decode(&mut d)?; $x }),*))
  523. }
  524. }
  525. );
  526. }
  527. tuple_encode!(T0, T1);
  528. tuple_encode!(T0, T1, T2, T3);
  529. tuple_encode!(T0, T1, T2, T3, T4, T5);
  530. tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7);
  531. #[cfg(test)]
  532. mod tests {
  533. use super::{deserialize, deserialize_partial, serialize, Encodable, Error, Result, VarInt};
  534. use crate::endian::{u16_to_array_le, u32_to_array_le, u64_to_array_le};
  535. use std::{io, mem::discriminant};
  536. #[test]
  537. fn serialize_int_test() {
  538. // bool
  539. assert_eq!(serialize(&false), vec![0u8]);
  540. assert_eq!(serialize(&true), vec![1u8]);
  541. // u8
  542. assert_eq!(serialize(&1u8), vec![1u8]);
  543. assert_eq!(serialize(&0u8), vec![0u8]);
  544. assert_eq!(serialize(&255u8), vec![255u8]);
  545. // u16
  546. assert_eq!(serialize(&1u16), vec![1u8, 0]);
  547. assert_eq!(serialize(&256u16), vec![0u8, 1]);
  548. assert_eq!(serialize(&5000u16), vec![136u8, 19]);
  549. // u32
  550. assert_eq!(serialize(&1u32), vec![1u8, 0, 0, 0]);
  551. assert_eq!(serialize(&256u32), vec![0u8, 1, 0, 0]);
  552. assert_eq!(serialize(&5000u32), vec![136u8, 19, 0, 0]);
  553. assert_eq!(serialize(&500000u32), vec![32u8, 161, 7, 0]);
  554. assert_eq!(serialize(&168430090u32), vec![10u8, 10, 10, 10]);
  555. // i32
  556. assert_eq!(serialize(&-1i32), vec![255u8, 255, 255, 255]);
  557. assert_eq!(serialize(&-256i32), vec![0u8, 255, 255, 255]);
  558. assert_eq!(serialize(&-5000i32), vec![120u8, 236, 255, 255]);
  559. assert_eq!(serialize(&-500000i32), vec![224u8, 94, 248, 255]);
  560. assert_eq!(serialize(&-168430090i32), vec![246u8, 245, 245, 245]);
  561. assert_eq!(serialize(&1i32), vec![1u8, 0, 0, 0]);
  562. assert_eq!(serialize(&256i32), vec![0u8, 1, 0, 0]);
  563. assert_eq!(serialize(&5000i32), vec![136u8, 19, 0, 0]);
  564. assert_eq!(serialize(&500000i32), vec![32u8, 161, 7, 0]);
  565. assert_eq!(serialize(&168430090i32), vec![10u8, 10, 10, 10]);
  566. // u64
  567. assert_eq!(serialize(&1u64), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
  568. assert_eq!(serialize(&256u64), vec![0u8, 1, 0, 0, 0, 0, 0, 0]);
  569. assert_eq!(serialize(&5000u64), vec![136u8, 19, 0, 0, 0, 0, 0, 0]);
  570. assert_eq!(serialize(&500000u64), vec![32u8, 161, 7, 0, 0, 0, 0, 0]);
  571. assert_eq!(serialize(&723401728380766730u64), vec![10u8, 10, 10, 10, 10, 10, 10, 10]);
  572. // i64
  573. assert_eq!(serialize(&-1i64), vec![255u8, 255, 255, 255, 255, 255, 255, 255]);
  574. assert_eq!(serialize(&-256i64), vec![0u8, 255, 255, 255, 255, 255, 255, 255]);
  575. assert_eq!(serialize(&-5000i64), vec![120u8, 236, 255, 255, 255, 255, 255, 255]);
  576. assert_eq!(serialize(&-500000i64), vec![224u8, 94, 248, 255, 255, 255, 255, 255]);
  577. assert_eq!(
  578. serialize(&-723401728380766730i64),
  579. vec![246u8, 245, 245, 245, 245, 245, 245, 245]
  580. );
  581. assert_eq!(serialize(&1i64), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
  582. assert_eq!(serialize(&256i64), vec![0u8, 1, 0, 0, 0, 0, 0, 0]);
  583. assert_eq!(serialize(&5000i64), vec![136u8, 19, 0, 0, 0, 0, 0, 0]);
  584. assert_eq!(serialize(&500000i64), vec![32u8, 161, 7, 0, 0, 0, 0, 0]);
  585. assert_eq!(serialize(&723401728380766730i64), vec![10u8, 10, 10, 10, 10, 10, 10, 10]);
  586. }
  587. #[test]
  588. fn serialize_varint_test() {
  589. assert_eq!(serialize(&VarInt(10)), vec![10u8]);
  590. assert_eq!(serialize(&VarInt(0xFC)), vec![0xFCu8]);
  591. assert_eq!(serialize(&VarInt(0xFD)), vec![0xFDu8, 0xFD, 0]);
  592. assert_eq!(serialize(&VarInt(0xFFF)), vec![0xFDu8, 0xFF, 0xF]);
  593. assert_eq!(serialize(&VarInt(0xF0F0F0F)), vec![0xFEu8, 0xF, 0xF, 0xF, 0xF]);
  594. assert_eq!(
  595. serialize(&VarInt(0xF0F0F0F0F0E0)),
  596. vec![0xFFu8, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0, 0]
  597. );
  598. assert_eq!(
  599. test_varint_encode(0xFF, &u64_to_array_le(0x100000000)).unwrap(),
  600. VarInt(0x100000000)
  601. );
  602. assert_eq!(test_varint_encode(0xFE, &u64_to_array_le(0x10000)).unwrap(), VarInt(0x10000));
  603. assert_eq!(test_varint_encode(0xFD, &u64_to_array_le(0xFD)).unwrap(), VarInt(0xFD));
  604. // Test that length calc is working correctly
  605. test_varint_len(VarInt(0), 1);
  606. test_varint_len(VarInt(0xFC), 1);
  607. test_varint_len(VarInt(0xFD), 3);
  608. test_varint_len(VarInt(0xFFFF), 3);
  609. test_varint_len(VarInt(0x10000), 5);
  610. test_varint_len(VarInt(0xFFFFFFFF), 5);
  611. test_varint_len(VarInt(0xFFFFFFFF + 1), 9);
  612. test_varint_len(VarInt(u64::max_value()), 9);
  613. }
  614. fn test_varint_len(varint: VarInt, expected: usize) {
  615. let mut encoder = io::Cursor::new(vec![]);
  616. assert_eq!(varint.encode(&mut encoder).unwrap(), expected);
  617. assert_eq!(varint.length(), expected);
  618. }
  619. fn test_varint_encode(n: u8, x: &[u8]) -> Result<VarInt> {
  620. let mut input = [0u8; 9];
  621. input[0] = n;
  622. input[1..x.len() + 1].copy_from_slice(x);
  623. deserialize_partial::<VarInt>(&input).map(|t| t.0)
  624. }
  625. #[test]
  626. fn deserialize_nonminimal_vec() {
  627. // Check the edges for variant int
  628. assert_eq!(
  629. discriminant(&test_varint_encode(0xFF, &u64_to_array_le(0x100000000 - 1)).unwrap_err()),
  630. discriminant(&Error::NonMinimalVarInt)
  631. );
  632. assert_eq!(
  633. discriminant(&test_varint_encode(0xFE, &u32_to_array_le(0x10000 - 1)).unwrap_err()),
  634. discriminant(&Error::NonMinimalVarInt)
  635. );
  636. assert_eq!(
  637. discriminant(&test_varint_encode(0xFD, &u16_to_array_le(0xFD - 1)).unwrap_err()),
  638. discriminant(&Error::NonMinimalVarInt)
  639. );
  640. assert_eq!(
  641. discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0x00, 0x00]).unwrap_err()),
  642. discriminant(&Error::NonMinimalVarInt)
  643. );
  644. assert_eq!(
  645. discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0xfc, 0x00]).unwrap_err()),
  646. discriminant(&Error::NonMinimalVarInt)
  647. );
  648. assert_eq!(
  649. discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0xfc, 0x00]).unwrap_err()),
  650. discriminant(&Error::NonMinimalVarInt)
  651. );
  652. assert_eq!(
  653. discriminant(&deserialize::<Vec<u8>>(&[0xfe, 0xff, 0x00, 0x00, 0x00]).unwrap_err()),
  654. discriminant(&Error::NonMinimalVarInt)
  655. );
  656. assert_eq!(
  657. discriminant(&deserialize::<Vec<u8>>(&[0xfe, 0xff, 0xff, 0x00, 0x00]).unwrap_err()),
  658. discriminant(&Error::NonMinimalVarInt)
  659. );
  660. assert_eq!(
  661. discriminant(
  662. &deserialize::<Vec<u8>>(&[0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
  663. .unwrap_err()
  664. ),
  665. discriminant(&Error::NonMinimalVarInt)
  666. );
  667. assert_eq!(
  668. discriminant(
  669. &deserialize::<Vec<u8>>(&[0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00])
  670. .unwrap_err()
  671. ),
  672. discriminant(&Error::NonMinimalVarInt)
  673. );
  674. let mut vec_256 = vec![0; 259];
  675. vec_256[0] = 0xfd;
  676. vec_256[1] = 0x00;
  677. vec_256[2] = 0x01;
  678. assert!(deserialize::<Vec<u8>>(&vec_256).is_ok());
  679. let mut vec_253 = vec![0; 256];
  680. vec_253[0] = 0xfd;
  681. vec_253[1] = 0xfd;
  682. vec_253[2] = 0x00;
  683. assert!(deserialize::<Vec<u8>>(&vec_253).is_ok());
  684. }
  685. #[test]
  686. fn serialize_vector_test() {
  687. assert_eq!(serialize(&vec![1u8, 2, 3]), vec![3u8, 1, 2, 3]);
  688. // TODO: test vectors of more interesting objects
  689. }
  690. #[test]
  691. fn serialize_strbuf_test() {
  692. assert_eq!(serialize(&"Andrew".to_string()), vec![6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]);
  693. }
  694. #[test]
  695. fn deserialize_int_test() {
  696. // bool
  697. assert!((deserialize(&[58u8, 0]) as Result<bool>).is_err());
  698. assert_eq!(deserialize(&[58u8]).ok(), Some(true));
  699. assert_eq!(deserialize(&[1u8]).ok(), Some(true));
  700. assert_eq!(deserialize(&[0u8]).ok(), Some(false));
  701. assert!((deserialize(&[0u8, 1]) as Result<bool>).is_err());
  702. // u8
  703. assert_eq!(deserialize(&[58u8]).ok(), Some(58u8));
  704. // u16
  705. assert_eq!(deserialize(&[0x01u8, 0x02]).ok(), Some(0x0201u16));
  706. assert_eq!(deserialize(&[0xABu8, 0xCD]).ok(), Some(0xCDABu16));
  707. assert_eq!(deserialize(&[0xA0u8, 0x0D]).ok(), Some(0xDA0u16));
  708. let failure16: Result<u16> = deserialize(&[1u8]);
  709. assert!(failure16.is_err());
  710. // u32
  711. assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABu32));
  712. assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD]).ok(), Some(0xCDAB0DA0u32));
  713. let failure32: Result<u32> = deserialize(&[1u8, 2, 3]);
  714. assert!(failure32.is_err());
  715. // TODO: test negative numbers
  716. assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABi32));
  717. assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0x2D]).ok(), Some(0x2DAB0DA0i32));
  718. let failurei32: Result<i32> = deserialize(&[1u8, 2, 3]);
  719. assert!(failurei32.is_err());
  720. // u64
  721. assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABu64));
  722. assert_eq!(
  723. deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(),
  724. Some(0x99000099CDAB0DA0u64)
  725. );
  726. let failure64: Result<u64> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
  727. assert!(failure64.is_err());
  728. // TODO: test negative numbers
  729. assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABi64));
  730. assert_eq!(
  731. deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(),
  732. Some(-0x66ffff663254f260i64)
  733. );
  734. let failurei64: Result<i64> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
  735. assert!(failurei64.is_err());
  736. }
  737. #[test]
  738. fn deserialize_vec_test() {
  739. assert_eq!(deserialize(&[3u8, 2, 3, 4]).ok(), Some(vec![2u8, 3, 4]));
  740. assert!((deserialize(&[4u8, 2, 3, 4, 5, 6]) as Result<Vec<u8>>).is_err());
  741. }
  742. #[test]
  743. fn deserialize_strbuf_test() {
  744. assert_eq!(
  745. deserialize(&[6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]).ok(),
  746. Some("Andrew".to_string())
  747. );
  748. assert_eq!(
  749. deserialize(&[6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]).ok(),
  750. Some(::std::borrow::Cow::Borrowed("Andrew"))
  751. );
  752. }
  753. }