authority_cleanup_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <cassert>
  2. #include <algorithm>
  3. #include "../Sunrise/src/middleware/bap/activity_message/authority_cleanup.h"
  4. #include "../Sunrise/src/middleware/encoding/bit_writer.h"
  5. using namespace sunrise::middleware::bap::activity_message;
  6. int main() {
  7. entity_slots::EntitySlotMask mask{};
  8. mask[0]=std::byte{1}; mask[127]=std::byte{0x80}; mask[1023]=std::byte{0x80};
  9. // Independently construct the unaligned client request, including boundary slots.
  10. for (unsigned reason=1; reason<=8; ++reason) {
  11. std::array<std::byte,entity_authority::kRequestPurgeByteCount> input{};
  12. sunrise::middleware::encoding::bits::Writer w(input);
  13. assert(w.write(reason-1,3));
  14. for (auto b:mask) assert(w.write(std::to_integer<unsigned>(b),8));
  15. std::size_t written{};assert(w.finish(written));
  16. host_control::PurgeAuthorityBody result{};
  17. assert(authority_cleanup::prepare(27,input,254,result));
  18. assert(result.slots==mask && result.epoch==255 && static_cast<unsigned>(result.reason)==reason);
  19. std::array<std::byte,host_control::kPurgeAuthorityByteCount> output{};
  20. assert(host_control::encode_purge_authority(result,output,written));
  21. host_control::PurgeAuthorityBody decoded{};
  22. assert(host_control::decode_purge_authority(output,decoded));
  23. assert(decoded.slots==mask && decoded.epoch==255 && static_cast<unsigned>(decoded.reason)==reason);
  24. assert(!authority_cleanup::prepare(27,std::span(input).first(input.size()-1),0,result));
  25. input.back() |= std::byte{1};
  26. assert(!authority_cleanup::prepare(27,input,0,result));
  27. }
  28. std::array<std::byte,entity_authority::kAbandonByteCount> abandoned{};
  29. abandoned[0]=std::byte{7};
  30. std::copy(mask.begin(),mask.end(),abandoned.begin()+1);
  31. abandoned.back()=std::byte{0x60}; // reason 4
  32. host_control::PurgeAuthorityBody result{};
  33. assert(authority_cleanup::prepare(26,abandoned,2,result));
  34. assert(result.slots==mask && result.reason==4 && result.epoch==3);
  35. for (unsigned current = 0; current < 255; ++current) {
  36. assert(authority_cleanup::prepare(26,abandoned,static_cast<std::uint8_t>(current),result));
  37. assert(result.epoch == current + 1);
  38. }
  39. assert(!authority_cleanup::prepare(26,abandoned,255,result));
  40. assert(result.epoch == 255);
  41. assert(!authority_cleanup::prepare(33,std::span(abandoned).first(1025),2,result));
  42. abandoned[0]=std::byte{65};assert(!authority_cleanup::prepare(26,abandoned,2,result));
  43. abandoned.fill(std::byte{});assert(!authority_cleanup::prepare(26,abandoned,2,result));
  44. assert(result.slots==mask && result.reason==4); // refused input never changes output
  45. }