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<IdmGetCredInfoCallbackInterface::CredentialInfo>> infos;
49 int32_t result = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(infos);
50 EXPECT_EQ(result, INVALID_PARAMETERS);
51 }
52
53 HWTEST_F(ResourceNodeUtilsTest, NotifyExecutorToDeleteTemplates002, TestSize.Level0)
54 {
55 auto credInfo = Common::MakeShared<MockCredentialInfo>();
56 EXPECT_NE(credInfo, nullptr);
57 EXPECT_CALL(*credInfo, GetExecutorIndex()).WillRepeatedly(Return(10));
58
59 std::vector<std::shared_ptr<IdmGetCredInfoCallbackInterface::CredentialInfo>> infos;
60 infos.push_back(credInfo);
61 int32_t result = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(infos);
62 EXPECT_EQ(result, SUCCESS);
63 }
64
65 HWTEST_F(ResourceNodeUtilsTest, NotifyExecutorToDeleteTemplates003, TestSize.Level0)
66 {
67 auto credInfo1 = Common::MakeShared<MockCredentialInfo>();
68 EXPECT_NE(credInfo1, nullptr);
69 EXPECT_CALL(*credInfo1, GetExecutorIndex()).WillRepeatedly(Return(10));
70 EXPECT_CALL(*credInfo1, GetTemplateId()).WillRepeatedly(Return(20));
71
72 auto credInfo2 = Common::MakeShared<MockCredentialInfo>();
73 EXPECT_NE(credInfo2, nullptr);
74 EXPECT_CALL(*credInfo2, GetExecutorIndex()).WillRepeatedly(Return(100));
75 EXPECT_CALL(*credInfo2, GetTemplateId()).WillRepeatedly(Return(200));
76
77 std::vector<std::shared_ptr<IdmGetCredInfoCallbackInterface::CredentialInfo>> infos;
78 infos.push_back(credInfo1);
79 infos.push_back(credInfo2);
80
81 auto resourceNode1 = Common::MakeShared<MockResourceNode>();
82 EXPECT_NE(resourceNode1, nullptr);
83 EXPECT_CALL(*resourceNode1, GetExecutorIndex()).WillRepeatedly(Return(10));
84 EXPECT_CALL(*resourceNode1, SetProperty(_)).WillRepeatedly(Return(FAIL));
85 auto resourceNode2 = Common::MakeShared<MockResourceNode>();
86 EXPECT_NE(resourceNode2, nullptr);
87 EXPECT_CALL(*resourceNode2, GetExecutorIndex()).WillRepeatedly(Return(100));
88 EXPECT_CALL(*resourceNode2, SetProperty(_)).WillRepeatedly(Return(SUCCESS));
89
90 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode1));
91 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode2));
92
93 int32_t result = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(infos);
94 EXPECT_EQ(result, SUCCESS);
95 ResourceNodePool::Instance().DeleteAll();
96 }
97
98 HWTEST_F(ResourceNodeUtilsTest, SendMsgToExecutor001, TestSize.Level0)
99 {
100 uint64_t testIndex = 10;
101 std::vector<uint8_t> testMsg = {1, 2, 3, 4};
102
103 ResourceNodeUtils::SendMsgToExecutor(testIndex, testMsg);
104 }
105
106 HWTEST_F(ResourceNodeUtilsTest, SendMsgToExecutor002, TestSize.Level0)
107 {
108 uint64_t testIndex1 = 10;
109 uint64_t testIndex2 = 100;
110 std::vector<uint8_t> testMsg = {1, 2, 3, 4};
111
112 auto resourceNode1 = Common::MakeShared<MockResourceNode>();
113 EXPECT_NE(resourceNode1, nullptr);
114 EXPECT_CALL(*resourceNode1, GetExecutorIndex()).WillRepeatedly(Return(10));
115 EXPECT_CALL(*resourceNode1, SetProperty(_)).WillRepeatedly(Return(FAIL));
116 auto resourceNode2 = Common::MakeShared<MockResourceNode>();
117 EXPECT_NE(resourceNode2, nullptr);
118 EXPECT_CALL(*resourceNode2, GetExecutorIndex()).WillRepeatedly(Return(100));
119 EXPECT_CALL(*resourceNode2, SetProperty(_)).WillRepeatedly(Return(SUCCESS));
120
121 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode1));
122 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode2));
123
124 ResourceNodeUtils::SendMsgToExecutor(testIndex1, testMsg);
125 ResourceNodeUtils::SendMsgToExecutor(testIndex2, testMsg);
126 ResourceNodePool::Instance().DeleteAll();
127 }
128 } // namespace UserAuth
129 } // namespace UserIam
130 } // namespace OHOS
131