serial.rs 27 KB

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