Skip to content

Commit ee424cd

Browse files
author
Roman Proskuryakov
authored
Merge pull request #358 from tox-rs/g_packets
Add serde for Conference packets
2 parents 981419a + 069b517 commit ee424cd

24 files changed

+1533
-30
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! Conference Packets
1+
/*! The implementation of conference
22
*/
33

44
pub mod packet;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*! Conference action action struct.
2+
*/
3+
4+
use std::str;
5+
use nom::{be_u16, be_u32, rest};
6+
7+
use crate::toxcore::binary_io::*;
8+
9+
/** Action is the struct that holds info to send action to a conference.
10+
11+
Sent to notify action to all member of conference.
12+
13+
Serialized form:
14+
15+
Length | Content
16+
--------- | ------
17+
`1` | `0x63`
18+
`2` | `conference id`
19+
`2` | `peer id`
20+
`4` | `action id`
21+
`1` | `0x41`
22+
variable | `action`(UTF-8 C String)
23+
24+
*/
25+
#[derive(Clone, Debug, Eq, PartialEq)]
26+
pub struct Action {
27+
/// Id of conference
28+
pub conference_id: u16,
29+
/// Target peer id
30+
pub peer_id: u16,
31+
/// Id of this message
32+
pub message_id: u32,
33+
/// Maximum length of action is the limit of NetCrypto packet.
34+
/// Do not check the length here.
35+
pub action: String,
36+
}
37+
38+
impl FromBytes for Action {
39+
named!(from_bytes<Action>, do_parse!(
40+
tag!("\x63") >>
41+
conference_id: be_u16 >>
42+
peer_id: be_u16 >>
43+
message_id: be_u32 >>
44+
tag!("\x41") >>
45+
action: map_res!(rest, str::from_utf8) >>
46+
(Action {
47+
conference_id,
48+
peer_id,
49+
message_id,
50+
action: action.to_string(),
51+
})
52+
));
53+
}
54+
55+
impl ToBytes for Action {
56+
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
57+
do_gen!(buf,
58+
gen_be_u8!(0x63) >>
59+
gen_be_u16!(self.conference_id) >>
60+
gen_be_u16!(self.peer_id) >>
61+
gen_be_u32!(self.message_id) >>
62+
gen_be_u8!(0x41) >>
63+
gen_slice!(self.action.as_bytes())
64+
)
65+
}
66+
}
67+
68+
impl Action {
69+
/// Create new Action object.
70+
pub fn new(conference_id: u16, peer_id: u16, message_id: u32, action: String) -> Self {
71+
Action {
72+
conference_id,
73+
peer_id,
74+
message_id,
75+
action,
76+
}
77+
}
78+
}
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use super::*;
83+
84+
encode_decode_test!(
85+
conference_action_encode_decode,
86+
Action::new(1, 2, 3, "1234".to_owned())
87+
);
88+
89+
#[test]
90+
fn conference_action_from_bytes_encoding_error() {
91+
let err_string = vec![0, 159, 146, 150]; // not UTF8 bytes.
92+
let mut buf = vec![0x63, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x41];
93+
buf.extend_from_slice(&err_string);
94+
assert!(Action::from_bytes(&buf).is_err());
95+
}
96+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*! Change name message struct.
2+
*/
3+
4+
use std::str;
5+
use nom::{be_u16, be_u32, rest};
6+
7+
use crate::toxcore::binary_io::*;
8+
use super::MAX_NAME_LENGTH_IN_CONFERENCE;
9+
10+
/** ChangeName is the struct that holds info to notify changing name of a peer to a conference.
11+
12+
Sent by a peer who wants to change its name or by a joining peer to notify its name to members of conference.
13+
14+
Serialized form:
15+
16+
Length | Content
17+
--------- | ------
18+
`1` | `0x63`
19+
`2` | `conference id`
20+
`2` | `peer id`
21+
`4` | `message id`
22+
`1` | `0x30`
23+
variable | `name`(UTF-8 C String)
24+
25+
*/
26+
#[derive(Clone, Debug, Eq, PartialEq)]
27+
pub struct ChangeName {
28+
/// Id of conference
29+
pub conference_id: u16,
30+
/// Target peer id
31+
pub peer_id: u16,
32+
/// Id of this message
33+
pub message_id: u32,
34+
/// Name to change
35+
pub name: String,
36+
}
37+
38+
impl FromBytes for ChangeName {
39+
named!(from_bytes<ChangeName>, do_parse!(
40+
tag!("\x63") >>
41+
conference_id: be_u16 >>
42+
peer_id: be_u16 >>
43+
message_id: be_u32 >>
44+
tag!("\x30") >>
45+
name: map_res!(verify!(rest, |name: &[u8]| name.len() <= MAX_NAME_LENGTH_IN_CONFERENCE),
46+
str::from_utf8) >>
47+
(ChangeName {
48+
conference_id,
49+
peer_id,
50+
message_id,
51+
name: name.to_string(),
52+
})
53+
));
54+
}
55+
56+
impl ToBytes for ChangeName {
57+
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
58+
do_gen!(buf,
59+
gen_be_u8!(0x63) >>
60+
gen_be_u16!(self.conference_id) >>
61+
gen_be_u16!(self.peer_id) >>
62+
gen_be_u32!(self.message_id) >>
63+
gen_be_u8!(0x30) >>
64+
gen_cond!(self.name.len() > MAX_NAME_LENGTH_IN_CONFERENCE, |buf| gen_error(buf, 0)) >>
65+
gen_slice!(self.name.as_bytes())
66+
)
67+
}
68+
}
69+
70+
impl ChangeName {
71+
/// Create new ChangeName object.
72+
pub fn new(conference_id: u16, peer_id: u16, message_id: u32, name: String) -> Self {
73+
ChangeName {
74+
conference_id,
75+
peer_id,
76+
message_id,
77+
name,
78+
}
79+
}
80+
}
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
86+
encode_decode_test!(
87+
change_name_encode_decode,
88+
ChangeName::new(1, 2, 3, "1234".to_owned())
89+
);
90+
91+
#[test]
92+
fn change_name_from_bytes_encoding_error() {
93+
let err_string = vec![0, 159, 146, 150]; // not UTF8 bytes.
94+
let mut buf = vec![0x63, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x30];
95+
buf.extend_from_slice(&err_string);
96+
assert!(ChangeName::from_bytes(&buf).is_err());
97+
}
98+
99+
#[test]
100+
fn change_name_from_bytes_overflow() {
101+
let large_string = vec![32; MAX_NAME_LENGTH_IN_CONFERENCE + 1];
102+
let mut buf = vec![0x63, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x30];
103+
buf.extend_from_slice(&large_string);
104+
assert!(ChangeName::from_bytes(&buf).is_err());
105+
}
106+
107+
#[test]
108+
fn change_name_to_bytes_overflow() {
109+
let large_string = String::from_utf8(vec![32u8; MAX_NAME_LENGTH_IN_CONFERENCE + 1]).unwrap();
110+
let large_name = ChangeName::new(1,2, 3, large_string);
111+
let mut buf = [0; MAX_NAME_LENGTH_IN_CONFERENCE + 1 + 2 + 2 + 4 + 1]; // packet id + conference id + peer id + message id + message kind.
112+
assert!(large_name.to_bytes((&mut buf, 0)).is_err());
113+
}
114+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*! Change title message struct.
2+
*/
3+
4+
use std::str;
5+
use nom::{be_u16, be_u32, rest};
6+
7+
use crate::toxcore::binary_io::*;
8+
use super::MAX_NAME_LENGTH_IN_CONFERENCE;
9+
10+
/** ChangeTitle is the struct that holds info to change title of a conference.
11+
12+
Sent by anyone who is member of conference.
13+
This packet is used to change the title of conference.
14+
15+
Serialized form:
16+
17+
Length | Content
18+
--------- | ------
19+
`1` | `0x63`
20+
`2` | `conference id`
21+
`2` | `peer id`
22+
`4` | `message id`
23+
`1` | `0x31`
24+
variable | `title`(UTF-8 C String)
25+
26+
*/
27+
#[derive(Clone, Debug, Eq, PartialEq)]
28+
pub struct ChangeTitle {
29+
/// Id of conference
30+
pub conference_id: u16,
31+
/// Target peer id
32+
pub peer_id: u16,
33+
/// Id of this message
34+
pub message_id: u32,
35+
/// Title to change
36+
pub title: String,
37+
}
38+
39+
impl FromBytes for ChangeTitle {
40+
named!(from_bytes<ChangeTitle>, do_parse!(
41+
tag!("\x63") >>
42+
conference_id: be_u16 >>
43+
peer_id: be_u16 >>
44+
message_id: be_u32 >>
45+
tag!("\x31") >>
46+
title: map_res!(verify!(rest, |title: &[u8]| title.len() <= MAX_NAME_LENGTH_IN_CONFERENCE),
47+
str::from_utf8) >>
48+
(ChangeTitle {
49+
conference_id,
50+
peer_id,
51+
message_id,
52+
title: title.to_string(),
53+
})
54+
));
55+
}
56+
57+
impl ToBytes for ChangeTitle {
58+
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
59+
do_gen!(buf,
60+
gen_be_u8!(0x63) >>
61+
gen_be_u16!(self.conference_id) >>
62+
gen_be_u16!(self.peer_id) >>
63+
gen_be_u32!(self.message_id) >>
64+
gen_be_u8!(0x31) >>
65+
gen_cond!(self.title.len() > MAX_NAME_LENGTH_IN_CONFERENCE, |buf| gen_error(buf, 0)) >>
66+
gen_slice!(self.title.as_bytes())
67+
)
68+
}
69+
}
70+
71+
impl ChangeTitle {
72+
/// Create new ChangeTitle object.
73+
pub fn new(conference_id: u16, peer_id: u16, message_id: u32, title: String) -> Self {
74+
ChangeTitle {
75+
conference_id,
76+
peer_id,
77+
message_id,
78+
title,
79+
}
80+
}
81+
}
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
87+
encode_decode_test!(
88+
change_title_encode_decode,
89+
ChangeTitle::new(1, 2, 3, "1234".to_owned())
90+
);
91+
92+
#[test]
93+
fn change_title_from_bytes_encoding_error() {
94+
let err_string = vec![0, 159, 146, 150]; // not UTF8 bytes.
95+
let mut buf = vec![0x63, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x31];
96+
buf.extend_from_slice(&err_string);
97+
assert!(ChangeTitle::from_bytes(&buf).is_err());
98+
}
99+
100+
#[test]
101+
fn change_title_from_bytes_overflow() {
102+
let large_string = vec![32; MAX_NAME_LENGTH_IN_CONFERENCE + 1];
103+
let mut buf = vec![0x63, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x31];
104+
buf.extend_from_slice(&large_string);
105+
assert!(ChangeTitle::from_bytes(&buf).is_err());
106+
}
107+
108+
#[test]
109+
fn change_title_to_bytes_overflow() {
110+
let large_string = String::from_utf8(vec![32u8; MAX_NAME_LENGTH_IN_CONFERENCE + 1]).unwrap();
111+
let large_title = ChangeTitle::new(1,2, 3, large_string);
112+
let mut buf = [0; MAX_NAME_LENGTH_IN_CONFERENCE + 1 + 2 + 2 + 4 + 1]; // packet id + conference id + peer id + message id + message kind.
113+
assert!(large_title.to_bytes((&mut buf, 0)).is_err());
114+
}
115+
}

0 commit comments

Comments
 (0)