diff options
| author | mat <github@matdoes.dev> | 2022-09-01 20:37:38 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-09-01 20:37:38 -0500 |
| commit | 32458d743f757da3193717fe5554f490703640c0 (patch) | |
| tree | d0e655ac09195c41f6f7d44462d28aab4d1183db | |
| parent | 4d072d8057518fe233b8e7390f6c1f4c54b1cfc0 (diff) | |
| download | azalea-drasl-32458d743f757da3193717fe5554f490703640c0.tar.xz | |
fix bad u32::write_into and add tests
| -rw-r--r-- | azalea-buf/src/serializable_uuid.rs | 11 | ||||
| -rw-r--r-- | azalea-buf/src/write.rs | 2 | ||||
| -rwxr-xr-x | azalea-protocol/src/lib.rs | 38 | ||||
| -rwxr-xr-x | azalea-protocol/src/packets/login/serverbound_hello_packet.rs | 23 |
4 files changed, 67 insertions, 7 deletions
diff --git a/azalea-buf/src/serializable_uuid.rs b/azalea-buf/src/serializable_uuid.rs index fad5edfc..be93c5fd 100644 --- a/azalea-buf/src/serializable_uuid.rs +++ b/azalea-buf/src/serializable_uuid.rs @@ -73,4 +73,15 @@ mod tests { let u = Uuid::from_int_array([0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd]); assert_eq!(u.to_string(), "6536bfed-8695-48fd-83a1-ecd24cf2a0fd"); } + + #[test] + fn read_write() { + let u = Uuid::parse_str("6536bfed-8695-48fd-83a1-ecd24cf2a0fd").unwrap(); + let mut buf = Vec::new(); + u.write_into(&mut buf).unwrap(); + println!("{:?}", buf); + assert_eq!(buf.len(), 16); + let u2 = Uuid::read_from(&mut buf.as_slice()).unwrap(); + assert_eq!(u, u2); + } } diff --git a/azalea-buf/src/write.rs b/azalea-buf/src/write.rs index 8def52b3..15fa9680 100644 --- a/azalea-buf/src/write.rs +++ b/azalea-buf/src/write.rs @@ -181,7 +181,7 @@ impl McBufWritable for String { impl McBufWritable for u32 { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - i16::write_into(&(*self as i16), buf) + i32::write_into(&(*self as i32), buf) } } diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs index b1d1a9c4..f5922e27 100755 --- a/azalea-protocol/src/lib.rs +++ b/azalea-protocol/src/lib.rs @@ -50,9 +50,39 @@ pub async fn connect(address: ServerAddress) -> Result<(), Box<dyn std::error::E #[cfg(test)] mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); + use super::*; + use crate::{ + packets::login::{ + serverbound_hello_packet::{ProfilePublicKeyData, ServerboundHelloPacket}, + ServerboundLoginPacket, + }, + read::read_packet, + write::write_packet, + }; + use std::io::Cursor; + use uuid::Uuid; + + #[tokio::test] + async fn test_hello_packet() { + let packet = ServerboundHelloPacket { + username: "test".to_string(), + public_key: Some(ProfilePublicKeyData { + expires_at: 0, + key: b"idontthinkthisreallymattersijustwantittobelongforthetest".to_vec(), + key_signature: b"idontthinkthisreallymattersijustwantittobelongforthetest".to_vec(), + }), + profile_id: Some(Uuid::from_u128(0)), + } + .get(); + let mut stream = Cursor::new(Vec::new()); + write_packet(packet, &mut stream, None, &mut None) + .await + .unwrap(); + + stream.set_position(0); + + let _ = read_packet::<ServerboundLoginPacket, _>(&mut stream, None, &mut None) + .await + .unwrap(); } } diff --git a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs index 7644beae..a6443737 100755 --- a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs @@ -2,16 +2,35 @@ use azalea_buf::McBuf; use packet_macros::ServerboundLoginPacket; use uuid::Uuid; -#[derive(Clone, Debug, McBuf, ServerboundLoginPacket)] +#[derive(Clone, Debug, ServerboundLoginPacket, McBuf, PartialEq)] pub struct ServerboundHelloPacket { pub username: String, pub public_key: Option<ProfilePublicKeyData>, pub profile_id: Option<Uuid>, } -#[derive(Clone, Debug, McBuf)] +#[derive(Clone, Debug, McBuf, PartialEq)] pub struct ProfilePublicKeyData { pub expires_at: u64, pub key: Vec<u8>, pub key_signature: Vec<u8>, } + +#[cfg(test)] +mod tests { + use super::*; + use azalea_buf::{McBufReadable, McBufWritable}; + + #[test] + fn test_read_write() { + let packet = ServerboundHelloPacket { + username: "test".to_string(), + public_key: None, + profile_id: Some(Uuid::from_u128(0)), + }; + let mut buf = Vec::new(); + packet.write_into(&mut buf).unwrap(); + let packet2 = ServerboundHelloPacket::read_from(&mut buf.as_slice()).unwrap(); + assert_eq!(packet, packet2); + } +} |
