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 "resource_node_utils_test.h"
17 
18 #include "iam_ptr.h"
19 #include "mock_credential_info.h"
20 #include "mock_resource_node.h"
21 #include "resource_node_pool.h"
22 #include "resource_node_utils.h"
23 
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 using namespace testing;
28 using namespace testing::ext;
29 
SetUpTestCase()30 void ResourceNodeUtilsTest::SetUpTestCase()
31 {
32 }
33 
TearDownTestCase()34 void ResourceNodeUtilsTest::TearDownTestCase()
35 {
36 }
37 
SetUp()38 void ResourceNodeUtilsTest::SetUp()
39 {
40 }
41 
TearDown()42 void ResourceNodeUtilsTest::TearDown()
43 {
44 }
45 
46 HWTEST_F(ResourceNodeUtilsTest, NotifyExecutorToDeleteTemplates001, TestSize.Level0)
47 {
48     std::vector<std::shared_ptr<CredentialInfoInterface>> infos;
49     std::string changeReason = "DeleteTemplate";
50     int32_t result = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(infos, changeReason);
51     EXPECT_EQ(result, INVALID_PARAMETERS);
52 }
53 
54 HWTEST_F(ResourceNodeUtilsTest, NotifyExecutorToDeleteTemplates002, TestSize.Level0)
55 {
56     auto credInfo = Common::MakeShared<MockCredentialInfo>();
57     EXPECT_NE(credInfo, nullptr);
58     EXPECT_CALL(*credInfo, GetExecutorIndex()).WillRepeatedly(Return(10));
59 
60     std::vector<std::shared_ptr<CredentialInfoInterface>> infos;
61     infos.push_back(credInfo);
62     std::string changeReason = "DeleteTemplate";
63     int32_t result = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(infos, changeReason);
64     EXPECT_EQ(result, SUCCESS);
65 }
66 
67 HWTEST_F(ResourceNodeUtilsTest, NotifyExecutorToDeleteTemplates003, TestSize.Level0)
68 {
69     auto credInfo1 = Common::MakeShared<MockCredentialInfo>();
70     EXPECT_NE(credInfo1, nullptr);
71     EXPECT_CALL(*credInfo1, GetExecutorIndex()).WillRepeatedly(Return(10));
72     EXPECT_CALL(*credInfo1, GetTemplateId()).WillRepeatedly(Return(20));
73 
74     auto credInfo2 = Common::MakeShared<MockCredentialInfo>();
75     EXPECT_NE(credInfo2, nullptr);
76     EXPECT_CALL(*credInfo2, GetExecutorIndex()).WillRepeatedly(Return(100));
77     EXPECT_CALL(*credInfo2, GetTemplateId()).WillRepeatedly(Return(200));
78 
79     std::vector<std::shared_ptr<CredentialInfoInterface>> infos;
80     infos.push_back(credInfo1);
81     infos.push_back(credInfo2);
82 
83     auto resourceNode1 = Common::MakeShared<MockResourceNode>();
84     EXPECT_NE(resourceNode1, nullptr);
85     EXPECT_CALL(*resourceNode1, GetExecutorIndex()).WillRepeatedly(Return(10));
86     EXPECT_CALL(*resourceNode1, SetProperty(_)).WillRepeatedly(Return(FAIL));
87     auto resourceNode2 = Common::MakeShared<MockResourceNode>();
88     EXPECT_NE(resourceNode2, nullptr);
89     EXPECT_CALL(*resourceNode2, GetExecutorIndex()).WillRepeatedly(Return(100));
90     EXPECT_CALL(*resourceNode2, SetProperty(_)).WillRepeatedly(Return(SUCCESS));
91 
92     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode1));
93     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode2));
94 
95     std::string changeReason = "DeleteTemplate";
96     int32_t result = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(infos, changeReason);
97     EXPECT_EQ(result, SUCCESS);
98     ResourceNodePool::Instance().DeleteAll();
99 }
100 
101 HWTEST_F(ResourceNodeUtilsTest, SendMsgToExecutor001, TestSize.Level0)
102 {
103     uint64_t testIndex = 10;
104     int32_t commandId = 1250;
105     std::vector<uint8_t> testMsg = {1, 2, 3, 4};
106 
107     ResourceNodeUtils::SendMsgToExecutor(testIndex, commandId, testMsg);
108 }
109 
110 HWTEST_F(ResourceNodeUtilsTest, SendMsgToExecutor002, TestSize.Level0)
111 {
112     uint64_t testIndex1 = 10;
113     uint64_t testIndex2 = 100;
114     int32_t commandId = 1250;
115     std::vector<uint8_t> testMsg = {1, 2, 3, 4};
116 
117     auto resourceNode1 = Common::MakeShared<MockResourceNode>();
118     EXPECT_NE(resourceNode1, nullptr);
119     EXPECT_CALL(*resourceNode1, GetExecutorIndex()).WillRepeatedly(Return(10));
120     EXPECT_CALL(*resourceNode1, SetProperty(_)).WillRepeatedly(Return(FAIL));
121     auto resourceNode2 = Common::MakeShared<MockResourceNode>();
122     EXPECT_NE(resourceNode2, nullptr);
123     EXPECT_CALL(*resourceNode2, GetExecutorIndex()).WillRepeatedly(Return(100));
124     EXPECT_CALL(*resourceNode2, SetProperty(_)).WillRepeatedly(Return(SUCCESS));
125 
126     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode1));
127     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode2));
128 
129     ResourceNodeUtils::SendMsgToExecutor(testIndex1, commandId, testMsg);
130     ResourceNodeUtils::SendMsgToExecutor(testIndex2, commandId, testMsg);
131     ResourceNodePool::Instance().DeleteAll();
132 }
133 
134 HWTEST_F(ResourceNodeUtilsTest, SetCachedTemplates001, TestSize.Level0)
135 {
136     const uint64_t testIndex = 10;
137     std::vector<std::shared_ptr<CredentialInfoInterface>> infos;
138     EXPECT_TRUE(ResourceNodePool::Instance().Select(testIndex).lock() == nullptr);
139     ResourceNodeUtils::SetCachedTemplates(testIndex, infos);
140 }
141 
142 HWTEST_F(ResourceNodeUtilsTest, SetCachedTemplates002, TestSize.Level0)
143 {
144     const uint64_t testIndex = 10;
145     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(testIndex);
146     EXPECT_CALL(*(static_cast<MockResourceNode *>(resourceNode.get())), SetProperty(_))
147         .WillRepeatedly(Return(FAIL));
148     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
149     auto mockCredentialInfo = Common::MakeShared<MockCredentialInfo>();
150     EXPECT_CALL(*mockCredentialInfo, GetTemplateId()).WillRepeatedly(Return(0));
151     std::vector<std::shared_ptr<CredentialInfoInterface>> infos = { mockCredentialInfo };
152     ResourceNodeUtils::SetCachedTemplates(testIndex, infos);
153     ResourceNodePool::Instance().DeleteAll();
154 }
155 
156 HWTEST_F(ResourceNodeUtilsTest, SetCachedTemplates003, TestSize.Level0)
157 {
158     const uint64_t testIndex = 10;
159     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(testIndex);
160     EXPECT_CALL(*(static_cast<MockResourceNode *>(resourceNode.get())), SetProperty(_))
161         .WillRepeatedly(Return(SUCCESS));
162     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
163     auto mockCredentialInfo = Common::MakeShared<MockCredentialInfo>();
164     EXPECT_CALL(*mockCredentialInfo, GetTemplateId()).WillRepeatedly(Return(0));
165     std::vector<std::shared_ptr<CredentialInfoInterface>> infos = { mockCredentialInfo };
166     ResourceNodeUtils::SetCachedTemplates(testIndex, infos);
167     ResourceNodePool::Instance().DeleteAll();
168 }
169 
170 HWTEST_F(ResourceNodeUtilsTest, ClassifyCredInfoByExecutor001, TestSize.Level0)
171 {
172     std::vector<std::shared_ptr<CredentialInfoInterface>> in = { nullptr };
173     std::map<uint64_t, std::vector<std::shared_ptr<CredentialInfoInterface>>> out;
174     EXPECT_EQ(ResourceNodeUtils::ClassifyCredInfoByExecutor(in, out), GENERAL_ERROR);
175 }
176 
177 HWTEST_F(ResourceNodeUtilsTest, ClassifyCredInfoByExecutor002, TestSize.Level0)
178 {
179     auto mockCredentialInfo = Common::MakeShared<MockCredentialInfo>();
180     EXPECT_CALL(*mockCredentialInfo, GetExecutorIndex()).WillRepeatedly(Return(0));
181     std::vector<std::shared_ptr<CredentialInfoInterface>> in = { mockCredentialInfo };
182     std::map<uint64_t, std::vector<std::shared_ptr<CredentialInfoInterface>>> out;
183     EXPECT_EQ(ResourceNodeUtils::ClassifyCredInfoByExecutor(in, out), SUCCESS);
184     EXPECT_EQ(ResourceNodeUtils::ClassifyCredInfoByExecutor(in, out), SUCCESS);
185 }
186 } // namespace UserAuth
187 } // namespace UserIam
188 } // namespace OHOS
189