From 75ad6724d4baca194113c57091abd8393677b222 Mon Sep 17 00:00:00 2001 From: Seulgi Kim Date: Thu, 17 Jan 2019 09:57:53 +0900 Subject: [PATCH 1/3] Enhance JSON test cases This patch adds tests to check whether metadata with single quotations is serialized/parsed properly. --- rpc/src/v1/types/action.rs | 59 ++++++++++++++++++++++++++++++-- rpc/src/v1/types/asset_input.rs | 6 ++-- rpc/src/v1/types/asset_output.rs | 6 ++-- rpc/src/v1/types/order.rs | 6 ++-- 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/rpc/src/v1/types/action.rs b/rpc/src/v1/types/action.rs index 9df48aa257..039eeca033 100644 --- a/rpc/src/v1/types/action.rs +++ b/rpc/src/v1/types/action.rs @@ -1,4 +1,4 @@ -// Copyright 2018 Kodebox, Inc. +// Copyright 2018-2019 Kodebox, Inc. // This file is part of CodeChain. // // This program is free software: you can redistribute it and/or modify @@ -26,7 +26,7 @@ use rustc_serialize::hex::FromHexError; use super::super::errors::ConversionError; use super::{AssetMintOutput, AssetTransferInput, AssetTransferOutput, OrderOnTransfer}; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, PartialEq)] #[serde(rename_all = "camelCase", tag = "type")] pub enum Action { #[serde(rename_all = "camelCase")] @@ -644,3 +644,58 @@ impl From for Result { }) } } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::{from_str, to_string}; + + #[test] + fn serialize_metadata_with_single_quotations() { + let mint = ActionWithId::MintAsset { + network_id: "ab".into(), + shard_id: 0, + metadata: "string with 'a single quotation'".to_string(), + approver: None, + administrator: None, + allowed_script_hashes: vec![], + + output: AssetMintOutput { + lock_script_hash: Default::default(), + parameters: vec![], + supply: Some(1.into()), + } + .into(), + + approvals: vec![], + id: Default::default(), + }; + let s = to_string(&mint).unwrap(); + let expected = r#"{"type":"mintAsset","networkId":"ab","shardId":0,"metadata":"string with 'a single quotation'","approver":null,"administrator":null,"allowedScriptHashes":[],"output":{"lockScriptHash":"0x0000000000000000000000000000000000000000","parameters":[],"supply":"0x1"},"approvals":[],"id":"0x0000000000000000000000000000000000000000000000000000000000000000"}"#; + assert_eq!(&s, expected); + } + + #[test] + fn parse_metadata_with_single_quotations() { + let input = r#"{"type":"mintAsset","networkId":"ab","shardId":0,"metadata":"string with 'a single quotation'","approver":null,"administrator":null,"allowedScriptHashes":[],"output":{"lockScriptHash":"0x0000000000000000000000000000000000000000","parameters":[],"supply":"0x1"},"approvals":[]}"#; + let mint = from_str(input).unwrap(); + let expected = Action::MintAsset { + network_id: "ab".into(), + shard_id: 0, + metadata: "string with 'a single quotation'".to_string(), + approver: None, + administrator: None, + allowed_script_hashes: vec![], + + output: AssetMintOutput { + lock_script_hash: Default::default(), + parameters: vec![], + supply: Some(1.into()), + } + .into(), + + approvals: vec![], + }; + assert_eq!(expected, mint); + } +} diff --git a/rpc/src/v1/types/asset_input.rs b/rpc/src/v1/types/asset_input.rs index 6ead84cd8a..d3029ab319 100644 --- a/rpc/src/v1/types/asset_input.rs +++ b/rpc/src/v1/types/asset_input.rs @@ -1,4 +1,4 @@ -// Copyright 2018 Kodebox, Inc. +// Copyright 2018-2019 Kodebox, Inc. // This file is part of CodeChain. // // This program is free software: you can redistribute it and/or modify @@ -18,7 +18,7 @@ use cjson::uint::Uint; use ctypes::transaction::{AssetOutPoint as AssetOutPointType, AssetTransferInput as AssetTransferInputType, Timelock}; use primitives::{Bytes, H256}; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct AssetOutPoint { pub tracker: H256, @@ -49,7 +49,7 @@ impl From for AssetOutPointType { } } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct AssetTransferInput { pub prev_out: AssetOutPoint, diff --git a/rpc/src/v1/types/asset_output.rs b/rpc/src/v1/types/asset_output.rs index 9564a4b81c..061a8a13cd 100644 --- a/rpc/src/v1/types/asset_output.rs +++ b/rpc/src/v1/types/asset_output.rs @@ -1,4 +1,4 @@ -// Copyright 2018 Kodebox, Inc. +// Copyright 2018-2019 Kodebox, Inc. // This file is part of CodeChain. // // This program is free software: you can redistribute it and/or modify @@ -22,7 +22,7 @@ use ctypes::transaction::{AssetMintOutput as AssetMintOutputType, AssetTransferO use primitives::{H160, H256}; use rustc_serialize::hex::{FromHex, FromHexError, ToHex}; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct AssetTransferOutput { pub lock_script_hash: H160, @@ -53,7 +53,7 @@ impl From for Result } } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct AssetMintOutput { pub lock_script_hash: H160, diff --git a/rpc/src/v1/types/order.rs b/rpc/src/v1/types/order.rs index 618b4cc1a9..3bba491b3c 100644 --- a/rpc/src/v1/types/order.rs +++ b/rpc/src/v1/types/order.rs @@ -1,4 +1,4 @@ -// Copyright 2018 Kodebox, Inc. +// Copyright 2018-2019 Kodebox, Inc. // This file is part of CodeChain. // // This program is free software: you can redistribute it and/or modify @@ -20,7 +20,7 @@ use primitives::{Bytes, H160, H256}; use super::AssetOutPoint; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct Order { pub asset_type_from: H256, @@ -75,7 +75,7 @@ impl From for OrderType { } } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct OrderOnTransfer { pub order: Order, From e8ddfc30387f0d4c5ff130fa252e86e61b5e7c0f Mon Sep 17 00:00:00 2001 From: Seulgi Kim Date: Thu, 17 Jan 2019 10:05:32 +0900 Subject: [PATCH 2/3] Enhance RLP test cases This patch adds a test to check whether metadata with single quotation is encoded/decoded properly. --- types/src/transaction/action.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/types/src/transaction/action.rs b/types/src/transaction/action.rs index de05b575e3..a894dd6a6e 100644 --- a/types/src/transaction/action.rs +++ b/types/src/transaction/action.rs @@ -1,4 +1,4 @@ -// Copyright 2018 Kodebox, Inc. +// Copyright 2018-2019 Kodebox, Inc. // This file is part of CodeChain. // // This program is free software: you can redistribute it and/or modify @@ -1080,6 +1080,23 @@ mod tests { }); } + #[test] + fn encode_and_decode_mint_with_single_quotation() { + rlp_encode_and_decode_test!(Action::MintAsset { + network_id: "tc".into(), + shard_id: 3, + metadata: "metadata has a single quotation(')".to_string(), + output: Box::new(AssetMintOutput { + lock_script_hash: H160::random(), + parameters: vec![vec![1, 2, 3], vec![4, 5, 6], vec![0, 7]], + supply: Some(10000), + }), + approver: None, + administrator: None, + allowed_script_hashes: vec![], + approvals: vec![Signature::random()], + }); + } #[test] fn encode_and_decode_transfer_asset() { From 89221e89881cb84047c578be6fe5938d57eb33ed Mon Sep 17 00:00:00 2001 From: Seulgi Kim Date: Thu, 17 Jan 2019 10:05:32 +0900 Subject: [PATCH 3/3] Enhance test cases This patch adds a test to check whether metadata with an apostrophe is encoded/decoded properly. --- rpc/src/v1/types/action.rs | 49 +++++++++++++++++++++++++++++++++ types/src/transaction/action.rs | 18 ++++++++++++ 2 files changed, 67 insertions(+) diff --git a/rpc/src/v1/types/action.rs b/rpc/src/v1/types/action.rs index 039eeca033..ce343de19e 100644 --- a/rpc/src/v1/types/action.rs +++ b/rpc/src/v1/types/action.rs @@ -698,4 +698,53 @@ mod tests { }; assert_eq!(expected, mint); } + + #[test] + fn parse_metadata_with_apostrophe() { + let input = r#"{"type":"mintAsset","networkId":"ab","shardId":0,"metadata":"string with 'an apostrophe’","approver":null,"administrator":null,"allowedScriptHashes":[],"output":{"lockScriptHash":"0x0000000000000000000000000000000000000000","parameters":[],"supply":"0x1"},"approvals":[]}"#; + let mint = from_str(input).unwrap(); + let expected = Action::MintAsset { + network_id: "ab".into(), + shard_id: 0, + metadata: "string with 'an apostrophe’".to_string(), + approver: None, + administrator: None, + allowed_script_hashes: vec![], + + output: AssetMintOutput { + lock_script_hash: Default::default(), + parameters: vec![], + supply: Some(1.into()), + } + .into(), + + approvals: vec![], + }; + assert_eq!(expected, mint); + } + + #[test] + fn serialize_metadata_with_apostrophe() { + let mint = ActionWithId::MintAsset { + network_id: "ab".into(), + shard_id: 0, + metadata: "string with 'an apostrophe’".to_string(), + approver: None, + administrator: None, + allowed_script_hashes: vec![], + + output: AssetMintOutput { + lock_script_hash: Default::default(), + parameters: vec![], + supply: Some(1.into()), + } + .into(), + + approvals: vec![], + id: Default::default(), + }; + let s = to_string(&mint).unwrap(); + let expected = r#"{"type":"mintAsset","networkId":"ab","shardId":0,"metadata":"string with 'an apostrophe’","approver":null,"administrator":null,"allowedScriptHashes":[],"output":{"lockScriptHash":"0x0000000000000000000000000000000000000000","parameters":[],"supply":"0x1"},"approvals":[],"id":"0x0000000000000000000000000000000000000000000000000000000000000000"}"#; + assert_eq!(&s, expected); + } } diff --git a/types/src/transaction/action.rs b/types/src/transaction/action.rs index a894dd6a6e..f962d8464a 100644 --- a/types/src/transaction/action.rs +++ b/types/src/transaction/action.rs @@ -1098,6 +1098,24 @@ mod tests { }); } + #[test] + fn encode_and_decode_mint_with_apostrophe() { + rlp_encode_and_decode_test!(Action::MintAsset { + network_id: "tc".into(), + shard_id: 3, + metadata: "metadata has an apostrophe(’)".to_string(), + output: Box::new(AssetMintOutput { + lock_script_hash: H160::random(), + parameters: vec![vec![1, 2, 3], vec![4, 5, 6], vec![0, 7]], + supply: Some(10000), + }), + approver: None, + administrator: None, + allowed_script_hashes: vec![], + approvals: vec![Signature::random()], + }); + } + #[test] fn encode_and_decode_transfer_asset() { let burns = vec![];