restart.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use std::io;
  5. use std::path::{Path, PathBuf};
  6. use std::process::Command;
  7. /// Helper for generic catch-all errors.
  8. type Result = std::result::Result<(), Box<dyn std::error::Error>>;
  9. /// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--1300-1699-
  10. #[cfg(windows)]
  11. const ERROR_PRIVILEGE_NOT_HELD: i32 = 1314;
  12. /// Represents a successfully created symlink.
  13. enum Symlink {
  14. /// Path to the created symlink
  15. Created(PathBuf),
  16. /// A symlink that failed due to missing permissions (Windows).
  17. #[allow(dead_code)]
  18. Privilege,
  19. }
  20. /// Compile the nested restart binary so that we have access to it during the test.
  21. ///
  22. /// This is compiled inside of this test so that we always have a literal binary file we can use
  23. /// for some filesystem related tests. Because of how integration tests work, the current working
  24. /// directory should be the same as the manifest of the crate (not workspace) this integration test
  25. /// is a part of.
  26. fn compile_restart_test_binary() -> io::Result<PathBuf> {
  27. let project = PathBuf::from("tests").join("restart");
  28. let mut cargo = Command::new("cargo");
  29. cargo.arg("build");
  30. cargo.arg("--manifest-path");
  31. cargo.arg(project.join("Cargo.toml"));
  32. // enable the dangerous macos flag on tauri if the test runner has the feature enabled
  33. if cfg!(feature = "process-relaunch-dangerous-allow-symlink-macos") {
  34. cargo.args([
  35. "--features",
  36. "tauri/process-relaunch-dangerous-allow-symlink-macos",
  37. ]);
  38. }
  39. let status = cargo.status()?;
  40. if !status.success() {
  41. return Err(io::Error::new(
  42. io::ErrorKind::Other,
  43. "Unable to compile restart test cargo project inside restart integration test",
  44. ));
  45. }
  46. let profile = if cfg!(debug_assertions) {
  47. "debug"
  48. } else {
  49. "release"
  50. };
  51. let bin = if cfg!(windows) {
  52. "restart.exe"
  53. } else {
  54. "restart"
  55. };
  56. Ok(project.join("target").join(profile).join(bin))
  57. }
  58. /// Compile the test binary, run it, and compare it with expected output.
  59. ///
  60. /// Failing to create a symlink due to permissions issues is also a success
  61. /// for the purpose of this runner.
  62. fn symlink_runner(create_symlinks: impl Fn(&Path) -> io::Result<Symlink>) -> Result {
  63. let compiled_binary = compile_restart_test_binary()?;
  64. // set up all the temporary file paths
  65. let temp = tempfile::TempDir::new()?;
  66. let bin = temp.path().canonicalize()?.join("restart.exe");
  67. // copy the built restart test binary to our temporary directory
  68. std::fs::copy(compiled_binary, &bin)?;
  69. if let Symlink::Created(link) = create_symlinks(&bin)? {
  70. // run the command from the symlink, so that we can test if restart resolves it correctly
  71. let mut cmd = Command::new(link);
  72. // add the restart parameter so that the invocation will call tauri::api::process::restart
  73. cmd.arg("restart");
  74. let output = cmd.output()?;
  75. // run `TempDir` destructors to prevent resource leaking if the assertion fails
  76. drop(temp);
  77. if output.status.success() {
  78. // gather the output into a string
  79. let stdout = String::from_utf8(output.stdout)?;
  80. // we expect the output to be the bin path, twice
  81. assert_eq!(stdout, format!("{bin}\n{bin}\n", bin = bin.display()));
  82. } else if cfg!(all(
  83. target_os = "macos",
  84. not(feature = "process-relaunch-dangerous-allow-symlink-macos")
  85. )) {
  86. // we expect this to fail on macOS without the dangerous symlink flag set
  87. let stderr = String::from_utf8(output.stderr)?;
  88. // make sure it's the error that we expect
  89. assert!(stderr.contains(
  90. "StartingBinary found current_exe() that contains a symlink on a non-allowed platform"
  91. ));
  92. } else {
  93. // we didn't expect the program to fail in this configuration, just panic
  94. panic!("restart integration test runner failed for unknown reason");
  95. }
  96. }
  97. Ok(())
  98. }
  99. /// Cross-platform way to create a symlink
  100. ///
  101. /// Symlinks that failed to create due to permissions issues (like on Windows)
  102. /// are also seen as successful for the purpose of this testing suite.
  103. fn create_symlink(original: &Path, link: PathBuf) -> io::Result<Symlink> {
  104. #[cfg(unix)]
  105. return std::os::unix::fs::symlink(original, &link).map(|()| Symlink::Created(link));
  106. #[cfg(windows)]
  107. return match std::os::windows::fs::symlink_file(original, &link) {
  108. Ok(()) => Ok(Symlink::Created(link)),
  109. Err(e) => match e.raw_os_error() {
  110. Some(ERROR_PRIVILEGE_NOT_HELD) => Ok(Symlink::Privilege),
  111. _ => Err(e),
  112. },
  113. };
  114. }
  115. /// Only use 1 test to prevent cargo from waiting on itself.
  116. ///
  117. /// While not ideal, this is fine because they use the same solution for both cases.
  118. #[test]
  119. fn restart_symlinks() -> Result {
  120. // single symlink
  121. symlink_runner(|bin| {
  122. let mut link = bin.to_owned();
  123. link.set_file_name("symlink");
  124. link.set_extension("exe");
  125. create_symlink(bin, link)
  126. })?;
  127. // nested symlinks
  128. symlink_runner(|bin| {
  129. let mut link1 = bin.to_owned();
  130. link1.set_file_name("symlink1");
  131. link1.set_extension("exe");
  132. create_symlink(bin, link1.clone())?;
  133. let mut link2 = bin.to_owned();
  134. link2.set_file_name("symlink2");
  135. link2.set_extension("exe");
  136. create_symlink(&link1, link2)
  137. })
  138. }