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