From 3318a98390f0dfba278e3ba6b556e592e49cf4bb Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Mon, 13 Apr 2015 22:49:03 -0400 Subject: [PATCH 1/2] Fixing immutable data, tests WIP --- src/data/immutable_data.rs | 116 +++++++++++++++++++++---------------- src/lib.rs | 2 + 2 files changed, 68 insertions(+), 50 deletions(-) diff --git a/src/data/immutable_data.rs b/src/data/immutable_data.rs index a43df8e2..bd14427b 100644 --- a/src/data/immutable_data.rs +++ b/src/data/immutable_data.rs @@ -15,9 +15,11 @@ See the Licences for the specific language governing permissions and limitations relating to use of the MaidSafe Software. */ + extern crate rustc_serialize; extern crate sodiumoxide; extern crate cbor; +extern crate core; use cbor::CborTagEncode; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; @@ -42,103 +44,117 @@ use Random; /// #[derive(Clone)] pub struct ImmutableData { - name: NameType, - value: Vec, + value: Vec, } impl RoutingTrait for ImmutableData { - fn get_name(&self) -> NameType { - let digest = crypto::hash::sha512::hash(&self.name.0); - NameType(digest.0) - } + fn get_name(&self) -> NameType { + self.calculate_name() + } } impl PartialEq for ImmutableData { fn eq(&self, other: &ImmutableData) -> bool { - self.name == other.name && self.value == other.value + self.value == other.value } } impl fmt::Debug for ImmutableData { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "ImmutableData( name: {:?}, value: {:?}", self.name, self.value) + write!(f, "ImmutableData( name: {:?}, value: {:?} )", self.calculate_name(), self.value) } } impl ImmutableData { - #[allow(dead_code)] - pub fn new(name: NameType, value: Vec) -> ImmutableData { - ImmutableData { - name: name, - value: value, - } - } - - pub fn get_name(&self) -> &NameType { - &self.name - } - - pub fn get_value(&self) -> &Vec { - &self.value - } + pub fn new(value: Vec) -> ImmutableData { + ImmutableData { + value: value, + } + } + // debug cannot call RoutingTrait due to current visibility + fn calculate_name(&self) -> NameType { + let digest = crypto::hash::sha512::hash(&self.value); + NameType(digest.0) + } + + pub fn get_value(&self) -> &Vec { + &self.value + } } #[allow(unused_variables)] impl Random for ImmutableData { - fn generate_random() -> ImmutableData { - let mut data = Vec::with_capacity(64); + fn generate_random() -> ImmutableData { + let mut data = Vec::with_capacity(64); for i in 0..data.len() { data.push(rand::random::()); } - ImmutableData { - name: NameType::generate_random(), + ImmutableData { value: data, } } } impl Encodable for ImmutableData { - fn encode(&self, e: &mut E)->Result<(), E::Error> { - CborTagEncode::new(5483_001, &(&self.name, &self.value)).encode(e) - } + fn encode(&self, e: &mut E)->Result<(), E::Error> { + CborTagEncode::new(5483_001, &(&self.value)).encode(e) + } } impl Decodable for ImmutableData { - fn decode(d: &mut D)->Result { - try!(d.read_u64()); - let (name, value) = try!(Decodable::decode(d)); - Ok(ImmutableData::new(name, value)) - } + fn decode(d: &mut D)->Result { + try!(d.read_u64()); + let value = try!(Decodable::decode(d)); + Ok(ImmutableData::new(value)) + } } + #[cfg(test)] -mod test { +mod test { use super::*; use cbor::{ Encoder, Decoder}; use rustc_serialize::{Decodable, Encodable}; use Random; - + use common::NameType; + + #[test] + fn creation() { + let ascii = "012456789ABCDEF".to_string().into_bytes(); + let data = "this is a known string".to_string().into_bytes(); + let expected_name = "c937425f55ed0096a97eea1ccb5585d0242c517afe\ + eb9a18f8f5a209adc852213fa6b210694f1b86f55b\ + 8cfc35b8d9c577af61e0304e79c10bfc1e4661d9ae15".to_string(); + let chunk = ImmutableData::new(data); + let actual_name = + chunk.calculate_name().0.iter() + .fold(Vec::::new(), |mut value, byte| { + value.push(*ascii.iter().skip((byte & 0x0F) as usize).take(1).next().unwrap()); + value.push(*ascii.iter().skip((byte >> 4) as usize).take(1).next().unwrap()); + value + }); + assert_eq!(&expected_name, String::from_utf8(actual_name).as_ref().unwrap()); + } + #[test] fn serialisation_immutable_data() { - let obj_before = ImmutableData::generate_random(); - let mut e = Encoder::from_memory(); - e.encode(&[&obj_before]).unwrap(); - - let mut d = Decoder::from_bytes(e.as_bytes()); - let obj_after: ImmutableData = d.decode().next().unwrap().unwrap(); - - assert_eq!(obj_before, obj_after); + let obj_before = ImmutableData::generate_random(); + let mut e = Encoder::from_memory(); + e.encode(&[&obj_before]).unwrap(); + + let mut d = Decoder::from_bytes(e.as_bytes()); + let obj_after: ImmutableData = d.decode().next().unwrap().unwrap(); + + assert_eq!(obj_before, obj_after); } - + #[test] fn equality_assertion_immutable_data() { let first_obj = ImmutableData::generate_random(); let second_obj = ImmutableData::generate_random(); let cloned_obj = second_obj.clone(); - + assert!(first_obj != second_obj); - assert!(second_obj == cloned_obj); + assert!(second_obj == cloned_obj); } - - } diff --git a/src/lib.rs b/src/lib.rs index f1c078bd..44205f72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,8 @@ //! //! [Project github page](https://github.com/dirvine/maidsafe_types) +#![feature(core)] + extern crate rustc_serialize; extern crate sodiumoxide; extern crate cbor; From 26fe3637313adaf4701a7b587aa05200a7c0ce14 Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Tue, 14 Apr 2015 11:29:41 -0400 Subject: [PATCH 2/2] Finished test immutable data, and fixed interface --- src/data/immutable_data.rs | 36 +++++++++++++++--------------------- src/lib.rs | 2 -- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/data/immutable_data.rs b/src/data/immutable_data.rs index bd14427b..491520a0 100644 --- a/src/data/immutable_data.rs +++ b/src/data/immutable_data.rs @@ -19,7 +19,6 @@ extern crate rustc_serialize; extern crate sodiumoxide; extern crate cbor; -extern crate core; use cbor::CborTagEncode; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; @@ -36,7 +35,8 @@ use Random; /// /// ``` /// // Create an ImmutableData using the new function. -/// let immutable_data = maidsafe_types::ImmutableData::new(maidsafe_types::NameType([0u8; 64]), vec![99u8; 10]); +/// use maidsafe_types::traits::RoutingTrait; +/// let immutable_data = maidsafe_types::ImmutableData::new(vec![99u8; 10]); /// // Retrieving values /// let ref name_type = immutable_data.get_name(); /// let ref value = immutable_data.get_value(); @@ -86,13 +86,14 @@ impl ImmutableData { #[allow(unused_variables)] impl Random for ImmutableData { fn generate_random() -> ImmutableData { - let mut data = Vec::with_capacity(64); - for i in 0..data.len() { - data.push(rand::random::()); - } - ImmutableData { - value: data, + use rand::Rng; + let size = 64; + let mut data = Vec::with_capacity(size); + let mut rng = rand::thread_rng(); + for i in 0..size { + data.push(rng.gen()); } + ImmutableData::new(data) } } @@ -116,24 +117,17 @@ mod test { use cbor::{ Encoder, Decoder}; use rustc_serialize::{Decodable, Encodable}; use Random; - use common::NameType; #[test] fn creation() { - let ascii = "012456789ABCDEF".to_string().into_bytes(); + use rustc_serialize::hex::ToHex; let data = "this is a known string".to_string().into_bytes(); - let expected_name = "c937425f55ed0096a97eea1ccb5585d0242c517afe\ - eb9a18f8f5a209adc852213fa6b210694f1b86f55b\ - 8cfc35b8d9c577af61e0304e79c10bfc1e4661d9ae15".to_string(); + let expected_name = "8758b09d420bdb901d68fdd6888b38ce9ede06aad7f\ + e1e0ea81feffc76260554b9d46fb6ea3b169ff8bb02\ + ef14a03a122da52f3063bcb1bfb22cffc614def522".to_string(); let chunk = ImmutableData::new(data); - let actual_name = - chunk.calculate_name().0.iter() - .fold(Vec::::new(), |mut value, byte| { - value.push(*ascii.iter().skip((byte & 0x0F) as usize).take(1).next().unwrap()); - value.push(*ascii.iter().skip((byte >> 4) as usize).take(1).next().unwrap()); - value - }); - assert_eq!(&expected_name, String::from_utf8(actual_name).as_ref().unwrap()); + let actual_name = chunk.calculate_name().0.as_ref().to_hex(); + assert_eq!(&expected_name, &actual_name); } #[test] diff --git a/src/lib.rs b/src/lib.rs index 44205f72..f1c078bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,8 +30,6 @@ //! //! [Project github page](https://github.com/dirvine/maidsafe_types) -#![feature(core)] - extern crate rustc_serialize; extern crate sodiumoxide; extern crate cbor;