Buttplug sex toy control library

chore: Add v4 message blocking through downgrader

Until v4 stablizes, require using a feature flag to turn it on through
the downgrader. If anyone is using ButtplugServer directly, it requires
the message type anyways so there's not much we can do there.

+30 -5
+30 -5
buttplug/src/server/server_downgrade_wrapper.rs
··· 7 7 8 8 use std::{fmt, sync::Arc}; 9 9 10 - use crate::core::message::{self, ButtplugClientMessageVariant, ButtplugMessageSpecVersion, ButtplugServerMessageV4, ButtplugServerMessageVariant, ErrorV0}; 10 + use crate::core::{errors::{ButtplugError, ButtplugMessageError}, message::{self, ButtplugClientMessageVariant, ButtplugMessageSpecVersion, ButtplugServerMessageV4, ButtplugServerMessageVariant, ErrorV0}}; 11 11 12 12 use super::{device::ServerDeviceManager, server_message_conversion::ButtplugServerMessageConverter, ButtplugServer, ButtplugServerResultFuture}; 13 13 use futures::{future::{self, BoxFuture, FutureExt}, Stream}; ··· 83 83 ) -> BoxFuture<'static, Result<ButtplugServerMessageVariant, ButtplugServerMessageVariant>> { 84 84 match msg { 85 85 ButtplugClientMessageVariant::V4(msg) => { 86 - let fut = self.server.parse_message(msg); 87 - async move { 88 - Ok(fut.await.map_err(|e| ButtplugServerMessageVariant::from(ButtplugServerMessageV4::from(e)))?.into()) 89 - }.boxed() 86 + if cfg!(feature="allow-unstable-v4-connections") { 87 + let fut = self.server.parse_message(msg); 88 + async move { 89 + Ok(fut.await.map_err(|e| ButtplugServerMessageVariant::from(ButtplugServerMessageV4::from(e)))?.into()) 90 + }.boxed() 91 + } else { 92 + future::ready(Err(ButtplugServerMessageV4::from(ErrorV0::from(ButtplugError::from(ButtplugMessageError::UnhandledMessage("Buttplug not compiled to handle v4 messages.".to_owned())))).into())).boxed() 93 + } 90 94 } 91 95 msg => { 92 96 let v = msg.version(); ··· 115 119 116 120 pub fn destroy(self) -> ButtplugServer { 117 121 self.server 122 + } 123 + } 124 + 125 + #[cfg(test)] 126 + mod test { 127 + use crate::{core::message::{ButtplugClientMessageV4, ButtplugClientMessageVariant, RequestServerInfoV1}, server::{ButtplugServerBuilder, ButtplugServerDowngradeWrapper}}; 128 + 129 + #[cfg_attr(feature="allow-unstable-v4-connections", ignore)] 130 + #[tokio::test] 131 + async fn test_v4_block() { 132 + let wrapper = ButtplugServerDowngradeWrapper::new(ButtplugServerBuilder::default().finish().unwrap()); 133 + assert!(wrapper.parse_message(ButtplugClientMessageVariant::V4(ButtplugClientMessageV4::RequestServerInfo(RequestServerInfoV1::new("TestClient", crate::core::message::ButtplugMessageSpecVersion::Version4)))).await.is_err()); 134 + } 135 + 136 + #[cfg_attr(not(feature="allow-unstable-v4-connections"), ignore)] 137 + #[tokio::test] 138 + async fn test_v4_allow() { 139 + let wrapper = ButtplugServerDowngradeWrapper::new(ButtplugServerBuilder::default().finish().unwrap()); 140 + let result = wrapper.parse_message(ButtplugClientMessageVariant::V4(ButtplugClientMessageV4::RequestServerInfo(RequestServerInfoV1::new("TestClient", crate::core::message::ButtplugMessageSpecVersion::Version4)))).await; 141 + println!("{:?}", result); 142 + assert!(result.is_ok()); 118 143 } 119 144 }