1 /*
2 * Copyright (c) 2022-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 <cstdio>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include "b_error/b_error.h"
21 #include "ext_extension_mock.h"
22 #include "module_ipc/svc_extension_proxy.h"
23 #include "unique_fd.h"
24
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27 using namespace testing;
28
29 class SvcExtensionProxyTest : public testing::Test {
30 public:
SetUpTestCase(void)31 static void SetUpTestCase(void) {};
TearDownTestCase()32 static void TearDownTestCase() {};
33 void SetUp() override;
34 void TearDown() override;
35 sptr<SvcExtensionProxy> proxy_ = nullptr;
36 sptr<BackupExtExtensionMock> mock_ = nullptr;
37 };
38
SetUp()39 void SvcExtensionProxyTest::SetUp()
40 {
41 mock_ = sptr(new BackupExtExtensionMock());
42 proxy_ = sptr(new SvcExtensionProxy(mock_));
43 }
TearDown()44 void SvcExtensionProxyTest::TearDown()
45 {
46 mock_ = nullptr;
47 proxy_ = nullptr;
48 }
49
50 /**
51 * @tc.number: SUB_Ext_Extension_proxy_GetFileHandle_0100
52 * @tc.name: SUB_Ext_Extension_proxy_GetFileHandle_0100
53 * @tc.desc: 测试 GetFileHandle 获取真实文件接口调用成功和失败
54 * @tc.size: MEDIUM
55 * @tc.type: FUNC
56 * @tc.level Level 1
57 * @tc.require: I6F3GV
58 */
59 HWTEST_F(SvcExtensionProxyTest, SUB_Ext_Extension_proxy_GetFileHandle_0100, testing::ext::TestSize.Level1)
60 {
61 GTEST_LOG_(INFO) << "SvcExtensionProxyTest-begin SUB_Ext_Extension_proxy_GetFileHandle_0100";
62 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
63 .Times(2)
64 .WillOnce(Invoke(mock_.GetRefPtr(), &BackupExtExtensionMock::InvokeGetFileHandleRequest))
65 .WillOnce(Return(EPERM));
66 string fileName = "1.tar";
67 UniqueFd fd = proxy_->GetFileHandle(fileName);
68 EXPECT_GT(fd, BError(BError::Codes::OK));
69
70 UniqueFd fdErr = proxy_->GetFileHandle(fileName);
71 EXPECT_LT(fdErr, BError(BError::Codes::OK));
72 GTEST_LOG_(INFO) << "SvcExtensionProxyTest-end SUB_Ext_Extension_proxy_GetFileHandle_0100";
73 }
74
75 /**
76 * @tc.number: SUB_Ext_Extension_proxy_HandleClear_0100
77 * @tc.name: SUB_Ext_Extension_proxy_HandleClear_0100
78 * @tc.desc: 测试 HandleClear 接口调用成功和失败
79 * @tc.size: MEDIUM
80 * @tc.type: FUNC
81 * @tc.level Level 1
82 * @tc.require: I6F3GV
83 */
84 HWTEST_F(SvcExtensionProxyTest, SUB_Ext_Extension_proxy_HandleClear_0100, testing::ext::TestSize.Level1)
85 {
86 GTEST_LOG_(INFO) << "SvcExtensionProxyTest-begin SUB_Ext_Extension_proxy_HandleClear_0100";
87 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
88 .Times(2)
89 .WillOnce(Invoke(mock_.GetRefPtr(), &BackupExtExtensionMock::InvokeSendRequest))
90 .WillOnce(Return(EPERM));
91 ErrCode ret = proxy_->HandleClear();
92 EXPECT_EQ(BError(BError::Codes::OK), ret);
93
94 ret = proxy_->HandleClear();
95 EXPECT_NE(BError(BError::Codes::OK), ret);
96 GTEST_LOG_(INFO) << "SvcExtensionProxyTest-end SUB_Ext_Extension_proxy_HandleClear_0100";
97 }
98
99 /**
100 * @tc.number: SUB_Ext_Extension_proxy_HandleBackup_0100
101 * @tc.name: SUB_Ext_Extension_proxy_HandleBackup_0100
102 * @tc.desc: 测试 HandleBackup 接口调用成功和失败
103 * @tc.size: MEDIUM
104 * @tc.type: FUNC
105 * @tc.level Level 1
106 * @tc.require: I6F3GV
107 */
108 HWTEST_F(SvcExtensionProxyTest, SUB_Ext_Extension_proxy_HandleBackup_0100, testing::ext::TestSize.Level1)
109 {
110 GTEST_LOG_(INFO) << "SvcExtensionProxyTest-begin SUB_Ext_Extension_proxy_HandleBackup_0100";
111 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
112 .Times(2)
113 .WillOnce(Invoke(mock_.GetRefPtr(), &BackupExtExtensionMock::InvokeSendRequest))
114 .WillOnce(Return(EPERM));
115 ErrCode ret = proxy_->HandleBackup();
116 EXPECT_EQ(BError(BError::Codes::OK), ret);
117
118 ret = proxy_->HandleBackup();
119 EXPECT_NE(BError(BError::Codes::OK), ret);
120 GTEST_LOG_(INFO) << "SvcExtensionProxyTest-end SUB_Ext_Extension_proxy_HandleBackup_0100";
121 }
122
123 /**
124 * @tc.number: SUB_Ext_Extension_proxy_PublishFile_0100
125 * @tc.name: SUB_Ext_Extension_proxy_PublishFile_0100
126 * @tc.desc: 测试 PublishFile 接口调用成功和失败
127 * @tc.size: MEDIUM
128 * @tc.type: FUNC
129 * @tc.level Level 1
130 * @tc.require: I6F3GV
131 */
132 HWTEST_F(SvcExtensionProxyTest, SUB_Ext_Extension_proxy_PublishFile_0100, testing::ext::TestSize.Level1)
133 {
134 GTEST_LOG_(INFO) << "SvcExtensionProxyTest-begin SUB_Ext_Extension_proxy_PublishFile_0100";
135 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
136 .Times(2)
137 .WillOnce(Invoke(mock_.GetRefPtr(), &BackupExtExtensionMock::InvokeSendRequest))
138 .WillOnce(Return(EPERM));
139 string fileName = "1.tar";
140 ErrCode ret = proxy_->PublishFile(fileName);
141 EXPECT_EQ(BError(BError::Codes::OK), ret);
142
143 ret = proxy_->PublishFile(fileName);
144 EXPECT_NE(BError(BError::Codes::OK), ret);
145 GTEST_LOG_(INFO) << "SvcExtensionProxyTest-end SUB_Ext_Extension_proxy_PublishFile_0100";
146 }
147 } // namespace OHOS::FileManagement::Backup