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 "executor_messenger_service_test.h"
17 
18 #include "context_pool.h"
19 #include "executor_messenger_service.h"
20 #include "mock_context.h"
21 #include "mock_schedule_node.h"
22 
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
26 using namespace testing;
27 using namespace testing::ext;
28 
SetUpTestCase()29 void ExecutorMessengerServiceTest::SetUpTestCase()
30 {
31 }
32 
TearDownTestCase()33 void ExecutorMessengerServiceTest::TearDownTestCase()
34 {
35 }
36 
SetUp()37 void ExecutorMessengerServiceTest::SetUp()
38 {
39 }
40 
TearDown()41 void ExecutorMessengerServiceTest::TearDown()
42 {
43 }
44 
45 HWTEST_F(ExecutorMessengerServiceTest, ExecutorMessengerServiceTest001, TestSize.Level0)
46 {
47     auto service1 = ExecutorMessengerService::GetInstance();
48     EXPECT_NE(service1, nullptr);
49     auto service2 = ExecutorMessengerService::GetInstance();
50     EXPECT_NE(service2, nullptr);
51     EXPECT_EQ(service1, service2);
52 }
53 
54 HWTEST_F(ExecutorMessengerServiceTest, ExecutorMessengerServiceTest002, TestSize.Level0)
55 {
56     uint64_t testScheduleId1 = 1545;
57     uint64_t testScheduleId2 = 1876;
58     uint64_t testContextId = 78545;
59     ExecutorRole testDstRole = VERIFIER;
60     ResultCode testResultCode = FAIL;
61     std::shared_ptr<Attributes> testFinalResult = nullptr;
62     std::vector<uint8_t> testMsg = {1, 2, 3, 4};
63 
64     auto service = ExecutorMessengerService::GetInstance();
65     EXPECT_NE(service, nullptr);
66 
67     int32_t result1 = service->SendData(testScheduleId1, testDstRole, testMsg);
68     EXPECT_EQ(result1, GENERAL_ERROR);
69 
70     int32_t result2 = service->Finish(testScheduleId1, testResultCode, testFinalResult);
71     EXPECT_EQ(result2, GENERAL_ERROR);
72 
73     auto scheduleNode1 = MockScheduleNode::CreateWithScheduleId(testScheduleId1);
74     EXPECT_NE(scheduleNode1, nullptr);
75     EXPECT_CALL(*scheduleNode1, SendMessage(_,  _)).WillRepeatedly(Return(false));
76     EXPECT_CALL(*scheduleNode1, ContinueSchedule(_, _)).WillRepeatedly(Return(false));
77     auto scheduleNode2 = MockScheduleNode::CreateWithScheduleId(testScheduleId2);
78     EXPECT_NE(scheduleNode2, nullptr);
79     EXPECT_CALL(*scheduleNode2, SendMessage(_,  _)).WillRepeatedly(Return(true));
80     EXPECT_CALL(*scheduleNode2, ContinueSchedule(_, _)).WillRepeatedly(Return(true));
81     std::set<std::shared_ptr<ScheduleNode>> scheduleNodeSet;
82     scheduleNodeSet.insert(scheduleNode1);
83     scheduleNodeSet.insert(scheduleNode2);
84 
85     auto context = MockContext::CreateContextWithScheduleNode(testContextId, scheduleNodeSet);
86     EXPECT_NE(context, nullptr);
87     EXPECT_TRUE(ContextPool::Instance().Insert(context));
88 
89     result1 = service->SendData(testScheduleId1, testDstRole, testMsg);
90     EXPECT_EQ(result1, GENERAL_ERROR);
91     result1 = service->SendData(testScheduleId2, testDstRole, testMsg);
92     EXPECT_EQ(result1, SUCCESS);
93 
94     result2 = service->Finish(testScheduleId1, testResultCode, testFinalResult);
95     EXPECT_EQ(result2, GENERAL_ERROR);
96     result2 = service->Finish(testScheduleId2, testResultCode, testFinalResult);
97     EXPECT_EQ(result2, SUCCESS);
98 
99     EXPECT_TRUE(ContextPool::Instance().Delete(testContextId));
100 }
101 } // namespace UserAuth
102 } // namespace UserIam
103 } // namespace OHOS