aboutsummaryrefslogtreecommitdiff
path: root/nearby/presence/np_cpp_ffi/tests/deserialize_v0_tests.cc
blob: 54b17ab92928dacd8062255f30b8443fdd62df94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "nearby_protocol.h"
#include "shared_test_util.h"
#include "np_cpp_test.h"

#include "gtest/gtest.h"

TEST_F(NpCppTest, InvalidCast) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(V0AdvSimple, book);
  ASSERT_EQ(deserialize_result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);

  // Now try to cast the result into the wrong type and verify the process
  // aborts
  ASSERT_DEATH({ [[maybe_unused]] auto failure = deserialize_result.IntoV1(); },
               "");
}

TEST_F(NpCppTest, V0SingleDataElementTxPower) {
  nearby_protocol::RawAdvertisementPayload adv(
      nearby_protocol::ByteBuffer<255>({
          4,
          {0x00,       // Adv Header
           0x03,       // Public DE header
           0x15, 0x03} // Length 1 Tx Power DE with value 3
      }));

  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto maybe_credential_book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab);
  ASSERT_TRUE(maybe_credential_book.ok());
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          adv, maybe_credential_book.value());

  ASSERT_EQ(deserialize_result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
  auto v0_adv = deserialize_result.IntoV0();

  ASSERT_EQ(v0_adv.GetKind(),
            nearby_protocol::DeserializedV0AdvertisementKind::Legible);
  auto legible_adv = v0_adv.IntoLegible();
  auto identity = legible_adv.GetIdentityKind();
  ASSERT_EQ(identity,
            nearby_protocol::DeserializedV0IdentityKind::Plaintext);

  auto num_des = legible_adv.GetNumberOfDataElements();
  ASSERT_EQ(num_des, 1);
  auto payload = legible_adv.IntoPayload();

  auto de_result = payload.TryGetDataElement(0);
  ASSERT_TRUE(de_result.ok());
  auto de = de_result.value();

  ASSERT_EQ(de.GetKind(), nearby_protocol::V0DataElementKind::TxPower);
  auto tx_power = de.AsTxPower();
  ASSERT_EQ(tx_power.tx_power, 3);
}

TEST_F(NpCppTest, V0LengthOneActionsDataElement) {
  nearby_protocol::RawAdvertisementPayload adv(
      nearby_protocol::ByteBuffer<255>({
          4,
          {0x00,       // Adv Header
           0x03,       // Public DE header
           0x16, 0x00} // Length 1 Actions DE
      }));

  auto maybe_credential_slab = nearby_protocol::CredentialSlab::TryCreate();
  ASSERT_TRUE(maybe_credential_slab.ok());
  auto maybe_credential_book = nearby_protocol::CredentialBook::TryCreateFromSlab(maybe_credential_slab.value());
  ASSERT_TRUE(maybe_credential_book.ok());
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          adv, maybe_credential_book.value());

  ASSERT_EQ(deserialize_result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
  auto v0_adv = deserialize_result.IntoV0();

  ASSERT_EQ(v0_adv.GetKind(),
            nearby_protocol::DeserializedV0AdvertisementKind::Legible);
  auto legible_adv = v0_adv.IntoLegible();
  auto identity = legible_adv.GetIdentityKind();
  ASSERT_EQ(identity,
            nearby_protocol::DeserializedV0IdentityKind::Plaintext);

  auto num_des = legible_adv.GetNumberOfDataElements();
  ASSERT_EQ(num_des, 1);
  auto payload = legible_adv.IntoPayload();

  auto de_result = payload.TryGetDataElement(0);
  ASSERT_TRUE(de_result.ok());
  auto de = de_result.value();

  ASSERT_EQ(de.GetKind(), nearby_protocol::V0DataElementKind::Actions);
  auto actions = de.AsActions();
  ASSERT_EQ(actions.GetAsU32(), (uint32_t)0);
}

