• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GetAsl(uint32_t authType, uint32_t *asl);
28     extern ResultCode GetAcl(int32_t userId, uint32_t authType, uint32_t *acl);
29 }
30 
31 namespace OHOS {
32 namespace UserIam {
33 namespace UserAuth {
34 using namespace testing;
35 using namespace testing::ext;
36 
37 class AuthLevelTest : public testing::Test {
38 public:
SetUpTestCase()39     static void SetUpTestCase() {};
40 
TearDownTestCase()41     static void TearDownTestCase() {};
42 
SetUp()43     void SetUp() {};
44 
TearDown()45     void TearDown() {};
46 };
47 
48 HWTEST_F(AuthLevelTest, TestGetAtl, TestSize.Level0)
49 {
50     EXPECT_EQ(GetAtl(0, 0), 0);
51 }
52 
53 HWTEST_F(AuthLevelTest, TestQueryScheduleAsl_001, TestSize.Level0)
54 {
55     EXPECT_EQ(QueryScheduleAsl(nullptr, nullptr), RESULT_BAD_PARAM);
56     CoAuthSchedule schedule = {};
57     EXPECT_EQ(QueryScheduleAsl(&schedule, nullptr), RESULT_BAD_PARAM);
58     uint32_t asl = 0;
59     schedule.executorSize = 0;
60     EXPECT_EQ(QueryScheduleAsl(&schedule, &asl), RESULT_BAD_PARAM);
61 }
62 
63 HWTEST_F(AuthLevelTest, TestQueryScheduleAsl_002, TestSize.Level0)
64 {
65     CoAuthSchedule schedule = {};
66     ExecutorInfoHal executorInfo = {};
67     executorInfo.esl = 10;
68     schedule.executors[0] = executorInfo;
69     schedule.executorSize = 1;
70     uint32_t asl = 0;
71     EXPECT_EQ(QueryScheduleAsl(&schedule, &asl), RESULT_SUCCESS);
72 }
73 
74 HWTEST_F(AuthLevelTest, TestQueryScheduleAtl, TestSize.Level0)
75 {
76     EXPECT_EQ(QueryScheduleAtl(nullptr, 0, nullptr), RESULT_BAD_PARAM);
77     CoAuthSchedule schedule = {};
78     EXPECT_EQ(QueryScheduleAtl(&schedule, 0, nullptr), RESULT_BAD_PARAM);
79     uint32_t atl = 0;
80     schedule.executorSize = 0;
81     EXPECT_EQ(QueryScheduleAtl(&schedule, 0, &atl), RESULT_BAD_PARAM);
82 }
83 
84 HWTEST_F(AuthLevelTest, TestGetAsl_001, TestSize.Level0)
85 {
86     uint32_t authType = 1;
87     uint32_t asl = 0;
88     EXPECT_EQ(GetAsl(authType, &asl), RESULT_UNKNOWN);
89 }
90 
91 HWTEST_F(AuthLevelTest, TestGetAsl_002, TestSize.Level0)
92 {
93     g_poolList = CreateLinkedList(DestroyExecutorInfo);
94     EXPECT_NE(g_poolList, nullptr);
95     ExecutorInfoHal executorInfo = {};
96     executorInfo.authType = 1;
97     executorInfo.executorRole = COLLECTOR;
98     g_poolList->insert(g_poolList, static_cast<void *>(&executorInfo));
99     uint32_t authType = 1;
100     uint32_t asl = 0;
101     EXPECT_EQ(GetAsl(authType, &asl), RESULT_GENERAL_ERROR);
102 }
103 
104 HWTEST_F(AuthLevelTest, TestGetAsl_003, TestSize.Level0)
105 {
106     g_poolList = CreateLinkedList(DestroyExecutorInfo);
107     EXPECT_NE(g_poolList, nullptr);
108     ExecutorInfoHal executorInfo = {};
109     executorInfo.authType = 1;
110     executorInfo.executorRole = ALL_IN_ONE;
111     executorInfo.esl = 0;
112     g_poolList->insert(g_poolList, static_cast<void *>(&executorInfo));
113     uint32_t authType = 1;
114     uint32_t asl = 0;
115     EXPECT_EQ(GetAsl(authType, &asl), RESULT_SUCCESS);
116 }
117 
118 HWTEST_F(AuthLevelTest, TestGetAcl, TestSize.Level0)
119 {
120     int32_t userId = 21361;
121     uint32_t authType = 1;
122     uint32_t acl = 0;
123     EXPECT_EQ(GetAcl(userId, authType, &acl), RESULT_NOT_ENROLLED);
124     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
125     EXPECT_NE(g_userInfoList, nullptr);
126     EXPECT_EQ(GetAcl(userId, authType, &acl), RESULT_NOT_ENROLLED);
127 }
128 
129 HWTEST_F(AuthLevelTest, TestSingleAuthTrustLevel_001, TestSize.Level0)
130 {
131     int32_t userId = 21356;
132     uint32_t authType = 1;
133     EXPECT_EQ(SingleAuthTrustLevel(userId, authType, nullptr), RESULT_BAD_PARAM);
134 }
135 
136 HWTEST_F(AuthLevelTest, TestSingleAuthTrustLevel_002, TestSize.Level0)
137 {
138     g_poolList = CreateLinkedList(DestroyExecutorInfo);
139     EXPECT_NE(g_poolList, nullptr);
140     ExecutorInfoHal executorInfo = {};
141     executorInfo.authType = 1;
142     executorInfo.executorRole = ALL_IN_ONE;
143     executorInfo.esl = 0;
144     g_poolList->insert(g_poolList, static_cast<void *>(&executorInfo));
145     g_userInfoList = nullptr;
146     int32_t userId = 21356;
147     uint32_t authType = 1;
148     uint32_t atl = 0;
149     EXPECT_EQ(SingleAuthTrustLevel(userId, authType, &atl), RESULT_NOT_ENROLLED);
150 }
151 } // namespace UserAuth
152 } // namespace UserIam
153 } // namespace OHOS
154