1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://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,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "mc_protocol_convertor_test.h"
18
19 using namespace testing;
20 using namespace testing::ext;
21
22 namespace OHOS {
23 namespace MechBodyController {
24 namespace {
25 const std::string TAG = "ProtocolConverter";
26 constexpr size_t MAX_DATA_SIZE = 251;
27 constexpr size_t MAX_SERVICE_NAME_SIZE = 0xFF;
28 constexpr size_t BT_HEADER_LENTH = 8;
29 constexpr size_t BT_SERVICE_HEADER_LENTH = 4;
30 constexpr size_t BT_COMMAND_HEADER_LENTH = 2;
31 constexpr int32_t BUF_MIN_LEN = 8;
32 }
33
SetUp()34 void ProtocolConverterTest::SetUp()
35 {
36 converter_ = std::make_unique<ProtocolConverter>();
37 }
38
TearDown()39 void ProtocolConverterTest::TearDown()
40 {
41 converter_ = nullptr;
42 }
43
SetUpTestCase()44 void ProtocolConverterTest::SetUpTestCase() {}
45
TearDownTestCase()46 void ProtocolConverterTest::TearDownTestCase() {}
47
48 /**
49 * @tc.name : Convert_ShouldReturnNull_WhenInputDataIsNull
50 * @tc.number: Convert_Test_001
51 * @tc.desc : Test the Convert function when the input data is nullptr.
52 */
53 HWTEST_F(ProtocolConverterTest, Convert_ShouldReturnNull_WhenInputDataIsNull, TestSize.Level1)
54 {
55 std::shared_ptr<MechDataBuffer> data = nullptr;
56 auto result = converter_->Convert(OptType::GET, 0, data);
57 EXPECT_EQ(result, nullptr);
58 }
59
60 /**
61 * @tc.name : Convert_ShouldReturnNull_WhenDataSizeExceedsMax
62 * @tc.number: Convert_Test_002
63 * @tc.desc : Test the Convert function when the input data size exceeds MAX_DATA_SIZE.
64 */
65 HWTEST_F(ProtocolConverterTest, Convert_ShouldReturnNull_WhenDataSizeExceedsMax, TestSize.Level1)
66 {
67 auto data = std::make_shared<MechDataBuffer>(MAX_DATA_SIZE + 1);
68 auto result = converter_->Convert(OptType::GET, 0, data);
69 EXPECT_EQ(result, nullptr);
70 }
71
72 /**
73 * @tc.name : Convert_ShouldReturnNull_WhenServiceNameSizeExceedsMax
74 * @tc.number: Convert_Test_003
75 * @tc.desc : Test the Convert function when the service name size exceeds MAX_SERVICE_NAME_SIZE.
76 */
77 HWTEST_F(ProtocolConverterTest, Convert_ShouldReturnNull_WhenServiceNameSizeExceedsMax, TestSize.Level1)
78 {
79 converter_->Convert(OptType::GET, 0, std::make_shared<MechDataBuffer>(1), "a");
80 auto result = converter_->Convert(OptType::GET, 0, std::make_shared<MechDataBuffer>(1),
81 std::string(MAX_SERVICE_NAME_SIZE + 1, 'a'));
82 EXPECT_EQ(result, nullptr);
83 }
84
85 /**
86 * @tc.name : Convert_ShouldReturnValidData_WhenInputIsValid
87 * @tc.number: Convert_Test_004
88 * @tc.desc : Test the Convert function when the input data is valid.
89 */
90 HWTEST_F(ProtocolConverterTest, Convert_ShouldReturnValidData_WhenInputIsValid, TestSize.Level1)
91 {
92 auto data = std::make_shared<MechDataBuffer>(10);
93 data->AppendFloat(0.0f);
94 auto result = converter_->Convert(OptType::GET, 0, data);
95 EXPECT_NE(result, nullptr);
96 EXPECT_EQ(result->Size(), BT_HEADER_LENTH + BT_SERVICE_HEADER_LENTH + converter_->serviceName_.size() + 0 +
97 BT_COMMAND_HEADER_LENTH + sizeof(float));
98 }
99
100 /**
101 * @tc.name : Convert_ShouldReturnNull_WhenProtocolSizeSizeExceedsMax
102 * @tc.number: Convert_Test_005
103 * @tc.desc : Test the Convert function when the service name size exceeds MAX_SERVICE_NAME_SIZE.
104 */
105 HWTEST_F(ProtocolConverterTest, Convert_ShouldReturnNull_WhenProtocolSizeSizeExceedsMax, TestSize.Level1)
106 {
107 converter_->Convert(OptType::GET, 0, std::make_shared<MechDataBuffer>(1), "a");
108 auto result = converter_->Convert(OptType::GET, 0, std::make_shared<MechDataBuffer>(1),
109 std::string(MAX_SERVICE_NAME_SIZE - 1, 'a'));
110 EXPECT_EQ(result, nullptr);
111 }
112
113 /**
114 * @tc.name : GetData_ShouldReturnValidData_WhenInputIsValid
115 * @tc.number: GetData_Test_001
116 * @tc.desc : Test the GetData function when the input data is valid.
117 */
118 HWTEST_F(ProtocolConverterTest, GetData_ShouldReturnValidData_WhenInputIsValid, TestSize.Level1)
119 {
120 auto data = std::make_shared<MechDataBuffer>(sizeof(float));
121 ASSERT_NE(data, nullptr);
122 data->AppendFloat(0.0f);
123
124 auto pclData = converter_->Convert(OptType::GET, 0, data, "aa");
125 uint16_t seqNo = 0;
126 bool isAck = false;
127 auto result = converter_->GetData(pclData, seqNo, isAck);
128 EXPECT_NE(result, nullptr);
129 EXPECT_EQ(result->Size(), data->Size() + BT_COMMAND_HEADER_LENTH);
130 EXPECT_EQ(seqNo, 0);
131 EXPECT_EQ(isAck, false);
132 }
133
134 /**
135 * @tc.name : GetData_ShouldReturnNull_WhenInputDataIsNull
136 * @tc.number: GetData_Test_002
137 * @tc.desc : Test the GetData function when the input data is nullptr.
138 */
139 HWTEST_F(ProtocolConverterTest, GetData_ShouldReturnNull_WhenInputDataIsNull, TestSize.Level1)
140 {
141 std::shared_ptr<MechDataBuffer> pclData = nullptr;
142 uint16_t seqNo = 0;
143 bool isAck = false;
144 auto result = converter_->GetData(pclData, seqNo, isAck);
145 EXPECT_EQ(result, nullptr);
146 }
147
148 /**
149 * @tc.name : Validate_ShouldReturnFalse_001
150 * @tc.number: Validate_Test_001
151 * @tc.desc : Test the Validate function when the input data is nullptr.
152 */
153 HWTEST_F(ProtocolConverterTest, Validate_ShouldReturnFalse_001, TestSize.Level1)
154 {
155 uint8_t* data = nullptr;
156 size_t length = 0;
157 auto result = converter_->Validate(data, length);
158 EXPECT_EQ(result, false);
159 }
160
161 /**
162 * @tc.name : Validate_ShouldReturnFalse_002
163 * @tc.number: Validate_Test_002
164 * @tc.desc : Test the Validate function when the input data is nullptr.
165 */
166 HWTEST_F(ProtocolConverterTest, Validate_ShouldReturnFalse_002, TestSize.Level1)
167 {
168 uint8_t data[BUF_MIN_LEN + 1] = {0};
169 size_t length = 0;
170 auto result = converter_->Validate(data, length);
171 EXPECT_EQ(result, false);
172 }
173
174 /**
175 * @tc.name : Validate_ShouldReturnFalse_003
176 * @tc.number: Validate_Test_003
177 * @tc.desc : Test the Validate function when the input data is nullptr.
178 */
179 HWTEST_F(ProtocolConverterTest, Validate_ShouldReturnFalse_003, TestSize.Level1)
180 {
181 uint8_t data[BUF_MIN_LEN + 1] = {0};
182 size_t length = MAX_DATA_SIZE + 1;
183 auto result = converter_->Validate(data, length);
184 EXPECT_EQ(result, false);
185 }
186
187 /**
188 * @tc.name : Validate_ShouldReturnTrue_001
189 * @tc.number: Validate_Test_004
190 * @tc.desc : Test the Validate function when the input data is nullptr.
191 */
192 HWTEST_F(ProtocolConverterTest, Validate_ShouldReturnTrue_001, TestSize.Level1)
193 {
194 uint8_t data[BUF_MIN_LEN + 1] = {0};
195 size_t length = MAX_DATA_SIZE - 1;
196 auto result = converter_->Validate(data, length);
197 EXPECT_EQ(result, true);
198 }
199 } // namespace MechBodyController
200 } // namespace OHOS
201