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 uint64_t testTransNum = 8751;
60 ExecutorRole testSrcRole = SCHEDULER;
61 ExecutorRole testDstRole = VERIFIER;
62 ResultCode testResultCode = FAIL;
63 std::shared_ptr<Attributes> testFinalResult = nullptr;
64 std::vector<uint8_t> testMsg = {1, 2, 3, 4};
65
66 auto service = ExecutorMessengerService::GetInstance();
67 EXPECT_NE(service, nullptr);
68
69 int32_t result1 = service->SendData(testScheduleId1, testTransNum, testSrcRole, testDstRole, testMsg);
70 EXPECT_EQ(result1, GENERAL_ERROR);
71
72 int32_t result2 = service->Finish(testScheduleId1, testSrcRole, testResultCode, testFinalResult);
73 EXPECT_EQ(result2, GENERAL_ERROR);
74
75 auto scheduleNode1 = MockScheduleNode::CreateWithScheduleId(testScheduleId1);
76 EXPECT_NE(scheduleNode1, nullptr);
77 EXPECT_CALL(*scheduleNode1, ContinueSchedule(_, _, _, _)).WillRepeatedly(Return(false));
78 EXPECT_CALL(*scheduleNode1, ContinueSchedule(_, _)).WillRepeatedly(Return(false));
79 auto scheduleNode2 = MockScheduleNode::CreateWithScheduleId(testScheduleId2);
80 EXPECT_NE(scheduleNode2, nullptr);
81 EXPECT_CALL(*scheduleNode2, ContinueSchedule(_, _, _, _)).WillRepeatedly(Return(true));
82 EXPECT_CALL(*scheduleNode2, ContinueSchedule(_, _)).WillRepeatedly(Return(true));
83 std::set<std::shared_ptr<ScheduleNode>> scheduleNodeSet;
84 scheduleNodeSet.insert(scheduleNode1);
85 scheduleNodeSet.insert(scheduleNode2);
86
87 auto context = MockContext::CreateContextWithScheduleNode(testContextId, scheduleNodeSet);
88 EXPECT_NE(context, nullptr);
89 EXPECT_TRUE(ContextPool::Instance().Insert(context));
90
91 result1 = service->SendData(testScheduleId1, testTransNum, testSrcRole, testDstRole, testMsg);
92 EXPECT_EQ(result1, GENERAL_ERROR);
93 result1 = service->SendData(testScheduleId2, testTransNum, testSrcRole, testDstRole, testMsg);
94 EXPECT_EQ(result1, SUCCESS);
95
96 result2 = service->Finish(testScheduleId1, testSrcRole, testResultCode, testFinalResult);
97 EXPECT_EQ(result2, GENERAL_ERROR);
98 result2 = service->Finish(testScheduleId2, testSrcRole, testResultCode, testFinalResult);
99 EXPECT_EQ(result2, SUCCESS);
100
101 EXPECT_TRUE(ContextPool::Instance().Delete(testContextId));
102 }
103 } // namespace UserAuth
104 } // namespace UserIam
105 } // namespace OHOS