From c33f7ca83070b0ae2049c90a46cf4c7558250cf6 Mon Sep 17 00:00:00 2001 From: Andy Newjack Date: Sat, 9 Nov 2019 12:57:42 -0400 Subject: [PATCH 1/2] initial test for banned user --- src/login/login.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/login/login.rs b/src/login/login.rs index 7cb71f4..021a06c 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -60,9 +60,15 @@ pub fn get_login_status(entity_gateway: &dyn EntityGateway, pkt: &Login) -> Resu let username = array_to_utf8(pkt.username).map_err(|_err| AccountStatus::Error)?; let password = array_to_utf8(pkt.password).map_err(|_err| AccountStatus::Error)?; let user = entity_gateway.get_user_by_name(username).ok_or(AccountStatus::InvalidUser)?; + let banned = user.banned; let verified = bcrypt::verify(password, user.password.as_str()).map_err(|_err| AccountStatus::Error)?; match verified { - true => Ok(user), + true => if banned { + Err(AccountStatus::Banned) + } + else { + Ok(user) + }, false => Err(AccountStatus::InvalidPassword) } } @@ -279,4 +285,47 @@ mod test { caps: 258 }))]) } + + #[test] + fn test_banned_user() { + struct TestData { + } + + impl EntityGateway for TestData { + fn get_user_by_name(&self, name: String) -> Option { + assert!(name == "testuser"); + Some(UserAccount { + id: 1, + username: "testuser".to_owned(), + password: bcrypt::hash("mypassword", 5).unwrap(), + guildcard: None, + team_id: None, + banned: true, + muted_until: SystemTime::now(), + created_at: SystemTime::now(), + flags: 0, + }) + } + } // end impl EntityGateway for TestData + + let mut server = LoginServerState::new(TestData {}); + let send = server.handle(ClientId(1), &LOGIN_PACKET).unwrap().collect::>(); + + assert!(send == vec![ + (ClientId(1), SendLoginPacket::LoginResponse(LoginResponse { + status: AccountStatus::Banned, + tag: 65536, + guildcard: 0, + team_id: 0, + session: Session { + version: [69, 108, 115, 101, 119, 97, 114, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // utf8_to_array!("Elseware", 30), + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + session_id: 0, + interserver_checksum: 0, + action: SessionAction::None, + character_slot: 0, + }, + caps: 258 + }))]) + } // end test_banned_user() } From 02947adc9804abafc91f350a1eb191440d38480f Mon Sep 17 00:00:00 2001 From: Andy Newjack Date: Tue, 26 Nov 2019 01:00:38 -0400 Subject: [PATCH 2/2] removed let banned and extra comments --- src/login/login.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/login/login.rs b/src/login/login.rs index 021a06c..9932202 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -60,10 +60,9 @@ pub fn get_login_status(entity_gateway: &dyn EntityGateway, pkt: &Login) -> Resu let username = array_to_utf8(pkt.username).map_err(|_err| AccountStatus::Error)?; let password = array_to_utf8(pkt.password).map_err(|_err| AccountStatus::Error)?; let user = entity_gateway.get_user_by_name(username).ok_or(AccountStatus::InvalidUser)?; - let banned = user.banned; let verified = bcrypt::verify(password, user.password.as_str()).map_err(|_err| AccountStatus::Error)?; match verified { - true => if banned { + true => if user.banned { Err(AccountStatus::Banned) } else { @@ -306,7 +305,7 @@ mod test { flags: 0, }) } - } // end impl EntityGateway for TestData + } let mut server = LoginServerState::new(TestData {}); let send = server.handle(ClientId(1), &LOGIN_PACKET).unwrap().collect::>(); @@ -327,5 +326,5 @@ mod test { }, caps: 258 }))]) - } // end test_banned_user() + } }