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