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 "dm_log.h"
17 #include "dm_anonymous.h"
18 #include "dm_constants.h"
19 #include "dm_auth_message_processor.h"
20 #include "dm_auth_context.h"
21 #include "dm_auth_state_machine.h"
22 #include "UTTest_dm_auth_message_processor.h"
23
24 using namespace testing;
25 namespace OHOS {
26 namespace DistributedHardware {
27
SetUpTestCase()28 void DmAuthMessageProcessorTest::SetUpTestCase()
29 {
30 }
31
TearDownTestCase()32 void DmAuthMessageProcessorTest::TearDownTestCase()
33 {
34 }
35
SetUp()36 void DmAuthMessageProcessorTest::SetUp()
37 {
38 }
39
TearDown()40 void DmAuthMessageProcessorTest::TearDown()
41 {
42 }
43
44 HWTEST_F(DmAuthMessageProcessorTest, CreateMessageForwardUltrasonicStart_001, testing::ext::TestSize.Level1)
45 {
46 JsonObject json;
47 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
48 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
49 EXPECT_EQ(processor->CreateMessageForwardUltrasonicStart(context, json), DM_OK);
50 }
51
52 HWTEST_F(DmAuthMessageProcessorTest, CreateMessageReverseUltrasonicDone_001, testing::ext::TestSize.Level1)
53 {
54 JsonObject json;
55 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
56 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
57 EXPECT_EQ(processor->CreateMessageReverseUltrasonicDone(context, json), DM_OK);
58 }
59
60 HWTEST_F(DmAuthMessageProcessorTest, CreateMessageReverseUltrasonicStart_001, testing::ext::TestSize.Level1)
61 {
62 JsonObject json;
63 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
64 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
65 EXPECT_EQ(processor->CreateMessageReverseUltrasonicStart(context, json), DM_OK);
66 }
67
68 HWTEST_F(DmAuthMessageProcessorTest, CreateMessageForwardUltrasonicNegotiate_001, testing::ext::TestSize.Level1)
69 {
70 JsonObject json;
71 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
72 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
73 EXPECT_EQ(processor->CreateMessageForwardUltrasonicNegotiate(context, json), DM_OK);
74 }
75
76 HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReverseUltrasonicStart_001, testing::ext::TestSize.Level1)
77 {
78 JsonObject json;
79 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
80 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
81 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
82 EXPECT_EQ(processor->ParseMessageReverseUltrasonicStart(json, context), DM_OK);
83 }
84
85 HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReverseUltrasonicDone_001, testing::ext::TestSize.Level1)
86 {
87 JsonObject json;
88 json["REPLY"] = "1";
89 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
90 context->reply = DM_OK;
91 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
92 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
93 EXPECT_EQ(processor->ParseMessageReverseUltrasonicDone(json, context), DM_OK);
94 }
95
96 HWTEST_F(DmAuthMessageProcessorTest, ParseMessageForwardUltrasonicStart_001, testing::ext::TestSize.Level1)
97 {
98 JsonObject json;
99 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
100 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
101 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
102 EXPECT_EQ(processor->ParseMessageForwardUltrasonicStart(json, context), DM_OK);
103 }
104
105 HWTEST_F(DmAuthMessageProcessorTest, ParseMessageForwardUltrasonicNegotiate_001, testing::ext::TestSize.Level1)
106 {
107 JsonObject json;
108 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
109 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
110 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
111 EXPECT_EQ(processor->ParseMessageForwardUltrasonicNegotiate(json, context), DM_OK);
112 }
113
114 HWTEST_F(DmAuthMessageProcessorTest, ParseNegotiateMessage_001, testing::ext::TestSize.Level1)
115 {
116 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
117 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
118 JsonObject jsonObject;
119 jsonObject[DM_TAG_LOGICAL_SESSION_ID] = 12345;
120
121 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
122 int32_t ret = processor->ParseNegotiateMessage(jsonObject, context);
123
124 EXPECT_EQ(ret, DM_OK);
125 EXPECT_EQ(context->logicalSessionId, 12345);
126 EXPECT_EQ(context->requestId, 12345);
127 }
128
129 HWTEST_F(DmAuthMessageProcessorTest, ParseNegotiateMessage_002, testing::ext::TestSize.Level1)
130 {
131 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
132 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
133 JsonObject jsonObject;
134 jsonObject[TAG_PEER_PKG_NAME] = "testPkgName";
135
136 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
137 int32_t ret = processor->ParseNegotiateMessage(jsonObject, context);
138
139 EXPECT_EQ(ret, DM_OK);
140 EXPECT_EQ(context->accessee.pkgName, "testPkgName");
141 }
142
143 HWTEST_F(DmAuthMessageProcessorTest, ParseNegotiateMessage_003, testing::ext::TestSize.Level1)
144 {
145 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
146 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
147 JsonObject jsonObject;
148 jsonObject[TAG_PEER_BUNDLE_NAME_V2] = "testBundleName";
149
150 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
151 int32_t ret = processor->ParseNegotiateMessage(jsonObject, context);
152
153 EXPECT_EQ(ret, DM_OK);
154 EXPECT_EQ(context->accessee.bundleName, "testBundleName");
155 }
156
157 HWTEST_F(DmAuthMessageProcessorTest, ParseNegotiateMessage_004, testing::ext::TestSize.Level1)
158 {
159 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
160 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
161 JsonObject jsonObject;
162 jsonObject[TAG_PEER_DISPLAY_ID] = 123;
163
164 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
165 int32_t ret = processor->ParseNegotiateMessage(jsonObject, context);
166
167 EXPECT_EQ(ret, DM_OK);
168 EXPECT_EQ(context->accessee.displayId, 123);
169 }
170
171 HWTEST_F(DmAuthMessageProcessorTest, ParseNegotiateMessage_005, testing::ext::TestSize.Level1)
172 {
173 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
174 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
175 JsonObject jsonObject;
176 jsonObject[TAG_HOST_PKGLABEL] = "testPkgLabel";
177
178 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
179 int32_t ret = processor->ParseNegotiateMessage(jsonObject, context);
180
181 EXPECT_EQ(ret, DM_OK);
182 EXPECT_EQ(context->pkgLabel, "testPkgLabel");
183 }
184
185 HWTEST_F(DmAuthMessageProcessorTest, ParseNegotiateMessage_006, testing::ext::TestSize.Level1)
186 {
187 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
188 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
189 JsonObject jsonObject;
190 jsonObject[DM_BUSINESS_ID] = "testBusinessId";
191
192 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
193 int32_t ret = processor->ParseNegotiateMessage(jsonObject, context);
194
195 EXPECT_EQ(ret, DM_OK);
196 EXPECT_EQ(context->businessId, "testBusinessId");
197 }
198
199 HWTEST_F(DmAuthMessageProcessorTest, ParseNegotiateMessage_007, testing::ext::TestSize.Level1)
200 {
201 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
202 context->authStateMachine = std::make_shared<DmAuthStateMachine>(context);
203 std::shared_ptr<DmAuthMessageProcessor> processor = std::make_shared<DmAuthMessageProcessor>();
204 JsonObject jsonObj;
205 jsonObj["ultrasonicSide"] = 0;
206 int32_t ret = processor->ParseNegotiateMessage(jsonObj, context);
207 jsonObj["ultrasonicSide"] = 1;
208 ret = processor->ParseNegotiateMessage(jsonObj, context);
209
210 EXPECT_EQ(ret, DM_OK);
211 EXPECT_EQ(context->accessee.extraInfo.empty(), false);
212 }
213 } // namespace DistributedHardware
214 } // namespace OHOS
215