1 use std::borrow::Cow;
2 
3 use x11rb::errors::ParseError;
4 use x11rb::x11_utils::RequestHeader;
5 
6 macro_rules! add_ne {
7     ($data:expr, $add:expr) => {
8         $data.extend(&$add.to_ne_bytes())
9     };
10 }
11 
12 #[test]
test_bad_request_header_opcode()13 fn test_bad_request_header_opcode() {
14     use x11rb::protocol::xproto::GetInputFocusRequest;
15     let header = RequestHeader {
16         major_opcode: 1,
17         minor_opcode: 0,
18         remaining_length: 0,
19     };
20     let body = &[];
21     assert_eq!(
22         GetInputFocusRequest::try_parse_request(header, body),
23         Err(ParseError::InvalidValue)
24     );
25 }
26 
27 #[test]
test_bad_request_header_length()28 fn test_bad_request_header_length() {
29     use x11rb::protocol::xproto::CreateWindowRequest;
30     let header = RequestHeader {
31         major_opcode: 1,
32         minor_opcode: 0,
33         remaining_length: 42,
34     };
35     let body = &[];
36     assert_eq!(
37         CreateWindowRequest::try_parse_request(header, body),
38         Err(ParseError::InsufficientData)
39     );
40 }
41 
42 #[test]
test_create_window1()43 fn test_create_window1() {
44     use x11rb::protocol::xproto::{CreateWindowAux, CreateWindowRequest, Gravity, WindowClass};
45     let header = RequestHeader {
46         major_opcode: 1,
47         minor_opcode: 0x18,
48         remaining_length: 10,
49     };
50     let mut body = vec![];
51     add_ne!(body, 0x05c0_0001u32);
52     add_ne!(body, 0x0000_0513u32);
53     add_ne!(body, 0x047bu16);
54     add_ne!(body, 0x0134u16);
55     add_ne!(body, 0x03f5u16);
56     add_ne!(body, 0x033bu16);
57     add_ne!(body, 0x0000u16);
58     add_ne!(body, 0x0001u16);
59     add_ne!(body, 0x0000_0047u32);
60     add_ne!(body, 0x0000_001au32);
61     add_ne!(body, 0xfff2_f1f0u32);
62     add_ne!(body, 0x0000_0000u32);
63     add_ne!(body, 0x0000_0001u32);
64     let r = CreateWindowRequest::try_parse_request(header, &body).unwrap();
65     assert_eq!(
66         r,
67         CreateWindowRequest {
68             depth: 0x18,
69             wid: 0x05c0_0001,
70             parent: 0x0000_0513,
71             x: 0x047b,
72             y: 0x0134,
73             width: 0x03f5,
74             height: 0x033b,
75             border_width: 0,
76             class: WindowClass::INPUT_OUTPUT,
77             visual: 0x47,
78             value_list: Cow::Owned(CreateWindowAux {
79                 background_pixel: Some(0xfff2_f1f0),
80                 border_pixel: Some(0),
81                 bit_gravity: Some(Gravity::NORTH_WEST),
82                 ..Default::default()
83             }),
84         }
85     );
86 }
87 
88 #[test]
test_create_window2()89 fn test_create_window2() {
90     use x11rb::protocol::xproto::{CreateWindowAux, CreateWindowRequest, Gravity, WindowClass};
91     let header = RequestHeader {
92         major_opcode: 1,
93         minor_opcode: 0x18,
94         remaining_length: 11,
95     };
96     let mut body = vec![];
97     add_ne!(body, 0x0540_0003u32);
98     add_ne!(body, 0x0000_0513u32);
99     add_ne!(body, 0x0000_0000u32);
100     add_ne!(body, 0x0001u16);
101     add_ne!(body, 0x0001u16);
102     add_ne!(body, 0x0000u16);
103     add_ne!(body, 0x0001u16);
104     add_ne!(body, 0x0000_035au32);
105     add_ne!(body, 0x0000_201au32);
106     add_ne!(body, 0x0000_0000u32);
107     add_ne!(body, 0x0000_0000u32);
108     add_ne!(body, 0x0000_0001u32);
109     add_ne!(body, 0x0540_0002u32);
110     let r = CreateWindowRequest::try_parse_request(header, &body).unwrap();
111     assert_eq!(
112         r,
113         CreateWindowRequest {
114             depth: 0x18,
115             wid: 0x0540_0003,
116             parent: 0x0000_0513,
117             x: 0,
118             y: 0,
119             width: 1,
120             height: 1,
121             border_width: 0,
122             class: WindowClass::INPUT_OUTPUT,
123             visual: 0x35a,
124             value_list: Cow::Owned(CreateWindowAux {
125                 background_pixel: Some(0),
126                 border_pixel: Some(0),
127                 bit_gravity: Some(Gravity::NORTH_WEST),
128                 colormap: Some(0x0540_0002),
129                 ..Default::default()
130             }),
131         }
132     );
133 }
134 
135 #[test]
test_change_window_attributes()136 fn test_change_window_attributes() {
137     use x11rb::protocol::xproto::{
138         ChangeWindowAttributesAux, ChangeWindowAttributesRequest, EventMask,
139     };
140     let header = RequestHeader {
141         major_opcode: 2,
142         minor_opcode: 0,
143         remaining_length: 3,
144     };
145     let mut body = vec![];
146     add_ne!(body, 0x0000_0513u32);
147     add_ne!(body, 0x0000_0800u32);
148     add_ne!(body, 0x0040_0000u32);
149     let r = ChangeWindowAttributesRequest::try_parse_request(header, &body).unwrap();
150     assert_eq!(
151         r,
152         ChangeWindowAttributesRequest {
153             window: 0x0513,
154             value_list: Cow::Owned(ChangeWindowAttributesAux {
155                 event_mask: Some(EventMask::PROPERTY_CHANGE.into()),
156                 ..Default::default()
157             }),
158         }
159     );
160 }
161 
162 #[test]
test_get_window_attributes()163 fn test_get_window_attributes() {
164     use x11rb::protocol::xproto::GetWindowAttributesRequest;
165     let header = RequestHeader {
166         major_opcode: 3,
167         minor_opcode: 0,
168         remaining_length: 1,
169     };
170     let mut body = vec![];
171     add_ne!(body, 0x00e0_0002u32);
172     let r = GetWindowAttributesRequest::try_parse_request(header, &body).unwrap();
173     assert_eq!(
174         r,
175         GetWindowAttributesRequest {
176             window: 0x00e0_0002
177         }
178     );
179 }
180 
181 #[test]
test_get_input_focus()182 fn test_get_input_focus() {
183     use x11rb::protocol::xproto::GetInputFocusRequest;
184     let header = RequestHeader {
185         major_opcode: 43,
186         minor_opcode: 0,
187         remaining_length: 0,
188     };
189     let body = &[];
190     let r = GetInputFocusRequest::try_parse_request(header, body).unwrap();
191     assert_eq!(r, GetInputFocusRequest,);
192 }
193 
194 #[test]
test_query_text_extents()195 fn test_query_text_extents() {
196     use x11rb::protocol::xproto::{Char2b, QueryTextExtentsRequest};
197     let header = RequestHeader {
198         major_opcode: 48,
199         minor_opcode: 0,
200         remaining_length: 2,
201     };
202     let mut body = vec![];
203     add_ne!(body, 0x1234_5678u32);
204     body.extend(&[0xbc, 0x9a, 0xf0, 0xde]);
205     let r = QueryTextExtentsRequest::try_parse_request(header, &body).unwrap();
206     assert_eq!(
207         r,
208         QueryTextExtentsRequest {
209             font: 0x1234_5678,
210             string: Cow::Owned(vec![
211                 Char2b {
212                     byte1: 0xbc,
213                     byte2: 0x9a,
214                 },
215                 Char2b {
216                     byte1: 0xf0,
217                     byte2: 0xde,
218                 },
219             ]),
220         }
221     );
222 }
223 
224 #[test]
test_query_text_extents_odd_length()225 fn test_query_text_extents_odd_length() {
226     use x11rb::protocol::xproto::{Char2b, QueryTextExtentsRequest};
227     let header = RequestHeader {
228         major_opcode: 48,
229         minor_opcode: 1,
230         remaining_length: 2,
231     };
232     let mut body = vec![];
233     add_ne!(body, 0x1234_5678u32);
234     body.extend(&[0xbc, 0x9a, 0xf0, 0xde]);
235     let r = QueryTextExtentsRequest::try_parse_request(header, &body).unwrap();
236     assert_eq!(
237         r,
238         QueryTextExtentsRequest {
239             font: 0x1234_5678,
240             string: Cow::Owned(vec![Char2b {
241                 byte1: 0xbc,
242                 byte2: 0x9a,
243             },]),
244         }
245     );
246 }
247 
248 #[cfg(feature = "randr")]
249 #[test]
test_randr_get_output_property()250 fn test_randr_get_output_property() {
251     use x11rb::protocol::randr::GetOutputPropertyRequest;
252     let header = RequestHeader {
253         major_opcode: 140,
254         minor_opcode: 15,
255         remaining_length: 6,
256     };
257     let mut body = vec![];
258     add_ne!(body, 0x0000_008au32);
259     add_ne!(body, 0x0000_0045u32);
260     add_ne!(body, 0x0000_0000u32);
261     add_ne!(body, 0x0000_0000u32);
262     add_ne!(body, 0x0000_0080u32);
263     add_ne!(body, 0x0000_0000u32);
264     let r = GetOutputPropertyRequest::try_parse_request(header, &body).unwrap();
265     assert_eq!(
266         r,
267         GetOutputPropertyRequest {
268             output: 0x8a,
269             property: 0x45,
270             type_: 0,
271             long_offset: 0,
272             long_length: 128,
273             delete: false,
274             pending: false,
275         },
276     );
277 }
278