TEST_F(NpCppTest, V0LengthTwoActionsDataElement) {
  nearby_protocol::RawAdvertisementPayload adv(
      nearby_protocol::ByteBuffer<255>({
          5,
          {0x00,             // Adv Header
           0x03,             // Public DE header
           0x26, 0xD0, 0x46} // Length 2 Actions DE
      }));

  auto maybe_credential_slab = nearby_protocol::CredentialSlab::TryCreate();
  ASSERT_TRUE(maybe_credential_slab.ok());
  auto maybe_credential_book = nearby_protocol::CredentialBook::TryCreateFromSlab(maybe_credential_slab.value());
  ASSERT_TRUE(maybe_credential_book.ok());
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          adv, maybe_credential_book.value());

  ASSERT_EQ(deserialize_result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
  auto v0_adv = deserialize_result.IntoV0();

  ASSERT_EQ(v0_adv.GetKind(),
            nearby_protocol::DeserializedV0AdvertisementKind::Legible);
  auto legible_adv = v0_adv.IntoLegible();
  auto identity = legible_adv.GetIdentityKind();
  ASSERT_EQ(identity,
            nearby_protocol::DeserializedV0IdentityKind::Plaintext);

  auto num_des = legible_adv.GetNumberOfDataElements();
  ASSERT_EQ(num_des, 1);

  auto payload = legible_adv.IntoPayload();
  auto de_result = payload.TryGetDataElement(0);
  ASSERT_TRUE(de_result.ok());
  auto de = de_result.value();

  ASSERT_EQ(de.GetKind(), nearby_protocol::V0DataElementKind::Actions);
  auto actions = de.AsActions();
  ASSERT_EQ(actions.GetAsU32(), 0xD0460000);

  ASSERT_TRUE(
      actions.HasAction(nearby_protocol::BooleanActionType::NearbyShare));
  ASSERT_TRUE(actions.HasAction(nearby_protocol::BooleanActionType::Finder));
  ASSERT_TRUE(
      actions.HasAction(nearby_protocol::BooleanActionType::FastPairSass));

  ASSERT_FALSE(
      actions.HasAction(nearby_protocol::BooleanActionType::ActiveUnlock));
  ASSERT_FALSE(
      actions.HasAction(nearby_protocol::BooleanActionType::InstantTethering));
  ASSERT_FALSE(actions.HasAction(nearby_protocol::BooleanActionType::PhoneHub));
  ASSERT_FALSE(
      actions.HasAction(nearby_protocol::BooleanActionType::PresenceManager));

  ASSERT_EQ(actions.GetContextSyncSequenceNumber(), 0xD);
}

TEST_F(NpCppTest, V0MultipleDataElements) {
  nearby_protocol::RawAdvertisementPayload adv(nearby_protocol::ByteBuffer<255>(
      {7,
       {
           0x00,             // Adv Header
           0x03,             // Public DE header
           0x15, 0x05,       // Tx Power value 5
           0x26, 0x00, 0x46, // Length 2 Actions
       }}));

  auto maybe_credential_slab = nearby_protocol::CredentialSlab::TryCreate();
  ASSERT_TRUE(maybe_credential_slab.ok());
  auto maybe_credential_book = nearby_protocol::CredentialBook::TryCreateFromSlab(maybe_credential_slab.value());
  ASSERT_TRUE(maybe_credential_book.ok());
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          adv, maybe_credential_book.value());

  ASSERT_EQ(deserialize_result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
  auto v0_adv = deserialize_result.IntoV0();

  ASSERT_EQ(v0_adv.GetKind(),
            nearby_protocol::DeserializedV0AdvertisementKind::Legible);
  auto legible_adv = v0_adv.IntoLegible();
  auto identity = legible_adv.GetIdentityKind();
  ASSERT_EQ(identity,
            nearby_protocol::DeserializedV0IdentityKind::Plaintext);

  auto num_des = legible_adv.GetNumberOfDataElements();
  ASSERT_EQ(num_des, 2);
  auto payload = legible_adv.IntoPayload();

  auto first_de_result = payload.TryGetDataElement(0);
  ASSERT_TRUE(first_de_result.ok());
  auto first_de = first_de_result.value();

  ASSERT_EQ(first_de.GetKind(), nearby_protocol::V0DataElementKind::TxPower);
  auto power = first_de.AsTxPower();
  ASSERT_EQ(power.tx_power, 5);

  auto second_de_result = payload.TryGetDataElement(1);
  ASSERT_TRUE(second_de_result.ok());
  auto second_de = second_de_result.value();

  ASSERT_EQ(second_de.GetKind(), nearby_protocol::V0DataElementKind::Actions);
  auto actions = second_de.AsActions();
  ASSERT_EQ(actions.GetAsU32(), (uint32_t)0x00460000);
  ASSERT_EQ(actions.GetContextSyncSequenceNumber(), (uint8_t)0);
}

