From 4bb68bb22d285078aa189ed4c049d459ffae3e1e Mon Sep 17 00:00:00 2001 From: vagola Date: Tue, 31 Dec 2019 04:07:45 +0300 Subject: [PATCH] Add tests for chat message, keep alive and join game --- protocol/src/game.rs | 150 +++++++++++++++++- protocol/src/lib.rs | 2 +- protocol/src/status.rs | 4 +- .../packet/game/client_bound_chat_message.dat | 1 + .../packet/game/client_bound_keep_alive.dat | Bin 0 -> 8 bytes protocol/test/packet/game/join_game.dat | Bin 0 -> 20 bytes .../packet/game/server_bound_chat_message.dat | 1 + .../packet/game/server_bound_keep_alive.dat | Bin 0 -> 8 bytes 8 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 protocol/test/packet/game/client_bound_chat_message.dat create mode 100644 protocol/test/packet/game/client_bound_keep_alive.dat create mode 100644 protocol/test/packet/game/join_game.dat create mode 100644 protocol/test/packet/game/server_bound_chat_message.dat create mode 100644 protocol/test/packet/game/server_bound_keep_alive.dat diff --git a/protocol/src/game.rs b/protocol/src/game.rs index b387153..8133dbc 100644 --- a/protocol/src/game.rs +++ b/protocol/src/game.rs @@ -204,7 +204,7 @@ impl PacketParser for JoinGame { writer.write_i32::(self.dimension)?; writer.write_u8(self.max_players)?; writer.write_string(&self.level_type, LEVEL_TYPE_MAX_LENGTH)?; - writer.write_var_u32(self.view_distance as u32)?; + writer.write_var_i32(self.view_distance as i32)?; writer.write_bool(self.reduced_debug_info)?; Ok(()) @@ -216,7 +216,7 @@ impl PacketParser for JoinGame { let dimension = reader.read_i32::()?; let max_players = reader.read_u8()?; let level_type = reader.read_string(LEVEL_TYPE_MAX_LENGTH)?; - let view_distance = reader.read_var_u32()? as u8; + let view_distance = reader.read_var_i32()? as u8; let reduced_debug_info = reader.read_bool()?; Ok(JoinGame { @@ -367,3 +367,149 @@ impl PacketParser for ChunkData { }) } } + +#[cfg(test)] +mod tests { + use crate::chat::{Message, Payload}; + use crate::game::{ + ClientBoundChatMessage, ClientBoundKeepAlive, GameMode, JoinGame, MessagePosition, + ServerBoundChatMessage, ServerBoundKeepAlive, + }; + use crate::PacketParser; + use std::io::Cursor; + + #[test] + fn test_server_bound_chat_message_encode() { + let chat_message = ServerBoundChatMessage { + message: String::from("hello server!"), + }; + + let mut vec = Vec::new(); + chat_message.encode(&mut vec).unwrap(); + + assert_eq!( + vec, + include_bytes!("../test/packet/game/server_bound_chat_message.dat").to_vec() + ); + } + + #[test] + fn test_server_bound_chat_message_decode() { + let mut cursor = Cursor::new( + include_bytes!("../test/packet/game/server_bound_chat_message.dat").to_vec(), + ); + let chat_message = ServerBoundChatMessage::decode(&mut cursor).unwrap(); + + assert_eq!(chat_message.message, "hello server!"); + } + + #[test] + fn test_client_bound_chat_message_encode() { + let chat_message = ClientBoundChatMessage { + message: Message::new(Payload::text("hello client!")), + position: MessagePosition::System, + }; + + let mut vec = Vec::new(); + chat_message.encode(&mut vec).unwrap(); + + assert_eq!( + vec, + include_bytes!("../test/packet/game/client_bound_chat_message.dat").to_vec() + ); + } + + #[test] + fn test_client_bound_chat_message_decode() { + let mut cursor = Cursor::new( + include_bytes!("../test/packet/game/client_bound_chat_message.dat").to_vec(), + ); + let chat_message = ClientBoundChatMessage::decode(&mut cursor).unwrap(); + + assert_eq!( + chat_message.message, + Message::new(Payload::text("hello client!")) + ); + + assert_eq!(chat_message.position, MessagePosition::System); + } + + #[test] + fn test_server_bound_keep_alive_encode() { + let keep_alive = ServerBoundKeepAlive { id: 31122019 }; + + let mut vec = Vec::new(); + keep_alive.encode(&mut vec).unwrap(); + + assert_eq!( + vec, + include_bytes!("../test/packet/game/server_bound_keep_alive.dat").to_vec() + ); + } + + #[test] + fn test_server_bound_keep_alive_decode() { + let mut cursor = + Cursor::new(include_bytes!("../test/packet/game/server_bound_keep_alive.dat").to_vec()); + let keep_alive = ServerBoundKeepAlive::decode(&mut cursor).unwrap(); + + assert_eq!(keep_alive.id, 31122019); + } + + #[test] + fn test_client_bound_keep_alive_encode() { + let keep_alive = ClientBoundKeepAlive { id: 240714 }; + + let mut vec = Vec::new(); + keep_alive.encode(&mut vec).unwrap(); + + assert_eq!( + vec, + include_bytes!("../test/packet/game/client_bound_keep_alive.dat").to_vec() + ); + } + + #[test] + fn test_client_bound_keep_alive_decode() { + let mut cursor = + Cursor::new(include_bytes!("../test/packet/game/client_bound_keep_alive.dat").to_vec()); + let keep_alive = ClientBoundKeepAlive::decode(&mut cursor).unwrap(); + + assert_eq!(keep_alive.id, 240714); + } + + #[test] + fn test_join_game_encode() { + let join_game = JoinGame { + entity_id: 27, + game_mode: GameMode::Spectator, + dimension: 23, + max_players: 100, + level_type: String::from("default"), + view_distance: 10, + reduced_debug_info: true, + }; + + let mut vec = Vec::new(); + join_game.encode(&mut vec).unwrap(); + + assert_eq!( + vec, + include_bytes!("../test/packet/game/join_game.dat").to_vec() + ); + } + + #[test] + fn test_join_game_decode() { + let mut cursor = Cursor::new(include_bytes!("../test/packet/game/join_game.dat").to_vec()); + let join_game = JoinGame::decode(&mut cursor).unwrap(); + + assert_eq!(join_game.entity_id, 27); + assert_eq!(join_game.game_mode, GameMode::Spectator); + assert_eq!(join_game.dimension, 23); + assert_eq!(join_game.max_players, 100); + assert_eq!(join_game.level_type, String::from("default")); + assert_eq!(join_game.view_distance, 10); + assert!(join_game.reduced_debug_info); + } +} diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 69bd201..af20277 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -23,7 +23,7 @@ pub mod login; pub mod status; /// Current supported protocol version. -pub const PROTOCOL_VERSION: u32 = 575; +pub const PROTOCOL_VERSION: u32 = 498; /// String maximum length. const STRING_MAX_LENGTH: u32 = 32_768; diff --git a/protocol/src/status.rs b/protocol/src/status.rs index 2dc1b7a..8a5cacc 100644 --- a/protocol/src/status.rs +++ b/protocol/src/status.rs @@ -167,7 +167,7 @@ mod tests { OnlinePlayer, OnlinePlayers, PingRequest, PingResponse, ServerStatus, ServerVersion, StatusResponse, }; - use crate::{PacketParser, PROTOCOL_VERSION}; + use crate::PacketParser; use std::io::Cursor; use uuid::Uuid; @@ -223,7 +223,7 @@ mod tests { fn test_status_response_encode() { let version = ServerVersion { name: String::from("1.15.1"), - protocol: PROTOCOL_VERSION, + protocol: 575, }; let player = OnlinePlayer { diff --git a/protocol/test/packet/game/client_bound_chat_message.dat b/protocol/test/packet/game/client_bound_chat_message.dat new file mode 100644 index 0000000..f66ac04 --- /dev/null +++ b/protocol/test/packet/game/client_bound_chat_message.dat @@ -0,0 +1 @@ +{"text":"hello client!"} \ No newline at end of file diff --git a/protocol/test/packet/game/client_bound_keep_alive.dat b/protocol/test/packet/game/client_bound_keep_alive.dat new file mode 100644 index 0000000000000000000000000000000000000000..51d9e4cb88bdeba56ec28ea7b2da446bf6fa714b GIT binary patch literal 8 NcmZQz00QPUUH}2J0Qvv` literal 0 HcmV?d00001 diff --git a/protocol/test/packet/game/join_game.dat b/protocol/test/packet/game/join_game.dat new file mode 100644 index 0000000000000000000000000000000000000000..e2e43830bbbc50042243e09ddbe8d36d11c7ac27 GIT binary patch literal 20 bcmZQzV31~JU|