1#include "../WaypointProtocol.h"
2
3#include <cstdio>
4#include <cstring>
5#include <stdexcept>
6#include <string>
7
8namespace
9{
10 void Check(bool condition, const char* message)
11 {
12 if (!condition) throw std::runtime_error(message);
13 }
14
15 void TestText()
16 {
18 const WCHAR* name = L"\u5317\u4EAC \u041C\u043E\u0441\u043A\u0432\u0430 Caf\u00E9 \U0001F680";
20 Check(wcscmp(waypoint.
text, name) == 0,
"Unicode waypoint name changed");
21
22 const std::wstring longName(80, L'\u5317');
24 Check(wcslen(waypoint.
text) == 63 && waypoint.
text[62] == L
'\u5317',
"Long name was not safely truncated");
26 Check(wcslen(waypoint.
text) == 63 && waypoint.
text[62] == 0xDE80,
"Fitting surrogate pair was lost");
28 Check(wcslen(waypoint.
text) == 62,
"Truncation split a surrogate pair");
30 Check(waypoint.
text[0] == L
'\0',
"Empty waypoint name was not preserved");
31
33 WCHAR chat[256];
34 swprintf_s(chat, _countof(chat), L
"TC_WAYPOINT,%i,%f,%f,%f,%ls", 42, 1.25, -2.5, 3.75, waypoint.
text);
35 WCHAR receivedName[64] = {};
37 const int fields = swscanf_s(chat, L
"TC_WAYPOINT,%i,%f,%f,%f,%63[^\n]", &received.
uniqueid,
39 receivedName, static_cast<unsigned>(_countof(receivedName)));
41 Check(fields == 5 && wcscmp(received.
text, name) == 0,
"Chat sharing did not preserve Unicode");
43 received.
location.z == 3.75f,
"Chat sharing changed waypoint coordinates");
44 }
45
47 {
50 return waypoint;
51 }
52
53 void TestDownloadLifecycle()
54 {
56 Check(!download.
TryRequest(
true, 60.0f),
"Requested waypoints before loading scheduled them");
57 Check(download.
Begin(),
"Initial loading did not schedule the waypoint download");
58
59 for (int frame = 0; frame < 120; ++frame)
60 Check(!download.
TryRequest(
false, 0.25f),
"Requested waypoints while the server still considers us offline");
62 "Late login conclusion did not release the queued download");
63 Check(!download.
TryRequest(
true, 0.0f),
"Sent duplicate request in the next frame");
64 Check(!download.
TryRequest(
true, 9.0f),
"Retried before the idle timeout");
66 "A silent/rejected download was never retried");
67
68
69 for (int record = 0; record < 290; ++record)
70 {
71 Check(!download.
TryRequest(
true, 1.0f),
"Retried while snapshot records were arriving");
73 }
74 Check(!download.
Begin(),
"Resource reload restarted the in-flight download");
75 Check(!download.
TryRequest(
true, 9.0f),
"Resource reload reset request timing");
76 Check(download.
TryRequest(
true, 1.0f),
"A partially received snapshot could not recover from a stall");
77 Check(download.
Complete(),
"Snapshot terminator did not complete the download");
78 Check(!download.
Complete(),
"Duplicate snapshot terminator changed state");
80 "Resource reload re-requested a completed session snapshot");
81
82
83 download = {};
85 "New connection bypassed login completion");
87 "New connection could not download waypoints");
88
89
91 "Empty snapshot completion failed to stop retries");
92
94 Check(!earlyConclusion.
Complete(),
"Unsolicited selection satisfied an unsent download");
95 Check(!earlyConclusion.
TryRequest(
true, 60.0f),
"Early conclusion skipped loading initialization");
96 Check(earlyConclusion.
Begin() && earlyConclusion.
TryRequest(
true, 0.0f),
97 "Conclusion arriving before stage 80 prevented the download");
98
99
100 std::vector<Swaypoint> records = { Waypoint(0) };
102 int current = 0;
103 Check(earlyConclusion.
Complete(),
"Early snapshot terminator was rejected");
104 selection.
Set(164909);
105 Check(!selection.
TryResolve(records, current),
"Missing selected record unexpectedly resolved");
106 Check(!earlyConclusion.
Begin(),
"Reload restarted a snapshot with delayed records");
107 records.push_back(Waypoint(164909));
109 Check(selection.
TryResolve(records, current) && current == 1,
110 "Reload/early snapshot completion lost the delayed active waypoint");
111 }
112
113 void TestNavigationCapability()
114 {
115 for (const int id : { 0, 42, 164909 })
116 {
118 Check(withoutMap.type == 37 && withoutMap.f_x == 0.0f &&
119 withoutMap.f_y == static_cast<float>(id) && withoutMap.f_w == 0.0f,
120 "Cockpit without StreetView must still select and save the waypoint");
121 Check(withoutMap.f_z == -1.0f, "Cockpit without StreetView must opt out of server routing");
123 Check(withMap.f_z == 0.0f, "Equipped cockpit must retain the legacy routing request");
124 Check(withMap.type == withoutMap.type && withMap.f_x == withoutMap.f_x &&
125 withMap.f_y == withoutMap.f_y && withMap.f_w == withoutMap.f_w,
126 "Navigation opt-out must not change the selected target or management operation");
127 }
128 }
129 void TestSelectionPacketOrdering()
130 {
131
132 std::vector<Swaypoint>
waypoints = { Waypoint(0), Waypoint(42) };
134 int current = 1;
135 selection.
Set(164909);
137 "Missing record discarded the current target or exposed an invalid index");
140 "An unrelated record satisfied the pending selection");
143 "Selection was not restored when the delayed record arrived");
144 current = 1;
146 "Resolved selection was applied again after a local target change");
147
148 selection.
Set(164909);
150 "Data-before-selection ordering failed");
153 "Server selection of the temporary waypoint failed");
154
159 "Superseded selection was applied");
162 "Newest pending selection was lost");
166 "An available new target did not replace the pending target");
169 "An older delayed record replaced the newer selection");
170
171
176 "Cancelled server selection replaced a local choice or crossed a reload");
177 std::vector<Swaypoint> empty;
178 current = 0;
179 selection.
Set(164909);
180 Check(!selection.
TryResolve(empty, current) && current == 0,
181 "Empty download produced an invalid target index");
182 empty.push_back(Waypoint(0));
183 empty.push_back(Waypoint(164909));
184 Check(selection.
TryResolve(empty, current) && current == 1,
185 "Selection received before an empty download was not retained");
186 }
187
188 void TestFolderRemoval()
189 {
191 std::vector<Swaypoint> records = { Waypoint(0), Waypoint(10), Waypoint(20) };
192 records[1].group = 2;
193 records[1].protectionLevel = 1;
194 records[1].heading = 123;
195 records[2].group = 3;
196 const auto originalRecords = records;
197 const auto initialOrder = folders.
Save();
198
199 Check(!folders.
CanRemove(0, 1, records),
"Enabled removal of the only folder");
200 Check(!folders.
Remove(0, 4, records),
"Removed the temporary-waypoint folder");
201 Check(!folders.
Remove(2, 4, records),
"Removed a populated folder");
202 Check(!folders.
Remove(-1, 4, records) && !folders.
Remove(4, 4, records) &&
203 !folders.
Remove(1,
MAX_TAB + 1, records),
"Accepted invalid tab bounds");
204 Check(folders.
Save() == initialOrder,
"Rejected removal changed folder order");
205
206
207
208 Check(folders.
CanRemove(1, 4, records) && folders.
Remove(1, 4, records),
209 "Could not remove an empty middle folder");
210 Check(folders.
Group(1) == 2 && folders.
Group(2) == 3 && folders.
Group(3) == 1,
211 "Removing a middle folder lost stable group IDs or the reusable slot");
213 "Populated shifted folders were considered empty");
214 Check(memcmp(records.data(), originalRecords.data(), records.size() *
sizeof(
Swaypoint)) == 0,
215 "Folder removal modified waypoint metadata");
216
218 Check(restored.
Load(folders.
Save()) && restored.
Tab(2) == 1 && restored.
Tab(3) == 2,
219 "Saved folder order did not survive restart/server redownload");
220 Check(restored.
Tab(255) == -1,
"Accepted a server group outside the tab capacity");
221
222
223
224 records.push_back(Waypoint(30));
225 records.back().group =
static_cast<unsigned char>(restored.
Group(3));
226 Check(records.back().group == 1 && !restored.
CanRemove(3, 4, records) &&
227 !restored.
Remove(3, 4, records),
"Received waypoint did not protect a reused folder");
228 records.pop_back();
229 Check(restored.
Remove(3, 4, records),
"Could not remove the empty rightmost folder");
230
231
232
233 records.erase(records.begin() + 1);
234 Check(restored.
Remove(1, 3, records) && restored.
Tab(3) == 1,
235 "Empty folder after deleting its last waypoint could not be removed");
236 records.resize(1);
237 Check(restored.
Remove(1, 2, records) && !restored.
Remove(0, 1, records),
238 "Could not reduce to exactly one folder");
239 for (
short tab = 0; tab <
MAX_TAB; ++tab)
240 Check(restored.
Tab(
static_cast<unsigned char>(restored.
Group(tab))) == tab,
241 "Repeated remove/add cycles duplicated a server group");
242
243 auto invalid = folders.
Save();
244 invalid[5] = invalid[4];
245 Check(!restored.
Load(invalid) && restored.
Save() == initialOrder,
246 "Duplicate persisted groups did not fall back to legacy order");
247 invalid = folders.
Save();
248 invalid[5] = 255;
249 Check(!restored.
Load(invalid),
"Accepted an out-of-range persisted group");
250 invalid = folders.
Save();
251 invalid[4] = 1;
252 Check(!restored.
Load(invalid),
"Allowed the temporary-waypoint group to move");
253 Check(!restored.
Load({}) && restored.
Save() == initialOrder,
254 "Legacy GUI configuration did not retain its original folder order");
255 }
256
257 void TestWirePacket()
258 {
259
260 unsigned char wire[260] = {};
261 const unsigned char name[] = { 0x17, 0x53, 0xAC, 0x4E, 0x3D, 0xD8, 0x80, 0xDE, 0, 0 };
262 memcpy(wire, name, sizeof name);
263 wire[128] = 3;
264 wire[129] = 1;
265 wire[130] = 0xFF;
266 wire[131] = 1;
267 const float location[] = { 1.25f, -2.5f, 3.75f };
268 memcpy(wire + 132, location, sizeof location);
269 wire[156] = 42;
270 wire[160] = 0xFF;
271 wire[161] = 0xFF;
272 wire[172] = 8;
273 wire[204] = 0xFE;
274 wire[205] = 5;
275 wire[208] = 99;
276 wire[256] = 18;
277
279 memcpy(&packet, wire, sizeof wire);
280 Check(wcscmp(packet.waypoint.text, L"\u5317\u4EAC\U0001F680") == 0, "Wire name decoded incorrectly");
281 Check(packet.waypoint.group == 3 && packet.waypoint.
type == 1 && packet.waypoint.ils == -1 &&
282 packet.waypoint.powered, "Waypoint flags have incorrect wire offsets");
283 Check(packet.waypoint.location ==
RiseMath::Vector3(1.25f, -2.5f, 3.75f),
"Wire coordinates decoded incorrectly");
284 Check(packet.waypoint.id == 42 && packet.waypoint.heading == -1 && packet.waypoint.source == 8 &&
285 packet.waypoint.reference == -2 && packet.waypoint.protectionLevel == 5 &&
286 packet.waypoint.uniqueid == 99 && packet.
type == 18,
"Waypoint metadata has incorrect wire offsets");
287
290 outgoing.waypoint = packet.waypoint;
291 Check(memcmp(&
outgoing, wire, 257) == 0,
"Upload changed payload bytes or packet type");
292 }
293}
294
296{
297 try
298 {
299 TestText();
300 TestWirePacket();
301 TestDownloadLifecycle();
302 TestSelectionPacketOrdering();
303 TestNavigationCapability();
304 TestFolderRemoval();
305 puts("PASS: Empty-folder removal, last-folder protection, stable groups, restart persistence, received waypoints, and legacy/corrupt configuration.");
306 puts("PASS: Login-gated waypoint download, idle retries, resource reload/reconnect, empty/early completion, Unicode names, wire layout, selection ordering, and StreetView navigation opt-out.");
307 return 0;
308 }
309 catch (const std::exception& error)
310 {
311 fprintf(stderr, "FAIL: %s\n", error.what());
312 return 1;
313 }
314}
unsigned Attempts() const
bool TryRequest(bool loginComplete, float elapsedSeconds)
bool Remove(short tab, short count, const std::vector< Swaypoint > &waypoints)
short Tab(unsigned char group) const
bool CanRemove(short tab, short count, const std::vector< Swaypoint > &waypoints) const
bool Load(const SavedState &saved)
int Group(short tab) const
bool TryResolve(const std::vector< Swaypoint > &waypoints, int ¤tIndex)
void SetText(Swaypoint &waypoint, const WCHAR *text)
SClientPacket Select(int waypointId, bool streetViewConfigured)
RiseMath::Vector3 location