ending_retirement_test.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <array>
  2. #include <cassert>
  3. #include "../Sunrise/src/client/hooks/ember_movies/readiness_rules.h"
  4. #include "../Sunrise/src/client/hooks/ember_movies/sunburn_rules.h"
  5. #include "../Sunrise/src/client/hooks/ember_movies/playback_rules.h"
  6. #include "../Sunrise/src/client/hooks/ember_movies/surface_rules.h"
  7. #include "../Sunrise/src/client/hooks/ember_movies/orbit_rules.h"
  8. #include "../Sunrise/src/client/hooks/mission_retirement/mission_retirement.h"
  9. #include "../Sunrise/src/middleware/bap/activity_message/roster_presence.h"
  10. #include "../Sunrise/src/middleware/encoding/bit_reader.h"
  11. #include "../Sunrise/src/state/activity/membership/teleport_rules.h"
  12. #include "../Sunrise/src/core/logging/log.h"
  13. namespace sunrise::core::log {
  14. bool accepts(Channel, Level) noexcept { return false; }
  15. void write(Channel, Level, std::string_view) noexcept {}
  16. }
  17. namespace wire = sunrise::middleware::bap::activity_message::sensor_auth_update;
  18. namespace retire = sunrise::client::hooks::mission_retirement;
  19. namespace member = sunrise::state::activity::membership;
  20. using sunrise::middleware::encoding::bits::Reader;
  21. static void expect(Reader& reader, unsigned width, std::uint64_t expected) {
  22. std::uint64_t value{};
  23. assert(reader.read(static_cast<std::uint8_t>(width), value) && value == expected);
  24. }
  25. int main() {
  26. namespace movies=sunrise::client::hooks::ember_movies;
  27. movies::OrbitReturn orbit;
  28. movies::OrbitObservation inApex{true,true,false,false,true,0,38,38,true};
  29. using OA=movies::OrbitAction;
  30. assert(orbit.observe(1000,inApex)==OA::none); // Playing movies cannot arm return.
  31. orbit.arm(1000);
  32. assert(orbit.observe(9000,inApex)==OA::none); // Host must actually publish completion.
  33. inApex.complete=true;
  34. assert(orbit.observe(10000,inApex)==OA::select); // First completion frame: no banner delay.
  35. assert(orbit.observe(10001,inApex)==OA::none); // No duplicate commit.
  36. auto inOrbit=inApex; inOrbit.emberSelected=false;inOrbit.orbitSelected=true;
  37. inOrbit.exactOwner=false; // Committing orbit may already retire the old ActivityClient link.
  38. assert(orbit.observe(10002,inOrbit)==OA::cleanup); // Requires destination readback.
  39. assert(orbit.observe(10003,inOrbit)==OA::none); // No repeated cleanup request.
  40. movies::OrbitObservation betweenWorlds{};betweenWorlds.step=28;
  41. assert(orbit.observe(10004,betweenWorlds)==OA::none);
  42. assert(orbit.active());
  43. inOrbit.step=29;
  44. assert(orbit.observe(10100,inOrbit)==OA::orbitSetup);
  45. assert(!orbit.active() && orbit.observe(20000,inApex)==OA::none);
  46. orbit.arm(30000);
  47. auto stale=inApex;stale.exactOwner=false;
  48. assert(orbit.observe(31000,stale)==OA::canceled);
  49. orbit.arm(40000);
  50. auto departed=inApex;departed.step=28;
  51. assert(orbit.observe(41000,departed)==OA::canceled);
  52. orbit.arm(50000);inApex.complete=false;
  53. assert(orbit.observe(140001,inApex)==OA::timedOut);
  54. orbit.arm(150000);inApex.complete=true;
  55. inApex.ready=false;
  56. assert(orbit.observe(150000,inApex)==OA::none);
  57. inApex.ready=true;
  58. assert(orbit.observe(150001,inApex)==OA::select); // No delay once native readiness returns.
  59. assert(orbit.observe(150002,departed)==OA::none); // Native cleanup already began.
  60. assert(orbit.observe(150100,inOrbit)==OA::orbitSetup);
  61. orbit.arm(160000);
  62. inApex.pendingStep=28;
  63. assert(orbit.observe(160000,inApex)==OA::none);
  64. inApex.pendingStep=38;
  65. assert(orbit.observe(160001,inApex)==OA::select);
  66. auto replaced=inOrbit;replaced.step=38;replaced.sameWorld=false;
  67. assert(orbit.observe(160002,replaced)==OA::canceled);
  68. // a47adbd: container 80BCA022 loaded, but definition 80BCA021 was
  69. // FEFE0000 / 001044FB / null. Native 1204163 faulted reading its slot.
  70. assert(!movies::movie_definition_resident(0xFEFE0000,0x1044FB,0));
  71. assert(!movies::movie_definition_resident(0xFEFE0000,0x1044FB,0x1234000));
  72. assert(!movies::movie_definition_resident(0xC0000010,0x44FB,0));
  73. assert(!movies::movie_definition_resident(0xC0000008,0x44FB,0x1234000));
  74. assert(!movies::movie_definition_resident(0xC0000010,0x254FB,0x1234000)); // raw buffer is not a definition
  75. assert(movies::movie_definition_resident(0xC0000010,0x44FB,0x1234000));
  76. assert(movies::movie_definition_resident(0xC0000010,0x4044FB,0x1234000)); // native residency flags
  77. for (unsigned i=0;i<6;++i) {
  78. assert(movies::movie_definition_matches(i,i+1,i<2 ? 0x7F : 0x23));
  79. assert(!movies::movie_definition_matches(i,0,i<2 ? 0x7F : 0x23));
  80. }
  81. assert(!movies::movie_definition_matches(0,1,0x23));
  82. assert(!movies::movie_definition_matches(6,7,0x23));
  83. movies::SurfaceRegistrations surfaces{};
  84. for (unsigned i=1;i<=6;++i) surfaces[i].entries[0]=movies::movie_surface_definitions[i-1];
  85. // Live black-video capture: old candidate handles remain, but no container
  86. // holds a registration and every selected surface is FFFFFFFF.
  87. assert(!movies::movie_surfaces_registered(surfaces));
  88. assert(movies::movie_surfaces_absent(surfaces)); // stale candidates do not retain references
  89. for (unsigned i=1;i<=6;++i) surfaces[i].count=1;
  90. assert(!movies::movie_surfaces_absent(surfaces)); // hold definitions until native unregister
  91. assert(movies::movie_surfaces_registered(surfaces));
  92. assert(!movies::movie_surfaces_selected(surfaces));
  93. for (unsigned i=1;i<=6;++i) surfaces[i].selected=surfaces[i].entries[0];
  94. assert(movies::movie_surfaces_selected(surfaces));
  95. surfaces[4].count=0;
  96. assert(!movies::movie_surfaces_registered(surfaces));
  97. surfaces[4].count=4;
  98. assert(!movies::movie_surfaces_registered(surfaces));
  99. surfaces[4].count=2;surfaces[4].entries[1]=123;
  100. assert(!movies::movie_surfaces_registered(surfaces)); // another surface owns the top
  101. surfaces[4].count=1;
  102. surfaces[7].count=1;surfaces[7].entries[0]=123;
  103. assert(!movies::movie_surfaces_registered(surfaces)); // do not publish an unrelated pending slot
  104. surfaces[7].selected=123;
  105. assert(movies::movie_surfaces_selected(surfaces));
  106. auto removed=surfaces;
  107. for (unsigned i=1;i<=6;++i) removed[i].count=0;
  108. assert(!movies::movie_surfaces_absent(removed)); // selection is still using the definition
  109. for (unsigned i=1;i<=6;++i) removed[i].selected=0xFFFFFFFF;
  110. assert(movies::movie_surfaces_absent(removed));
  111. // Native kind 2 resolves shared-tag records; these ordinary movie tags require 1.
  112. static_assert(movies::movie_resource_kind==1);
  113. assert((movies::movie_metadata(0x80BCA001)==std::array<std::uint32_t,4>{0x80BCA001,0x80BCA000,0x80B9EB33,0x80BCA032}));
  114. assert((movies::movie_metadata(0x80BCA003)==std::array<std::uint32_t,4>{0x80BCA003,0x80BCA002,0x80B9EB34,0x80BCA032}));
  115. assert((movies::movie_metadata(0x80BCA034)==std::array<std::uint32_t,4>{})); // media is not a wrapper
  116. assert(movies::movie_stream(0x80BCA001)==0x80BCA034);
  117. assert(movies::movie_stream(0x80BCA003)==0x80C7C000);
  118. assert(movies::movie_stream(0x80BCA000)==0xFFFFFFFF);
  119. // The 13f07ee capture: metadata was loaded, but the video stream still held a
  120. // free-list entry (FEFE0035, 0, location 0). Native CRI entered error state 7.
  121. assert(!movies::movie_stream_ready(0xFEFE0035,0,0));
  122. assert(movies::movie_stream_ready(0xE80779A0,0x41363B,0x10000005));
  123. assert(!movies::movie_stream_ready(0xC0000000,0x41363B,0x10000005));
  124. assert(!movies::movie_stream_ready(0x280779A0,0x41363B,0x10000005));
  125. assert(!movies::movie_stream_ready(0xE80779A0,0x40103B,0x10000005));
  126. assert(!movies::movie_stream_ready(0xE80779A0,0x41363B,0));
  127. assert(!movies::movie_stream_ready(0xE80779A0,0x41363B,0x100000005ULL));
  128. // Captured crash: registered movie tags contain free-list entries, not resident headers.
  129. assert(!movies::movie_resources_ready(1,0x80BCA001,false,0,false,0xFFFFFFFF));
  130. assert(!movies::movie_resources_ready(2,0x80BCA001,false,0x80BCA000,true,0x80BCA034));
  131. assert(!movies::movie_resources_ready(2,0x80BCA001,true,0x80BCA000,false,0x80BCA034));
  132. assert(!movies::movie_resources_ready(2,0x80BCA001,true,0x80BCA002,true,0x80BCA034));
  133. assert(!movies::movie_resources_ready(3,0x80BCA001,true,0x80BCA000,true,0x80BCA034));
  134. assert(!movies::movie_resources_ready(2,0x80BCA001,true,0x80BCA000,true,0xFFFFFFFF));
  135. assert(movies::movie_resources_ready(2,0x80BCA001,true,0x80BCA000,true,0x80BCA034));
  136. assert(movies::movie_resources_ready(2,0x80BCA003,true,0x80BCA002,true,0x80C7C000));
  137. assert(!movies::resource_can_release(0) && !movies::resource_can_release(1));
  138. assert(movies::resource_can_release(2) && movies::resource_can_release(3));
  139. // Only Ember slot 43 may borrow sunburn. The global Foundry effect and rail shock stay distinct.
  140. assert(movies::ember_burn_source(0x80B3C0C6,0x80809540,0xAC8,0x80C1D9E0));
  141. assert(!movies::ember_burn_source(0x80BEB26F,0x80809540,0xAC8,0x80C1D9E0));
  142. assert(!movies::ember_burn_source(0x80B3C0C6,0x8080953F,0xAC8,0x80C1D9E0));
  143. assert(!movies::ember_burn_source(0x80B3C0C6,0x80809540,0xAC0,0x80C1D9E0));
  144. assert(!movies::ember_burn_source(0x80B3C0C6,0x80809540,0xAC8,0x80C1D389));
  145. movies::Playback playback;
  146. assert(playback.observe(false,5,true)==movies::Status::preparing);
  147. assert(playback.observe(true,0,false)==movies::Status::preparing);
  148. assert(playback.observe(true,6,false)==movies::Status::preparing);
  149. assert(playback.observe(true,3,true)==movies::Status::preparing);
  150. assert(playback.observe(true,5,true)==movies::Status::playing);
  151. assert(playback.observe(true,6,true)==movies::Status::playing);
  152. assert(playback.observe(true,6,false)==movies::Status::complete);
  153. assert(playback.observe(false,6,false)==movies::Status::failed);
  154. assert(playback.observe(true,7,false)==movies::Status::failed);
  155. movies::Playback skipped;
  156. assert(skipped.observe(true,5,true)==movies::Status::playing);
  157. assert(skipped.observe(true,0,false)==movies::Status::complete);
  158. std::array<std::uint32_t, 5> history{102, 103, 104};
  159. std::size_t historyCount = 3;
  160. const std::array<std::uint32_t, 3> movieOne{105, 102, 104};
  161. const std::array<std::uint32_t, 3> movieTwo{106, 105, 104};
  162. assert(wire::extend_key_order(history, historyCount, movieOne));
  163. assert(wire::extend_key_order(history, historyCount, movieTwo));
  164. assert(historyCount == 5 && (history == std::array<std::uint32_t, 5>{102,103,104,105,106}));
  165. assert(wire::extend_key_order(history, historyCount, movieOne));
  166. const std::array<std::uint32_t, 1> overflow{107};
  167. assert(!wire::extend_key_order(history, historyCount, overflow));
  168. // Unknown/empty worlds and a different owner cannot masquerade as cleanup.
  169. retire::Progress progress{retire::Status::baselinePending, 0};
  170. progress.observe(0, 1234, 4, 0);
  171. assert(progress.value == retire::Status::baselinePending);
  172. progress.observe(0, 1234, 8, 4);
  173. assert(progress.value == retire::Status::retiring);
  174. progress.observe(1, 1234, 4, 0);
  175. progress.observe(0, 5678, 4, 0);
  176. progress.observe(0, 1234, 0, 0);
  177. progress.observe(0, 1234, 5, 1);
  178. assert(progress.value == retire::Status::retiring);
  179. progress.observe(0, 1234, 4, 0);
  180. assert(progress.value == retire::Status::complete);
  181. // Exercise the actual encoder: old keys retain their ordinal, a clear bit removes
  182. // only the retired key, and phase two contains no body for that removed group.
  183. static wire::Snapshot snapshot{};
  184. std::array<std::uint8_t, 1> types{1}, flags{0};
  185. std::array<std::uint16_t, 1> indices{0};
  186. std::array<std::uint32_t, 3> keys{102, 103, 104};
  187. std::array<wire::BubbleSubBlock, 1> blocks{{{0, keys}}};
  188. snapshot.roster.groupCount = 4; snapshot.roster.topLevelGroupCount = 1;
  189. snapshot.roster.bubbleSubBlocks = blocks;
  190. for (unsigned i = 0; i < 4; ++i) {
  191. auto& group = snapshot.roster.groups[i];
  192. group.key = 101 + i; group.slotTypes = types; group.slotFlags = flags; group.slotIndices = indices;
  193. }
  194. snapshot.roster.groups[2].retired = true;
  195. std::array<std::byte, 4096> bytes{}; std::size_t size{};
  196. assert(wire::encode_sensor_auth_update(snapshot, bytes, size));
  197. Reader mask{std::span(bytes).first(size)};
  198. assert(mask.skip(wire::kLatchBitWithoutGrant + 1 + wire::delta_bits(1, {}) - 1));
  199. expect(mask, 1, 1); expect(mask, 7, 1); // Field one, one bubble.
  200. expect(mask, 1, 1); expect(mask, 32, 0x80000000U);
  201. expect(mask, 1, 1); expect(mask, 1, 1); expect(mask, 7, 3);
  202. for (auto key : keys) expect(mask, 32, key);
  203. expect(mask, 1, 1); expect(mask, 32, 5); // Keep ordinals 0 and 2.
  204. expect(mask, 32, 0); expect(mask, 32, 0);
  205. Reader bodies{std::span(bytes).first(size)};
  206. assert(bodies.skip(wire::kLatchBitWithoutGrant + 1 + wire::delta_bits(1, blocks)));
  207. for (auto key : {101U, 102U, 104U}) {
  208. expect(bodies, 1, 1); expect(bodies, 32, key); expect(bodies, 32, 0);
  209. expect(bodies, 1, 1); expect(bodies, 32, key); expect(bodies, 7, 2);
  210. expect(bodies, 16, 32768); expect(bodies, 32, 0); expect(bodies, 1, 0);
  211. }
  212. expect(bodies, 1, 0); expect(bodies, 1, 0);
  213. snapshot.roster.groups[0].retired = true;
  214. assert(!wire::encode_sensor_auth_update(snapshot, bytes, size));
  215. member::MembershipState state{};
  216. state.hasHostTeleport = true; state.hostTeleportQualified = true;
  217. state.hostTeleport = {1, 8, 1, 123}; state.teleport = {2, 8, 1, 123};
  218. state.region.index = 1; state.currentReported = true; state.currentRegion.index = 0;
  219. member::observe_qualified_teleport(state);
  220. assert(state.hostTeleport.state == 1); // Pending-region advertisement is not arrival.
  221. state.teleport.state = 3; state.currentRegion.index = 1;
  222. state.teleport.sliceSetHash = 124;
  223. member::observe_qualified_teleport(state); assert(state.hostTeleport.state == 1);
  224. state.teleport.sliceSetHash = 123; state.teleport.token = 7;
  225. member::observe_qualified_teleport(state); assert(state.hostTeleport.state == 1);
  226. state.teleport.token = 8;
  227. member::observe_qualified_teleport(state); assert(state.hostTeleport.state == 3);
  228. state.teleport.state = 0;
  229. member::observe_qualified_teleport(state); assert(!state.hasHostTeleport);
  230. assert(member::next_teleport_token(255) == 1 && member::next_teleport_token(8) == 9);
  231. }