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 #include "dlp_sandbox_change_callback_stub_test.h"
17 #include <cerrno>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include "dlp_permission.h"
21 #include "dlp_permission_log.h"
22 #include "dlp_sandbox_callback_info_parcel.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::Security::DlpPermission;
27 using namespace std;
28 
29 namespace {
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
31     LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpSandboxChangeCallbackStubTest"};
32 constexpr uint32_t DLP_SANDBOX_STATE_CHANGE = 0;
33 }
34 
SetUpTestCase()35 void DlpSandboxChangeCallbackStubTest::SetUpTestCase() {}
36 
TearDownTestCase()37 void DlpSandboxChangeCallbackStubTest::TearDownTestCase() {}
38 
SetUp()39 void DlpSandboxChangeCallbackStubTest::SetUp() {}
40 
TearDown()41 void DlpSandboxChangeCallbackStubTest::TearDown() {}
42 
43 class DlpSandboxChangeCallbackTest : public DlpSandboxChangeCallbackStub {
44 public:
45     DlpSandboxChangeCallbackTest() = default;
46     virtual ~DlpSandboxChangeCallbackTest() = default;
47 
48     void DlpSandboxStateChangeCallback(DlpSandboxCallbackInfo& result) override;
49 };
50 
DlpSandboxStateChangeCallback(DlpSandboxCallbackInfo & result)51 void DlpSandboxChangeCallbackTest::DlpSandboxStateChangeCallback(DlpSandboxCallbackInfo& result) {}
52 
53 /**
54  * @tc.name: OnLoadSystemAbilityFail001
55  * @tc.desc: OnLoadSystemAbilityFail test
56  * @tc.type: FUNC
57  * @tc.require:
58  */
59 HWTEST_F(DlpSandboxChangeCallbackStubTest, OnLoadSystemAbilityFail001, TestSize.Level1)
60 {
61     DLP_LOG_INFO(LABEL, "OnLoadSystemAbilityFail001");
62 
63     uint32_t code = DLP_SANDBOX_STATE_CHANGE;
64     MessageParcel data;
65     MessageParcel reply;
66     MessageOption option;
67 
68     auto stub = new (std::nothrow) DlpSandboxChangeCallbackTest();
69     ASSERT_FALSE(stub == nullptr);
70 
71     int32_t ret = stub->OnRemoteRequest(code, data, reply, option);
72     ASSERT_EQ(DLP_SERVICE_ERROR_IPC_REQUEST_FAIL, ret);
73 
74     std::u16string descriptor = IDlpSandboxStateChangeCallback::GetDescriptor();
75     data.WriteInterfaceToken(descriptor);
76     ret = stub->OnRemoteRequest(code, data, reply, option);
77     ASSERT_EQ(DLP_SERVICE_ERROR_PARCEL_OPERATE_FAIL, ret);
78 
79     delete stub;
80 }
81 
82 /**
83  * @tc.name: OnLoadSystemAbilityFail002
84  * @tc.desc: OnLoadSystemAbilityFail test
85  * @tc.type: FUNC
86  * @tc.require:
87  */
88 HWTEST_F(DlpSandboxChangeCallbackStubTest, OnLoadSystemAbilityFail002, TestSize.Level1)
89 {
90     DLP_LOG_INFO(LABEL, "OnLoadSystemAbilityFail002");
91 
92     uint32_t code = DLP_SANDBOX_STATE_CHANGE;
93     MessageParcel data;
94     MessageParcel reply;
95     MessageOption option;
96 
97     auto stub = new (std::nothrow) DlpSandboxChangeCallbackTest();
98     ASSERT_FALSE(stub == nullptr);
99 
100     std::u16string descriptor = IDlpSandboxStateChangeCallback::GetDescriptor();
101     data.WriteInterfaceToken(descriptor);
102 
103     sptr<DlpSandboxCallbackInfoParcel> policyParcel = new (std::nothrow) DlpSandboxCallbackInfoParcel();
104     ASSERT_FALSE(policyParcel == nullptr);
105     data.WriteParcelable(policyParcel);
106 
107     int32_t ret = stub->OnRemoteRequest(code, data, reply, option);
108     ASSERT_EQ(DLP_OK, ret);
109 
110     delete stub;
111 }
112 
113 /**
114  * @tc.name: OnLoadSystemAbilityFail003
115  * @tc.desc: OnLoadSystemAbilityFail test
116  * @tc.type: FUNC
117  * @tc.require:
118  */
119 HWTEST_F(DlpSandboxChangeCallbackStubTest, OnLoadSystemAbilityFail003, TestSize.Level1)
120 {
121     DLP_LOG_INFO(LABEL, "OnLoadSystemAbilityFail003");
122 
123     uint32_t code = DLP_SANDBOX_STATE_CHANGE + 1;
124     MessageParcel data;
125     MessageParcel reply;
126     MessageOption option;
127 
128     auto stub = new (std::nothrow) DlpSandboxChangeCallbackTest();
129     ASSERT_FALSE(stub == nullptr);
130 
131     std::u16string descriptor = IDlpSandboxStateChangeCallback::GetDescriptor();
132     data.WriteInterfaceToken(descriptor);
133 
134     stub->OnRemoteRequest(code, data, reply, option);
135     ASSERT_NE(DLP_SANDBOX_STATE_CHANGE, code);
136 
137     delete stub;
138 }
139