Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions core/libs/tests/GuestExecHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,36 @@ void Arguments(const std::uintptr_t* block, const std::vector<std::string>& expe
Require(argv[expected.size()] == nullptr && kernel[expected.size()] == nullptr, "argument terminator");
}

bool Utf8Environment(const char* name, std::string& value) {
#ifdef _WIN32
const std::wstring wideName = std::filesystem::path(name).wstring();
std::wstring wide(32768, L'\0');
const auto length = GetEnvironmentVariableW(wideName.c_str(), wide.data(), static_cast<DWORD>(wide.size()));
if (length == 0 || length >= wide.size()) return false;
wide.resize(length);
const auto converted = std::filesystem::path(wide).u8string();
value.assign(reinterpret_cast<const char*>(converted.data()), converted.size());
return true;
#else
const auto* found = std::getenv(name);
if (found == nullptr) return false;
value = found;
return true;
#endif
}

std::string LoaderEnvironment() {
const auto* value = std::getenv("LD_LIBRARY_PATH");
return value != nullptr ? std::string("LD_LIBRARY_PATH=") + value : std::string();
std::string value;
return Utf8Environment("LD_LIBRARY_PATH", value) ? "LD_LIBRARY_PATH=" + value : std::string();
}

std::vector<std::string> SecondEnvironment() {
std::vector<std::string> entries{"EXEC_SECOND=stage2"};
auto loader = LoaderEnvironment();
if (!loader.empty()) entries.push_back(loader);
const auto* root = std::getenv("ANYPS5_LOADEXEC_TEST_ROOT");
Require(root != nullptr, "missing fixture directory for the environment");
entries.emplace_back(std::string("ANYPS5_LOADEXEC_TEST_ROOT=") + root);
std::string root;
Require(Utf8Environment("ANYPS5_LOADEXEC_TEST_ROOT", root), "missing fixture directory for the environment");
entries.emplace_back("ANYPS5_LOADEXEC_TEST_ROOT=" + root);
return entries;
}

