1 /*
2 * Copyright (c) 2024 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 #include <asset/asset_recv_callback_stub.h>
16
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include "dfs_error.h"
21 #include "asset/asset_callback_interface_code.h"
22
23 namespace OHOS::Storage::DistributedFile::Test {
24 using namespace OHOS::FileManagement;
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace std;
28
29 class MockAssetRecvCallbackStubStub final : public AssetRecvCallbackStub {
30 public:
31 MOCK_METHOD4(OnStart, int32_t(const std::string &srcNetworkId,
32 const std::string &dstNetworkId,
33 const std::string &sessionId,
34 const std::string &dstBundleName));
35 MOCK_METHOD3(OnFinished, int32_t(const std::string &srcNetworkId,
36 const sptr<AssetObj> &assetObj,
37 int32_t result));
38 };
39
40 class AssetRecvCallbackStubTest : public testing::Test {
41 public:
42 static void SetUpTestCase(void);
43 static void TearDownTestCase(void);
44 void SetUp();
45 void TearDown();
46 public:
47 static inline shared_ptr<MockAssetRecvCallbackStubStub> mockStub_ = nullptr;
48 };
49
SetUpTestCase(void)50 void AssetRecvCallbackStubTest::SetUpTestCase(void)
51 {
52 GTEST_LOG_(INFO) << "SetUpTestCase";
53 mockStub_ = make_shared<MockAssetRecvCallbackStubStub>();
54 }
55
TearDownTestCase(void)56 void AssetRecvCallbackStubTest::TearDownTestCase(void)
57 {
58 GTEST_LOG_(INFO) << "TearDownTestCase";
59 mockStub_ = nullptr;
60 }
61
SetUp(void)62 void AssetRecvCallbackStubTest::SetUp(void)
63 {
64 GTEST_LOG_(INFO) << "SetUp";
65 }
66
TearDown(void)67 void AssetRecvCallbackStubTest::TearDown(void)
68 {
69 GTEST_LOG_(INFO) << "TearDown";
70 }
71
72 /**
73 * @tc.name: AssetRecvCallbackStub_OnRemoteRequest_0100
74 * @tc.desc: The execution of the OnRemoteRequest failed.
75 * @tc.type: FUNC
76 * @tc.require: I7TDJK
77 */
78 HWTEST_F(AssetRecvCallbackStubTest, AssetRecvCallbackStub_OnRemoteRequest_0100, TestSize.Level1)
79 {
80 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_OnRemoteRequest_0100 Start";
81 uint32_t code = static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_START);
82 MessageParcel data;
83 MessageParcel reply;
84 MessageOption option;
85 auto ret = mockStub_->OnRemoteRequest(code, data, reply, option);
86 EXPECT_EQ(ret, AssetRecvCallbackStub::ASSET_RECV_CALLBACK_DESCRIPTOR_IS_EMPTY);
87
88 data.WriteInterfaceToken(IAssetRecvCallback::GetDescriptor());
89 uint32_t errCode = 65535;
90 ret = mockStub_->OnRemoteRequest(errCode, data, reply, option);
91 EXPECT_EQ(ret, 305);
92
93 mockStub_->opToInterfaceMap_.insert(make_pair(errCode, nullptr));
94 data.WriteInterfaceToken(IAssetRecvCallback::GetDescriptor());
95 ret = mockStub_->OnRemoteRequest(errCode, data, reply, option);
96 EXPECT_EQ(ret, 305);
97 mockStub_->opToInterfaceMap_.erase(errCode);
98 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_OnRemoteRequest_0100 End";
99 }
100
101 /**
102 * @tc.name: AssetRecvCallbackStub_OnRemoteRequest_0200
103 * @tc.desc: The execution of the OnRemoteRequest success.
104 * @tc.type: FUNC
105 * @tc.require: I7TDJK
106 */
107 HWTEST_F(AssetRecvCallbackStubTest, AssetRecvCallbackStub_OnRemoteRequest_0200, TestSize.Level1)
108 {
109 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_OnRemoteRequest_0200 Start";
110 uint32_t code = static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_START);
111 MessageParcel data;
112 MessageParcel reply;
113 MessageOption option;
114 data.WriteInterfaceToken(IAssetRecvCallback::GetDescriptor());
115 data.WriteString("srcNetworkId");
116 data.WriteString("destNetworkId");
117 data.WriteString("sessionId");
118 data.WriteString("dstBundleName");
119
120 EXPECT_CALL(*mockStub_, OnStart(_, _, _, _)).WillOnce(Return(E_OK));
121 auto ret = mockStub_->OnRemoteRequest(code, data, reply, option);
122 EXPECT_EQ(ret, E_OK);
123 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_OnRemoteRequest_0200 End";
124 }
125
126 /**
127 * @tc.name: AssetRecvCallbackStub_HandleOnStart_0100
128 * @tc.desc: verify HandleOnStart.
129 * @tc.type: FUNC
130 * @tc.require: I7TDJK
131 */
132 HWTEST_F(AssetRecvCallbackStubTest, AssetRecvCallbackStub_HandleOnStart_0100, TestSize.Level1)
133 {
134 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_HandleOnStart_0100 Start";
135 MessageParcel data;
136 MessageParcel reply;
137 auto ret = mockStub_->HandleOnStart(data, reply);
138 EXPECT_EQ(ret, E_INVAL_ARG);
139
140 data.WriteString("srcNetworkId");
141 ret = mockStub_->HandleOnStart(data, reply);
142 EXPECT_EQ(ret, E_INVAL_ARG);
143
144 data.WriteString("srcNetworkId");
145 data.WriteString("destNetworkId");
146 ret = mockStub_->HandleOnStart(data, reply);
147 EXPECT_EQ(ret, E_INVAL_ARG);
148
149 data.WriteString("srcNetworkId");
150 data.WriteString("destNetworkId");
151 data.WriteString("sessionId");
152 ret = mockStub_->HandleOnStart(data, reply);
153 EXPECT_EQ(ret, E_INVAL_ARG);
154
155 data.WriteString("srcNetworkId");
156 data.WriteString("destNetworkId");
157 data.WriteString("sessionId");
158 data.WriteString("dstBundleName");
159 EXPECT_CALL(*mockStub_, OnStart(_, _, _, _)).WillOnce(Return(E_PERMISSION_DENIED));
160 ret = mockStub_->HandleOnStart(data, reply);
161 EXPECT_EQ(ret, E_BROKEN_IPC);
162 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_HandleOnStart_0100 End";
163 }
164
165 /**
166 * @tc.name: AssetRecvCallbackStub_HandleOnFinished_0100
167 * @tc.desc: verify HandleOnFinished.
168 * @tc.type: FUNC
169 * @tc.require: I7TDJK
170 */
171 HWTEST_F(AssetRecvCallbackStubTest, AssetRecvCallbackStub_HandleOnFinished_0100, TestSize.Level1)
172 {
173 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_HandleOnFinished_0100 Start";
174 MessageParcel data;
175 MessageParcel reply;
176 auto ret = mockStub_->HandleOnFinished(data, reply);
177 EXPECT_EQ(ret, E_INVAL_ARG);
178
179 data.WriteString("srcNetworkId");
180 ret = mockStub_->HandleOnFinished(data, reply);
181 EXPECT_EQ(ret, E_INVAL_ARG);
182
183 data.WriteString("srcNetworkId");
184 sptr<AssetObj> assetObj(new (std::nothrow) AssetObj());
185 data.WriteParcelable(assetObj);
186 ret = mockStub_->HandleOnFinished(data, reply);
187 EXPECT_EQ(ret, E_INVAL_ARG);
188
189 data.WriteString("srcNetworkId");
190 data.WriteParcelable(assetObj);
191 data.WriteInt32(0);
192 EXPECT_CALL(*mockStub_, OnFinished(_, _, _)).WillOnce(Return(E_PERMISSION_DENIED));
193 ret = mockStub_->HandleOnFinished(data, reply);
194 EXPECT_EQ(ret, E_BROKEN_IPC);
195 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_HandleOnFinished_0100 End";
196 }
197
198 /**
199 * @tc.name: AssetRecvCallbackStub_HandleOnFinished_0200
200 * @tc.desc: verify HandleOnFinished.
201 * @tc.type: FUNC
202 * @tc.require: I7TDJK
203 */
204 HWTEST_F(AssetRecvCallbackStubTest, AssetRecvCallbackStub_HandleOnFinished_0200, TestSize.Level1)
205 {
206 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_HandleOnFinished_0200 Start";
207 MessageParcel data;
208 MessageParcel reply;
209
210 data.WriteString("srcNetworkId");
211 sptr<AssetObj> assetObj(new (std::nothrow) AssetObj());
212 data.WriteParcelable(assetObj);
213 data.WriteInt32(0);
214 EXPECT_CALL(*mockStub_, OnFinished(_, _, _)).WillOnce(Return(E_OK));
215 auto ret = mockStub_->HandleOnFinished(data, reply);
216 EXPECT_EQ(ret, E_OK);
217 GTEST_LOG_(INFO) << "AssetRecvCallbackStub_HandleOnFinished_0200 End";
218 }
219 }