@@ -316,12 +316,16 @@ mod tests {
316
316
use super :: * ;
317
317
use crate :: packets:: from_client:: auth:: AuthLogin ;
318
318
use crate :: packets:: from_client:: protocol:: ProtocolVersion ;
319
- use l2_core:: shared_packets:: common:: { ReadablePacket , SendablePacket } ;
320
- use l2_core:: shared_packets:: gs_2_ls:: PlayerAuthRequest ;
319
+ use crate :: packets:: to_client:: PlayerLoginResponse ;
320
+ use l2_core:: shared_packets:: common:: { PacketType , ReadablePacket , SendablePacket } ;
321
+ use l2_core:: shared_packets:: gs_2_ls:: { PlayerAuthRequest , PlayerInGame } ;
322
+ use l2_core:: shared_packets:: ls_2_gs:: PlayerAuthResponse ;
323
+ use l2_core:: shared_packets:: read:: ReadablePacketBuffer ;
321
324
use l2_core:: shared_packets:: write:: SendablePacketBuffer ;
322
325
use l2_core:: tests:: get_test_db;
323
326
use l2_core:: traits:: ServerConfig ;
324
327
use ntest:: timeout;
328
+ use sea_orm:: { ActiveModelTrait , ActiveValue , TryIntoModel } ;
325
329
use tokio:: io:: { split, AsyncReadExt , AsyncWriteExt , DuplexStream } ;
326
330
use tokio:: task:: JoinHandle ;
327
331
@@ -447,41 +451,80 @@ mod tests {
447
451
& self . writer
448
452
}
449
453
}
454
+
455
+ /// This test is testing whole logic for authenticating user from Protocol version
456
+ /// and authenticating on login server.
457
+ /// I decided to do integration test instead of small unit tests just to be able to change
458
+ /// internals while still not braking functionality.
450
459
#[ tokio:: test]
451
460
#[ timeout( 2000 ) ]
452
461
async fn test_integration_auth_ok ( ) {
453
462
// Create a listener on a local port
454
463
let ( mut client, server) = tokio:: io:: duplex ( 1024 ) ;
455
464
let ( login_client, mut login_server) = tokio:: io:: duplex ( 1024 ) ;
456
-
457
465
let cfg = Arc :: new ( GSServer :: from_string ( include_str ! (
458
466
"../../test_data/game.yaml"
459
467
) ) ) ;
460
- let controller = Controller :: new ( cfg) ;
468
+ let user_record = user:: ActiveModel {
469
+ id : ActiveValue :: NotSet ,
470
+ username : ActiveValue :: Set ( "test" . to_string ( ) ) ,
471
+ password : ActiveValue :: Set ( "password_hash" . to_string ( ) ) ,
472
+ access_level : ActiveValue :: Set ( 0 ) ,
473
+ ban_duration : ActiveValue :: NotSet ,
474
+ ban_ip : ActiveValue :: NotSet ,
475
+ } ;
476
+ let user_model = user_record. save ( & get_test_db ( ) . await ) . await . unwrap ( ) ;
477
+ let controller = Arc :: new ( Controller :: new ( cfg) ) ;
461
478
let test_packet_sender = Arc :: new ( TestPacketSender {
462
479
writer : Arc :: new ( Mutex :: new ( login_client) ) ,
463
480
} ) ;
464
481
controller
465
482
. message_broker
466
483
. register_packet_handler ( LoginHandler :: HANDLER_ID , test_packet_sender. clone ( ) ) ;
467
- build_client_handler ( server, Some ( Arc :: new ( controller) ) ) ;
484
+ let handle = build_client_handler ( server, Some ( controller. clone ( ) ) ) ;
468
485
let mut login_packet = ProtocolVersion :: new ( 6_553_697 ) . unwrap ( ) ;
469
486
let bytes = login_packet. get_bytes ( false ) ;
470
487
client. write_all ( bytes) . await . unwrap ( ) ;
488
+ let mut protocol_response = [ 0 ; 26 ] ;
489
+ client. read_exact ( & mut protocol_response) . await . unwrap ( ) ;
490
+ assert_eq ! ( protocol_response[ 0 ] , 26 ) ;
471
491
let mut auth = AuthLogin :: new ( ) . unwrap ( ) ;
472
492
client. write_all ( auth. get_bytes ( false ) ) . await . unwrap ( ) ;
473
493
let mut auth_login_packet = [ 0 ; 29 ] ;
474
494
login_server
475
495
. read_exact ( & mut auth_login_packet)
476
496
. await
477
497
. unwrap ( ) ;
478
- println ! ( "Auth success: {auth_login_packet:?}" ) ;
479
498
let p = PlayerAuthRequest :: read ( & auth_login_packet[ 2 ..] ) . unwrap ( ) ;
480
- client. shutdown ( ) . await . unwrap ( ) ;
481
499
assert_eq ! ( p. account_name, "test" ) ;
482
500
assert_eq ! ( p. session. play_ok1, 1 ) ; //it should be vice versa
483
501
assert_eq ! ( p. session. play_ok2, 0 ) ;
484
502
assert_eq ! ( p. session. login_ok1, 2 ) ;
485
503
assert_eq ! ( p. session. login_ok2, 3 ) ;
504
+ let auth_ok_packet = PlayerAuthResponse :: new ( "test" , true ) ;
505
+ //login ok
506
+ controller. message_broker . respond_to_message (
507
+ Some ( LoginHandler :: HANDLER_ID ) ,
508
+ "test" ,
509
+ PacketType :: PlayerAuthResp ( auth_ok_packet) ,
510
+ ) ;
511
+ let mut player_in_game = [ 0 ; 15 ] ;
512
+ login_server. read_exact ( & mut player_in_game) . await . unwrap ( ) ;
513
+ let pig = PlayerInGame :: read ( & player_in_game[ 2 ..] ) . unwrap ( ) ;
514
+ assert_eq ! ( pig. accounts, [ "test" ] ) ;
515
+ let mut auth_ok_resp = [ 0 ; 11 ] ;
516
+ client. read_exact ( & mut auth_ok_resp) . await . unwrap ( ) ;
517
+ let mut buffer = ReadablePacketBuffer :: new ( & auth_ok_resp[ 2 ..] ) ;
518
+ let p_id = buffer. read_byte ( ) . unwrap ( ) ;
519
+ let is_ok = buffer. read_i32 ( ) . unwrap ( ) ;
520
+ let reason = buffer. read_u32 ( ) . unwrap ( ) ;
521
+ assert_eq ! ( PlayerLoginResponse :: PACKET_ID , p_id) ;
522
+ assert_eq ! ( is_ok, -1 ) ;
523
+ assert_eq ! ( reason, 0 ) ;
524
+ client. shutdown ( ) . await . unwrap ( ) ;
525
+ let ( ch, _) = handle. await . unwrap ( ) ;
526
+ assert_eq ! ( ch. protocol, Some ( 6_553_697 ) ) ;
527
+ assert_eq ! ( ch. session_key. unwrap( ) , p. session) ;
528
+ assert_eq ! ( ch. user. unwrap( ) , user_model. try_into_model( ) . unwrap( ) ) ;
486
529
}
487
530
}
0 commit comments