TEST_F(NpCppTest, V0EmptyPayload) {
  auto maybe_credential_slab = nearby_protocol::CredentialSlab::TryCreate();
  ASSERT_TRUE(maybe_credential_slab.ok());
  auto maybe_credential_book = nearby_protocol::CredentialBook::TryCreateFromSlab(maybe_credential_slab.value());
  ASSERT_TRUE(maybe_credential_book.ok());
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          V0AdvEmpty, maybe_credential_book.value());

  ASSERT_EQ(deserialize_result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::Error);
}

TEST_F(NpCppTest, TestV0AdvMoveConstructor) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  auto result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto adv = result.IntoV0();

  // Now move the adv into a new value, and make sure its still valid
  nearby_protocol::DeserializedV0Advertisement moved_adv(std::move(adv));
  ASSERT_EQ(moved_adv.GetKind(),
            np_ffi::internal::DeserializedV0AdvertisementKind::Legible);

  // trying to use the moved object should result in a use after free which
  // triggers an abort
  ASSERT_DEATH([[maybe_unused]] auto failure =
                   adv.IntoLegible(), // NOLINT(bugprone-use-after-move
               "");
  ASSERT_DEATH([[maybe_unused]] auto failure = adv.GetKind(), "");

  // moving again should still preserve the moved state and also lead to an
  // abort
  nearby_protocol::DeserializedV0Advertisement moved_again(std::move(adv));
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.IntoLegible(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.GetKind(), "");
}

TEST_F(NpCppTest, TestV0AdvMoveAssignment) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  auto result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto adv = result.IntoV0();

  // create a second result
  auto another_result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(another_result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto adv2 = another_result.IntoV0();

  // move adv2 into adv, the original should be deallocated by assignment
  adv2 = std::move(adv);
  ASSERT_EQ(adv2.GetKind(),
            np_ffi::internal::DeserializedV0AdvertisementKind::Legible);

  // original result should now be invalid, using it will trigger a use after
  // free abort.
  ASSERT_DEATH([[maybe_unused]] auto failure =
                   adv.IntoLegible(), // NOLINT(bugprone-use-after-move)
               "");
  ASSERT_DEATH([[maybe_unused]] auto failure = adv.GetKind(), "");

  // moving again should still lead to an error
  auto moved_again = std::move(adv);
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.IntoLegible(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.GetKind(), "");
}

static nearby_protocol::DeserializeAdvertisementResult
CreateAdv(nearby_protocol::CredentialBook &book) {
  auto adv = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  return adv;
}

TEST_F(NpCppTest, V0AdvDestructor) {
  auto maybe_credential_slab = nearby_protocol::CredentialSlab::TryCreate();
  ASSERT_TRUE(maybe_credential_slab.ok());
  auto book_result = nearby_protocol::CredentialBook::TryCreateFromSlab(maybe_credential_slab.value());
  ASSERT_TRUE(book_result.ok());
  {
    auto deserialize_result = CreateAdv(book_result.value());
    auto deserialize_result2 = CreateAdv(book_result.value());
    // Deserialize 2 advertisements, which will take up 2 slots in the handle
    // map
    ASSERT_EQ(deserialize_result.GetKind(),
              np_ffi::internal::DeserializeAdvertisementResultKind::V0);
    ASSERT_EQ(deserialize_result2.GetKind(),
              np_ffi::internal::DeserializeAdvertisementResultKind::V0);

    // Going over max amount should result in error
    auto deserialize_result3 =
        nearby_protocol::Deserializer::DeserializeAdvertisement(
            V0AdvSimple, book_result.value());
    ASSERT_EQ(deserialize_result3.GetKind(),
              np_ffi::internal::DeserializeAdvertisementResultKind::Error);

    // Calling IntoV0() should move the underlying resources into the v0
    // object when both go out of scope only one should be freed
    auto v0_adv = deserialize_result.IntoV0();
  }

  // Now that the first v0 adv is out of scope, it should be de-allocated which
  // will create room for one more to be created.
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          V0AdvSimple, book_result.value());
  ASSERT_EQ(deserialize_result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
}

