darkness_zone_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <array>
  2. #include <cassert>
  3. #include "../Sunrise/src/middleware/bap/activity_message/darkness_zone_auth.h"
  4. #include "../Sunrise/src/middleware/bap/activity_message/sensor_auth_update.h"
  5. #include "../Sunrise/src/core/logging/log.h"
  6. namespace sunrise::core::log {
  7. void write(Channel, Level, std::string_view) noexcept {}
  8. bool accepts(Channel, Level) noexcept { return false; }
  9. }
  10. // The tested participation/lifetime bodies never use the generic zero-padding arm.
  11. namespace sunrise::middleware::bap::activity_message::sensor_auth_update {
  12. bool pad_bits(encoding::bits::Writer&, std::size_t) noexcept { assert(false);return false; }
  13. }
  14. namespace msg=sunrise::middleware::bap::activity_message::sensor_auth_update;
  15. namespace zone=sunrise::middleware::bap::activity_message::darkness_zone;
  16. namespace bits=sunrise::middleware::encoding::bits;
  17. std::uint64_t read(std::span<const std::byte> body, unsigned offset, unsigned width) {
  18. bits::Reader reader(body);std::uint64_t value{};
  19. assert(reader.skip(offset)&&reader.read(width,value));return value;
  20. }
  21. int main() {
  22. std::array<std::byte,zone::kBytes> body{};bool enabled{};
  23. assert(zone::encode(true,body)&&zone::decode(body,359,enabled)&&enabled);
  24. assert(body[0]==std::byte{0x90}); // enabled, no forced wipe, selector0, no countdown (-1).
  25. for (unsigned i=1;i<body.size();++i) assert(body[i]==std::byte{});
  26. body[0]|=std::byte{2}; // active wipe timer is not a darkness-only request.
  27. assert(!zone::decode(body,359,enabled));
  28. assert(zone::encode(false,body)&&zone::decode(body,359,enabled)&&!enabled);
  29. assert(!zone::decode(body,358,enabled));
  30. assert(!zone::encode(true,std::span(body).first(44)));
  31. for (int seconds=3;seconds>=0;--seconds) {
  32. assert(zone::encode(true,body,seconds)&&zone::decode(body,359,enabled)&&enabled);
  33. assert(read(body,4,2)==1 && read(body,6,1)==1);
  34. const auto elapsed=read(body,7+2*64,64), remaining=read(body,7+3*64,64);
  35. assert(elapsed+remaining==3*673200 && remaining==std::uint64_t(seconds)*673200);
  36. assert(read(body,7,64)==elapsed&&read(body,7+64,64)==elapsed);
  37. }
  38. assert(!zone::encode(false,body,3)&&!zone::encode(true,body,4));
  39. msg::Snapshot snapshot{};snapshot.lifetime=3;snapshot.hasRegion=true;snapshot.region=64;
  40. snapshot.hasDarknessPolicy=true;snapshot.darknessEnabled=true;
  41. auto lifetime=[&] {
  42. std::array<std::byte,128> b{};bits::Writer writer(b);std::size_t written{};
  43. assert(msg::write_auth_body(writer,snapshot,17,false)&&writer.finish(written));
  44. assert(writer.bit_count()==520&&written==65);
  45. assert(read(b,0,4)==4); // Preserve the selected activity lifetime.
  46. return read(b,72,32); // schema 8080991A .5, native +12.
  47. };
  48. auto participation=[&] {
  49. std::array<std::byte,64> b{};bits::Writer writer(b);std::size_t written{};
  50. assert(msg::write_auth_body(writer,snapshot,13,true)&&writer.finish(written));
  51. assert(writer.bit_count()==(snapshot.hasDarknessPolicy?240U:224U));
  52. assert(written==(snapshot.hasDarknessPolicy?30U:28U));
  53. assert(read(b,176,4)==(snapshot.awaitClientSync?2U:0U));
  54. assert(read(b,175,1)==1); // No extra "Finding Spawn Location" hold.
  55. assert(read(b,180,1)==0); // Authored revive delay remains absent.
  56. assert(read(b,181,1)==snapshot.hasDarknessPolicy);
  57. const unsigned tail=snapshot.hasDarknessPolicy?198U:182U;
  58. assert(read(b,tail,2)==0&&read(b,tail+2,8)==128);
  59. assert(read(b,tail+10,32)==0x80000000U); // No shifted following field.
  60. return snapshot.hasDarknessPolicy ? read(b,182,16) : 0;
  61. };
  62. assert(lifetime()==0x80000008U); // Region64 is bubble8, not bubble64.
  63. assert(participation()==0x4F80); // Native IEEE half 30 seconds.
  64. snapshot.awaitClientSync=true;assert(participation()==0x4F80); // Arrival hold stays intact.
  65. snapshot.darknessEnabled=false;
  66. assert(lifetime()==0x7fffffffU&&participation()==0x4200); // IEEE half 3 seconds.
  67. snapshot.awaitClientSync=false;assert(participation()==0x4200);
  68. snapshot.darknessEnabled=true;assert(participation()==0x4F80);
  69. snapshot.darknessEnabled=false;
  70. snapshot.hasDarknessPolicy=false;
  71. assert(lifetime()==0x80000000U&&participation()==0); // Unscripted missions unchanged.
  72. }