1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::{Error, PublicKey, Result, Signature, XorName};
use serde::{Deserialize, Serialize};
pub const MAX_LOGIN_PACKET_BYTES: usize = 1024 * 1024;
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub struct LoginPacket {
destination: XorName,
authorised_getter: PublicKey,
data: Vec<u8>,
signature: Signature,
}
impl LoginPacket {
pub fn new(
destination: XorName,
authorised_getter: PublicKey,
data: Vec<u8>,
signature: Signature,
) -> Result<Self> {
let login_packet_data = Self {
destination,
authorised_getter,
data,
signature,
};
if login_packet_data.size_is_valid() {
Ok(login_packet_data)
} else {
Err(Error::ExceededSize)
}
}
pub fn size_is_valid(&self) -> bool {
self.data.len() <= MAX_LOGIN_PACKET_BYTES
}
pub fn destination(&self) -> &XorName {
&self.destination
}
pub fn authorised_getter(&self) -> &PublicKey {
&self.authorised_getter
}
pub fn data(&self) -> &[u8] {
&self.data
}
pub fn signature(&self) -> &Signature {
&self.signature
}
pub fn into_data_and_signature(self) -> (Vec<u8>, Signature) {
(self.data, self.signature)
}
}
#[cfg(test)]
mod tests {
use super::{LoginPacket, MAX_LOGIN_PACKET_BYTES};
use crate::{ClientFullId, Error};
use rand;
#[test]
fn exceed_size_limit() {
let our_id = ClientFullId::new_ed25519(&mut rand::thread_rng());
let acc_data = vec![0; MAX_LOGIN_PACKET_BYTES + 1];
let signature = our_id.sign(&acc_data);
let res = LoginPacket::new(
rand::random(),
*our_id.public_id().public_key(),
acc_data,
signature,
);
match res {
Err(Error::ExceededSize) => (),
Ok(_) => panic!("Unexpected success"),
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
#[test]
fn valid() {
let our_id = ClientFullId::new_ed25519(&mut rand::thread_rng());
let acc_data = vec![1; 16];
let signature = our_id.sign(&acc_data);
let res = LoginPacket::new(
rand::random(),
*our_id.public_id().public_key(),
acc_data.clone(),
signature,
);
match res {
Ok(ad) => {
assert_eq!(ad.data(), acc_data.as_slice());
}
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
}