TEST_F(NpCppTest, V0AdvUseAfterMove) {
  auto maybe_credential_slab = nearby_protocol::CredentialSlab::TryCreate();
  ASSERT_TRUE(maybe_credential_slab.ok());
  auto maybe_credential_book = nearby_protocol::CredentialBook::TryCreateFromSlab(maybe_credential_slab.value());
  ASSERT_TRUE(maybe_credential_book.ok());
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          V0AdvSimple, maybe_credential_book.value());

  ASSERT_EQ(deserialize_result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
  auto v0_adv = deserialize_result.IntoV0();

  ASSERT_EQ(v0_adv.GetKind(),
            nearby_protocol::DeserializedV0AdvertisementKind::Legible);

  // Moves the adv into a legible adv, so the original v0_adv is no longer valid
  [[maybe_unused]] auto legible_adv = v0_adv.IntoLegible();
  ASSERT_DEATH([[maybe_unused]] auto failure = v0_adv.GetKind(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure = v0_adv.IntoLegible(), "");
}

TEST_F(NpCppTest, TestLegibleAdvMoveConstructor) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  auto result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto legible = result.IntoV0().IntoLegible();

  // Now move the adv into a new value, and make sure its still valid
  nearby_protocol::LegibleDeserializedV0Advertisement moved(std::move(legible));
  ASSERT_EQ(moved.GetNumberOfDataElements(), 1);
  ASSERT_EQ(moved.GetIdentityKind(),
            np_ffi::internal::DeserializedV0IdentityKind::Plaintext);

  // trying to use the moved object should result in a use after free which
  // triggers an abort
  ASSERT_DEATH([[maybe_unused]] auto failure =
                   legible.GetIdentityKind(), // NOLINT(bugprone-use-after-move
               "");
  ASSERT_DEATH(
      [[maybe_unused]] auto failure = legible.GetNumberOfDataElements(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure = legible.IntoPayload(), "");

  // moving again should still preserve the moved state and also lead to an
  // abort
  nearby_protocol::LegibleDeserializedV0Advertisement moved_again(
      std::move(legible));
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.GetIdentityKind(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure =
                   moved_again.GetNumberOfDataElements(),
               "");
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.IntoPayload(), "");
}

