Additional tests for uint deserialization. (#10279)

This commit is contained in:
Tomasz Drwięga 2019-02-03 12:26:25 +01:00 committed by Afri Schoedon
parent 0f9b2218da
commit 3b23c2e86d
3 changed files with 7 additions and 3 deletions

View File

@ -74,7 +74,7 @@ impl<'a> Visitor<'a> for BytesVisitor {
} }
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error { fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error {
if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 { if value.len() >= 2 && value.starts_with("0x") && value.len() & 1 == 0 {
Ok(Bytes::new(FromHex::from_hex(&value[2..]).map_err(|e| Error::custom(format!("Invalid hex: {}", e)))?)) Ok(Bytes::new(FromHex::from_hex(&value[2..]).map_err(|e| Error::custom(format!("Invalid hex: {}", e)))?))
} else { } else {
Err(Error::custom("Invalid bytes format. Expected a 0x-prefixed hex string with even length")) Err(Error::custom("Invalid bytes format. Expected a 0x-prefixed hex string with even length"))
@ -101,6 +101,7 @@ mod tests {
#[test] #[test]
fn test_bytes_deserialize() { fn test_bytes_deserialize() {
let bytes0: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""∀∂""#);
let bytes1: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""""#); let bytes1: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""""#);
let bytes2: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""0x123""#); let bytes2: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""0x123""#);
let bytes3: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""0xgg""#); let bytes3: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""0xgg""#);
@ -109,6 +110,7 @@ mod tests {
let bytes5: Bytes = serde_json::from_str(r#""0x12""#).unwrap(); let bytes5: Bytes = serde_json::from_str(r#""0x12""#).unwrap();
let bytes6: Bytes = serde_json::from_str(r#""0x0123""#).unwrap(); let bytes6: Bytes = serde_json::from_str(r#""0x0123""#).unwrap();
assert!(bytes0.is_err());
assert!(bytes1.is_err()); assert!(bytes1.is_err());
assert!(bytes2.is_err()); assert!(bytes2.is_err());
assert!(bytes3.is_err()); assert!(bytes3.is_err());

View File

@ -129,7 +129,7 @@ macro_rules! impl_hash {
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: serde::de::Error { fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: serde::de::Error {
if value.len() < 2 || &value[0..2] != "0x" { if value.len() < 2 || !value.starts_with("0x") {
return Err(E::custom("expected a hex-encoded hash with 0x prefix")); return Err(E::custom("expected a hex-encoded hash with 0x prefix"));
} }
if value.len() != 2 + $size * 2 { if value.len() != 2 + $size * 2 {

View File

@ -72,7 +72,7 @@ macro_rules! impl_uint {
} }
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: serde::de::Error { fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: serde::de::Error {
if value.len() < 2 || &value[0..2] != "0x" { if value.len() < 2 || !value.starts_with("0x") {
return Err(E::custom("expected a hex-encoded numbers with 0x prefix")) return Err(E::custom("expected a hex-encoded numbers with 0x prefix"))
} }
@ -140,12 +140,14 @@ mod tests {
#[test] #[test]
fn should_fail_to_deserialize_decimals() { fn should_fail_to_deserialize_decimals() {
let deserialized0: Res = serde_json::from_str(r#""∀∂""#);
let deserialized1: Res = serde_json::from_str(r#""""#); let deserialized1: Res = serde_json::from_str(r#""""#);
let deserialized2: Res = serde_json::from_str(r#""0""#); let deserialized2: Res = serde_json::from_str(r#""0""#);
let deserialized3: Res = serde_json::from_str(r#""10""#); let deserialized3: Res = serde_json::from_str(r#""10""#);
let deserialized4: Res = serde_json::from_str(r#""1000000""#); let deserialized4: Res = serde_json::from_str(r#""1000000""#);
let deserialized5: Res = serde_json::from_str(r#""1000000000000000000""#); let deserialized5: Res = serde_json::from_str(r#""1000000000000000000""#);
assert!(deserialized0.is_err());
assert!(deserialized1.is_err()); assert!(deserialized1.is_err());
assert!(deserialized2.is_err()); assert!(deserialized2.is_err());
assert!(deserialized3.is_err()); assert!(deserialized3.is_err());