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