Diff
Not logged in

Differences From Artifact [57004c0590]:

To Artifact [94e101ec6e]:


     1      1   // Copyright 2016 MaidSafe.net limited.
     2      2   //
     3      3   // This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
     4      4   // version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
     5      5   // licence you accepted on initial access to the Software (the "Licences").
     6      6   //
     7      7   // By contributing code to the SAFE Network Software, or to this project generally, you agree to be
     8         -// bound by the terms of the MaidSafe Contributor Agreement, version 1.1.  This, along with the
     9         -// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
            8  +// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
            9  +// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
    10     10   //
    11     11   // Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
    12     12   // under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    13     13   // KIND, either express or implied.
    14     14   //
    15     15   // Please review the Licences for the specific language governing permissions and limitations
    16     16   // relating to use of the SAFE Network Software.
    17     17   
    18     18   /// Maximum allowed length for a [message's `body`](struct.MpidMessage.html#method.new) (101,760
    19     19   /// bytes).
    20     20   pub const MAX_BODY_SIZE: usize = 102400 - 512 - super::MAX_HEADER_METADATA_SIZE;
    21     21   
           22  +use super::{Error, MpidHeader};
    22     23   use maidsafe_utilities::serialisation::serialise;
    23     24   use rust_sodium::crypto::sign::{self, PublicKey, SecretKey, Signature};
    24     25   use std::fmt::{self, Debug, Formatter};
    25         -use super::{Error, MpidHeader};
    26     26   use utils;
    27     27   use xor_name::XorName;
    28     28   
    29         -#[derive(PartialEq, Eq, Hash, Clone, RustcDecodable, RustcEncodable)]
           29  +#[derive(PartialEq, Eq, Hash, Clone, Deserialize, Serialize)]
    30     30   struct Detail {
    31     31       recipient: XorName,
    32     32       body: Vec<u8>,
    33     33   }
    34     34   
    35     35   /// A full message including header and body which can be sent to or retrieved from the network.
    36         -#[derive(PartialEq, Eq, Hash, Clone, RustcDecodable, RustcEncodable)]
           36  +#[derive(PartialEq, Eq, Hash, Clone, Deserialize, Serialize)]
    37     37   pub struct MpidMessage {
    38     38       header: MpidHeader,
    39     39       detail: Detail,
    40     40       signature: Signature,
    41     41   }
    42     42   
    43     43   impl MpidMessage {
................................................................................
    70     70           let detail = Detail {
    71     71               recipient: recipient,
    72     72               body: body,
    73     73           };
    74     74   
    75     75           let recipient_and_body = serialise(&detail)?;
    76     76           Ok(MpidMessage {
    77         -            header: header,
    78         -            detail: detail,
    79         -            signature: sign::sign_detached(&recipient_and_body, secret_key),
    80         -        })
           77  +               header: header,
           78  +               detail: detail,
           79  +               signature: sign::sign_detached(&recipient_and_body, secret_key),
           80  +           })
    81     81       }
    82     82   
    83     83       /// Getter for `MpidHeader` member, created when calling `new()`.
    84     84       pub fn header(&self) -> &MpidHeader {
    85     85           &self.header
    86     86       }
    87     87   
................................................................................
   123    123                  utils::format_binary_array(&self.detail.body),
   124    124                  utils::format_binary_array(&self.signature))
   125    125       }
   126    126   }
   127    127   
   128    128   #[cfg(test)]
   129    129   mod tests {
          130  +    use super::*;
   130    131       use messaging;
   131    132       use rand;
   132    133       use rust_sodium::crypto::sign;
   133         -    use super::*;
   134    134       use xor_name::XorName;
   135    135   
   136    136       #[test]
   137    137       fn full() {
   138    138           let (mut public_key, secret_key) = sign::gen_keypair();
   139    139           let sender: XorName = rand::random();
   140    140           let metadata = messaging::generate_random_bytes(messaging::MAX_HEADER_METADATA_SIZE);
................................................................................
   151    151           }
   152    152           let mut body = messaging::generate_random_bytes(MAX_BODY_SIZE);
   153    153           let message = unwrap!(MpidMessage::new(sender.clone(),
   154    154                                                  metadata.clone(),
   155    155                                                  recipient.clone(),
   156    156                                                  body.clone(),
   157    157                                                  &secret_key));
   158         -        assert!(*message.body() == body);
          158  +        assert_eq!(*message.body(), body);
   159    159           body.push(0);
   160    160           assert!(MpidMessage::new(sender.clone(),
   161    161                                    metadata.clone(),
   162    162                                    recipient.clone(),
   163    163                                    body.clone(),
   164    164                                    &secret_key)
   165         -            .is_err());
          165  +                        .is_err());
   166    166           let _ = body.pop();
   167    167   
   168    168           // Check verify function with a valid and invalid key
   169    169           assert!(message.verify(&public_key));
   170    170           if public_key.0[0] != 255 {
   171    171               public_key.0[0] += 1;
   172    172           } else {
   173    173               public_key.0[0] = 0;
   174    174           }
   175    175           assert!(!message.verify(&public_key));
   176    176       }
   177    177   }