leejunyoung5819 wayland-android-remote
공개Ubuntu(Wayland/X11)에서 Android 기기 화면 미러링 및 원격 제어 (Rust)
| 1 | //! 호스트 연결 계층을 가짜 기기 서버와 맞물려 검증한다 (기획서 5.3, 5.5, 7.6). |
| 2 | |
| 3 | mod common; |
| 4 | |
| 5 | use std::time::Duration; |
| 6 | |
| 7 | use common::fake_device::{FakeDevice, FakeDeviceConfig, PendingPolicy}; |
| 8 | use wayland_android_remote::net::{ |
| 9 | ConnectError, ConnectEvent, ConnectOptions, FramedError, HostIdentity, connect, |
| 10 | }; |
| 11 | use wayland_android_remote::protocol::{ |
| 12 | AuthGrant, AuthResultCode, CodecMask, Message, VideoCodec, VideoConfigRequest, |
| 13 | }; |
| 14 | |
| 15 | fn fast() -> ConnectOptions { |
| 16 | ConnectOptions { |
| 17 | connect_timeout: Duration::from_secs(3), |
| 18 | step_timeout: Duration::from_secs(3), |
| 19 | approval_timeout: Duration::from_secs(3), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | async fn device(pending: PendingPolicy) -> FakeDevice { |
| 24 | let config = FakeDeviceConfig { |
| 25 | pending, |
| 26 | ..FakeDeviceConfig::default() |
| 27 | }; |
| 28 | FakeDevice::start("127.0.0.1:0".parse().unwrap(), config) |
| 29 | .await |
| 30 | .unwrap() |
| 31 | } |
| 32 | |
| 33 | fn approve_always() -> PendingPolicy { |
| 34 | PendingPolicy::ApproveAlways { |
| 35 | after: Duration::from_millis(50), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[tokio::test] |
| 40 | async fn first_connect_waits_for_approval_and_shows_matching_code() { |
| 41 | let dev = device(approve_always()).await; |
| 42 | let id = HostIdentity::generate().unwrap(); |
| 43 | let mut events = Vec::new(); |
| 44 | |
| 45 | let session = connect(dev.addr(), &id, None, fast(), |e| events.push(e)) |
| 46 | .await |
| 47 | .unwrap(); |
| 48 | |
| 49 | assert_eq!(session.fingerprint, dev.cert_sha256()); |
| 50 | assert_eq!(session.grant, AuthGrant::Always); |
| 51 | assert_eq!( |
| 52 | events, |
| 53 | vec![ |
| 54 | ConnectEvent::TlsEstablished { |
| 55 | fingerprint: dev.cert_sha256(), |
| 56 | confirm_code: dev.confirm_code(), |
| 57 | pinned: false, |
| 58 | }, |
| 59 | ConnectEvent::AwaitingApproval { |
| 60 | confirm_code: dev.confirm_code(), |
| 61 | }, |
| 62 | ] |
| 63 | ); |
| 64 | assert_eq!(dev.whitelist(), vec![id.public_key()]); |
| 65 | } |
| 66 | |
| 67 | #[tokio::test] |
| 68 | async fn whitelisted_pc_reconnects_without_prompt() { |
| 69 | let dev = device(approve_always()).await; |
| 70 | let id = HostIdentity::generate().unwrap(); |
| 71 | |
| 72 | let first = connect(dev.addr(), &id, None, fast(), |_| {}) |
| 73 | .await |
| 74 | .unwrap(); |
| 75 | drop(first); |
| 76 | |
| 77 | let mut events = Vec::new(); |
| 78 | let second = connect(dev.addr(), &id, Some(dev.cert_sha256()), fast(), |e| { |
| 79 | events.push(e) |
| 80 | }) |
| 81 | .await |
| 82 | .unwrap(); |
| 83 | |
| 84 | assert_eq!(second.grant, AuthGrant::Always); |
| 85 | assert!( |
| 86 | !events |
| 87 | .iter() |
| 88 | .any(|e| matches!(e, ConnectEvent::AwaitingApproval { .. })) |
| 89 | ); |
| 90 | assert_eq!(dev.approval_prompts(), 1); |
| 91 | } |
| 92 | |
| 93 | #[tokio::test] |
| 94 | async fn pinned_fingerprint_mismatch_is_reported() { |
| 95 | let dev = device(approve_always()).await; |
| 96 | let id = HostIdentity::generate().unwrap(); |
| 97 | |
| 98 | let err = connect(dev.addr(), &id, Some([0xAB; 32]), fast(), |_| {}) |
| 99 | .await |
| 100 | .err() |
| 101 | .unwrap(); |
| 102 | |
| 103 | match err { |
| 104 | ConnectError::FingerprintMismatch { expected, actual } => { |
| 105 | assert_eq!(expected, [0xAB; 32]); |
| 106 | assert_eq!(actual, dev.cert_sha256()); |
| 107 | } |
| 108 | other => panic!("unexpected: {other:?}"), |
| 109 | } |
| 110 | assert_eq!(dev.approval_prompts(), 0); |
| 111 | } |
| 112 | |
| 113 | #[tokio::test] |
| 114 | async fn rejected_and_once_grants() { |
| 115 | let id = HostIdentity::generate().unwrap(); |
| 116 | |
| 117 | let dev = device(PendingPolicy::Reject { |
| 118 | after: Duration::from_millis(20), |
| 119 | }) |
| 120 | .await; |
| 121 | let err = connect(dev.addr(), &id, None, fast(), |_| {}) |
| 122 | .await |
| 123 | .err() |
| 124 | .unwrap(); |
| 125 | assert!(matches!( |
| 126 | err, |
| 127 | ConnectError::AuthFailed(AuthResultCode::Rejected) |
| 128 | )); |
| 129 | |
| 130 | let dev = device(PendingPolicy::ApproveOnce { |
| 131 | after: Duration::from_millis(20), |
| 132 | }) |
| 133 | .await; |
| 134 | let session = connect(dev.addr(), &id, None, fast(), |_| {}) |
| 135 | .await |
| 136 | .unwrap(); |
| 137 | assert_eq!(session.grant, AuthGrant::Once); |
| 138 | assert!(dev.whitelist().is_empty()); |
| 139 | } |
| 140 | |
| 141 | #[tokio::test] |
| 142 | async fn approval_timeout() { |
| 143 | let dev = device(PendingPolicy::NeverAnswer).await; |
| 144 | let id = HostIdentity::generate().unwrap(); |
| 145 | let options = ConnectOptions { |
| 146 | approval_timeout: Duration::from_millis(300), |
| 147 | ..fast() |
| 148 | }; |
| 149 | |
| 150 | let err = connect(dev.addr(), &id, None, options, |_| {}) |
| 151 | .await |
| 152 | .err() |
| 153 | .unwrap(); |
| 154 | assert!(matches!(err, ConnectError::Timeout(_)), "{err:?}"); |
| 155 | } |
| 156 | |
| 157 | #[tokio::test] |
| 158 | async fn connection_refused() { |
| 159 | let dev = device(approve_always()).await; |
| 160 | let addr = dev.addr(); |
| 161 | dev.shutdown(); |
| 162 | tokio::time::sleep(Duration::from_millis(50)).await; |
| 163 | |
| 164 | let id = HostIdentity::generate().unwrap(); |
| 165 | let err = connect(addr, &id, None, fast(), |_| {}) |
| 166 | .await |
| 167 | .err() |
| 168 | .unwrap(); |
| 169 | match err { |
| 170 | ConnectError::Io(e) => assert_eq!(e.kind(), std::io::ErrorKind::ConnectionRefused), |
| 171 | other => panic!("unexpected: {other:?}"), |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #[tokio::test] |
| 176 | async fn ping_pong_and_video_config() { |
| 177 | let dev = device(approve_always()).await; |
| 178 | let id = HostIdentity::generate().unwrap(); |
| 179 | let mut session = connect(dev.addr(), &id, None, fast(), |_| {}) |
| 180 | .await |
| 181 | .unwrap(); |
| 182 | let control = session.control(); |
| 183 | |
| 184 | let seq = control.send(&Message::Ping).await.unwrap(); |
| 185 | let (pong_seq, msg) = control.read_message().await.unwrap(); |
| 186 | assert_eq!((pong_seq, msg), (seq, Message::Pong)); |
| 187 | |
| 188 | control |
| 189 | .send(&Message::VideoConfigRequest(VideoConfigRequest { |
| 190 | max_size: 1920, |
| 191 | max_fps: 60, |
| 192 | codecs: CodecMask::H264 | CodecMask::H265, |
| 193 | bitrate: 8_000_000, |
| 194 | })) |
| 195 | .await |
| 196 | .unwrap(); |
| 197 | let (_, msg) = control.read_message().await.unwrap(); |
| 198 | let Message::VideoConfig(cfg) = msg else { |
| 199 | panic!("expected VIDEO_CONFIG, got {msg:?}"); |
| 200 | }; |
| 201 | assert_eq!(cfg.codec, VideoCodec::H265); |
| 202 | assert!(cfg.width.max(cfg.height) <= 1920); |
| 203 | assert_eq!(cfg.config_gen, 1); |
| 204 | } |
| 205 | |
| 206 | #[tokio::test] |
| 207 | async fn video_channel_streams_config_then_keyframe_and_token_is_single_use() { |
| 208 | let dev = device(approve_always()).await; |
| 209 | let id = HostIdentity::generate().unwrap(); |
| 210 | let session = connect(dev.addr(), &id, None, fast(), |_| {}) |
| 211 | .await |
| 212 | .unwrap(); |
| 213 | |
| 214 | let mut video = session.open_video(Duration::from_secs(3)).await.unwrap(); |
| 215 | let (h, data) = video.read_packet().await.unwrap(); |
| 216 | assert!(h.is_config && !data.is_empty()); |
| 217 | let (h, _) = video.read_packet().await.unwrap(); |
| 218 | assert!(h.is_keyframe); |
| 219 | let mut last_pts = h.pts_us; |
| 220 | for _ in 0..5 { |
| 221 | let (h, _) = video.read_packet().await.unwrap(); |
| 222 | assert!(h.pts_us > last_pts); |
| 223 | last_pts = h.pts_us; |
| 224 | } |
| 225 | |
| 226 | let mut reused = session.open_video(Duration::from_secs(3)).await.unwrap(); |
| 227 | let err = tokio::time::timeout(Duration::from_secs(3), reused.read_packet()) |
| 228 | .await |
| 229 | .expect("재사용 토큰 연결은 바로 닫혀야 한다") |
| 230 | .err() |
| 231 | .unwrap(); |
| 232 | assert!( |
| 233 | matches!(err, FramedError::Closed | FramedError::Io(_)), |
| 234 | "{err}" |
| 235 | ); |
| 236 | } |