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 ErrCode ret = plugin.OnSetPolicy(policies);
62 ASSERT_TRUE(ret == ERR_OK);
63 }
64
65 /**
66 * @tc.name: TestOnSetPolicyUserIdEmpty
67 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when user id value is empty.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyUserIdEmpty, TestSize.Level1)
71 {
72 DisallowAddOsAccountByUserPlugin plugin;
73 std::map<std::string, std::string> policies;
74 policies.insert(std::make_pair("", "true"));
75 ErrCode ret = plugin.OnSetPolicy(policies);
76 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
77 }
78
79 /**
80 * @tc.name: TestOnSetPolicyUserIdUnavailable
81 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when user id value is unavailable.
82 * @tc.type: FUNC
83 */
84 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyUserIdUnavailable, TestSize.Level1)
85 {
86 DisallowAddOsAccountByUserPlugin plugin;
87 std::map<std::string, std::string> policies;
88 policies.insert(std::make_pair(STR_UNAVAIL_USER_ID, "true"));
89 ErrCode ret = plugin.OnSetPolicy(policies);
90 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
91 }
92
93 /**
94 * @tc.name: TestOnSetPolicyTrue
95 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when value is true.
96 * @tc.type: FUNC
97 */
98 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyTrue, TestSize.Level1)
99 {
100 DisallowAddOsAccountByUserPlugin plugin;
101 std::map<std::string, std::string> policies;
102 policies.insert(std::make_pair(STR_DEFAULT_USER_ID, "true"));
103 ErrCode ret = plugin.OnSetPolicy(policies);
104 ASSERT_TRUE(ret == ERR_OK);
105 }
106
107 /**
108 * @tc.name: TestOnSetPolicyFalse
109 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnSetPolicy function when value is false.
110 * @tc.type: FUNC
111 */
112 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnSetPolicyFalse, TestSize.Level1)
113 {
114 DisallowAddOsAccountByUserPlugin plugin;
115 std::map<std::string, std::string> policies;
116 policies.insert(std::make_pair(STR_DEFAULT_USER_ID, "false"));
117 ErrCode ret = plugin.OnSetPolicy(policies);
118 ASSERT_TRUE(ret == ERR_OK);
119 }
120
121 /**
122 * @tc.name: TestOnGetPolicy
123 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnGetPolicy function when policy is read write.
124 * @tc.type: FUNC
125 */
126 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnGetPolicy, TestSize.Level1)
127 {
128 MessageParcel data;
129 data.WriteInt32(DEFAULT_USER_ID);
130 MessageParcel reply;
131 std::shared_ptr<IPlugin> plugin = DisallowAddOsAccountByUserPlugin::GetPlugin();
132 std::string policyData{""};
133 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
134 ASSERT_TRUE(ret == ERR_OK);
135 ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
136 ASSERT_FALSE(reply.ReadBool());
137 }
138
139 /**
140 * @tc.name: TestOnGetPolicyUserIdUnavailable
141 * @tc.desc: Test DisallowAddOsAccountByUserPlugin::OnGetPolicy function when user id is unavailable.
142 * @tc.type: FUNC
143 */
144 HWTEST_F(DisallowAddOsAccountByUserPluginTest, TestOnGetPolicyUserIdUnavailable, TestSize.Level1)
145 {
146 MessageParcel data;
147 data.WriteInt32(UNAVAIL_USER_ID);
148 MessageParcel reply;
149 std::shared_ptr<IPlugin> plugin = DisallowAddOsAccountByUserPlugin::GetPlugin();
150 std::string policyData{""};
151 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
152 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
153 ASSERT_TRUE(reply.ReadInt32() == EdmReturnErrCode::PARAM_ERROR);
154 }
155 } // namespace TEST
156 } // namespace EDM
157 } // namespace OHOS