1 /*
2 * Copyright (c) 2024 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 "disallow_add_os_account_by_user_plugin.h"
17
18 #include <gtest/gtest.h>
19
20 #include "edm_ipc_interface_code.h"
21 #include "utils.h"
22
23 using namespace testing::ext;
24 using namespace testing;
25
26 namespace OHOS {
27 namespace EDM {
28 namespace TEST {
29 const std::string STR_DEFAULT_USER_ID = "100";
30 const std::string STR_UNAVAIL_USER_ID = "9876";
31 const int32_t UNAVAIL_USER_ID = 9876;
32
33 class DisallowAddOsAccountByUserPluginTest : public testing::Test {
34 protected:
35 static void SetUpTestSuite(void);
36
37 static void TearDownTestSuite(void);
38 };
39
SetUpTestSuite(void)40 void DisallowAddOsAccountByUserPluginTest::SetUpTestSuite(void)
41 {
42 Utils::SetEdmInitialEnv();
43 }
44
TearDownTestSuite(void)45 void DisallowAddOsAccountByUserPluginTest::TearDownTestSuite(void)
46 {
47 Utils::ResetTokenTypeAndUid();
48 ASSERT_TRUE(Utils::IsOriginalUTEnv());
49 std::cout << "now ut process is original ut env : " << Utils::IsOriginalUTEnv() << std::endl;
50 }
51
52 /**
53 * @tc.name: TestOnSetPolicyEmpty
54 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when policy is empty.
55 * @tc.type: FUNC
56 */
57 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyEmpty, TestSize.Level1)
58 {
59 DisallowAddOsAccountByUserPlugin plugin;
60 std::map<std::string, std::string> policies;
61 std::map<std::string, std::string> currentData;
62 std::map<std::string, std::string> mergeData;
63 ErrCode ret = plugin.OnSetPolicy(policies, currentData, mergeData, DEFAULT_USER_ID);
64 ASSERT_TRUE(ret == ERR_OK);
65 }
66
67 /**
68 * @tc.name: TestOnSetPolicyUserIdEmpty
69 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when user id value is empty.
70 * @tc.type: FUNC
71 */
72 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyUserIdEmpty, TestSize.Level1)
73 {
74 DisallowAddOsAccountByUserPlugin plugin;
75 std::map<std::string, std::string> policies;
76 policies.insert(std::make_pair("", "true"));
77 std::map<std::string, std::string> currentData;
78 std::map<std::string, std::string> mergeData;
79 ErrCode ret = plugin.OnSetPolicy(policies, currentData, mergeData, DEFAULT_USER_ID);
80 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
81 }
82
83 /**
84 * @tc.name: TestOnSetPolicyUserIdUnavailable
85 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when user id value is unavailable.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyUserIdUnavailable, TestSize.Level1)
89 {
90 DisallowAddOsAccountByUserPlugin plugin;
91 std::map<std::string, std::string> policies;
92 policies.insert(std::make_pair(STR_UNAVAIL_USER_ID, "true"));
93 std::map<std::string, std::string> currentData;
94 std::map<std::string, std::string> mergeData;
95 ErrCode ret = plugin.OnSetPolicy(policies, currentData, mergeData, DEFAULT_USER_ID);
96 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
97 }
98
99 /**
100 * @tc.name: TestOnSetPolicyTrue
101 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when value is true.
102 * @tc.type: FUNC
103 */
104 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyTrue, TestSize.Level1)
105 {
106 DisallowAddOsAccountByUserPlugin plugin;
107 std::map<std::string, std::string> policies;
108 policies.insert(std::make_pair(STR_DEFAULT_USER_ID, "true"));
109 std::map<std::string, std::string> currentData;
110 std::map<std::string, std::string> mergeData;
111 ErrCode ret = plugin.OnSetPolicy(policies, currentData, mergeData, DEFAULT_USER_ID);
112 ASSERT_TRUE(ret == ERR_OK);
113 }
114
115 /**
116 * @tc.name: TestOnSetPolicyFalse
117 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when value is false.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyFalse, TestSize.Level1)
121 {
122 DisallowAddOsAccountByUserPlugin plugin;
123 std::map<std::string, std::string> policies;
124 policies.insert(std::make_pair(STR_DEFAULT_USER_ID, "false"));
125 std::map<std::string, std::string> currentData;
126 std::map<std::string, std::string> mergeData;
127 ErrCode ret = plugin.OnSetPolicy(policies, currentData, mergeData, DEFAULT_USER_ID);
128 ASSERT_TRUE(ret == ERR_OK);
129 }
130
131 /**
132 * @tc.name: TestOnGetPolicy
133 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnGetPolicy function when policy is read write.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnGetPolicy, TestSize.Level1)
137 {
138 MessageParcel data;
139 data.WriteInt32(DEFAULT_USER_ID);
140 MessageParcel reply;
141 std::shared_ptr<IPlugin> plugin = DisallowAddOsAccountByUserPlugin::GetPlugin();
142 std::string policyData{""};
143 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
144 ASSERT_TRUE(ret == ERR_OK);
145 ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
146 ASSERT_FALSE(reply.ReadBool());
147 }
148
149 /**
150 * @tc.name: TestOnGetPolicyUserIdUnavailable
151 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnGetPolicy function when user id is unavailable.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnGetPolicyUserIdUnavailable, TestSize.Level1)
155 {
156 MessageParcel data;
157 data.WriteInt32(UNAVAIL_USER_ID);
158 MessageParcel reply;
159 std::shared_ptr<IPlugin> plugin = DisallowAddOsAccountByUserPlugin::GetPlugin();
160 std::string policyData{""};
161 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
162 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
163 ASSERT_TRUE(reply.ReadInt32() == EdmReturnErrCode::PARAM_ERROR);
164 }
165 } // namespace TEST
166 } // namespace EDM
167 } // namespace OHOS