Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/WaypointProtocolTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

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 {
17 Swaypoint waypoint = {};
18 const WCHAR* name = L"\u5317\u4EAC \u041C\u043E\u0441\u043A\u0432\u0430 Caf\u00E9 \U0001F680";
19 WaypointProtocol::SetText(waypoint, name);
20 Check(wcscmp(waypoint.text, name) == 0, "Unicode waypoint name changed");
21
22 const std::wstring longName(80, L'\u5317');
23 WaypointProtocol::SetText(waypoint, longName.c_str());
24 Check(wcslen(waypoint.text) == 63 && waypoint.text[62] == L'\u5317', "Long name was not safely truncated");
25 WaypointProtocol::SetText(waypoint, (std::wstring(61, L'x') + L"\U0001F680").c_str());
26 Check(wcslen(waypoint.text) == 63 && waypoint.text[62] == 0xDE80, "Fitting surrogate pair was lost");
27 WaypointProtocol::SetText(waypoint, (std::wstring(62, L'x') + L"\U0001F680").c_str());
28 Check(wcslen(waypoint.text) == 62, "Truncation split a surrogate pair");
29 WaypointProtocol::SetText(waypoint, L"");
30 Check(waypoint.text[0] == L'\0', "Empty waypoint name was not preserved");
31
32 WaypointProtocol::SetText(waypoint, name);
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] = {};
36 Swaypoint received = {};
37 const int fields = swscanf_s(chat, L"TC_WAYPOINT,%i,%f,%f,%f,%63[^\n]", &received.uniqueid,
38 &received.location.x, &received.location.y, &received.location.z,
39 receivedName, static_cast<unsigned>(_countof(receivedName)));
40 WaypointProtocol::SetText(received, receivedName);
41 Check(fields == 5 && wcscmp(received.text, name) == 0, "Chat sharing did not preserve Unicode");
42 Check(received.uniqueid == 42 && received.location.x == 1.25f && received.location.y == -2.5f &&
43 received.location.z == 3.75f, "Chat sharing changed waypoint coordinates");
44 }
45
46 Swaypoint Waypoint(int id)
47 {
48 Swaypoint waypoint = {};
49 waypoint.id = id;
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 // Replay the incident: stage 80 finishes before password/completion data.
59 for (int frame = 0; frame < 120; ++frame)
60 Check(!download.TryRequest(false, 0.25f), "Requested waypoints while the server still considers us offline");
61 Check(download.TryRequest(true, 0.0f) && download.Attempts() == 1,
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");
65 Check(download.TryRequest(true, 1.0f) && download.Attempts() == 2,
66 "A silent/rejected download was never retried");
67
68 // A slow but active snapshot must not trigger repeated full downloads.
69 for (int record = 0; record < 290; ++record)
70 {
71 Check(!download.TryRequest(true, 1.0f), "Retried while snapshot records were arriving");
72 download.ReceivedRecord();
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");
79 Check(!download.Begin() && !download.TryRequest(true, 60.0f),
80 "Resource reload re-requested a completed session snapshot");
81
82 // Reconnect must re-arm the download and wait for this session's EOT.
83 download = {};
84 Check(download.Begin() && !download.TryRequest(false, 60.0f),
85 "New connection bypassed login completion");
86 Check(download.TryRequest(true, 0.0f) && download.Attempts() == 1,
87 "New connection could not download waypoints");
88
89 // No saved waypoints is a valid snapshot, not a reason to retry forever.
90 Check(download.Complete() && !download.TryRequest(true, 60.0f),
91 "Empty snapshot completion failed to stop retries");
92
93 WaypointProtocol::DownloadState earlyConclusion;
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 // Legacy servers may deliver the terminator before the selected record.
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));
108 earlyConclusion.ReceivedRecord();
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 {
117 const auto withoutMap = WaypointProtocol::Select(id, false);
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");
122 auto withMap = WaypointProtocol::Select(id, true);
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 // Replay type 37 selecting 164909 before type 18 supplies its record.
132 std::vector<Swaypoint> waypoints = { Waypoint(0), Waypoint(42) };
134 int current = 1;
135 selection.Set(164909);
136 Check(!selection.TryResolve(waypoints, current) && current == 1,
137 "Missing record discarded the current target or exposed an invalid index");
138 waypoints.push_back(Waypoint(99));
139 Check(!selection.TryResolve(waypoints, current) && current == 1,
140 "An unrelated record satisfied the pending selection");
141 waypoints.push_back(Waypoint(164909));
142 Check(selection.TryResolve(waypoints, current) && current == 3,
143 "Selection was not restored when the delayed record arrived");
144 current = 1;
145 Check(!selection.TryResolve(waypoints, current) && current == 1,
146 "Resolved selection was applied again after a local target change");
147
148 selection.Set(164909);
149 Check(selection.TryResolve(waypoints, current) && current == 3,
150 "Data-before-selection ordering failed");
151 selection.Set(0);
152 Check(selection.TryResolve(waypoints, current) && current == 0,
153 "Server selection of the temporary waypoint failed");
154
155 selection.Set(500);
156 selection.Set(600);
157 waypoints.push_back(Waypoint(500));
158 Check(!selection.TryResolve(waypoints, current) && current == 0,
159 "Superseded selection was applied");
160 waypoints.push_back(Waypoint(600));
161 Check(selection.TryResolve(waypoints, current) && current == 5,
162 "Newest pending selection was lost");
163 selection.Set(700);
164 selection.Set(42);
165 Check(selection.TryResolve(waypoints, current) && current == 1,
166 "An available new target did not replace the pending target");
167 waypoints.push_back(Waypoint(700));
168 Check(!selection.TryResolve(waypoints, current) && current == 1,
169 "An older delayed record replaced the newer selection");
170
171 // Local selection/deletion and a fresh download cancel pending state.
172 selection.Set(800);
173 selection.Clear();
174 waypoints.push_back(Waypoint(800));
175 Check(!selection.TryResolve(waypoints, current) && current == 1,
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 // Delete an empty middle tab, then verify both existing folders retain their
207 // server IDs and all waypoint metadata without requiring an upload.
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");
212 Check(!folders.CanRemove(1, 3, records) && !folders.CanRemove(2, 3, records),
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 // Add reuses the freed group. A received waypoint must immediately block
223 // removal even when the dialog's cached rows have not refreshed yet.
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 // Last-waypoint deletion/move makes its folder removable, down to the
232 // single permanent folder. Adding again must retain a unique group per tab.
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 // Build a Server.NET-compatible little-endian payload independently of the C++ fields.
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); // Beijing plus a supplementary character, UTF-16LE.
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
278 SPacketHuge packet;
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
289 outgoing.type = 18;
290 outgoing.waypoint = packet.waypoint;
291 Check(memcmp(&outgoing, wire, 257) == 0, "Upload changed payload bytes or packet type");
292 }
293}
294
295int main()
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}
#define MAX_TAB
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)
bool TryResolve(const std::vector< Swaypoint > &waypoints, int &currentIndex)
char outgoing[80]
Definition globals.cpp:60
void SetText(Swaypoint &waypoint, const WCHAR *text)
SClientPacket Select(int waypointId, bool streetViewConfigured)
unsigned char type
RiseMath::Vector3 location
WCHAR text[64]