1 use super::*;
2 use crate::error::Result;
3 
4 use std::net::Ipv4Addr;
5 
6 struct DummyRelayConnObserver {
7     turn_server_addr: String,
8     username: Username,
9     realm: Realm,
10 }
11 
12 #[async_trait]
13 impl RelayConnObserver for DummyRelayConnObserver {
turn_server_addr(&self) -> String14     fn turn_server_addr(&self) -> String {
15         self.turn_server_addr.clone()
16     }
17 
username(&self) -> Username18     fn username(&self) -> Username {
19         self.username.clone()
20     }
21 
realm(&self) -> Realm22     fn realm(&self) -> Realm {
23         self.realm.clone()
24     }
25 
write_to(&self, _data: &[u8], _to: &str) -> std::result::Result<usize, util::Error>26     async fn write_to(&self, _data: &[u8], _to: &str) -> std::result::Result<usize, util::Error> {
27         Ok(0)
28     }
29 
perform_transaction( &mut self, _msg: &Message, _to: &str, _dont_wait: bool, ) -> Result<TransactionResult>30     async fn perform_transaction(
31         &mut self,
32         _msg: &Message,
33         _to: &str,
34         _dont_wait: bool,
35     ) -> Result<TransactionResult> {
36         Err(Error::ErrFakeErr)
37     }
38 }
39 
40 #[tokio::test]
test_relay_conn() -> Result<()>41 async fn test_relay_conn() -> Result<()> {
42     let obs = DummyRelayConnObserver {
43         turn_server_addr: String::new(),
44         username: Username::new(ATTR_USERNAME, "username".to_owned()),
45         realm: Realm::new(ATTR_REALM, "realm".to_owned()),
46     };
47 
48     let (_read_ch_tx, read_ch_rx) = mpsc::channel(100);
49 
50     let config = RelayConnConfig {
51         relayed_addr: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 0),
52         integrity: MessageIntegrity::default(),
53         nonce: Nonce::new(ATTR_NONCE, "nonce".to_owned()),
54         lifetime: Duration::from_secs(0),
55         binding_mgr: Arc::new(Mutex::new(BindingManager::new())),
56         read_ch_rx: Arc::new(Mutex::new(read_ch_rx)),
57     };
58 
59     let rc = RelayConn::new(Arc::new(Mutex::new(obs)), config).await;
60 
61     let rci = rc.relay_conn.lock().await;
62     let (bind_addr, bind_number) = {
63         let mut bm = rci.binding_mgr.lock().await;
64         let b = bm
65             .create(SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 1234))
66             .unwrap();
67         (b.addr, b.number)
68     };
69 
70     //let binding_mgr = Arc::clone(&rci.binding_mgr);
71     let rc_obs = Arc::clone(&rci.obs);
72     let nonce = rci.nonce.clone();
73     let integrity = rci.integrity.clone();
74 
75     if let Err(err) =
76         RelayConnInternal::bind(rc_obs, bind_addr, bind_number, nonce, integrity).await
77     {
78         assert!(Error::ErrUnexpectedResponse != err);
79     } else {
80         panic!("should fail");
81     }
82 
83     Ok(())
84 }
85