Buttplug sex toy control library
1// Buttplug Rust Source Code File - See https://buttplug.io for more info.
2//
3// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved.
4//
5// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
6// for full license information.
7
8mod util;
9
10use buttplug_client::ButtplugClientError;
11use buttplug_core::{
12 connector::transport::ButtplugTransportIncomingMessage,
13 errors::{ButtplugError, ButtplugUnknownError},
14 message::{
15 BUTTPLUG_CURRENT_API_MAJOR_VERSION,
16 ButtplugClientMessageV4,
17 ButtplugMessage,
18 ButtplugServerMessageV4,
19 ErrorV0,
20 serializer::ButtplugSerializedMessage,
21 },
22 util::async_manager,
23};
24use buttplug_server::message::{
25 ButtplugClientMessageVariant,
26 ButtplugServerMessageVariant,
27 DeviceListV3,
28 ServerInfoV2,
29};
30use std::sync::Arc;
31use tokio::sync::Notify;
32use util::channel_transport::ChannelClientTestHelper;
33
34#[tokio::test]
35#[ignore = "Needs update to v4"]
36async fn test_garbled_client_rsi_response() {
37 let helper = Arc::new(ChannelClientTestHelper::new());
38 let helper_clone = helper.clone();
39 let finish_notifier = Arc::new(Notify::new());
40 let finish_notifier_clone = finish_notifier.clone();
41 async_manager::spawn(async move {
42 helper_clone
43 .connect_without_reply()
44 .await
45 .expect("Test, assuming infallible.");
46 finish_notifier_clone.notify_waiters();
47 });
48 // Just assume we get an RSI message
49 let _ = helper.recv_outgoing().await;
50 // Send back crap.
51 helper
52 .send_incoming(ButtplugTransportIncomingMessage::Message(
53 ButtplugSerializedMessage::Text("Not the JSON we're expecting".to_owned()),
54 ))
55 .await;
56 helper
57 .send_client_incoming(ButtplugServerMessageVariant::V3(
58 ServerInfoV2::new("test server", BUTTPLUG_CURRENT_API_MAJOR_VERSION, 0).into(),
59 ))
60 .await;
61 let _ = helper.recv_outgoing().await;
62 let mut dl = DeviceListV3::new(vec![]);
63 dl.set_id(2);
64 helper
65 .send_client_incoming(ButtplugServerMessageVariant::V3(dl.into()))
66 .await;
67 finish_notifier.notified().await;
68}
69
70#[tokio::test]
71async fn test_serialized_error_relay() {
72 let helper = Arc::new(ChannelClientTestHelper::new());
73 helper.simulate_successful_connect().await;
74 let helper_clone = helper.clone();
75 async_manager::spawn(async move {
76 assert!(matches!(
77 helper_clone.next_client_message().await,
78 ButtplugClientMessageVariant::V4(ButtplugClientMessageV4::StartScanning(..))
79 ));
80 let mut error_msg = ButtplugServerMessageV4::Error(ErrorV0::from(ButtplugError::from(
81 ButtplugUnknownError::NoDeviceCommManagers,
82 )));
83 error_msg.set_id(3);
84 helper_clone
85 .send_client_incoming(ButtplugServerMessageVariant::V4(error_msg))
86 .await;
87 });
88 assert!(matches!(
89 helper.client().start_scanning().await.unwrap_err(),
90 ButtplugClientError::ButtplugError(ButtplugError::ButtplugUnknownError(
91 buttplug_core::errors::ButtplugUnknownError::NoDeviceCommManagers
92 ))
93 ));
94}
95
96// TODO Test bad incoming JSON
97// TODO Test deserialization of concatenated messages
98// TODO Test message with negative message id
99// TODO Test device message with negative device id