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

Commit

Permalink
Merge pull request #81 from ustulation/remove-payload-introduce-cbor-…
Browse files Browse the repository at this point in the history
…tagging

Remove payload introduce cbor tagging
  • Loading branch information
dirvine committed Jun 25, 2015
2 parents 43d1a3b + edb347b commit 52fa45f
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 211 deletions.
39 changes: 20 additions & 19 deletions src/data/immutable_data.rs
Expand Up @@ -36,19 +36,19 @@ pub struct ImmutableDataSacrificialTypeTag;

impl TypeTag for ImmutableDataTypeTag {
fn type_tag(&self) -> u64 {
return 101;
::data_tags::IMMUTABLE_DATA_TAG
}
}

impl TypeTag for ImmutableDataBackupTypeTag {
fn type_tag(&self) -> u64 {
return 102;
::data_tags::IMMUTABLE_DATA_BACKUP_TAG
}
}

impl TypeTag for ImmutableDataSacrificialTypeTag {
fn type_tag(&self) -> u64 {
return 103;
::data_tags::IMMUTABLE_DATA_SACRIFICIAL_TAG
}
}

Expand Down Expand Up @@ -113,13 +113,12 @@ impl ImmutableData {

impl Encodable for ImmutableData {
fn encode<E: Encoder>(&self, e: &mut E)->Result<(), E::Error> {
CborTagEncode::new(5483_001, &(&self.value)).encode(e)
CborTagEncode::new(::data_tags::IMMUTABLE_DATA_TAG, &(&self.value)).encode(e)
}
}

impl Decodable for ImmutableData {
fn decode<D: Decoder>(d: &mut D)->Result<ImmutableData, D::Error> {
try!(d.read_u64());
let value = try!(Decodable::decode(d));
Ok(ImmutableData::new(value))
}
Expand Down Expand Up @@ -187,13 +186,12 @@ impl ImmutableDataBackup {

impl Encodable for ImmutableDataBackup {
fn encode<E: Encoder>(&self, e: &mut E)->Result<(), E::Error> {
CborTagEncode::new(5483_001, &(&self.value)).encode(e)
CborTagEncode::new(::data_tags::IMMUTABLE_DATA_BACKUP_TAG, &(&self.value)).encode(e)
}
}

impl Decodable for ImmutableDataBackup {
fn decode<D: Decoder>(d: &mut D)->Result<ImmutableDataBackup, D::Error> {
try!(d.read_u64());
let value = try!(Decodable::decode(d));
Ok(ImmutableDataBackup::new(ImmutableData::new(value)))
}
Expand Down Expand Up @@ -262,13 +260,12 @@ impl ImmutableDataSacrificial {

impl Encodable for ImmutableDataSacrificial {
fn encode<E: Encoder>(&self, e: &mut E)->Result<(), E::Error> {
CborTagEncode::new(5483_001, &(&self.value)).encode(e)
CborTagEncode::new(::data_tags::IMMUTABLE_DATA_SACRIFICIAL_TAG, &(&self.value)).encode(e)
}
}

impl Decodable for ImmutableDataSacrificial {
fn decode<D: Decoder>(d: &mut D)->Result<ImmutableDataSacrificial, D::Error> {
try!(d.read_u64());
let value = try!(Decodable::decode(d));
Ok(ImmutableDataSacrificial::new(ImmutableData::new(value)))
}
Expand Down Expand Up @@ -340,26 +337,30 @@ mod test {
immutable_data_encoder.encode(&[&immutable_data]).unwrap();
let mut immutable_data_decoder =
Decoder::from_bytes(immutable_data_encoder.as_bytes());
let decoded_immutable_data: ImmutableData =
immutable_data_decoder.decode().next().unwrap().unwrap();
match immutable_data_decoder.decode().next().unwrap().unwrap() {
::test_utils::Parser::ImmutData(decoded_immutable_data) => assert_eq!(immutable_data, decoded_immutable_data),
_ => panic!("Unexpected!"),
}

// ImmutableDataBackup
let mut immutable_data_backup_encoder = Encoder::from_memory();
immutable_data_backup_encoder.encode(&[&immutable_data_backup]).unwrap();
let mut immutable_data_backup_decoder =
Decoder::from_bytes(immutable_data_backup_encoder.as_bytes());
let decoded_immutable_data_backup: ImmutableDataBackup =
immutable_data_backup_decoder.decode().next().unwrap().unwrap();
match immutable_data_backup_decoder.decode().next().unwrap().unwrap() {
::test_utils::Parser::ImmutDataBkup(decoded_immutable_data_backup) => assert_eq!(immutable_data_backup, decoded_immutable_data_backup),
_ => panic!("Unexpected!"),
}

// ImmutableDataSacrificial
let mut immutable_data_sacrificial_encoder = Encoder::from_memory();
immutable_data_sacrificial_encoder.encode(&[&immutable_data_sacrificial]).unwrap();
let mut immutable_data_sacrificial_decoder =
Decoder::from_bytes(immutable_data_sacrificial_encoder.as_bytes());
let decoded_immutable_data_sacrificial: ImmutableDataSacrificial =
immutable_data_sacrificial_decoder.decode().next().unwrap().unwrap();

assert_eq!(immutable_data, decoded_immutable_data);
assert_eq!(immutable_data_backup, decoded_immutable_data_backup);
assert_eq!(immutable_data_sacrificial, decoded_immutable_data_sacrificial);
match immutable_data_sacrificial_decoder.decode().next().unwrap().unwrap() {
::test_utils::Parser::ImmutDataSacrificial(decoded_immutable_data_sacrificial) => assert_eq!(immutable_data_sacrificial, decoded_immutable_data_sacrificial),
_ => panic!("Unexpected!"),
}
}

#[test]
Expand Down
20 changes: 11 additions & 9 deletions src/data/structured_data.rs
Expand Up @@ -30,7 +30,7 @@ pub struct StructuredDataTypeTag;

impl TypeTag for StructuredDataTypeTag {
fn type_tag(&self) -> u64 {
return 100;
::data_tags::STRUCTURED_DATA_TAG
}
}

Expand Down Expand Up @@ -111,13 +111,12 @@ impl StructuredData {

impl Encodable for StructuredData {
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
CborTagEncode::new(5483_002, &(&self.name, &self.owner, &self.value)).encode(e)
CborTagEncode::new(::data_tags::STRUCTURED_DATA_TAG, &(&self.name, &self.owner, &self.value)).encode(e)
}
}

impl Decodable for StructuredData {
fn decode<D: Decoder>(d: &mut D) -> Result<StructuredData, D::Error> {
try!(d.read_u64());
let (name, owner, value) = try!(Decodable::decode(d));
let structured = StructuredData {
type_tag: StructuredDataTypeTag,
Expand Down Expand Up @@ -161,7 +160,7 @@ mod test {
let structured_data = StructuredData::generate_random();
let data = StructuredData::new(structured_data.name(), structured_data.owner().unwrap(), structured_data.value());
assert_eq!(data, structured_data);
assert_eq!(structured_data.type_tag(), 100u64);
assert_eq!(structured_data.type_tag(), ::data_tags::STRUCTURED_DATA_TAG);
}

#[test]
Expand All @@ -174,10 +173,13 @@ mod test {
e.encode(&[&obj_before]).unwrap();

let mut d = Decoder::from_bytes(e.as_bytes());
let obj_after: StructuredData = d.decode().next().unwrap().unwrap();

assert_eq!(obj_before, obj_after);
assert!(!(obj_before != obj_before_clone));
assert!(obj_before != obj_before1);
match d.decode().next().unwrap().unwrap() {
::test_utils::Parser::StructData(obj_after) => {
assert_eq!(obj_before, obj_after);
assert!(!(obj_before != obj_before_clone));
assert!(obj_before != obj_before1);
},
_ => panic!("Unexpected!"),
}
}
}
28 changes: 15 additions & 13 deletions src/id/id_type.rs
Expand Up @@ -112,7 +112,7 @@ impl Encodable for IdType {
let (crypto::sign::SecretKey(sec_sign_vec), crypto::box_::SecretKey(sec_asym_vec)) = self.secret_keys;
let type_vec = self.type_tag.to_string().into_bytes();

CborTagEncode::new(5483_001, &(
CborTagEncode::new(self.type_tag, &(
type_vec,
pub_sign_vec.as_ref(),
pub_asym_vec.as_ref(),
Expand All @@ -123,7 +123,6 @@ impl Encodable for IdType {

impl Decodable for IdType {
fn decode<D: Decoder>(d: &mut D)-> Result<IdType, D::Error> {
try!(d.read_u64());
let (tag_type_vec, pub_sign_vec, pub_asym_vec, sec_sign_vec, sec_asym_vec) : (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>) = try!(Decodable::decode(d));
let pub_sign_arr = convert_to_array!(pub_sign_vec, crypto::sign::PUBLICKEYBYTES);
let pub_asym_arr = convert_to_array!(pub_asym_vec, crypto::box_::PUBLICKEYBYTES);
Expand Down Expand Up @@ -177,17 +176,20 @@ mod test {
e.encode(&[&obj_before]).unwrap();

let mut d = cbor::Decoder::from_bytes(e.as_bytes());
let obj_after: IdType = d.decode().next().unwrap().unwrap();

let &(crypto::sign::PublicKey(pub_sign_arr_before), crypto::box_::PublicKey(pub_asym_arr_before)) = obj_before.public_keys();
let &(crypto::sign::PublicKey(pub_sign_arr_after), crypto::box_::PublicKey(pub_asym_arr_after)) = obj_after.public_keys();
let &(crypto::sign::SecretKey(sec_sign_arr_before), crypto::box_::SecretKey(sec_asym_arr_before)) = &obj_before.secret_keys;
let &(crypto::sign::SecretKey(sec_sign_arr_after), crypto::box_::SecretKey(sec_asym_arr_after)) = &obj_after.secret_keys;

assert_eq!(pub_sign_arr_before, pub_sign_arr_after);
assert_eq!(pub_asym_arr_before, pub_asym_arr_after);
assert!(slice_equal(&sec_sign_arr_before, &sec_sign_arr_after));
assert_eq!(sec_asym_arr_before, sec_asym_arr_after);
match d.decode().next().unwrap().unwrap() {
::test_utils::Parser::Maid(obj_after) => {
let &(crypto::sign::PublicKey(pub_sign_arr_before), crypto::box_::PublicKey(pub_asym_arr_before)) = obj_before.public_keys();
let &(crypto::sign::PublicKey(pub_sign_arr_after), crypto::box_::PublicKey(pub_asym_arr_after)) = obj_after.public_keys();
let &(crypto::sign::SecretKey(sec_sign_arr_before), crypto::box_::SecretKey(sec_asym_arr_before)) = &obj_before.secret_keys;
let &(crypto::sign::SecretKey(sec_sign_arr_after), crypto::box_::SecretKey(sec_asym_arr_after)) = &obj_after.secret_keys;

assert_eq!(pub_sign_arr_before, pub_sign_arr_after);
assert_eq!(pub_asym_arr_before, pub_asym_arr_after);
assert!(slice_equal(&sec_sign_arr_before, &sec_sign_arr_after));
assert_eq!(sec_asym_arr_before, sec_asym_arr_after);
},
_ => panic!("Unexpected!"),
}
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions src/id/public_id_type.rs
Expand Up @@ -127,7 +127,7 @@ impl Encodable for PublicIdType {
let crypto::sign::PublicKey(ref revocation_public_key_vec) = self.revocation_public_key;
let crypto::sign::Signature(ref signature) = self.signature;
let type_vec = self.type_tag.to_string().into_bytes();
CborTagEncode::new(5483_001, &(
CborTagEncode::new(self.type_tag, &(
type_vec,
pub_sign_vec.as_ref(),
pub_asym_vec.as_ref(),
Expand All @@ -138,7 +138,6 @@ impl Encodable for PublicIdType {

impl Decodable for PublicIdType {
fn decode<D: Decoder>(d: &mut D)-> Result<PublicIdType, D::Error> {
try!(d.read_u64());
let (tag_type_vec, pub_sign_vec, pub_asym_vec, revocation_public_key_vec, signature_vec): (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>) = try!(Decodable::decode(d));
let pub_sign_arr = convert_to_array!(pub_sign_vec, crypto::sign::PUBLICKEYBYTES);
let pub_asym_arr = convert_to_array!(pub_asym_vec, crypto::box_::PUBLICKEYBYTES);
Expand Down Expand Up @@ -201,9 +200,10 @@ mod test {
e.encode(&[&obj_before]).unwrap();

let mut d = cbor::Decoder::from_bytes(e.as_bytes());
let obj_after: PublicIdType = d.decode().next().unwrap().unwrap();

assert_eq!(obj_before, obj_after);
match d.decode().next().unwrap().unwrap() {
::test_utils::Parser::PubMaid(obj_after) => assert_eq!(obj_before, obj_after),
_ => panic!("Unexpected!"),
}
}

#[test]
Expand Down
11 changes: 6 additions & 5 deletions src/id/revocation_id_type.rs
Expand Up @@ -92,6 +92,7 @@ impl RevocationIdType {
&self.type_tags
}
/// Returns type tag
/// TODO needless reference for built in POD
pub fn type_tag(&self) -> &u64 {
&self.type_tags.0
}
Expand Down Expand Up @@ -126,7 +127,7 @@ impl Encodable for RevocationIdType {
let revocation_type_tag_vec = self.type_tags.0.to_string().into_bytes();
let id_type_tag_vec = self.type_tags.1.to_string().into_bytes();
let public_id_type_tag_vec = self.type_tags.2.to_string().into_bytes();
CborTagEncode::new(5483_001,
CborTagEncode::new(*self.type_tag(),
&(revocation_type_tag_vec,
id_type_tag_vec,
public_id_type_tag_vec,
Expand All @@ -136,7 +137,6 @@ impl Encodable for RevocationIdType {

impl Decodable for RevocationIdType {
fn decode<D: Decoder>(d: &mut D)-> Result<RevocationIdType, D::Error> {
try!(d.read_u64());
let(revocation_type_tag_vec, id_type_tag_vec, public_id_type_tag_vec , pub_sign_vec, sec_sign_vec) : (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>) = try!(Decodable::decode(d));
let pub_sign_arr = convert_to_array!(pub_sign_vec, crypto::sign::PUBLICKEYBYTES);
let sec_sign_arr = convert_to_array!(sec_sign_vec, crypto::sign::SECRETKEYBYTES);
Expand Down Expand Up @@ -186,9 +186,10 @@ mod test {
e.encode(&[&obj_before]).unwrap();

let mut d = cbor::Decoder::from_bytes(e.as_bytes());
let obj_after: RevocationIdType = d.decode().next().unwrap().unwrap();

assert_eq!(obj_before, obj_after);
match d.decode().next().unwrap().unwrap() {
::test_utils::Parser::AnMaid(obj_after) => assert_eq!(obj_before, obj_after),
_ => panic!("Unexpected!"),
}
}

#[test]
Expand Down

0 comments on commit 52fa45f

Please sign in to comment.