1mod arq;
35mod datatype;
36pub mod error;
37mod fragment;
38mod log;
39mod packet;
40mod sequence;
41mod server;
42mod socket;
43mod utils;
44
45pub use crate::arq::Reliability;
46pub use crate::log::enable_raknet_log;
47pub use crate::server::*;
48pub use crate::socket::*;
49
50#[test]
51fn test_raknet_error_implements_std_error() {
52 fn assert_error<T: std::error::Error>() {}
53 assert_error::<error::RaknetError>();
54 assert_eq!(
55 error::RaknetError::NotListen.to_string(),
56 "the RakNet listener is not listening"
57 );
58}
59
60#[tokio::test]
61async fn test_listener_updates_motd_after_listen() {
62 let mut listener = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
63 .await
64 .unwrap();
65 let local_addr = listener.local_addr().unwrap();
66 listener.listen().await;
67
68 let motd = "MCPE;Updated MOTD;486;1.20.0;0;20;123;World;Survival;1;19132;";
69 listener.set_full_motd(motd.to_owned()).await.unwrap();
70
71 let (_, received_motd) = tokio::time::timeout(
72 std::time::Duration::from_secs(3),
73 RaknetSocket::ping(&local_addr),
74 )
75 .await
76 .expect("server did not answer the unconnected ping")
77 .unwrap();
78 assert_eq!(received_motd, motd);
79
80 listener.close().await.unwrap();
81}
82
83#[tokio::test]
84async fn test_listener_accepts_more_than_one_queue_of_connections() {
85 const CONNECTIONS: usize = 12;
86
87 let mut listener = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
88 .await
89 .unwrap();
90 let local_addr = listener.local_addr().unwrap();
91 listener.listen().await;
92
93 let accept_task = tokio::spawn(async move {
94 let mut accepted = Vec::with_capacity(CONNECTIONS);
95 for _ in 0..CONNECTIONS {
96 accepted.push(
97 tokio::time::timeout(std::time::Duration::from_secs(5), listener.accept())
98 .await
99 .expect("listener stopped accepting connections")
100 .unwrap(),
101 );
102 }
103 (accepted, listener)
104 });
105
106 let mut clients = Vec::with_capacity(CONNECTIONS);
107 for _ in 0..CONNECTIONS {
108 clients.push(
109 tokio::time::timeout(
110 std::time::Duration::from_secs(5),
111 RaknetSocket::connect(&local_addr),
112 )
113 .await
114 .expect("client connection timed out")
115 .unwrap(),
116 );
117 }
118
119 let (accepted, mut listener) =
120 tokio::time::timeout(std::time::Duration::from_secs(5), accept_task)
121 .await
122 .expect("listener accept loop timed out")
123 .unwrap();
124 assert_eq!(accepted.len(), CONNECTIONS);
125
126 for socket in clients {
127 socket.close().await.unwrap();
128 }
129 for socket in accepted {
130 socket.close().await.unwrap();
131 }
132 listener.close().await.unwrap();
133}
134
135#[tokio::test]
136async fn test_connect() {
137 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
138 .await
139 .unwrap();
140 let local_addr = server.local_addr().unwrap();
141 server.listen().await;
142
143 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
144 let notify2 = notify.clone();
145
146 tokio::spawn(async move {
147 let client1 = server.accept().await.unwrap();
148 assert!(client1.local_addr().unwrap() == local_addr);
149 client1
150 .send(&[0xfe, 2, 3], Reliability::Reliable)
151 .await
152 .unwrap();
153 notify2.notified().await;
154 });
155 let client2 = RaknetSocket::connect(&local_addr).await.unwrap();
156 assert!(client2.peer_addr().unwrap() == local_addr);
157 let buf = client2.recv().await.unwrap();
158 assert!(buf == vec![0xfe, 2, 3]);
159
160 notify.notify_one();
161}
162
163#[tokio::test]
164async fn test_send_recv_fragment_data() {
165 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
166 .await
167 .unwrap();
168 let local_addr = server.local_addr().unwrap();
169 server.listen().await;
170
171 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
172 let notify2 = notify.clone();
173
174 tokio::spawn(async move {
175 let client1 = server.accept().await.unwrap();
176 assert!(client1.local_addr().unwrap() == local_addr);
177
178 let mut a = vec![3u8; 1000];
179 let mut b = vec![2u8; 1000];
180 let mut c = vec![0xfe; 1000];
181 b.append(&mut a);
182 c.append(&mut b);
183
184 client1
185 .send(&c, Reliability::ReliableOrdered)
186 .await
187 .unwrap();
188
189 notify2.notified().await;
190 });
191 let client2 = RaknetSocket::connect(&local_addr).await.unwrap();
192 assert!(client2.peer_addr().unwrap() == local_addr);
193 let buf = client2.recv().await.unwrap();
194 assert!(buf.len() == 3000);
195 assert!(buf[0..1000] == vec![0xfe; 1000]);
196 assert!(buf[1000..2000] == vec![2u8; 1000]);
197 assert!(buf[2000..3000] == vec![3u8; 1000]);
198
199 notify.notify_one();
200}
201
202#[tokio::test]
203async fn test_send_recv_more_reliability_type_packet() {
204 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
205 .await
206 .unwrap();
207 let local_addr = server.local_addr().unwrap();
208 server.listen().await;
209
210 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
211 let notify2 = notify.clone();
212
213 tokio::spawn(async move {
214 let client1 = server.accept().await.unwrap();
215 assert!(client1.local_addr().unwrap() == local_addr);
216
217 client1
218 .send(&[0xfe, 1, 2, 3], Reliability::Unreliable)
219 .await
220 .unwrap();
221 let data = client1.recv().await.unwrap();
222 assert!(data == [0xfe, 4, 5, 6].to_vec());
223
224 client1
225 .send(&[0xfe, 7, 8, 9], Reliability::UnreliableSequenced)
226 .await
227 .unwrap();
228 let data = client1.recv().await.unwrap();
229 assert!(data == [0xfe, 10, 11, 12].to_vec());
230
231 client1
232 .send(&[0xfe, 13, 14, 15], Reliability::Reliable)
233 .await
234 .unwrap();
235 let data = client1.recv().await.unwrap();
236 assert!(data == [0xfe, 16, 17, 18].to_vec());
237
238 let mut a = vec![3u8; 1000];
239 let mut b = vec![2u8; 1000];
240 let mut c = vec![0xfe; 1000];
241 b.append(&mut a);
242 c.append(&mut b);
243
244 client1
245 .send(&c, Reliability::ReliableOrdered)
246 .await
247 .unwrap();
248
249 let buf = client1.recv().await.unwrap();
250 assert!(buf.len() == 3000);
251 assert!(buf[0..1000] == vec![0xfe; 1000]);
252 assert!(buf[1000..2000] == vec![2u8; 1000]);
253 assert!(buf[2000..3000] == vec![3u8; 1000]);
254
255 client1
256 .send(&[0xfe, 19, 20, 21], Reliability::ReliableSequenced)
257 .await
258 .unwrap();
259 let data = client1.recv().await.unwrap();
260 assert!(data == [0xfe, 22, 23, 24].to_vec());
261
262 notify2.notified().await;
263 });
264 let client2 = RaknetSocket::connect(&local_addr).await.unwrap();
265 assert!(client2.peer_addr().unwrap() == local_addr);
266
267 let buf = client2.recv().await.unwrap();
268 assert!(buf == [0xfe, 1, 2, 3]);
269
270 client2
271 .send(&[0xfe, 4, 5, 6], Reliability::Unreliable)
272 .await
273 .unwrap();
274
275 let buf = client2.recv().await.unwrap();
276 assert!(buf == [0xfe, 7, 8, 9]);
277
278 client2
279 .send(&[0xfe, 10, 11, 12], Reliability::UnreliableSequenced)
280 .await
281 .unwrap();
282
283 let buf = client2.recv().await.unwrap();
284 assert!(buf == [0xfe, 13, 14, 15]);
285
286 client2
287 .send(&[0xfe, 16, 17, 18], Reliability::Reliable)
288 .await
289 .unwrap();
290
291 let buf = client2.recv().await.unwrap();
292 assert!(buf.len() == 3000);
293 assert!(buf[0..1000] == vec![0xfe; 1000]);
294 assert!(buf[1000..2000] == vec![2u8; 1000]);
295 assert!(buf[2000..3000] == vec![3u8; 1000]);
296
297 let mut a = vec![3u8; 1000];
298 let mut b = vec![2u8; 1000];
299 let mut c = vec![0xfe; 1000];
300 b.append(&mut a);
301 c.append(&mut b);
302
303 client2
304 .send(&c, Reliability::ReliableOrdered)
305 .await
306 .unwrap();
307
308 let buf = client2.recv().await.unwrap();
309 assert!(buf == [0xfe, 19, 20, 21]);
310
311 client2
312 .send(&[0xfe, 22, 23, 24], Reliability::ReliableSequenced)
313 .await
314 .unwrap();
315
316 notify.notify_one();
317}
318
319#[tokio::test]
320async fn test_loss_packet1() {
321 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
322 let notify2 = notify.clone();
323 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
324 .await
325 .unwrap();
326 let local_addr = server.local_addr().unwrap();
327 server.listen().await;
328 tokio::spawn(async move {
329 let mut client1 = server.accept().await.unwrap();
330 client1.set_loss_rate(2);
332
333 for i in 0..10 {
334 let mut flag = vec![0xfe_u8];
335 let mut data = vec![i as u8; 2000];
336 flag.append(&mut data);
337 client1
338 .send(&flag, Reliability::ReliableOrdered)
339 .await
340 .unwrap();
341
342 let data = client1.recv().await.unwrap();
343 assert!(data == flag);
344 }
345
346 notify2.notified().await;
347 });
348 let mut client2 = RaknetSocket::connect(&local_addr).await.unwrap();
349 client2.set_loss_rate(2);
351
352 for i in 0..10 {
353 let mut flag = vec![0xfe_u8];
354 let mut data = vec![i as u8; 2000];
355 flag.append(&mut data);
356 client2
357 .send(&flag, Reliability::ReliableOrdered)
358 .await
359 .unwrap();
360
361 let data = client2.recv().await.unwrap();
362 assert!(data == flag);
363 }
364 notify.notify_one();
365}
366
367#[tokio::test]
368async fn test_loss_packet2() {
369 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
370 let notify2 = notify.clone();
371 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
372 .await
373 .unwrap();
374 let local_addr = server.local_addr().unwrap();
375 server.listen().await;
376 tokio::spawn(async move {
377 let mut client1 = server.accept().await.unwrap();
378 client1.set_loss_rate(2);
380
381 for i in 0..10 {
382 let mut flag = vec![0xfe_u8];
383 let mut data = vec![i as u8; 2000];
384 flag.append(&mut data);
385 client1
386 .send(&flag, Reliability::ReliableOrdered)
387 .await
388 .unwrap();
389 }
390
391 for i in 0..10 {
392 let mut flag = vec![0xfe_u8];
393 let mut data = vec![i as u8; 2000];
394 flag.append(&mut data);
395 let data = client1.recv().await.unwrap();
396 assert!(data == flag);
397 }
398 notify2.notified().await;
399 });
400 let mut client2 = RaknetSocket::connect(&local_addr).await.unwrap();
401 client2.set_loss_rate(2);
403
404 for i in 0..10 {
405 let mut flag = vec![0xfe_u8];
406 let mut data = vec![i as u8; 2000];
407 flag.append(&mut data);
408 client2
409 .send(&flag, Reliability::ReliableOrdered)
410 .await
411 .unwrap();
412 }
413
414 for i in 0..10 {
415 let mut flag = vec![0xfe_u8];
416 let mut data = vec![i as u8; 2000];
417 flag.append(&mut data);
418 let data = client2.recv().await.unwrap();
419 assert!(data == flag);
420 }
421 notify.notify_one();
422}
423
424#[tokio::test]
425async fn test_loss_packet_with_sequenced() {
426 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
427 let notify2 = notify.clone();
428 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
429 .await
430 .unwrap();
431 let local_addr = server.local_addr().unwrap();
432 server.listen().await;
433 tokio::spawn(async move {
434 let mut client1 = server.accept().await.unwrap();
435 client1.set_loss_rate(2);
437
438 for i in 0..100 {
439 let mut flag = vec![0xfe_u8];
440 let mut data = vec![i as u8; 20];
441 flag.append(&mut data);
442 client1
443 .send(&flag, Reliability::ReliableSequenced)
444 .await
445 .unwrap();
446 }
447
448 let mut last = 0;
449 for i in 0..50 {
450 let mut flag = vec![0xfe_u8];
451 let mut data = vec![i as u8; 20];
452 flag.append(&mut data);
453 let data = client1.recv().await.unwrap();
454 assert!(data[1] >= last);
455 last = data[1];
456 }
457 notify2.notified().await;
458 });
459 let mut client2 = RaknetSocket::connect(&local_addr).await.unwrap();
460 client2.set_loss_rate(2);
462
463 for i in 0..100 {
464 let mut flag = vec![0xfe_u8];
465 let mut data = vec![i as u8; 20];
466 flag.append(&mut data);
467 client2
468 .send(&flag, Reliability::ReliableSequenced)
469 .await
470 .unwrap();
471 }
472
473 let mut last = 0;
474 for i in 0..50 {
475 let mut flag = vec![0xfe_u8];
476 let mut data = vec![i as u8; 20];
477 flag.append(&mut data);
478 let data = client2.recv().await.unwrap();
479 assert!(data[1] >= last);
480 last = data[1];
481 }
482 notify.notify_one();
483}
484
485#[tokio::test]
486async fn test_raknet_server_close() {
487 for _ in 0..10 {
488 let mut server = RaknetListener::bind(&"127.0.0.1:19132".parse().unwrap())
489 .await
490 .unwrap();
491 server.listen().await;
492 let client = RaknetSocket::connect(&"127.0.0.1:19132".parse().unwrap())
493 .await
494 .unwrap();
495 let mut a = vec![3u8; 1000];
496 let mut b = vec![2u8; 1000];
497 let mut c = vec![0xfe; 1000];
498 b.append(&mut a);
499 c.append(&mut b);
500 client.send(&c, Reliability::ReliableOrdered).await.unwrap();
501
502 let client2 = server.accept().await.unwrap();
503 let buf = client2.recv().await.unwrap();
504 assert!(buf.len() == 3000);
505 assert!(buf[0..1000] == vec![0xfe; 1000]);
506 assert!(buf[1000..2000] == vec![2u8; 1000]);
507 assert!(buf[2000..3000] == vec![3u8; 1000]);
508
509 server.close().await.unwrap();
510 client.close().await.unwrap();
511 }
512
513 let mut server1 = RaknetListener::bind(&"127.0.0.1:19132".parse().unwrap())
514 .await
515 .unwrap();
516 server1.listen().await;
517 server1.close().await.unwrap();
518 let mut server2 = RaknetListener::bind(&"127.0.0.1:19132".parse().unwrap())
519 .await
520 .unwrap();
521 server2.listen().await;
522}
523
524#[tokio::test]
525async fn test_send_recv_full_packet() {
526 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
527 .await
528 .unwrap();
529 let remote_addr = format!("127.0.0.1:{}", server.local_addr().unwrap().port());
530 tokio::spawn(async move {
531 server.listen().await;
532 let client = server.accept().await.unwrap();
533
534 for _ in 0..50 {
537 client
538 .send(&vec![0xfe; 1000], Reliability::ReliableOrdered)
539 .await
540 .unwrap();
541 }
542 client.flush().await.unwrap();
543
544 client.close().await.unwrap();
545 server.close().await.unwrap();
546 });
547
548 let client = RaknetSocket::connect(&remote_addr.parse().unwrap())
549 .await
550 .unwrap();
551
552 for _ in 0..50 {
553 let buf = client.recv().await.unwrap();
554 assert!(buf == [0xfe; 1000]);
555 }
556}
557
558#[tokio::test]
559async fn test_send_recv_with_flush() {
560 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
561 .await
562 .unwrap();
563 let remote_addr = format!("127.0.0.1:{}", server.local_addr().unwrap().port());
564 let s1 = std::sync::Arc::new(tokio::sync::Semaphore::new(1));
565 let s2 = s1.clone();
566 tokio::spawn(async move {
567 server.listen().await;
568 let client = server.accept().await.unwrap();
569
570 for _ in 0..50 {
571 #[allow(unused_must_use)]
572 {
573 s1.acquire().await.unwrap();
574 }
575 client
576 .send(&vec![0xfe; 1000], Reliability::ReliableSequenced)
577 .await
578 .unwrap();
579 client.flush().await.unwrap();
580 }
581
582 client.close().await.unwrap();
583 for _ in 0..5 {
584 client
585 .send(&vec![0xfe; 1000], Reliability::ReliableSequenced)
586 .await
587 .unwrap();
588 match match client.flush().await {
589 Ok(_) => panic!("incorrect return"),
590 Err(e) => e,
591 } {
592 error::RaknetError::ConnectionClosed => {}
593 _ => panic!("incorrect return"),
594 };
595 }
596 server.close().await.unwrap();
597 });
598
599 let client = RaknetSocket::connect(&remote_addr.parse().unwrap())
600 .await
601 .unwrap();
602
603 for _ in 0..50 {
604 let buf = client.recv().await.unwrap();
605 assert!(buf == [0xfe; 1000]);
606 s2.add_permits(1);
607 }
608}
609
610