1 /*
2  * Copyright (c) 2022 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 
18 #include "auth_level.h"
19 #include "idm_common.h"
20 #include "pool.h"
21 
22 extern "C" {
23     extern LinkedList *g_poolList;
24     extern LinkedList *g_userInfoList;
25     extern void DestroyExecutorInfo(void *data);
26     extern ResultCode QueryScheduleAsl(const CoAuthSchedule *coAuthSchedule, uint32_t *asl);
27     extern ResultCode GetExecutorAslAndAcl(uint32_t authType, uint32_t *asl, uint32_t *acl);
28 }
29 
30 namespace OHOS {
31 namespace UserIam {
32 namespace UserAuth {
33 using namespace testing;
34 using namespace testing::ext;
35 
36 class AuthLevelTest : public testing::Test {
37 public:
SetUpTestCase()38     static void SetUpTestCase() {};
39 
TearDownTestCase()40     static void TearDownTestCase() {};
41 
SetUp()42     void SetUp() {};
43 
TearDown()44     void TearDown() {};
45 };
46 
47 HWTEST_F(AuthLevelTest, TestGetAtl, TestSize.Level0)
48 {
49     EXPECT_EQ(GetAtl(0, 0), 0);
50 }
51 
52 HWTEST_F(AuthLevelTest, TestQueryScheduleAsl_001, TestSize.Level0)
53 {
54     EXPECT_EQ(QueryScheduleAsl(nullptr, nullptr), RESULT_BAD_PARAM);
55     CoAuthSchedule schedule = {};
56     EXPECT_EQ(QueryScheduleAsl(&schedule, nullptr), RESULT_BAD_PARAM);
57     uint32_t asl = 0;
58     schedule.executorSize = 0;
59     EXPECT_EQ(QueryScheduleAsl(&schedule, &asl), RESULT_BAD_PARAM);
60 }
61 
62 HWTEST_F(AuthLevelTest, TestQueryScheduleAsl_002, TestSize.Level0)
63 {
64     CoAuthSchedule schedule = {};
65     ExecutorInfoHal executorInfo = {};
66     constexpr uint32_t esl = 10;
67     constexpr uint32_t excutorSize = 1;
68     executorInfo.esl = esl;
69     schedule.executors[0] = executorInfo;
70     schedule.executorSize = excutorSize;
71     uint32_t asl = 0;
72     EXPECT_EQ(QueryScheduleAsl(&schedule, &asl), RESULT_SUCCESS);
73 }
74 
75 HWTEST_F(AuthLevelTest, TestQueryScheduleAtl, TestSize.Level0)
76 {
77     EXPECT_EQ(QueryScheduleAtl(nullptr, 0, nullptr), RESULT_BAD_PARAM);
78     CoAuthSchedule schedule = {};
79     EXPECT_EQ(QueryScheduleAtl(&schedule, 0, nullptr), RESULT_BAD_PARAM);
80     uint32_t atl = 0;
81     schedule.executorSize = 0;
82     EXPECT_EQ(QueryScheduleAtl(&schedule, 0, &atl), RESULT_BAD_PARAM);
83 }
84 
85 HWTEST_F(AuthLevelTest, TestGetExecutorAslAndAcl_001, TestSize.Level0)
86 {
87     constexpr uint32_t authType = 1;
88     uint32_t asl = 0;
89     uint32_t acl = 0;
90     EXPECT_EQ(GetExecutorAslAndAcl(authType, &asl, &acl), RESULT_UNKNOWN);
91 }
92 
93 HWTEST_F(AuthLevelTest, TestGetExecutorAslAndAcl_002, TestSize.Level0)
94 {
95     g_poolList = CreateLinkedList(DestroyExecutorInfo);
96     EXPECT_NE(g_poolList, nullptr);
97     ExecutorInfoHal executorInfo = {};
98     executorInfo.authType = 1;
99     executorInfo.executorRole = COLLECTOR;
100     g_poolList->insert(g_poolList, static_cast<void *>(&executorInfo));
101     constexpr uint32_t authType = 1;
102     uint32_t asl = 0;
103     uint32_t acl = 0;
104     EXPECT_EQ(GetExecutorAslAndAcl(authType, &asl, &acl), RESULT_SUCCESS);
105 }
106 
107 HWTEST_F(AuthLevelTest, TestGetExecutorAslAndAcl_003, TestSize.Level0)
108 {
109     g_poolList = CreateLinkedList(DestroyExecutorInfo);
110     EXPECT_NE(g_poolList, nullptr);
111     ExecutorInfoHal executorInfo = {};
112     executorInfo.authType = 1;
113     executorInfo.executorRole = ALL_IN_ONE;
114     executorInfo.esl = 0;
115     g_poolList->insert(g_poolList, static_cast<void *>(&executorInfo));
116     constexpr uint32_t authType = 1;
117     uint32_t asl = 0;
118     uint32_t acl = 0;
119     EXPECT_EQ(GetExecutorAslAndAcl(authType, &asl, &acl), RESULT_SUCCESS);
120 }
121 } // namespace UserAuth
122 } // namespace UserIam
123 } // namespace OHOS
124