|
| 1 | +// Copyright 2023-2026 Divy Srivastava <dj.srivastava23@gmail.com> |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +use anyhow::Result; |
| 15 | +use fastwebsockets::FragmentCollector; |
| 16 | +use fastwebsockets::Frame; |
| 17 | +use fastwebsockets::OpCode; |
| 18 | +use fastwebsockets::Role; |
| 19 | +use fastwebsockets::WebSocket; |
| 20 | +use tokio::io::AsyncWriteExt; |
| 21 | +use tokio::io::DuplexStream; |
| 22 | + |
| 23 | +async fn write_masked_frame( |
| 24 | + stream: &mut DuplexStream, |
| 25 | + fin: bool, |
| 26 | + opcode: OpCode, |
| 27 | + mask: [u8; 4], |
| 28 | + payload: &[u8], |
| 29 | +) -> Result<()> { |
| 30 | + let mut masked = payload.to_vec(); |
| 31 | + fastwebsockets::unmask(&mut masked, mask); |
| 32 | + |
| 33 | + let mut frame = Frame::new(fin, opcode, Some(mask), masked.into()); |
| 34 | + let mut buf = Vec::new(); |
| 35 | + stream.write_all(frame.write(&mut buf)).await?; |
| 36 | + Ok(()) |
| 37 | +} |
| 38 | + |
| 39 | +#[tokio::test] |
| 40 | +async fn unfragmented_masked_text_can_be_manually_unmasked() -> Result<()> { |
| 41 | + let (mut client, server) = tokio::io::duplex(1024); |
| 42 | + let mut ws = WebSocket::after_handshake(server, Role::Server); |
| 43 | + ws.set_auto_apply_mask(false); |
| 44 | + let mut ws = FragmentCollector::new(ws); |
| 45 | + |
| 46 | + write_masked_frame( |
| 47 | + &mut client, |
| 48 | + true, |
| 49 | + OpCode::Text, |
| 50 | + [0xff, 0xff, 0xff, 0xff], |
| 51 | + b"hello", |
| 52 | + ) |
| 53 | + .await?; |
| 54 | + |
| 55 | + let mut frame = ws.read_frame().await?; |
| 56 | + assert_eq!(frame.opcode, OpCode::Text); |
| 57 | + assert_ne!(&frame.payload[..], b"hello"); |
| 58 | + |
| 59 | + frame.unmask(); |
| 60 | + assert_eq!(&frame.payload[..], b"hello"); |
| 61 | + |
| 62 | + Ok(()) |
| 63 | +} |
| 64 | + |
| 65 | +#[tokio::test] |
| 66 | +async fn fragmented_masked_text_is_unmasked_before_validation() -> Result<()> { |
| 67 | + let (mut client, server) = tokio::io::duplex(1024); |
| 68 | + let mut ws = WebSocket::after_handshake(server, Role::Server); |
| 69 | + ws.set_auto_apply_mask(false); |
| 70 | + let mut ws = FragmentCollector::new(ws); |
| 71 | + |
| 72 | + write_masked_frame( |
| 73 | + &mut client, |
| 74 | + false, |
| 75 | + OpCode::Text, |
| 76 | + [0xff, 0xff, 0xff, 0xff], |
| 77 | + b"hello ", |
| 78 | + ) |
| 79 | + .await?; |
| 80 | + write_masked_frame( |
| 81 | + &mut client, |
| 82 | + true, |
| 83 | + OpCode::Continuation, |
| 84 | + [0x80, 0x80, 0x80, 0x80], |
| 85 | + b"world", |
| 86 | + ) |
| 87 | + .await?; |
| 88 | + |
| 89 | + let mut frame = ws.read_frame().await?; |
| 90 | + assert_eq!(frame.opcode, OpCode::Text); |
| 91 | + assert_eq!(&frame.payload[..], b"hello world"); |
| 92 | + |
| 93 | + frame.unmask(); |
| 94 | + assert_eq!(&frame.payload[..], b"hello world"); |
| 95 | + |
| 96 | + Ok(()) |
| 97 | +} |
| 98 | + |
| 99 | +#[tokio::test] |
| 100 | +async fn fragmented_masked_binary_is_unmasked() -> Result<()> { |
| 101 | + let (mut client, server) = tokio::io::duplex(1024); |
| 102 | + let mut ws = WebSocket::after_handshake(server, Role::Server); |
| 103 | + ws.set_auto_apply_mask(false); |
| 104 | + let mut ws = FragmentCollector::new(ws); |
| 105 | + |
| 106 | + write_masked_frame( |
| 107 | + &mut client, |
| 108 | + false, |
| 109 | + OpCode::Binary, |
| 110 | + [0x7f, 0x7f, 0x7f, 0x7f], |
| 111 | + &[0, 1, 2, 3], |
| 112 | + ) |
| 113 | + .await?; |
| 114 | + write_masked_frame( |
| 115 | + &mut client, |
| 116 | + true, |
| 117 | + OpCode::Continuation, |
| 118 | + [0x55, 0x55, 0x55, 0x55], |
| 119 | + &[4, 5, 6, 7], |
| 120 | + ) |
| 121 | + .await?; |
| 122 | + |
| 123 | + let frame = ws.read_frame().await?; |
| 124 | + assert_eq!(frame.opcode, OpCode::Binary); |
| 125 | + assert_eq!(&frame.payload[..], &[0, 1, 2, 3, 4, 5, 6, 7]); |
| 126 | + |
| 127 | + Ok(()) |
| 128 | +} |
0 commit comments