1 /*
2 * Copyright (c) 2024-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 "el5_filekey_manager_kit_unittest.h"
17
18 #include "accesstoken_kit.h"
19 #include "user_app_key_info.h"
20 #include "el5_filekey_callback_interface_stub.h"
21 #include "el5_filekey_manager_error.h"
22 #include "el5_filekey_manager_kit.h"
23 #include "token_setproc.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::Security::AccessToken;
27
SetUpTestCase()28 void El5FilekeyManagerKitTest::SetUpTestCase()
29 {
30 }
31
TearDownTestCase()32 void El5FilekeyManagerKitTest::TearDownTestCase()
33 {
34 }
35
SetUp()36 void El5FilekeyManagerKitTest::SetUp()
37 {
38 }
39
TearDown()40 void El5FilekeyManagerKitTest::TearDown()
41 {
42 }
43
44 class TestEl5FilekeyCallback : public El5FilekeyCallbackInterfaceStub {
45 public:
OnRegenerateAppKey(std::vector<AppKeyInfo> & infos)46 OHOS::ErrCode OnRegenerateAppKey(std::vector<AppKeyInfo> &infos)
47 {
48 GTEST_LOG_(INFO) << "OnRegenerateAppKey.";
49 return OHOS::ERR_OK;
50 }
51 };
52
53 /**
54 * @tc.name: AcquireAccess001
55 * @tc.desc: Acquire media type data access without permission.
56 * @tc.type: FUNC
57 * @tc.require: issueI9JGMV
58 */
59 HWTEST_F(El5FilekeyManagerKitTest, AcquireAccess001, TestSize.Level1)
60 {
61 DataLockType type = DataLockType::MEDIA_DATA;
62 ASSERT_EQ(El5FilekeyManagerKit::AcquireAccess(static_cast<DataLockType>(type)), EFM_ERR_NO_PERMISSION);
63 }
64
65 /**
66 * @tc.name: AcquireAccess002
67 * @tc.desc: Acquire all type data access without permission.
68 * @tc.type: FUNC
69 * @tc.require: issueI9JGMV
70 */
71 HWTEST_F(El5FilekeyManagerKitTest, AcquireAccess002, TestSize.Level1)
72 {
73 DataLockType type = DataLockType::ALL_DATA;
74 ASSERT_EQ(El5FilekeyManagerKit::AcquireAccess(static_cast<DataLockType>(type)), EFM_ERR_NO_PERMISSION);
75 }
76
77 /**
78 * @tc.name: ReleaseAccess001
79 * @tc.desc: Release media type data access without permission.
80 * @tc.type: FUNC
81 * @tc.require: issueI9JGMV
82 */
83 HWTEST_F(El5FilekeyManagerKitTest, ReleaseAccess001, TestSize.Level1)
84 {
85 DataLockType type = DataLockType::MEDIA_DATA;
86 ASSERT_EQ(El5FilekeyManagerKit::ReleaseAccess(static_cast<DataLockType>(type)), EFM_ERR_NO_PERMISSION);
87 }
88
89 /**
90 * @tc.name: ReleaseAccess002
91 * @tc.desc: Release all type data access without permission.
92 * @tc.type: FUNC
93 * @tc.require: issueI9JGMV
94 */
95 HWTEST_F(El5FilekeyManagerKitTest, ReleaseAccess002, TestSize.Level1)
96 {
97 DataLockType type = DataLockType::ALL_DATA;
98 ASSERT_EQ(El5FilekeyManagerKit::ReleaseAccess(static_cast<DataLockType>(type)), EFM_ERR_NO_PERMISSION);
99 }
100
101 /**
102 * @tc.name: GenerateAppKey001
103 * @tc.desc: Generate app key by uid and bundle name without permission.
104 * @tc.type: FUNC
105 * @tc.require: issueI9JGMV
106 */
107 HWTEST_F(El5FilekeyManagerKitTest, GenerateAppKey001, TestSize.Level1)
108 {
109 int32_t uid = 10010;
110 std::string bundleName = "com.ohos.systemui";
111 std::string keyId;
112 ASSERT_EQ(El5FilekeyManagerKit::GenerateAppKey(uid, bundleName, keyId), EFM_ERR_NO_PERMISSION);
113 }
114
115 /**
116 * @tc.name: DeleteAppKey001
117 * @tc.desc: Delete app key by bundle name and user id without permission.
118 * @tc.type: FUNC
119 * @tc.require: issueI9JGMV
120 */
121 HWTEST_F(El5FilekeyManagerKitTest, DeleteAppKey001, TestSize.Level1)
122 {
123 std::string bundleName = "";
124 int32_t userId = 100;
125 ASSERT_EQ(El5FilekeyManagerKit::DeleteAppKey(bundleName, userId), EFM_ERR_NO_PERMISSION);
126 }
127
128 /**
129 * @tc.name: GetUserAppKey001
130 * @tc.desc: Find key infos of the specified user id without permission.
131 * @tc.type: FUNC
132 * @tc.require: issueI9JGMV
133 */
134 HWTEST_F(El5FilekeyManagerKitTest, GetUserAppKey001, TestSize.Level1)
135 {
136 int32_t userId = 100;
137 std::vector<std::pair<int32_t, std::string>> keyInfos;
138 ASSERT_EQ(El5FilekeyManagerKit::GetUserAppKey(userId, keyInfos), EFM_ERR_NO_PERMISSION);
139 }
140
141 /**
142 * @tc.name: UserAppKeyInfo001
143 * @tc.desc: interface coverage
144 * @tc.type: FUNC
145 * @tc.require: issueI9JGMV
146 */
147 HWTEST_F(El5FilekeyManagerKitTest, UserAppKeyInfo001, TestSize.Level0)
148 {
149 std::string mockKeyInfo("mockKeyInfo");
150 UserAppKeyInfo src(100, mockKeyInfo);
151 OHOS::Parcel parcel{};
152 EXPECT_TRUE(src.Marshalling(parcel));
153
154 UserAppKeyInfo *dst = UserAppKeyInfo::Unmarshalling(parcel);
155 EXPECT_NE(dst, nullptr);
156 EXPECT_EQ(src.first, dst->first);
157 EXPECT_EQ(src.second, dst->second);
158 delete dst;
159 }
160
161 /**
162 * @tc.name: ChangeUserAppkeysLoadInfo001
163 * @tc.desc: Change key infos of the specified user id without permission.
164 * @tc.type: FUNC
165 * @tc.require: issueI9JGMV
166 */
167 HWTEST_F(El5FilekeyManagerKitTest, ChangeUserAppkeysLoadInfo001, TestSize.Level1)
168 {
169 int32_t userId = 100;
170 std::vector<std::pair<std::string, bool>> loadInfos;
171 loadInfos.emplace_back(std::make_pair("", true));
172 ASSERT_EQ(El5FilekeyManagerKit::ChangeUserAppkeysLoadInfo(userId, loadInfos), EFM_ERR_NO_PERMISSION);
173 }
174
175 /**
176 * @tc.name: SetFilePathPolicy001
177 * @tc.desc: Set path policy without permission.
178 * @tc.type: FUNC
179 * @tc.require: issueI9Q6K2
180 */
181 HWTEST_F(El5FilekeyManagerKitTest, SetFilePathPolicy001, TestSize.Level1)
182 {
183 ASSERT_EQ(El5FilekeyManagerKit::SetFilePathPolicy(), EFM_ERR_NO_PERMISSION);
184 }
185
186 /**
187 * @tc.name: RegisterCallback001
188 * @tc.desc: Register app key generation callback without permission.
189 * @tc.type: FUNC
190 * @tc.require: issueI9Q6K2
191 */
192 HWTEST_F(El5FilekeyManagerKitTest, RegisterCallback001, TestSize.Level1)
193 {
194 ASSERT_NE(El5FilekeyManagerKit::RegisterCallback((new (std::nothrow) TestEl5FilekeyCallback())), EFM_SUCCESS);
195 }
196
197 /**
198 * @tc.name: GetUserAllAppKey001
199 * @tc.desc: GetUserAllAppKey function test without permission.
200 * @tc.type: FUNC
201 * @tc.require: issueI9Q6K2
202 */
203 HWTEST_F(El5FilekeyManagerKitTest, GetUserAllAppKey001, TestSize.Level1)
204 {
205 int32_t userId = 100;
206 std::vector<std::pair<int32_t, std::string>> keyInfos;
207 keyInfos.emplace_back(std::make_pair(100, ""));
208 ASSERT_EQ(El5FilekeyManagerKit::GetUserAllAppKey(userId, keyInfos), EFM_ERR_NO_PERMISSION);
209 }
210
211 /**
212 * @tc.name: GenerateGroupIDKey001
213 * @tc.desc: Generate data group key by userId and group id without permission.
214 * @tc.type: FUNC
215 * @tc.require: issueI9JGMV
216 */
217 HWTEST_F(El5FilekeyManagerKitTest, GenerateGroupIDKey001, TestSize.Level1)
218 {
219 uint32_t uid = 100;
220 std::string groupID = "abcdefghijklmn";
221 std::string keyId;
222 ASSERT_EQ(El5FilekeyManagerKit::GenerateGroupIDKey(uid, groupID, keyId), EFM_ERR_NO_PERMISSION);
223 }
224
225 /**
226 * @tc.name: DeleteGroupIDKey001
227 * @tc.desc: Delete data group key by user id and group id without permission.
228 * @tc.type: FUNC
229 * @tc.require: issueI9JGMV
230 */
231 HWTEST_F(El5FilekeyManagerKitTest, DeleteGroupIDKey001, TestSize.Level1)
232 {
233 uint32_t uid = 100;
234 std::string groupID = "";
235 ASSERT_EQ(El5FilekeyManagerKit::DeleteGroupIDKey(uid, groupID), EFM_ERR_NO_PERMISSION);
236 }
237
238 /**
239 * @tc.name: QueryAppKeyState001
240 * @tc.desc: Query media type app key without permission.
241 * @tc.type: FUNC
242 * @tc.require: issueI9JGMV
243 */
244 HWTEST_F(El5FilekeyManagerKitTest, QueryAppKeyState001, TestSize.Level1)
245 {
246 DataLockType type = DataLockType::MEDIA_DATA;
247 ASSERT_EQ(El5FilekeyManagerKit::QueryAppKeyState(static_cast<DataLockType>(type)), EFM_ERR_NO_PERMISSION);
248 }
249
250 /**
251 * @tc.name: QueryAppKeyState002
252 * @tc.desc: Query all type app key without permission.
253 * @tc.type: FUNC
254 * @tc.require: issueI9JGMV
255 */
256 HWTEST_F(El5FilekeyManagerKitTest, QueryAppKeyState002, TestSize.Level1)
257 {
258 DataLockType type = DataLockType::ALL_DATA;
259 ASSERT_EQ(El5FilekeyManagerKit::QueryAppKeyState(static_cast<DataLockType>(type)), EFM_ERR_NO_PERMISSION);
260 }
261