1 // Copyright 2023 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_sapphire/internal/host/l2cap/fcs.h"
16
17 #include "pw_unit_test/framework.h"
18
19 namespace bt::l2cap {
20 namespace {
21
22 constexpr const char kTestData[] = ""; // Carb-heavy dataset
23 const BufferView kTestBuffer = BufferView(kTestData, sizeof(kTestData) - 1);
24
TEST(FcsTest,EmptyBufferProducesInitialValue)25 TEST(FcsTest, EmptyBufferProducesInitialValue) {
26 EXPECT_EQ(0, ComputeFcs(BufferView()).fcs);
27 EXPECT_EQ(5, ComputeFcs(BufferView(), FrameCheckSequence{5}).fcs);
28 }
29
TEST(FcsTest,FcsOfSimpleValues)30 TEST(FcsTest, FcsOfSimpleValues) {
31 // By inspection, the FCS has value zero if all inputs are 0.
32 EXPECT_EQ(0, ComputeFcs(StaticByteBuffer(0).view()).fcs);
33
34 // If only the "last" bit (i.e. MSb of the message) is set, then the FCS
35 // should equal the generator polynomial because there's exactly one round of
36 // feedback.
37 EXPECT_EQ(0b1010'0000'0000'0001,
38 ComputeFcs(StaticByteBuffer(0b1000'0000).view()).fcs);
39 }
40
TEST(FcsTest,Example1)41 TEST(FcsTest, Example1) {
42 // Core Spec v5.0, Vol 3, Part A, Section 3.3.5, Example 1.
43 const StaticByteBuffer kExample1Data(0x0E,
44 0x00,
45 0x40,
46 0x00,
47 0x02,
48 0x00,
49 0x00,
50 0x01,
51 0x02,
52 0x03,
53 0x04,
54 0x05,
55 0x06,
56 0x07,
57 0x08,
58 0x09);
59 EXPECT_EQ(0x6138, ComputeFcs(kExample1Data.view()).fcs);
60 }
61
TEST(FcsTest,Example2)62 TEST(FcsTest, Example2) {
63 // Core Spec v5.0, Vol 3, Part A, Section 3.3.5, Example 2.
64 const StaticByteBuffer kExample2Data(0x04, 0x00, 0x40, 0x00, 0x01, 0x01);
65 EXPECT_EQ(0x14D4, ComputeFcs(kExample2Data.view()).fcs);
66 }
67
TEST(FcsTest,FcsOfSlicesSameAsFcsOfWhole)68 TEST(FcsTest, FcsOfSlicesSameAsFcsOfWhole) {
69 const FrameCheckSequence whole_fcs = ComputeFcs(kTestBuffer);
70 const auto slice0 = kTestBuffer.view(0, 4);
71 const auto slice1 = kTestBuffer.view(slice0.size());
72 const FrameCheckSequence sliced_fcs = ComputeFcs(slice1, ComputeFcs(slice0));
73 EXPECT_EQ(whole_fcs.fcs, sliced_fcs.fcs);
74 }
75
76 } // namespace
77 } // namespace bt::l2cap
78