TEST_F(NpCppTest, TestLegibleAdvMoveAssignment) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  auto result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto legible = result.IntoV0().IntoLegible();

  // create a second result
  auto another_result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(another_result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto legible2 = another_result.IntoV0().IntoLegible();

  // move adv2 into adv, the original should be deallocated by assignment
  legible2 = std::move(legible);
  ASSERT_EQ(legible2.GetIdentityKind(),
            np_ffi::internal::DeserializedV0IdentityKind::Plaintext);

  // original result should now be invalid, using it will trigger a use after
  // free abort.
  ASSERT_DEATH([[maybe_unused]] auto failure =
                   legible.GetIdentityKind(), // NOLINT(bugprone-use-after-move)
               "");
  ASSERT_DEATH(
      [[maybe_unused]] auto failure = legible.GetNumberOfDataElements(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure = legible.IntoPayload(), "");

  // moving again should still lead to an error
  auto moved_again = std::move(legible);
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.IntoPayload(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.GetIdentityKind(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure =
                   moved_again.GetNumberOfDataElements(),
               "");
}

nearby_protocol::LegibleDeserializedV0Advertisement
CreateLegibleAdv(nearby_protocol::CredentialBook &book) {
  auto adv = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  auto v0_adv = adv.IntoV0();
  return v0_adv.IntoLegible();
}

TEST_F(NpCppTest, V0LegibleAdvUseAfterMove) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  auto legible_adv = CreateLegibleAdv(book);

  // Should be able to use the valid legible adv even though its original parent
  // is now out of scope.
  ASSERT_EQ(legible_adv.GetIdentityKind(),
            nearby_protocol::DeserializedV0IdentityKind::Plaintext);
  ASSERT_EQ(legible_adv.GetNumberOfDataElements(), 1);
  [[maybe_unused]] auto payload = legible_adv.IntoPayload();

  // now that the legible adv has moved into the payload it should no longer be
  // valid
  ASSERT_DEATH([[maybe_unused]] auto failure = legible_adv.GetIdentityKind(), "");
  ASSERT_DEATH([[maybe_unused]] auto failure =
                   legible_adv.GetNumberOfDataElements(),
               "");
  ASSERT_DEATH([[maybe_unused]] auto failure = legible_adv.IntoPayload(), "");
}

TEST_F(NpCppTest, LegibleAdvDestructor) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  {
    auto legible_adv = CreateLegibleAdv(book);
    auto legible_adv2 = CreateLegibleAdv(book);

    // check that legible advs are valid.
    ASSERT_EQ(legible_adv.GetIdentityKind(),
              nearby_protocol::DeserializedV0IdentityKind::Plaintext);
    ASSERT_EQ(legible_adv.GetNumberOfDataElements(), 1);
    ASSERT_EQ(legible_adv2.GetIdentityKind(),
              nearby_protocol::DeserializedV0IdentityKind::Plaintext);
    ASSERT_EQ(legible_adv2.GetNumberOfDataElements(), 1);

    // allocation slots should be full
    ASSERT_EQ(nearby_protocol::Deserializer::DeserializeAdvertisement(
                  V0AdvSimple, book)
                  .GetKind(),
              nearby_protocol::DeserializeAdvertisementResultKind::Error);
  }

  // Verify the handle was de-allocated when legible adv went out of scope
  auto result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
}

nearby_protocol::V0Payload
CreatePayload(nearby_protocol::CredentialBook &book) {
  auto legible_adv = CreateLegibleAdv(book);
  return legible_adv.IntoPayload();
}

TEST_F(NpCppTest, V0PayloadDestructor) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  {
    auto payload = CreatePayload(book);
    auto payload2 = CreatePayload(book);

    // check that payload adv is valid even though its parent is out of scope
    ASSERT_TRUE(payload.TryGetDataElement(0).ok());
    ASSERT_TRUE(payload2.TryGetDataElement(0).ok());

    // allocation slots should be full
    ASSERT_EQ(nearby_protocol::Deserializer::DeserializeAdvertisement(
                  V0AdvSimple, book)
                  .GetKind(),
              nearby_protocol::DeserializeAdvertisementResultKind::Error);
  }

  // Now that the payload is out of scope its destructor should have been called
  // freeing the parent handle
  auto result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
}

TEST_F(NpCppTest, TestV0PayloadMoveConstructor) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  auto result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto payload = result.IntoV0().IntoLegible().IntoPayload();

  // Now move the adv into a new value, and make sure its still valid
  nearby_protocol::V0Payload moved(std::move(payload));
  ASSERT_TRUE(moved.TryGetDataElement(0).ok());
  ASSERT_TRUE(absl::IsOutOfRange(moved.TryGetDataElement(1).status()));

  // trying to use the moved object should result in a use after free which
  // triggers an abort
  ASSERT_DEATH([[maybe_unused]] auto failure = payload.TryGetDataElement(
                   0), // NOLINT(bugprone-use-after-move
               "");

  // moving again should still preserve the moved state and also lead to an
  // abort
  nearby_protocol::V0Payload moved_again(std::move(payload));
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.TryGetDataElement(0),
               "");
}

