1 /*
2  * Copyright (c) 2023 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 #define private public
17 #define protected public
18 #include "domain_executer.h"
19 #include "executer_utils.h"
20 #undef protected
21 #undef private
22 
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 
26 #include "executer_utils_mock.h"
27 
28 using namespace testing::ext;
29 using namespace testing;
30 using namespace OHOS::EDM::IPTABLES;
31 
32 using ::testing::Return;
33 using ::testing::DoAll;
34 using ::testing::Invoke;
35 
36 namespace OHOS {
37 namespace EDM {
38 namespace IPTABLES {
39 namespace TEST {
40 
41 class DomainExecuterTest : public testing::Test {
42 public:
43     std::shared_ptr<ExecuterUtilsMock> executerUtilsMock;
44 
45 protected:
46     void SetUp() override;
47 };
48 
SetUp()49 void DomainExecuterTest::SetUp()
50 {
51     executerUtilsMock = std::make_shared<ExecuterUtilsMock>();
52     ExecuterUtils::instance_ = executerUtilsMock;
53 }
54 
55 /**
56  * @tc.name: TestInit
57  * @tc.desc: Test Init func.
58  * @tc.type: FUNC
59  */
60 HWTEST_F(DomainExecuterTest, TestInit, TestSize.Level1)
61 {
62     EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
63 
64     DomainExecuter initOk{"chainName"};
65     EXPECT_TRUE(initOk.Init() == ERR_OK);
66 
67     EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(-1)));
68     DomainExecuter initFail{"chainName"};
69     EXPECT_FALSE(initFail.Init() == ERR_OK);
70 }
71 } // namespace TEST
72 } // namespace IPTABLES
73 } // namespace EDM
74 } // namespace OHOS