Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit b4e4038

Browse files
kirushik5chdn
authored andcommitted
Additional tests for uint deserialization. (#10280)
1 parent 7a8e597 commit b4e4038

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

rpc/src/v1/types/bytes.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a> Visitor<'a> for BytesVisitor {
7474
}
7575

7676
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error {
77-
if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 {
77+
if value.len() >= 2 && value.starts_with("0x") && value.len() & 1 == 0 {
7878
Ok(Bytes::new(FromHex::from_hex(&value[2..]).map_err(|e| Error::custom(format!("Invalid hex: {}", e)))?))
7979
} else {
8080
Err(Error::custom("Invalid bytes format. Expected a 0x-prefixed hex string with even length"))
@@ -101,6 +101,7 @@ mod tests {
101101

102102
#[test]
103103
fn test_bytes_deserialize() {
104+
let bytes0: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""∀∂""#);
104105
let bytes1: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""""#);
105106
let bytes2: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""0x123""#);
106107
let bytes3: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""0xgg""#);
@@ -109,6 +110,7 @@ mod tests {
109110
let bytes5: Bytes = serde_json::from_str(r#""0x12""#).unwrap();
110111
let bytes6: Bytes = serde_json::from_str(r#""0x0123""#).unwrap();
111112

113+
assert!(bytes0.is_err());
112114
assert!(bytes1.is_err());
113115
assert!(bytes2.is_err());
114116
assert!(bytes3.is_err());

rpc/src/v1/types/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ macro_rules! impl_hash {
129129

130130
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: serde::de::Error {
131131

132-
if value.len() < 2 || &value[0..2] != "0x" {
132+
if value.len() < 2 || !value.starts_with("0x") {
133133
return Err(E::custom("expected a hex-encoded hash with 0x prefix"));
134134
}
135135
if value.len() != 2 + $size * 2 {

rpc/src/v1/types/uint.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ macro_rules! impl_uint {
7272
}
7373

7474
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: serde::de::Error {
75-
if value.len() < 2 || &value[0..2] != "0x" {
75+
if value.len() < 2 || !value.starts_with("0x") {
7676
return Err(E::custom("expected a hex-encoded numbers with 0x prefix"))
7777
}
7878

@@ -140,12 +140,14 @@ mod tests {
140140

141141
#[test]
142142
fn should_fail_to_deserialize_decimals() {
143+
let deserialized0: Res = serde_json::from_str(r#""∀∂""#);
143144
let deserialized1: Res = serde_json::from_str(r#""""#);
144145
let deserialized2: Res = serde_json::from_str(r#""0""#);
145146
let deserialized3: Res = serde_json::from_str(r#""10""#);
146147
let deserialized4: Res = serde_json::from_str(r#""1000000""#);
147148
let deserialized5: Res = serde_json::from_str(r#""1000000000000000000""#);
148149

150+
assert!(deserialized0.is_err());
149151
assert!(deserialized1.is_err());
150152
assert!(deserialized2.is_err());
151153
assert!(deserialized3.is_err());

0 commit comments

Comments
 (0)