1 /*
2 * Copyright (c) 2023 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 <sys/stat.h>
18 #include <sys/types.h>
19 #include <string>
20 #include <vector>
21 #include <iostream>
22
23 #include "ipc_utils.h"
24 #include "distributed_device_profile_constants.h"
25 #include "distributed_device_profile_log.h"
26 #include "distributed_device_profile_errors.h"
27
28 namespace OHOS {
29 namespace DistributedDeviceProfile {
30 using namespace testing::ext;
31 using namespace std;
32 namespace {
33 const std::string TAG = "IpcUtilsTest";
34 }
35 class IpcUtilsTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 };
42
SetUpTestCase()43 void IpcUtilsTest::SetUpTestCase()
44 {
45 }
46
TearDownTestCase()47 void IpcUtilsTest::TearDownTestCase()
48 {
49 }
50
SetUp()51 void IpcUtilsTest::SetUp()
52 {
53 }
54
TearDown()55 void IpcUtilsTest::TearDown()
56 {
57 }
58
59 /*
60 * @tc.name: Marshalling_001
61 * @tc.desc: Normal testCase of IpcUtilsTest for CRUD
62 * @tc.type: FUNC
63 */
64 HWTEST_F(IpcUtilsTest, Marshalling_001, TestSize.Level1)
65 {
66 OHOS::MessageParcel parcel;
67 std::vector<TrustDeviceProfile> profiles;
68 bool ret = IpcUtils::Marshalling(parcel, profiles);
69 EXPECT_EQ(ret, false);
70 ret = IpcUtils::UnMarshalling(parcel, profiles);
71 EXPECT_EQ(ret, false);
72 TrustDeviceProfile trustProfile;
73 profiles.push_back(trustProfile);
74 ret = IpcUtils::Marshalling(parcel, profiles);
75 EXPECT_EQ(ret, true);
76 ret = IpcUtils::UnMarshalling(parcel, profiles);
77 EXPECT_EQ(ret, true);
78 }
79
80 /*
81 * @tc.name: Marshalling_002
82 * @tc.desc: Normal testCase of IpcUtilsTest for CRUD
83 * @tc.type: FUNC
84 */
85 HWTEST_F(IpcUtilsTest, Marshalling_002, TestSize.Level1)
86 {
87 OHOS::MessageParcel parcel;
88 std::vector<AccessControlProfile> profiles;
89 bool ret = IpcUtils::Marshalling(parcel, profiles);
90 EXPECT_EQ(ret, false);
91 ret = IpcUtils::UnMarshalling(parcel, profiles);
92 EXPECT_EQ(ret, false);
93 AccessControlProfile aclProfile;
94 profiles.push_back(aclProfile);
95 ret = IpcUtils::Marshalling(parcel, profiles);
96 EXPECT_EQ(ret, true);
97 ret = IpcUtils::UnMarshalling(parcel, profiles);
98 EXPECT_EQ(ret, true);
99 }
100
101 /*
102 * @tc.name: Marshalling_003
103 * @tc.desc: Normal testCase of IpcUtilsTest for CRUD
104 * @tc.type: FUNC
105 */
106 HWTEST_F(IpcUtilsTest, Marshalling_003, TestSize.Level1)
107 {
108 OHOS::MessageParcel parcel;
109 std::vector<ServiceProfile> profiles;
110 bool ret = IpcUtils::Marshalling(parcel, profiles);
111 EXPECT_EQ(ret, false);
112 ret = IpcUtils::UnMarshalling(parcel, profiles);
113 EXPECT_EQ(ret, false);
114 ServiceProfile serviceProfile;
115 profiles.push_back(serviceProfile);
116 ret = IpcUtils::Marshalling(parcel, profiles);
117 EXPECT_EQ(ret, true);
118 ret = IpcUtils::UnMarshalling(parcel, profiles);
119 EXPECT_EQ(ret, true);
120 }
121
122 /*
123 * @tc.name: Marshalling_004
124 * @tc.desc: Normal testCase of IpcUtilsTest for CRUD
125 * @tc.type: FUNC
126 */
127 HWTEST_F(IpcUtilsTest, Marshalling_004, TestSize.Level1)
128 {
129 OHOS::MessageParcel parcel;
130 std::vector<CharacteristicProfile> profiles;
131 bool ret = IpcUtils::Marshalling(parcel, profiles);
132 EXPECT_EQ(ret, false);
133 ret = IpcUtils::UnMarshalling(parcel, profiles);
134 EXPECT_EQ(ret, false);
135 CharacteristicProfile chacProfile;
136 profiles.push_back(chacProfile);
137 ret = IpcUtils::Marshalling(parcel, profiles);
138 EXPECT_EQ(ret, true);
139 ret = IpcUtils::UnMarshalling(parcel, profiles);
140 EXPECT_EQ(ret, true);
141 }
142
143 /*
144 * @tc.name: Marshalling_005
145 * @tc.desc: Normal testCase of IpcUtilsTest for CRUD
146 * @tc.type: FUNC
147 */
148 HWTEST_F(IpcUtilsTest, Marshalling_005, TestSize.Level1)
149 {
150 OHOS::MessageParcel parcel;
151 std::map<std::string, std::string> params;
152 bool ret = IpcUtils::Marshalling(parcel, params);
153 EXPECT_EQ(ret, false);
154 ret = IpcUtils::UnMarshalling(parcel, params);
155 EXPECT_EQ(ret, false);
156 params.insert({"userId", "u1"});
157 ret = IpcUtils::Marshalling(parcel, params);
158 EXPECT_EQ(ret, true);
159 ret = IpcUtils::UnMarshalling(parcel, params);
160 EXPECT_EQ(ret, true);
161 }
162
163 /*
164 * @tc.name: Marshalling_006
165 * @tc.desc: Normal testCase of IpcUtilsTest for CRUD
166 * @tc.type: FUNC
167 */
168 HWTEST_F(IpcUtilsTest, Marshalling_006, TestSize.Level1)
169 {
170 OHOS::MessageParcel parcel;
171 std::map<std::string, OHOS::DistributedDeviceProfile::SubscribeInfo> listenerMap;
172 bool ret = IpcUtils::Marshalling(parcel, listenerMap);
173 EXPECT_EQ(ret, false);
174 ret = IpcUtils::UnMarshalling(parcel, listenerMap);
175 EXPECT_EQ(ret, false);
176 SubscribeInfo subscribeInfo;
177 listenerMap.insert({"12345", subscribeInfo});
178 ret = IpcUtils::Marshalling(parcel, listenerMap);
179 EXPECT_EQ(ret, false);
180 ret = IpcUtils::UnMarshalling(parcel, listenerMap);
181 EXPECT_EQ(ret, false);
182 }
183
184 /*
185 * @tc.name: Marshalling_007
186 * @tc.desc: Normal testCase of IpcUtilsTest for CRUD
187 * @tc.type: FUNC
188 */
189 HWTEST_F(IpcUtilsTest, Marshalling_007, TestSize.Level1)
190 {
191 OHOS::MessageParcel parcel;
192 std::unordered_set<ProfileChangeType> changeTypes;
193 bool ret = IpcUtils::Marshalling(parcel, changeTypes);
194 EXPECT_EQ(ret, false);
195 ret = IpcUtils::UnMarshalling(parcel, changeTypes);
196 EXPECT_EQ(ret, false);
197 changeTypes.insert(ProfileChangeType::TRUST_DEVICE_PROFILE_ADD);
198 changeTypes.insert(ProfileChangeType::TRUST_DEVICE_PROFILE_UPDATE);
199 changeTypes.insert(ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE);
200 ret = IpcUtils::Marshalling(parcel, changeTypes);
201 EXPECT_EQ(ret, true);
202 ret = IpcUtils::UnMarshalling(parcel, changeTypes);
203 EXPECT_EQ(ret, true);
204 }
205 } // namespace DistributedDeviceProfile
206 } // namespace OHOS
207