TEST_F(NpCppTest, TestV0PayloadMoveAssignment) {
  auto slab = nearby_protocol::CredentialSlab::TryCreate().value();
  auto book = nearby_protocol::CredentialBook::TryCreateFromSlab(slab).value();
  auto result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto payload = result.IntoV0().IntoLegible().IntoPayload();

  // create a second result
  auto another_result = nearby_protocol::Deserializer::DeserializeAdvertisement(
      V0AdvSimple, book);
  ASSERT_EQ(another_result.GetKind(),
            np_ffi::internal::DeserializeAdvertisementResultKind::V0);
  auto payload2 = another_result.IntoV0().IntoLegible().IntoPayload();

  // original should be deallocated by assignment
  payload2 = std::move(payload);
  ASSERT_TRUE(payload2.TryGetDataElement(0).ok());

  // original result should now be invalid, using it will trigger a use after
  // free abort.
  ASSERT_DEATH([[maybe_unused]] auto failure = payload.TryGetDataElement(
                   0), // NOLINT(bugprone-use-after-move)
               "");

  // moving again should still lead to an error
  auto moved_again = std::move(payload);
  ASSERT_DEATH([[maybe_unused]] auto failure = moved_again.TryGetDataElement(0),
               "");
}

TEST_F(NpCppTest, InvalidDataElementCast) {
  auto maybe_credential_slab = nearby_protocol::CredentialSlab::TryCreate();
  ASSERT_TRUE(maybe_credential_slab.ok());
  auto maybe_credential_book = nearby_protocol::CredentialBook::TryCreateFromSlab(maybe_credential_slab.value());
  ASSERT_TRUE(maybe_credential_book.ok());
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          V0AdvSimple, maybe_credential_book.value());

  ASSERT_EQ(deserialize_result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
  auto v0_adv = deserialize_result.IntoV0();
  ASSERT_EQ(v0_adv.GetKind(),
            nearby_protocol::DeserializedV0AdvertisementKind::Legible);
  auto legible_adv = v0_adv.IntoLegible();
  auto identity = legible_adv.GetIdentityKind();
  ASSERT_EQ(identity,
            nearby_protocol::DeserializedV0IdentityKind::Plaintext);

  auto num_des = legible_adv.GetNumberOfDataElements();
  ASSERT_EQ(num_des, 1);
  auto payload = legible_adv.IntoPayload();

  auto de_result = payload.TryGetDataElement(0);
  ASSERT_TRUE(de_result.ok());
  auto de = de_result.value();

  ASSERT_EQ(de.GetKind(), nearby_protocol::V0DataElementKind::TxPower);
  ASSERT_DEATH([[maybe_unused]] auto failure = de.AsActions();, "");
}

TEST_F(NpCppTest, InvalidDataElementIndex) {
  auto maybe_credential_slab = nearby_protocol::CredentialSlab::TryCreate();
  ASSERT_TRUE(maybe_credential_slab.ok());
  auto maybe_credential_book = nearby_protocol::CredentialBook::TryCreateFromSlab(maybe_credential_slab.value());
  ASSERT_TRUE(maybe_credential_book.ok());
  auto deserialize_result =
      nearby_protocol::Deserializer::DeserializeAdvertisement(
          V0AdvSimple, maybe_credential_book.value());

  ASSERT_EQ(deserialize_result.GetKind(),
            nearby_protocol::DeserializeAdvertisementResultKind::V0);
  auto v0_adv = deserialize_result.IntoV0();

  ASSERT_EQ(v0_adv.GetKind(),
            nearby_protocol::DeserializedV0AdvertisementKind::Legible);
  auto legible_adv = v0_adv.IntoLegible();
  auto identity = legible_adv.GetIdentityKind();
  ASSERT_EQ(identity,
            nearby_protocol::DeserializedV0IdentityKind::Plaintext);

  auto num_des = legible_adv.GetNumberOfDataElements();
  ASSERT_EQ(num_des, 1);
  auto payload = legible_adv.IntoPayload();

  auto de_result = payload.TryGetDataElement(1);
  ASSERT_FALSE(de_result.ok());
  ASSERT_TRUE(absl::IsOutOfRange(de_result.status()));
}