xref: /aosp_15_r20/external/pigweed/pw_snapshot/uuid_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_snapshot/uuid.h"
16 
17 #include <array>
18 
19 #include "pw_bytes/span.h"
20 #include "pw_protobuf/encoder.h"
21 #include "pw_result/result.h"
22 #include "pw_snapshot_metadata_proto/snapshot_metadata.pwpb.h"
23 #include "pw_span/span.h"
24 #include "pw_status/status.h"
25 #include "pw_unit_test/framework.h"
26 
27 namespace pw::snapshot {
28 namespace {
29 
EncodeSnapshotWithUuid(ConstByteSpan uuid,ByteSpan dest)30 ConstByteSpan EncodeSnapshotWithUuid(ConstByteSpan uuid, ByteSpan dest) {
31   pwpb::SnapshotBasicInfo::MemoryEncoder snapshot_encoder(dest);
32   {
33     pwpb::Metadata::StreamEncoder metadata_encoder =
34         snapshot_encoder.GetMetadataEncoder();
35     EXPECT_EQ(OkStatus(), metadata_encoder.WriteSnapshotUuid(uuid));
36   }
37   EXPECT_EQ(OkStatus(), snapshot_encoder.status());
38 
39   return snapshot_encoder;
40 }
41 
TEST(ReadUuid,ReadUuid)42 TEST(ReadUuid, ReadUuid) {
43   const std::array<uint8_t, 8> kExpectedUuid = {
44       0x1F, 0x8F, 0xBF, 0xC4, 0x86, 0x0E, 0xED, 0xD4};
45   std::array<std::byte, 16> snapshot_buffer;
46   ConstByteSpan snapshot =
47       EncodeSnapshotWithUuid(as_bytes(span(kExpectedUuid)), snapshot_buffer);
48 
49   std::array<std::byte, kUuidSizeBytes> uuid_dest;
50   Result<ConstByteSpan> result = ReadUuidFromSnapshot(snapshot, uuid_dest);
51   EXPECT_EQ(OkStatus(), result.status());
52   EXPECT_EQ(kExpectedUuid.size(), result->size());
53   EXPECT_EQ(0, memcmp(result->data(), kExpectedUuid.data(), result->size()));
54 }
55 
TEST(ReadUuid,NoUuid)56 TEST(ReadUuid, NoUuid) {
57   std::array<std::byte, 16> snapshot_buffer;
58 
59   // Write some snapshot metadata, but no UUID.
60   pwpb::SnapshotBasicInfo::MemoryEncoder snapshot_encoder(snapshot_buffer);
61   {
62     pwpb::Metadata::StreamEncoder metadata_encoder =
63         snapshot_encoder.GetMetadataEncoder();
64     EXPECT_EQ(OkStatus(), metadata_encoder.WriteFatal(true));
65   }
66   EXPECT_EQ(OkStatus(), snapshot_encoder.status());
67 
68   ConstByteSpan snapshot(snapshot_encoder);
69   std::array<std::byte, kUuidSizeBytes> uuid_dest;
70   Result<ConstByteSpan> result = ReadUuidFromSnapshot(snapshot, uuid_dest);
71   EXPECT_EQ(Status::NotFound(), result.status());
72 }
73 
TEST(ReadUuid,UndersizedBuffer)74 TEST(ReadUuid, UndersizedBuffer) {
75   const std::array<uint8_t, 17> kExpectedUuid = {0xF4,
76                                                  0x1B,
77                                                  0xE1,
78                                                  0x2D,
79                                                  0x10,
80                                                  0x9B,
81                                                  0xB2,
82                                                  0x1A,
83                                                  0x88,
84                                                  0xE0,
85                                                  0xC4,
86                                                  0x77,
87                                                  0xCA,
88                                                  0x18,
89                                                  0x83,
90                                                  0xB5,
91                                                  0xBB};
92   std::array<std::byte, 32> snapshot_buffer;
93   ConstByteSpan snapshot =
94       EncodeSnapshotWithUuid(as_bytes(span(kExpectedUuid)), snapshot_buffer);
95 
96   std::array<std::byte, kUuidSizeBytes> uuid_dest;
97   Result<ConstByteSpan> result = ReadUuidFromSnapshot(snapshot, uuid_dest);
98   EXPECT_EQ(Status::ResourceExhausted(), result.status());
99 }
100 
101 }  // namespace
102 }  // namespace pw::snapshot
103