| 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | |
| 5 | // Currently the mozdevice API is not safe for multiple requests at the same |
| 6 | // time. It is recommended to run each of the unit tests on its own. Also adb |
| 7 | // specific tests cannot be run in CI yet. To check those locally, also run |
| 8 | // the ignored tests. |
| 9 | // |
| 10 | // Use the following command to accomplish that: |
| 11 | // |
| 12 | // $ cargo test -- --ignored --test-threads=1 |
| 13 | |
| 14 | use crate::*; |
| 15 | |
| 16 | use futures::future::BoxFuture; |
| 17 | use std::collections::BTreeSet; |
| 18 | use std::panic; |
| 19 | use std::path::PathBuf; |
| 20 | use tempfile::{TempDir, tempdir}; |
| 21 | |
| 22 | #[tokio::test] |
| 23 | async fn read_length_from_valid_string() { |
| 24 | async fn test(message: &str) -> Result<usize> { |
| 25 | read_length(&mut tokio::io::BufReader::new(message.as_bytes())).await |
| 26 | } |
| 27 | |
| 28 | assert_eq!(test("0000").await.unwrap(), 0); |
| 29 | assert_eq!(test("0001").await.unwrap(), 1); |
| 30 | assert_eq!(test("000F").await.unwrap(), 15); |
| 31 | assert_eq!(test("00FF").await.unwrap(), 255); |
| 32 | assert_eq!(test("0FFF").await.unwrap(), 4095); |
| 33 | assert_eq!(test("FFFF").await.unwrap(), 65535); |
| 34 | |
| 35 | assert_eq!(test("FFFF0").await.unwrap(), 65535); |
| 36 | } |
| 37 | |
| 38 | #[tokio::test] |
| 39 | async fn read_length_from_invalid_string() { |
| 40 | async fn test(message: &str) -> Result<usize> { |
| 41 | read_length(&mut tokio::io::BufReader::new(message.as_bytes())).await |
| 42 | } |
| 43 | |
| 44 | test("").await.expect_err("empty string"); |
| 45 | test("G").await.expect_err("invalid hex character"); |
| 46 | test("-1").await.expect_err("negative number"); |
| 47 | test("000").await.expect_err("shorter than 4 bytes"); |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn encode_message_with_valid_string() { |
| 52 | assert_eq!(encode_message("").unwrap(), "0000".to_string()); |
| 53 | assert_eq!(encode_message("a").unwrap(), "0001a".to_string()); |
| 54 | assert_eq!( |
| 55 | encode_message(&"a".repeat(15)).unwrap(), |
| 56 | format!("000F{}", "a".repeat(15)) |
| 57 | ); |
| 58 | assert_eq!( |
| 59 | encode_message(&"a".repeat(255)).unwrap(), |
| 60 | format!("00FF{}", "a".repeat(255)) |
| 61 | ); |
| 62 | assert_eq!( |
| 63 | encode_message(&"a".repeat(4095)).unwrap(), |
| 64 | format!("0FFF{}", "a".repeat(4095)) |
| 65 | ); |
| 66 | assert_eq!( |
| 67 | encode_message(&"a".repeat(65535)).unwrap(), |
| 68 | format!("FFFF{}", "a".repeat(65535)) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn encode_message_with_invalid_string() { |
| 74 | encode_message(&"a".repeat(65536)).expect_err("string lengths exceeds 4 bytes"); |
| 75 | } |
| 76 | |
| 77 | async fn run_device_test<F>(test: F) |
| 78 | where |
| 79 | F: for<'a> FnOnce(&'a Device, &'a TempDir, &'a UnixPath) -> BoxFuture<'a, ()> |
| 80 | + panic::UnwindSafe, |
| 81 | { |
| 82 | let host = Host { |
| 83 | ..Default::default() |
| 84 | }; |
| 85 | let device = host |
| 86 | .device_or_default::<String>(None, AndroidStorageInput::Auto) |
| 87 | .await |
| 88 | .expect("device_or_default"); |
| 89 | |
| 90 | let tmp_dir = tempdir().expect("create temp dir"); |
| 91 | let response = device |
| 92 | .execute_host_shell_command("echo $EXTERNAL_STORAGE") |
| 93 | .await |
| 94 | .unwrap(); |
| 95 | let mut test_root = UnixPathBuf::from(response.trim_end_matches('\n')); |
| 96 | test_root.push("mozdevice"); |
| 97 | |
| 98 | let _ = device.remove(&test_root).await; |
| 99 | |
| 100 | // TODO: we've removed panic::catch_unwind here, if the test crashes the forwarding isn't cleaned up |
| 101 | let _result = test(&device, &tmp_dir, &test_root).await; |
| 102 | |
| 103 | let _ = device.kill_forward_all_ports().await; |
| 104 | // let _ = device.kill_reverse_all_ports(); |
| 105 | |
| 106 | // assert!(result.is_ok()) |
| 107 | } |
| 108 | |
| 109 | #[tokio::test] |
| 110 | #[ignore] |
| 111 | async fn host_features() { |
| 112 | let host = Host { |
| 113 | ..Default::default() |
| 114 | }; |
| 115 | |
| 116 | let set = host |
| 117 | .features::<BTreeSet<_>>() |
| 118 | .await |
| 119 | .expect("to query features"); |
| 120 | assert!(set.contains("cmd")); |
| 121 | assert!(set.contains("shell_v2")); |
| 122 | } |
| 123 | |
| 124 | #[tokio::test] |
| 125 | #[ignore] |
| 126 | async fn host_devices() { |
| 127 | let host = Host { |
| 128 | ..Default::default() |
| 129 | }; |
| 130 | |
| 131 | let set: BTreeSet<_> = host.devices().await.expect("to query devices"); |
| 132 | assert_eq!(1, set.len()); |
| 133 | } |
| 134 | |
| 135 | #[tokio::test] |
| 136 | #[ignore] |
| 137 | async fn host_device_or_default() { |
| 138 | let host = Host { |
| 139 | ..Default::default() |
| 140 | }; |
| 141 | |
| 142 | let devices: Vec<_> = host.devices().await.expect("to query devices"); |
| 143 | let expected_device = devices.first().expect("found a device"); |
| 144 | |
| 145 | let device = host |
| 146 | .device_or_default::<String>(Some(&expected_device.serial), AndroidStorageInput::App) |
| 147 | .await |
| 148 | .expect("connected device with serial"); |
| 149 | assert_eq!(device.run_as_package, None); |
| 150 | assert_eq!(device.serial, expected_device.serial); |
| 151 | assert!(device.tempfile.starts_with("/data/local/tmp")); |
| 152 | } |
| 153 | |
| 154 | #[tokio::test] |
| 155 | #[ignore] |
| 156 | async fn host_device_or_default_invalid_serial() { |
| 157 | let host = Host { |
| 158 | ..Default::default() |
| 159 | }; |
| 160 | |
| 161 | host.device_or_default::<String>(Some(&"foobar".to_owned()), AndroidStorageInput::Auto) |
| 162 | .await |
| 163 | .expect_err("invalid serial"); |
| 164 | } |
| 165 | |
| 166 | #[tokio::test] |
| 167 | #[ignore] |
| 168 | async fn host_device_or_default_no_serial() { |
| 169 | let host = Host { |
| 170 | ..Default::default() |
| 171 | }; |
| 172 | |
| 173 | let devices: Vec<_> = host.devices().await.expect("to query devices"); |
| 174 | let expected_device = devices.first().expect("found a device"); |
| 175 | |
| 176 | let device = host |
| 177 | .device_or_default::<String>(None, AndroidStorageInput::Auto) |
| 178 | .await |
| 179 | .expect("connected device with serial"); |
| 180 | assert_eq!(device.serial, expected_device.serial); |
| 181 | } |
| 182 | |
| 183 | #[tokio::test] |
| 184 | #[ignore] |
| 185 | async fn host_device_or_default_storage_as_app() { |
| 186 | let host = Host { |
| 187 | ..Default::default() |
| 188 | }; |
| 189 | |
| 190 | let device = host |
| 191 | .device_or_default::<String>(None, AndroidStorageInput::App) |
| 192 | .await |
| 193 | .expect("connected device"); |
| 194 | assert_eq!(device.storage, AndroidStorage::App); |
| 195 | } |
| 196 | |
| 197 | #[tokio::test] |
| 198 | #[ignore] |
| 199 | async fn host_device_or_default_storage_as_auto() { |
| 200 | let host = Host { |
| 201 | ..Default::default() |
| 202 | }; |
| 203 | |
| 204 | let device = host |
| 205 | .device_or_default::<String>(None, AndroidStorageInput::Auto) |
| 206 | .await |
| 207 | .expect("connected device"); |
| 208 | assert_eq!(device.storage, AndroidStorage::Sdcard); |
| 209 | } |
| 210 | |
| 211 | #[tokio::test] |
| 212 | #[ignore] |
| 213 | async fn host_device_or_default_storage_as_internal() { |
| 214 | let host = Host { |
| 215 | ..Default::default() |
| 216 | }; |
| 217 | |
| 218 | let device = host |
| 219 | .device_or_default::<String>(None, AndroidStorageInput::Internal) |
| 220 | .await |
| 221 | .expect("connected device"); |
| 222 | assert_eq!(device.storage, AndroidStorage::Internal); |
| 223 | } |
| 224 | |
| 225 | #[tokio::test] |
| 226 | #[ignore] |
| 227 | async fn host_device_or_default_storage_as_sdcard() { |
| 228 | let host = Host { |
| 229 | ..Default::default() |
| 230 | }; |
| 231 | |
| 232 | let device = host |
| 233 | .device_or_default::<String>(None, AndroidStorageInput::Sdcard) |
| 234 | .await |
| 235 | .expect("connected device"); |
| 236 | assert_eq!(device.storage, AndroidStorage::Sdcard); |
| 237 | } |
| 238 | |
| 239 | #[tokio::test] |
| 240 | #[ignore] |
| 241 | async fn device_shell_command() { |
| 242 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 243 | Box::pin(async { |
| 244 | assert_eq!( |
| 245 | "Linux\n", |
| 246 | device |
| 247 | .execute_host_shell_command("uname") |
| 248 | .await |
| 249 | .expect("to have shell output") |
| 250 | ); |
| 251 | }) |
| 252 | }) |
| 253 | .await; |
| 254 | } |
| 255 | |
| 256 | #[tokio::test] |
| 257 | #[ignore] |
| 258 | async fn device_forward_port_hardcoded() { |
| 259 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 260 | Box::pin(async { |
| 261 | assert_eq!( |
| 262 | 3035, |
| 263 | device |
| 264 | .forward_port(3035, 3036) |
| 265 | .await |
| 266 | .expect("forwarded local port") |
| 267 | ); |
| 268 | // TODO: check with forward --list |
| 269 | }) |
| 270 | }) |
| 271 | .await; |
| 272 | } |
| 273 | |
| 274 | // #[test] |
| 275 | // #[ignore] |
| 276 | // TODO: "adb server response to `forward tcp:0 ...` was not a u16: \"000559464\"") |
| 277 | // fn device_forward_port_system_allocated() { |
| 278 | // run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 279 | // let local_port = device.forward_port(0, 3037).expect("local_port"); |
| 280 | // assert_ne!(local_port, 0); |
| 281 | // // TODO: check with forward --list |
| 282 | // }); |
| 283 | // } |
| 284 | |
| 285 | #[tokio::test] |
| 286 | #[ignore] |
| 287 | async fn device_kill_forward_port_no_forwarded_port() { |
| 288 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 289 | Box::pin(async { |
| 290 | device |
| 291 | .kill_forward_port(3038) |
| 292 | .await |
| 293 | .expect_err("adb error: listener 'tcp:3038' "); |
| 294 | }) |
| 295 | }) |
| 296 | .await; |
| 297 | } |
| 298 | |
| 299 | #[tokio::test] |
| 300 | #[ignore] |
| 301 | async fn device_kill_forward_port_twice() { |
| 302 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 303 | Box::pin(async { |
| 304 | let local_port = device |
| 305 | .forward_port(3039, 3040) |
| 306 | .await |
| 307 | .expect("forwarded local port"); |
| 308 | assert_eq!(local_port, 3039); |
| 309 | // TODO: check with forward --list |
| 310 | device |
| 311 | .kill_forward_port(local_port) |
| 312 | .await |
| 313 | .expect("to remove forwarded port"); |
| 314 | device |
| 315 | .kill_forward_port(local_port) |
| 316 | .await |
| 317 | .expect_err("adb error: listener 'tcp:3039' "); |
| 318 | }) |
| 319 | }) |
| 320 | .await; |
| 321 | } |
| 322 | |
| 323 | #[tokio::test] |
| 324 | #[ignore] |
| 325 | async fn device_kill_forward_all_ports_no_forwarded_port() { |
| 326 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 327 | Box::pin(async { |
| 328 | device |
| 329 | .kill_forward_all_ports() |
| 330 | .await |
| 331 | .expect("to not fail for no forwarded ports"); |
| 332 | }) |
| 333 | }) |
| 334 | .await; |
| 335 | } |
| 336 | |
| 337 | #[tokio::test] |
| 338 | #[ignore] |
| 339 | async fn device_kill_forward_all_ports_twice() { |
| 340 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 341 | Box::pin(async { |
| 342 | let local_port1 = device |
| 343 | .forward_port(3039, 3040) |
| 344 | .await |
| 345 | .expect("forwarded local port"); |
| 346 | assert_eq!(local_port1, 3039); |
| 347 | let local_port2 = device |
| 348 | .forward_port(3041, 3042) |
| 349 | .await |
| 350 | .expect("forwarded local port"); |
| 351 | assert_eq!(local_port2, 3041); |
| 352 | // TODO: check with forward --list |
| 353 | device |
| 354 | .kill_forward_all_ports() |
| 355 | .await |
| 356 | .expect("to remove all forwarded ports"); |
| 357 | device |
| 358 | .kill_forward_all_ports() |
| 359 | .await |
| 360 | .expect("to not fail for no forwarded ports"); |
| 361 | }) |
| 362 | }) |
| 363 | .await; |
| 364 | } |
| 365 | |
| 366 | #[tokio::test] |
| 367 | #[ignore] |
| 368 | async fn device_reverse_port_hardcoded() { |
| 369 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 370 | Box::pin(async { |
| 371 | assert_eq!( |
| 372 | 4035, |
| 373 | device.reverse_port(4035, 4036).await.expect("remote_port") |
| 374 | ); |
| 375 | // TODO: check with reverse --list |
| 376 | }) |
| 377 | }) |
| 378 | .await; |
| 379 | } |
| 380 | |
| 381 | // #[test] |
| 382 | // #[ignore] |
| 383 | // TODO: No adb response: ParseInt(ParseIntError { kind: Empty }) |
| 384 | // fn device_reverse_port_system_allocated() { |
| 385 | // run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 386 | // let reverse_port = device.reverse_port(0, 4037).expect("remote port"); |
| 387 | // assert_ne!(reverse_port, 0); |
| 388 | // // TODO: check with reverse --list |
| 389 | // }); |
| 390 | // } |
| 391 | |
| 392 | #[tokio::test] |
| 393 | #[ignore] |
| 394 | async fn device_kill_reverse_port_no_reverse_port() { |
| 395 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 396 | Box::pin(async { |
| 397 | device |
| 398 | .kill_reverse_port(4038) |
| 399 | .await |
| 400 | .expect_err("listener 'tcp:4038' not found"); |
| 401 | }) |
| 402 | }) |
| 403 | .await; |
| 404 | } |
| 405 | |
| 406 | // #[test] |
| 407 | // #[ignore] |
| 408 | // TODO: "adb error: adb server response did not contain expected hexstring length: \"\"" |
| 409 | // fn device_kill_reverse_port_twice() { |
| 410 | // run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 411 | // let remote_port = device |
| 412 | // .reverse_port(4039, 4040) |
| 413 | // .expect("reversed local port"); |
| 414 | // assert_eq!(remote_port, 4039); |
| 415 | // // TODO: check with reverse --list |
| 416 | // device |
| 417 | // .kill_reverse_port(remote_port) |
| 418 | // .expect("to remove reverse port"); |
| 419 | // device |
| 420 | // .kill_reverse_port(remote_port) |
| 421 | // .expect_err("listener 'tcp:4039' not found"); |
| 422 | // }); |
| 423 | // } |
| 424 | |
| 425 | #[tokio::test] |
| 426 | #[ignore] |
| 427 | async fn device_kill_reverse_all_ports_no_reversed_port() { |
| 428 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 429 | Box::pin(async { |
| 430 | device |
| 431 | .kill_reverse_all_ports() |
| 432 | .await |
| 433 | .expect("to not fail for no reversed ports"); |
| 434 | }) |
| 435 | }) |
| 436 | .await; |
| 437 | } |
| 438 | |
| 439 | #[tokio::test] |
| 440 | #[ignore] |
| 441 | async fn device_kill_reverse_all_ports_twice() { |
| 442 | run_device_test(|device: &Device, _: &TempDir, _: &UnixPath| { |
| 443 | Box::pin(async { |
| 444 | let local_port1 = device |
| 445 | .forward_port(4039, 4040) |
| 446 | .await |
| 447 | .expect("forwarded local port"); |
| 448 | assert_eq!(local_port1, 4039); |
| 449 | let local_port2 = device |
| 450 | .forward_port(4041, 4042) |
| 451 | .await |
| 452 | .expect("forwarded local port"); |
| 453 | assert_eq!(local_port2, 4041); |
| 454 | // TODO: check with reverse --list |
| 455 | device |
| 456 | .kill_reverse_all_ports() |
| 457 | .await |
| 458 | .expect("to remove all reversed ports"); |
| 459 | device |
| 460 | .kill_reverse_all_ports() |
| 461 | .await |
| 462 | .expect("to not fail for no reversed ports"); |
| 463 | }) |
| 464 | }) |
| 465 | .await; |
| 466 | } |
| 467 | |
| 468 | #[tokio::test] |
| 469 | #[ignore] |
| 470 | async fn device_push_pull_text_file() { |
| 471 | run_device_test( |
| 472 | |device: &Device, _: &TempDir, remote_root_path: &UnixPath| { |
| 473 | Box::pin(async { |
| 474 | let content = "test"; |
| 475 | let remote_path = remote_root_path.join("foo.txt"); |
| 476 | |
| 477 | device |
| 478 | .push( |
| 479 | &mut tokio::io::BufReader::new(content.as_bytes()), |
| 480 | &remote_path, |
| 481 | 0o777, |
| 482 | ) |
| 483 | .await |
| 484 | .expect("file has been pushed"); |
| 485 | |
| 486 | let file_content = device |
| 487 | .execute_host_shell_command(&format!("cat {}", remote_path.display())) |
| 488 | .await |
| 489 | .expect("host shell command for 'cat' to succeed"); |
| 490 | |
| 491 | assert_eq!(file_content, content); |
| 492 | |
| 493 | // And as second step pull it off the device. |
| 494 | let mut buffer = Vec::new(); |
| 495 | device |
| 496 | .pull(&remote_path, &mut buffer) |
| 497 | .await |
| 498 | .expect("file has been pulled"); |
| 499 | assert_eq!(buffer, content.as_bytes()); |
| 500 | }) |
| 501 | }, |
| 502 | ) |
| 503 | .await; |
| 504 | } |
| 505 | |
| 506 | #[tokio::test] |
| 507 | #[ignore] |
| 508 | async fn device_push_pull_large_binary_file() { |
| 509 | run_device_test( |
| 510 | |device: &Device, _: &TempDir, remote_root_path: &UnixPath| { |
| 511 | Box::pin(async { |
| 512 | let remote_path = remote_root_path.join("foo.binary"); |
| 513 | |
| 514 | let mut content = Vec::new(); |
| 515 | |
| 516 | // Needs to be larger than 64kB to test multiple chunks. |
| 517 | for i in 0..100000u32 { |
| 518 | content.push(b'0' + (i % 10) as u8); |
| 519 | } |
| 520 | |
| 521 | device |
| 522 | .push( |
| 523 | &mut std::io::Cursor::new(content.clone()), |
| 524 | &remote_path, |
| 525 | 0o777, |
| 526 | ) |
| 527 | .await |
| 528 | .expect("large file has been pushed"); |
| 529 | |
| 530 | let output = device |
| 531 | .execute_host_shell_command(&format!("ls -l {}", remote_path.display())) |
| 532 | .await |
| 533 | .expect("host shell command for 'ls' to succeed"); |
| 534 | |
| 535 | assert!(output.contains(remote_path.to_str().unwrap())); |
| 536 | |
| 537 | let mut buffer = Vec::new(); |
| 538 | |
| 539 | device |
| 540 | .pull(&remote_path, &mut buffer) |
| 541 | .await |
| 542 | .expect("large binary file has been pulled"); |
| 543 | assert_eq!(buffer, content); |
| 544 | }) |
| 545 | }, |
| 546 | ) |
| 547 | .await; |
| 548 | } |
| 549 | |
| 550 | #[tokio::test] |
| 551 | #[ignore] |
| 552 | async fn device_push_permission() { |
| 553 | run_device_test( |
| 554 | |device: &Device, _: &TempDir, remote_root_path: &UnixPath| { |
| 555 | Box::pin(async { |
| 556 | fn adjust_mode(mode: u32) -> u32 { |
| 557 | // Adjust the mode by copying the user permissions to |
| 558 | // group and other as indicated in |
| 559 | // [send_impl](https://android.googlesource.com/platform/system/core/+/master/adb/daemon/file_sync_service.cpp#516). |
| 560 | // This ensures that group and other can both access a |
| 561 | // file if the user can access it. |
| 562 | let mut m = mode & 0o777; |
| 563 | m |= (m >> 3) & 0o070; |
| 564 | m |= (m >> 3) & 0o007; |
| 565 | m |
| 566 | } |
| 567 | |
| 568 | fn get_permissions(mode: u32) -> String { |
| 569 | // Convert the mode integer into the string representation |
| 570 | // of the mode returned by `ls`. This assumes the object is |
| 571 | // a file and not a directory. |
| 572 | let mut perms = ["-", "r", "w", "x", "r", "w", "x", "r", "w", "x"]; |
| 573 | let mut bit_pos = 0; |
| 574 | while bit_pos < 9 { |
| 575 | if (1 << bit_pos) & mode == 0 { |
| 576 | perms[9 - bit_pos] = "-" |
| 577 | } |
| 578 | bit_pos += 1; |
| 579 | } |
| 580 | perms.concat() |
| 581 | } |
| 582 | let content = "test"; |
| 583 | let remote_path = remote_root_path.join("foo.bar"); |
| 584 | |
| 585 | // First push the file to the device |
| 586 | let modes = vec![0o421, 0o644, 0o666, 0o777]; |
| 587 | for mode in modes { |
| 588 | let adjusted_mode = adjust_mode(mode); |
| 589 | let adjusted_perms = get_permissions(adjusted_mode); |
| 590 | device |
| 591 | .push( |
| 592 | &mut tokio::io::BufReader::new(content.as_bytes()), |
| 593 | &remote_path, |
| 594 | mode, |
| 595 | ) |
| 596 | .await |
| 597 | .expect("file has been pushed"); |
| 598 | |
| 599 | let output = device |
| 600 | .execute_host_shell_command(&format!("ls -l {}", remote_path.display())) |
| 601 | .await |
| 602 | .expect("host shell command for 'ls' to succeed"); |
| 603 | |
| 604 | assert!(output.contains(remote_path.to_str().unwrap())); |
| 605 | assert!(output.starts_with(&adjusted_perms)); |
| 606 | } |
| 607 | |
| 608 | let output = device |
| 609 | .execute_host_shell_command(&format!("ls -ld {}", remote_root_path.display())) |
| 610 | .await |
| 611 | .expect("host shell command for 'ls parent' to succeed"); |
| 612 | |
| 613 | assert!(output.contains(remote_root_path.to_str().unwrap())); |
| 614 | assert!(output.starts_with("drwxrwxrwx")); |
| 615 | }) |
| 616 | }, |
| 617 | ) |
| 618 | .await; |
| 619 | } |
| 620 | |
| 621 | #[tokio::test] |
| 622 | #[ignore] |
| 623 | async fn device_pull_fails_for_missing_file() { |
| 624 | run_device_test( |
| 625 | |device: &Device, _: &TempDir, remote_root_path: &UnixPath| { |
| 626 | Box::pin(async { |
| 627 | let mut buffer = Vec::new(); |
| 628 | |
| 629 | device |
| 630 | .pull(&remote_root_path.join("missing"), &mut buffer) |
| 631 | .await |
| 632 | .expect_err("missing file should not be pulled"); |
| 633 | }) |
| 634 | }, |
| 635 | ) |
| 636 | .await; |
| 637 | } |
| 638 | |
| 639 | #[tokio::test] |
| 640 | #[ignore] |
| 641 | async fn device_push_and_list_dir() { |
| 642 | run_device_test( |
| 643 | |device: &Device, tmp_dir: &TempDir, remote_root_path: &UnixPath| { |
| 644 | Box::pin(async move { |
| 645 | let files = ["foo1.bar", "foo2.bar", "bar/foo3.bar", "bar/more/foo3.bar"]; |
| 646 | |
| 647 | for file in files.iter() { |
| 648 | let path = tmp_dir.path().join(Path::new(file)); |
| 649 | let _ = std::fs::create_dir_all(path.parent().unwrap()); |
| 650 | |
| 651 | let f = File::create(path).await.expect("to create file"); |
| 652 | let mut f = tokio::io::BufWriter::new(f); |
| 653 | f.write_all(file.as_bytes()).await.expect("to write data"); |
| 654 | } |
| 655 | |
| 656 | device |
| 657 | .push_dir(tmp_dir.path(), remote_root_path, 0o777) |
| 658 | .await |
| 659 | .expect("to push_dir"); |
| 660 | |
| 661 | for file in files.iter() { |
| 662 | let path = append_components(remote_root_path, Path::new(file)).unwrap(); |
| 663 | let output = device |
| 664 | .execute_host_shell_command(&format!("ls {}", path.display())) |
| 665 | .await |
| 666 | .expect("host shell command for 'ls' to succeed"); |
| 667 | |
| 668 | assert!(output.contains(path.to_str().unwrap())); |
| 669 | } |
| 670 | |
| 671 | let mut listings = device |
| 672 | .list_dir(remote_root_path) |
| 673 | .await |
| 674 | .expect("to list_dir"); |
| 675 | listings.sort(); |
| 676 | assert_eq!( |
| 677 | listings, |
| 678 | vec![ |
| 679 | RemoteDirEntry { |
| 680 | depth: 0, |
| 681 | name: "foo1.bar".to_string(), |
| 682 | metadata: RemoteMetadata::RemoteFile(RemoteFileMetadata { |
| 683 | mode: 0b110110000, |
| 684 | size: 8 |
| 685 | }) |
| 686 | }, |
| 687 | RemoteDirEntry { |
| 688 | depth: 0, |
| 689 | name: "foo2.bar".to_string(), |
| 690 | metadata: RemoteMetadata::RemoteFile(RemoteFileMetadata { |
| 691 | mode: 0b110110000, |
| 692 | size: 8 |
| 693 | }) |
| 694 | }, |
| 695 | RemoteDirEntry { |
| 696 | depth: 0, |
| 697 | name: "bar".to_string(), |
| 698 | metadata: RemoteMetadata::RemoteDir |
| 699 | }, |
| 700 | RemoteDirEntry { |
| 701 | depth: 1, |
| 702 | name: "bar/foo3.bar".to_string(), |
| 703 | metadata: RemoteMetadata::RemoteFile(RemoteFileMetadata { |
| 704 | mode: 0b110110000, |
| 705 | size: 12 |
| 706 | }) |
| 707 | }, |
| 708 | RemoteDirEntry { |
| 709 | depth: 1, |
| 710 | name: "bar/more".to_string(), |
| 711 | metadata: RemoteMetadata::RemoteDir |
| 712 | }, |
| 713 | RemoteDirEntry { |
| 714 | depth: 2, |
| 715 | name: "bar/more/foo3.bar".to_string(), |
| 716 | metadata: RemoteMetadata::RemoteFile(RemoteFileMetadata { |
| 717 | mode: 0b110110000, |
| 718 | size: 17 |
| 719 | }) |
| 720 | } |
| 721 | ] |
| 722 | ); |
| 723 | }) |
| 724 | }, |
| 725 | ) |
| 726 | .await; |
| 727 | } |
| 728 | |
| 729 | #[tokio::test] |
| 730 | #[ignore] |
| 731 | async fn device_push_and_pull_dir() { |
| 732 | run_device_test( |
| 733 | |device: &Device, tmp_dir: &TempDir, remote_root_path: &UnixPath| { |
| 734 | Box::pin(async move { |
| 735 | let files = ["foo1.bar", "foo2.bar", "bar/foo3.bar", "bar/more/foo3.bar"]; |
| 736 | |
| 737 | let src_dir = tmp_dir.path().join(Path::new("src")); |
| 738 | let dest_dir = tmp_dir.path().join(Path::new("src")); |
| 739 | |
| 740 | for file in files.iter() { |
| 741 | let path = src_dir.join(Path::new(file)); |
| 742 | let _ = std::fs::create_dir_all(path.parent().unwrap()); |
| 743 | |
| 744 | let f = File::create(path).await.expect("to create file"); |
| 745 | let mut f = tokio::io::BufWriter::new(f); |
| 746 | f.write_all(file.as_bytes()).await.expect("to write data"); |
| 747 | } |
| 748 | |
| 749 | device |
| 750 | .push_dir(&src_dir, remote_root_path, 0o777) |
| 751 | .await |
| 752 | .expect("to push_dir"); |
| 753 | |
| 754 | device |
| 755 | .pull_dir(remote_root_path, &dest_dir) |
| 756 | .await |
| 757 | .expect("to pull_dir"); |
| 758 | |
| 759 | for file in files.iter() { |
| 760 | let path = dest_dir.join(Path::new(file)); |
| 761 | let mut f = File::open(path).await.expect("to open file"); |
| 762 | let mut buf = String::new(); |
| 763 | f.read_to_string(&mut buf).await.expect("to read content"); |
| 764 | assert_eq!(buf, *file); |
| 765 | } |
| 766 | }) |
| 767 | }, |
| 768 | ) |
| 769 | .await |
| 770 | } |
| 771 | |
| 772 | #[tokio::test] |
| 773 | #[ignore] |
| 774 | async fn device_push_and_list_dir_flat() { |
| 775 | run_device_test( |
| 776 | |device: &Device, tmp_dir: &TempDir, remote_root_path: &UnixPath| { |
| 777 | Box::pin(async move { |
| 778 | let content = "test"; |
| 779 | |
| 780 | let files = [ |
| 781 | PathBuf::from("foo1.bar"), |
| 782 | PathBuf::from("foo2.bar"), |
| 783 | PathBuf::from("bar").join("foo3.bar"), |
| 784 | ]; |
| 785 | |
| 786 | for file in files.iter() { |
| 787 | let path = tmp_dir.path().join(file); |
| 788 | let _ = std::fs::create_dir_all(path.parent().unwrap()); |
| 789 | |
| 790 | let f = File::create(path).await.expect("to create file"); |
| 791 | let mut f = tokio::io::BufWriter::new(f); |
| 792 | f.write_all(content.as_bytes()) |
| 793 | .await |
| 794 | .expect("to write data"); |
| 795 | } |
| 796 | |
| 797 | device |
| 798 | .push_dir(tmp_dir.path(), remote_root_path, 0o777) |
| 799 | .await |
| 800 | .expect("to push_dir"); |
| 801 | |
| 802 | for file in files.iter() { |
| 803 | let path = append_components(remote_root_path, file).unwrap(); |
| 804 | let output = device |
| 805 | .execute_host_shell_command(&format!("ls {}", path.display())) |
| 806 | .await |
| 807 | .expect("host shell command for 'ls' to succeed"); |
| 808 | |
| 809 | assert!(output.contains(path.to_str().unwrap())); |
| 810 | } |
| 811 | |
| 812 | let mut listings = device |
| 813 | .list_dir_flat(remote_root_path, 7, "prefix".to_string()) |
| 814 | .await |
| 815 | .expect("to list_dir_flat"); |
| 816 | listings.sort(); |
| 817 | assert_eq!( |
| 818 | listings, |
| 819 | vec![ |
| 820 | RemoteDirEntry { |
| 821 | depth: 7, |
| 822 | metadata: RemoteMetadata::RemoteFile(RemoteFileMetadata { |
| 823 | mode: 0b110110000, |
| 824 | size: 4 |
| 825 | }), |
| 826 | name: "prefix/foo1.bar".to_string(), |
| 827 | }, |
| 828 | RemoteDirEntry { |
| 829 | depth: 7, |
| 830 | metadata: RemoteMetadata::RemoteFile(RemoteFileMetadata { |
| 831 | mode: 0b110110000, |
| 832 | size: 4 |
| 833 | }), |
| 834 | name: "prefix/foo2.bar".to_string(), |
| 835 | }, |
| 836 | RemoteDirEntry { |
| 837 | depth: 7, |
| 838 | metadata: RemoteMetadata::RemoteDir, |
| 839 | name: "prefix/bar".to_string(), |
| 840 | }, |
| 841 | ] |
| 842 | ); |
| 843 | }) |
| 844 | }, |
| 845 | ) |
| 846 | .await; |
| 847 | } |
| 848 | |
| 849 | #[test] |
| 850 | fn format_own_device_error_types() { |
| 851 | assert_eq!( |
| 852 | format!("{}", DeviceError::InvalidStorage), |
| 853 | "Invalid storage".to_string() |
| 854 | ); |
| 855 | assert_eq!( |
| 856 | format!("{}", DeviceError::MissingPackage), |
| 857 | "Missing package".to_string() |
| 858 | ); |
| 859 | assert_eq!( |
| 860 | format!("{}", DeviceError::MultipleDevices), |
| 861 | "Multiple Android devices online".to_string() |
| 862 | ); |
| 863 | |
| 864 | assert_eq!( |
| 865 | format!("{}", DeviceError::Adb("foo".to_string())), |
| 866 | "foo".to_string() |
| 867 | ); |
| 868 | } |