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_test.h"
17 
18 #include "iam_ptr.h"
19 #include "co_auth_interface.h"
20 #include "resource_node.h"
21 
22 #include "mock_executor_callback.h"
23 #include "mock_iuser_auth_interface.h"
24 #include "mock_resource_node.h"
25 
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 using namespace testing;
30 using namespace testing::ext;
31 using ExecutorRegisterInfo = CoAuthInterface::ExecutorRegisterInfo;
SetUpTestCase()32 void ResourceNodeTest::SetUpTestCase()
33 {
34 }
35 
TearDownTestCase()36 void ResourceNodeTest::TearDownTestCase()
37 {
38 }
39 
SetUp()40 void ResourceNodeTest::SetUp()
41 {
42     MockIUserAuthInterface::Holder::GetInstance().Reset();
43 }
44 
TearDown()45 void ResourceNodeTest::TearDown()
46 {
47     MockIUserAuthInterface::Holder::GetInstance().Reset();
48 }
49 
50 HWTEST_F(ResourceNodeTest, HdiError, TestSize.Level0)
51 {
52     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
53     EXPECT_CALL(*mock, AddExecutor(_, _, _, _)).WillRepeatedly(Return(1));
54     ExecutorRegisterInfo info {};
55     std::vector<uint64_t> templateIdList {};
56     std::vector<uint8_t> fwkPublicKey {};
57 
58     auto node = ResourceNode::MakeNewResource(info, nullptr, templateIdList, fwkPublicKey);
59     EXPECT_EQ(node, nullptr);
60 }
61 
62 HWTEST_F(ResourceNodeTest, InsertSuccessWithIndex, TestSize.Level0)
63 {
64     constexpr uint64_t DEST_EXECUTOR_INDEX = 0x12345678;
65 
66     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
67     EXPECT_CALL(*mock, AddExecutor(_, _, _, _)).WillRepeatedly(DoAll(SetArgReferee<1>(DEST_EXECUTOR_INDEX), Return(0)));
68     EXPECT_CALL(*mock, DeleteExecutor(DEST_EXECUTOR_INDEX)).Times(Exactly(1)).WillRepeatedly(Return(0));
69     {
70         ExecutorRegisterInfo info {};
71         std::vector<uint64_t> templateIdList {};
72         std::vector<uint8_t> fwkPublicKey {};
73         auto node = ResourceNode::MakeNewResource(info, nullptr, templateIdList, fwkPublicKey);
74         ASSERT_NE(node, nullptr);
75 
76         EXPECT_EQ(node->GetExecutorIndex(), DEST_EXECUTOR_INDEX);
77     }
78 }
79 
80 HWTEST_F(ResourceNodeTest, InsertSuccessWithTemplateIdList, TestSize.Level0)
81 {
82     constexpr uint64_t DEST_EXECUTOR_INDEX = 0x12345678;
83 
__anonfdd249a10102(std::vector<uint64_t> &list) 84     auto fillTemplateIdList = [](std::vector<uint64_t> &list) {
85         std::vector<uint64_t> HDI_TEMPLATE_ID_LIST {1, 3, 5, 7, 9};
86         list.swap(HDI_TEMPLATE_ID_LIST);
87     };
88 
__anonfdd249a10202(std::vector<uint8_t> &key) 89     auto fillFwkPublicKey = [](std::vector<uint8_t> &key) {
90         std::vector<uint8_t> FWK_PUB_KEY {5, 3, 7, 1, 4};
91         key.swap(FWK_PUB_KEY);
92     };
93 
94     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
95     EXPECT_CALL(*mock, AddExecutor(_, _, _, _))
96         .WillRepeatedly(DoAll(SetArgReferee<1>(DEST_EXECUTOR_INDEX), WithArg<2>(fillFwkPublicKey),
97             WithArg<3>(fillTemplateIdList), Return(0)));
98 
99     EXPECT_CALL(*mock, DeleteExecutor(DEST_EXECUTOR_INDEX)).Times(Exactly(1)).WillRepeatedly(Return(0));
100     {
101         ExecutorRegisterInfo info {};
102         std::vector<uint64_t> templateIdList {};
103         std::vector<uint8_t> fwkPublicKey {};
104         auto node = ResourceNode::MakeNewResource(info, nullptr, templateIdList, fwkPublicKey);
105 
106         ASSERT_NE(node, nullptr);
107 
108         EXPECT_EQ(node->GetExecutorIndex(), DEST_EXECUTOR_INDEX);
109         EXPECT_THAT(templateIdList, ElementsAre(1, 3, 5, 7, 9));
110         EXPECT_THAT(fwkPublicKey, ElementsAre(5, 3, 7, 1, 4));
111     }
112 }
113 
114 HWTEST_F(ResourceNodeTest, ResourceNodeTest001, TestSize.Level0)
115 {
116     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
117     EXPECT_NE(mockHdi, nullptr);
118     EXPECT_CALL(*mockHdi, AddExecutor(_, _, _, _)).Times(1);
119     EXPECT_CALL(*mockHdi, DeleteExecutor(_)).Times(1);
120 
121     ExecutorRegisterInfo info = {};
122     std::vector<uint64_t> templateIdList;
123     std::vector<uint8_t> fwkPublicKey;
124     std::shared_ptr<ExecutorCallbackInterface> testCallback = nullptr;
125     auto node = ResourceNode::MakeNewResource(info, testCallback, templateIdList, fwkPublicKey);
126     EXPECT_NE(node, nullptr);
127 
128     uint64_t scheduleId = 10;
129     std::vector<uint8_t> publicKey;
130     Attributes command;
131     Attributes properties;
132     Attributes condition;
133     Attributes values;
134     EXPECT_EQ(node->BeginExecute(scheduleId, publicKey, command), GENERAL_ERROR);
135     EXPECT_EQ(node->EndExecute(scheduleId, command), GENERAL_ERROR);
136     EXPECT_EQ(node->SetProperty(properties), GENERAL_ERROR);
137     EXPECT_EQ(node->GetProperty(condition, values), GENERAL_ERROR);
138 }
139 
140 HWTEST_F(ResourceNodeTest, ResourceNodeTest002, TestSize.Level0)
141 {
142     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
143     EXPECT_NE(mockHdi, nullptr);
144     EXPECT_CALL(*mockHdi, AddExecutor(_, _, _, _)).Times(1);
145     EXPECT_CALL(*mockHdi, DeleteExecutor(_)).Times(1);
146 
147     ExecutorRegisterInfo info = {};
148     std::vector<uint64_t> templateIdList;
149     std::vector<uint8_t> fwkPublicKey;
150     auto testCallback = Common::MakeShared<MockExecutorCallback>();
151     EXPECT_NE(testCallback, nullptr);
152     EXPECT_CALL(*testCallback, OnBeginExecute(_, _, _)).WillRepeatedly(Return(SUCCESS));
153     EXPECT_CALL(*testCallback, OnEndExecute(_, _)).WillRepeatedly(Return(SUCCESS));
154     EXPECT_CALL(*testCallback, OnSetProperty(_)).WillRepeatedly(Return(SUCCESS));
155     EXPECT_CALL(*testCallback, OnGetProperty(_, _)).WillRepeatedly(Return(SUCCESS));
156     auto node = ResourceNode::MakeNewResource(info, testCallback, templateIdList, fwkPublicKey);
157     EXPECT_NE(node, nullptr);
158 
159     uint64_t scheduleId = 10;
160     std::vector<uint8_t> publicKey;
161     Attributes command;
162     Attributes properties;
163     Attributes condition;
164     Attributes values;
165     EXPECT_EQ(node->BeginExecute(scheduleId, publicKey, command), SUCCESS);
166     EXPECT_EQ(node->EndExecute(scheduleId, command), SUCCESS);
167     EXPECT_EQ(node->SetProperty(properties), SUCCESS);
168     EXPECT_EQ(node->GetProperty(condition, values), SUCCESS);
169 }
170 } // namespace UserAuth
171 } // namespace UserIam
172 } // namespace OHOS
173