Expand Down
6 changes: 5 additions & 1 deletion core/libs/tests/GuestSystemFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ static std::string CaptureStderr(const std::filesystem::path& path, TRun run) {
#else
const int saved = dup(2);
#endif
std::FILE* file = std::fopen(path.string().c_str(), "wb");
#ifdef _WIN32
std::FILE* file = _wfopen(path.c_str(), L"wb");
#else
std::FILE* file = std::fopen(path.c_str(), "wb");
#endif
Require(saved >= 0 && file != nullptr);
#ifdef _WIN32
Require(_dup2(_fileno(file), 2) == 0);
Expand Down
22 changes: 11 additions & 11 deletions tools/tests/test_unwrap_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_output_aliases_cannot_replace_the_input_container(self):
for output in (source, source.parent / "." / source.name, alias):
with self.subTest(output=output):
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), str(source), str(output)],
capture_output=True, text=True, timeout=30)
capture_output=True, encoding="utf-8", timeout=30)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("output aliases the input", result.stderr)
self.assertNotIn("Traceback", result.stderr)
Expand All @@ -168,7 +168,7 @@ def test_symbolic_link_loops_are_reported_without_a_traceback(self):
for arguments in ((source, loop), (loop, output)):
with self.subTest(arguments=arguments):
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), *map(str, arguments)],
capture_output=True, text=True, timeout=30)
capture_output=True, encoding="utf-8", timeout=30)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertEqual(result.stdout, "")
self.assertIn("FAIL: ", result.stderr)
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_segment_stale_offset_before_the_recorded_shift_is_refused(self):
output = Path(self.directory) / "output.elf"
source.write_bytes(container)
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), str(source), str(output)],
capture_output=True, text=True, timeout=30)
capture_output=True, encoding="utf-8", timeout=30)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("does not fit the file", result.stderr)
self.assertNotIn("Traceback", result.stderr)
Expand All @@ -219,7 +219,7 @@ def test_tls_layouts_the_relinker_rejects_are_refused(self):
output = Path(self.directory) / "output.elf"
source.write_bytes(container)
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), str(source), str(output)],
capture_output=True, text=True, timeout=30)
capture_output=True, encoding="utf-8", timeout=30)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn(f"FAIL: {source}: {message}", result.stderr)
self.assertNotIn("Traceback", result.stderr)
Expand Down Expand Up @@ -250,7 +250,7 @@ def test_huge_stale_offset_is_refused_instead_of_exhausting_memory(self):
output = Path(self.directory) / "output.elf"
source.write_bytes(container)
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), str(source), str(output)],
capture_output=True, text=True, timeout=30)
capture_output=True, encoding="utf-8", timeout=30)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("does not fit the file", result.stderr)
self.assertNotIn("Traceback", result.stderr)
Expand All @@ -263,7 +263,7 @@ def test_many_load_segments_over_one_range_are_refused_instead_of_exhausting_mem
output = Path(self.directory) / "output.elf"
source.write_bytes(container)
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), str(source), str(output)],
capture_output=True, text=True, timeout=60)
capture_output=True, encoding="utf-8", timeout=60)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("bytes of output", result.stderr)
self.assertNotIn("Traceback", result.stderr)
Expand Down Expand Up @@ -417,14 +417,14 @@ def test_temporary_name_collision_preserves_all_existing_files(self):
def test_new_output_inherits_the_destination_directory_acl(self):
directory = Path(self.directory)
grant = subprocess.run(["icacls.exe", str(directory), "/grant", "*S-1-1-0:(OI)(CI)(RX)"],
capture_output=True, text=True, timeout=30)
capture_output=True, text=True, encoding="oem", errors="replace", timeout=30)
self.assertEqual(grant.returncode, 0, grant.stdout + grant.stderr)
container = build_container(default_segments(), default_records(), default_payloads())
result, output = self.convert(container)
self.assertEqual(result.returncode, 0, result.stderr)
permissions = directory / "output-acl.txt"
saved = subprocess.run(["icacls.exe", str(output), "/save", str(permissions)],
capture_output=True, text=True, timeout=30)
capture_output=True, text=True, encoding="oem", errors="replace", timeout=30)
self.assertEqual(saved.returncode, 0, saved.stdout + saved.stderr)
self.assertIn(";;;WD)", permissions.read_text(encoding="utf-16-le"))
self.assertTrue(output.read_bytes().startswith(b"\x7fELF"))
Expand All @@ -441,7 +441,7 @@ def test_new_output_permissions_follow_the_process_umask(self):
with self.subTest(umask=oct(mask)):
output = directory / f"output-{mask:o}.elf"
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), str(source), str(output)],
capture_output=True, text=True, timeout=30, umask=mask)
capture_output=True, encoding="utf-8", timeout=30, umask=mask)
self.assertEqual(result.returncode, 0, result.stderr)
mode = output.stat().st_mode & 0o777
self.assertEqual(mode, 0o666 & ~mask)
Expand Down Expand Up @@ -508,7 +508,7 @@ def convert(self, container, name="input.self"):
output = directory / "output.elf"
source.write_bytes(container)
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), str(source), str(output)],
capture_output=True, text=True, timeout=30)
capture_output=True, encoding="utf-8", timeout=30)
return result, output

def setUp(self):
Expand Down Expand Up @@ -1061,7 +1061,7 @@ def test_the_verbose_report_names_every_program_header(self):
output = directory / "output.elf"
source.write_bytes(container)
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), "--verbose",
str(source), str(output)], capture_output=True, text=True, timeout=30)
str(source), str(output)], capture_output=True, encoding="utf-8", timeout=30)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("PT_LOAD", result.stdout)
self.assertIn("PT_DYNAMIC", result.stdout)
Expand Down
Loading