1 // Copyright 2024 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_bluetooth/emboss_util.h"
16
17 #include "pw_bluetooth/hci_test.emb.h"
18 #include "pw_unit_test/framework.h" // IWYU pragma: keep
19
20 namespace pw::bluetooth::proxy {
21 namespace {
22
TEST(EmbossUtilTest,MakeEmbossViewFromSpan)23 TEST(EmbossUtilTest, MakeEmbossViewFromSpan) {
24 std::array<uint8_t, 4> buffer = {0x00, 0x01, 0x02, 0x03};
25 auto span = pw::span(buffer);
26 PW_TEST_ASSERT_OK_AND_ASSIGN(
27 auto view, MakeEmbossView<emboss::TestCommandPacketView>(span));
28 EXPECT_EQ(view.payload().Read(), 0x03);
29
30 PW_TEST_ASSERT_OK_AND_ASSIGN(
31 view,
32 MakeEmbossView<emboss::TestCommandPacketView>(span.data(), span.size()));
33 EXPECT_EQ(view.payload().Read(), 0x03);
34
35 auto failed_view =
36 MakeEmbossView<emboss::TestCommandPacketView>(span.subspan(1));
37 EXPECT_EQ(failed_view.status(), pw::Status::DataLoss());
38 }
39
TEST(EmbossUtilTest,MakeEmbossWriterFromSpan)40 TEST(EmbossUtilTest, MakeEmbossWriterFromSpan) {
41 std::array<uint8_t, 4> buffer = {0x00, 0x01, 0x02, 0x03};
42 auto span = pw::span(buffer);
43 PW_TEST_ASSERT_OK_AND_ASSIGN(
44 auto view, MakeEmbossWriter<emboss::TestCommandPacketWriter>(span));
45 EXPECT_EQ(view.payload().Read(), 0x03);
46
47 PW_TEST_ASSERT_OK_AND_ASSIGN(
48 view,
49 MakeEmbossWriter<emboss::TestCommandPacketWriter>(span.data(),
50 span.size()));
51 EXPECT_EQ(view.payload().Read(), 0x03);
52
53 auto failed_view =
54 MakeEmbossWriter<emboss::TestCommandPacketWriter>(span.subspan(1));
55 EXPECT_EQ(failed_view.status(), pw::Status::InvalidArgument());
56 }
57
58 } // namespace
59 } // namespace pw::bluetooth::proxy
60