1#include "../OptionalDiagnosticFile.h"
2#include <windows.h>
3#include <crtdbg.h>
4#include <cstdlib>
5#include <filesystem>
6#include <fstream>
7#include <iostream>
8#include <stdexcept>
9#include <string>
10
11namespace
12{
13 int invalidCalls = 0;
14 void __cdecl InvalidParameter(
const wchar_t*,
const wchar_t*,
const wchar_t*,
unsigned, uintptr_t)
15 {
16 ++invalidCalls;
17 }
18 void Check(bool ok, const char* message)
19 {
20 if (!ok) throw std::runtime_error(message);
21 }
22 std::string Read(const char* path)
23 {
24 std::ifstream file(path, std::ios::binary);
25 return std::string(std::istreambuf_iterator<char>(file), {});
26 }
27}
28
30{
31 _CrtSetReportMode(_CRT_ASSERT, 0);
32 _CrtSetReportMode(_CRT_ERROR, 0);
33 const auto previous = _set_invalid_parameter_handler(InvalidParameter);
34 try
35 {
36 const auto root = std::filesystem::absolute(
"optional-log-tests-" + std::to_string(GetCurrentProcessId()));
37 std::filesystem::create_directories(root);
38 std::filesystem::current_path(root);
39
40
41 HANDLE locked = CreateFileA("legacy-friction.log", GENERIC_WRITE, FILE_SHARE_READ,
42 nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
43 Check(locked != INVALID_HANDLE_VALUE, "Could not lock legacy fixture");
44 int descriptor = -1;
45 const int error = _sopen_s(&descriptor, "legacy-friction.log",
46 _O_RDWR | _O_CREAT | _O_TRUNC | _O_TEXT | _O_SEQUENTIAL, _SH_DENYWR, _S_IWRITE);
47 Check(error != 0 && descriptor == -1, "Second client did not fail to open the locked log");
48 Check(_write(descriptor, "original", 8) == -1 && invalidCalls == 1,
49 "Original failed-open write did not reproduce CRT invalid parameter");
50 CloseHandle(locked);
51 std::cout << "PASS: original two-client log lock reproduces CRT invalid parameter.\n";
52
53 for (const char* path : { "friction.log", "odb2.log", "drag.log" })
54 {
55 {
57 Check(first.Printf("first %i\n", 42), "First client could not write diagnostic log");
59 Check(!second.Printf("second\n"), "Second client unexpectedly acquired locked log");
60 Check(first.Printf("still running\n"), "Second client disrupted first client's stream");
61 }
62 Check(Read(path) == "first 42\r\nstill running\r\n", "Locked log was truncated or normal output changed");
64 Check(nextClient.Printf("reopened\n"), "Next client did not regain the released log");
65 }
66 Check(invalidCalls == 1, "Protected two-client logging called invalid parameter handler");
67 std::cout << "PASS: two clients, all three physics logs, output preservation and reopen.\n";
68
70 Check(!missing.Printf("optional\n"), "Missing-parent log unexpectedly opened");
71 { std::ofstream file("readonly.log"); file << "preserved"; }
72 Check(SetFileAttributesA("readonly.log", FILE_ATTRIBUTE_READONLY), "Could not make read-only fixture");
73 {
75 Check(!readonly.Printf("optional\n"), "Read-only log unexpectedly opened");
76 }
77 SetFileAttributesA("readonly.log", FILE_ATTRIBUTE_NORMAL);
78 Check(Read("readonly.log") == "preserved", "Read-only log contents changed");
79 {
81 const std::string text(8192, 'x');
82 Check(longLog.Printf("%s", text.c_str()), "Oversized optional diagnostic failed");
83 }
85 "Oversized diagnostic was not safely bounded");
86 Check(invalidCalls == 1, "Failed open or long diagnostic called invalid parameter handler");
87 std::cout << "PASS: read-only file, missing parent and oversized diagnostic are nonfatal.\n";
88 _set_invalid_parameter_handler(previous);
89 return 0;
90 }
91 catch (const std::exception& error)
92 {
93 _set_invalid_parameter_handler(previous);
94 std::cerr << "FAIL: " << error.what() << '\n';
95 return 1;
96 }
97}
static constexpr unsigned BufferSize