mod file permission testcase

Signed-off-by: lvyuanyuan <lvyuanyuan7@huawei.com>
Change-Id: I4153efddf7f82eafe0ac3c03399b97acb9841e72
This commit is contained in:
lvyuanyuan 2024-07-25 09:03:00 +08:00
parent 16bb074711
commit 67450d5799
7 changed files with 96 additions and 34 deletions

View File

@ -85,7 +85,7 @@ private:
deque<struct PolicyErrorResult> &errorResults);
static vector<PolicyInfo> GetPathPolicyInfoFromUriPolicyInfo(const vector<UriPolicyInfo> &uriPolicies,
vector<bool> &errorResults);
static int32_t CheckUriPersistentPermission(uint64_t tokenId,
static int32_t CheckUriPersistentPermission(uint32_t tokenId,
const vector<UriPolicyInfo> &uriPolicies,
vector<bool> &errorResults);
#endif

View File

@ -209,7 +209,7 @@ static bool IsFileManagerUri(const string &pathStr)
return pathStr.find(DOWNLOAD_PATH) == 0 || pathStr.find(DESKTOP_PATH) == 0 || pathStr.find(DOCUMENTS_PATH) == 0;
}
static bool CheckFileManagerUriPermission(uint64_t providerTokenId, const string &pathStr)
static bool CheckFileManagerUriPermission(uint32_t providerTokenId, const string &pathStr)
{
return (IsFileManagerUri(pathStr) && CheckPermission(providerTokenId, FILE_ACCESS_MANAGER_PERMISSION)) ||
(pathStr.find(DOWNLOAD_PATH) == 0 && CheckPermission(providerTokenId, READ_WRITE_DOWNLOAD_PERMISSION)) ||
@ -217,7 +217,7 @@ static bool CheckFileManagerUriPermission(uint64_t providerTokenId, const string
(pathStr.find(DOCUMENTS_PATH) == 0 && CheckPermission(providerTokenId, READ_WRITE_DOCUMENTS_PERMISSION));
}
int32_t FilePermission::CheckUriPersistentPermission(uint64_t tokenId,
int32_t FilePermission::CheckUriPersistentPermission(uint32_t tokenId,
const vector<UriPolicyInfo> &uriPolicies,
vector<bool> &errorResults)
{
@ -226,13 +226,18 @@ int32_t FilePermission::CheckUriPersistentPermission(uint64_t tokenId,
if (pathPolicies.size() == 0) {
return EPERM;
}
vector<bool> resultCodes;
int32_t sandboxManagerErrorCode = SandboxManagerKit::CheckPersistPolicy(tokenId, pathPolicies, resultCodes);
for (size_t i = resultCodes.size(); i < pathPolicies.size(); i++) {
resultCodes.emplace_back(false);
}
for (size_t i = 0; i < pathPolicies.size(); i++) {
if (!resultCodes[i]) {
resultCodes[i] = CheckFileManagerUriPermission(tokenId, pathPolicies[i].path);
}
}
errorCode = ErrorCodeConversion(sandboxManagerErrorCode);
ParseErrorResults(resultCodes, errorResults);
return errorCode;

View File

@ -12,6 +12,7 @@
# limitations under the License.
import("//build/test.gni")
import("//foundation/filemanagement/app_file_service/app_file_service.gni")
ohos_unittest("file_permission_test") {
branch_protector_ret = "pac_ret"
@ -20,14 +21,19 @@ ohos_unittest("file_permission_test") {
cfi = true
cfi_cross_dso = true
debug = false
blocklist = "${app_file_service_path}/cfi_blocklist.txt"
}
module_out_path = "filemanagement/app_file_service"
include_dirs = [
"include",
"${app_file_service_path}/tests/mock/file_permission_native/include",
"//third_party/googletest/include",
]
sources = [ "src/file_permission_test.cpp" ]
sources = [
"${app_file_service_path}/tests/mock/file_permission_native/src/sandbox_mock.cpp",
"src/file_permission_test.cpp",
]
external_deps = [
"ability_base:base",
@ -44,6 +50,11 @@ ohos_unittest("file_permission_test") {
"ipc:ipc_core",
"samgr:samgr_proxy",
]
defines = []
if (sandbox_manarer) {
external_deps += [ "sandbox_manager:libsandbox_manager_sdk" ]
defines += [ "SANDBOX_MANAGER" ]
}
deps = [
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",

View File

@ -17,14 +17,20 @@
#include <gtest/gtest.h>
#include "sandbox_mock.h"
namespace OHOS {
namespace AppFileService {
using namespace std;
class FilePermissionTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp() {};
void TearDown() {};
#ifdef SANDBOX_MANAGER
static inline shared_ptr<SandboxMock> sandboxMock_ = nullptr;
#endif
};
} // namespace AppFileService
} // namespace OHOS

View File

@ -36,6 +36,7 @@
#include "permission_state_full.h"
#include "token_setproc.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS {
namespace AppFileService {
@ -109,10 +110,18 @@ void FilePermissionTest::SetUpTestCase()
tokenIdEx = OHOS::Security::AccessToken::AccessTokenKit::AllocHapToken(g_testInfoParams, g_testPolicyParams);
g_mockToken = tokenIdEx.tokenIdExStruct.tokenID;
EXPECT_EQ(0, SetSelfTokenID(tokenIdEx.tokenIdExStruct.tokenID));
#ifdef SANDBOX_MANAGER
sandboxMock_ = make_shared<SandboxMock>();
SandboxMock::sandboxManagerKitMock = sandboxMock_;
#endif
}
void FilePermissionTest::TearDownTestCase()
{
OHOS::Security::AccessToken::AccessTokenKit::DeleteToken(g_mockToken);
#ifdef SANDBOX_MANAGER
SandboxMock::sandboxManagerKitMock = nullptr;
sandboxMock_ = nullptr;
#endif
}
static bool CheckFileManagerFullMountEnable()
@ -125,6 +134,8 @@ static bool CheckFileManagerFullMountEnable()
LOGE("Not supporting all mounts");
return false;
}
#ifdef SANDBOX_MANAGER
/**
* @tc.name: PersistPermission_test_0000
* @tc.desc: Test function of PersistPermission() interface for SUCCESS.
@ -141,6 +152,7 @@ HWTEST_F(FilePermissionTest, PersistPermission_test_0000, testing::ext::TestSize
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, PersistPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::PersistPermission(uriPolicies, errorResults);
EXPECT_EQ(ret, 0);
GTEST_LOG_(INFO) << "FileShareTest-end PersistPermission_test_0000";
@ -163,6 +175,10 @@ HWTEST_F(FilePermissionTest, PersistPermission_test_0001, testing::ext::TestSize
uriPolicies.emplace_back(infoA);
uriPolicies.emplace_back(infoB);
deque<struct PolicyErrorResult> errorResults;
vector<uint32_t> resultCodes;
resultCodes.push_back(PolicyErrorCode::INVALID_MODE);
EXPECT_CALL(*sandboxMock_, PersistPolicy(_, _))
.WillOnce(DoAll(SetArgReferee<1>(resultCodes), Return(SANDBOX_MANAGER_OK)));
int32_t ret = FilePermission::PersistPermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, EPERM);
@ -188,6 +204,7 @@ HWTEST_F(FilePermissionTest, PersistPermission_test_0002, testing::ext::TestSize
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, PersistPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::PersistPermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, EPERM);
@ -214,6 +231,7 @@ HWTEST_F(FilePermissionTest, ActivatePermission_test_0000, testing::ext::TestSiz
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, StartAccessingPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::ActivatePermission(uriPolicies, errorResults);
EXPECT_EQ(ret, 0);
GTEST_LOG_(INFO) << "FileShareTest-end ActivatePermission_test_0000";
@ -236,6 +254,10 @@ HWTEST_F(FilePermissionTest, ActivatePermission_test_0001, testing::ext::TestSiz
uriPolicies.emplace_back(infoA);
uriPolicies.emplace_back(infoB);
deque<struct PolicyErrorResult> errorResults;
vector<uint32_t> resultCodes;
resultCodes.push_back(PolicyErrorCode::INVALID_MODE);
EXPECT_CALL(*sandboxMock_, StartAccessingPolicy(_, _))
.WillOnce(DoAll(SetArgReferee<1>(resultCodes), Return(SANDBOX_MANAGER_OK)));
int32_t ret = FilePermission::ActivatePermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, EPERM);
@ -261,6 +283,7 @@ HWTEST_F(FilePermissionTest, ActivatePermission_test_0002, testing::ext::TestSiz
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, StartAccessingPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::ActivatePermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, EPERM);
@ -287,6 +310,7 @@ HWTEST_F(FilePermissionTest, DeactivatePermission_test_0000, testing::ext::TestS
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, StopAccessingPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::DeactivatePermission(uriPolicies, errorResults);
EXPECT_EQ(ret, 0);
GTEST_LOG_(INFO) << "FileShareTest-end DeactivatePermission_test_0000";
@ -309,6 +333,10 @@ HWTEST_F(FilePermissionTest, DeactivatePermission_test_0001, testing::ext::TestS
uriPolicies.emplace_back(infoA);
uriPolicies.emplace_back(infoB);
deque<struct PolicyErrorResult> errorResults;
vector<uint32_t> resultCodes;
resultCodes.push_back(PolicyErrorCode::INVALID_MODE);
EXPECT_CALL(*sandboxMock_, StopAccessingPolicy(_, _))
.WillOnce(DoAll(SetArgReferee<1>(resultCodes), Return(SANDBOX_MANAGER_OK)));
int32_t ret = FilePermission::DeactivatePermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, EPERM);
@ -334,6 +362,7 @@ HWTEST_F(FilePermissionTest, DeactivatePermission_test_0002, testing::ext::TestS
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, StopAccessingPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::DeactivatePermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, EPERM);
@ -360,6 +389,7 @@ HWTEST_F(FilePermissionTest, RevokePermission_test_0000, testing::ext::TestSize.
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, UnPersistPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::RevokePermission(uriPolicies, errorResults);
EXPECT_EQ(ret, 0);
GTEST_LOG_(INFO) << "FileShareTest-end RevokePermission_test_0000";
@ -379,6 +409,10 @@ HWTEST_F(FilePermissionTest, RevokePermission_test_0001, testing::ext::TestSize.
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
vector<uint32_t> resultCodes;
resultCodes.push_back(PolicyErrorCode::INVALID_MODE);
EXPECT_CALL(*sandboxMock_, UnPersistPolicy(_, _))
.WillOnce(DoAll(SetArgReferee<1>(resultCodes), Return(SANDBOX_MANAGER_OK)));
int32_t ret = FilePermission::RevokePermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, EPERM);
@ -404,6 +438,7 @@ HWTEST_F(FilePermissionTest, RevokePermission_test_0002, testing::ext::TestSize.
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, UnPersistPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::RevokePermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, EPERM);
@ -493,6 +528,7 @@ HWTEST_F(FilePermissionTest, CheckPersistentPermission_test_0001, testing::ext::
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
vector<bool> errorResults;
EXPECT_CALL(*sandboxMock_, CheckPersistPolicy(_, _, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::CheckPersistentPermission(uriPolicies, errorResults);
if (CheckFileManagerFullMountEnable()) {
EXPECT_EQ(ret, 0);
@ -519,6 +555,8 @@ HWTEST_F(FilePermissionTest, CheckPersistentPermission_test_0002, testing::ext::
std::vector<UriPolicyInfo> uriPolicies;
uriPolicies.emplace_back(infoA);
deque<struct PolicyErrorResult> errorResults;
EXPECT_CALL(*sandboxMock_, PersistPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
EXPECT_CALL(*sandboxMock_, CheckPersistPolicy(_, _, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
int32_t ret = FilePermission::PersistPermission(uriPolicies, errorResults);
EXPECT_EQ(ret, 0);
vector<bool> errorResult;
@ -530,6 +568,7 @@ HWTEST_F(FilePermissionTest, CheckPersistentPermission_test_0002, testing::ext::
} else {
EXPECT_EQ(ret, 0);
};
EXPECT_CALL(*sandboxMock_, UnPersistPolicy(_, _)).WillOnce(Return(SANDBOX_MANAGER_OK));
ret = FilePermission::RevokePermission(uriPolicies, errorResults);
GTEST_LOG_(INFO) << "FileShareTest-end CheckPersistentPermission_test_0002";
}
@ -559,5 +598,6 @@ HWTEST_F(FilePermissionTest, CheckPersistentPermission_test_0003, testing::ext::
};
GTEST_LOG_(INFO) << "FileShareTest-end CheckPersistentPermission_test_0003";
}
#endif
} // namespace AppFileService
} // namespace OHOS

View File

@ -15,42 +15,37 @@
#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_SANDBOX_MOCK_H
#define FILEMANAGEMENT_APP_FILE_SERVICE_SANDBOX_MOCK_H
#ifdef SANDBOX_MANAGER
#include <gmock/gmock.h>
#include <memory>
#include "accesstoken_kit.h"
#include "bundle_constants.h"
#include "hap_token_info.h"
#include "ipc_skeleton.h"
#include "n_error.h"
#include "sandbox_helper.h"
#include "sandbox_manager_err_code.h"
#include "sandbox_manager_kit.h"
#include "tokenid_kit.h"
namespace OHOS::AccessControl::SandboxManager {
class SandboxManagerKit {
namespace OHOS {
namespace AppFileService {
using namespace OHOS::AccessControl::SandboxManager;
class SandboxManagerKitMock {
public:
virtual ~SandboxManagerKit() = default;
virtual int32_t CheckPersistPolicy(uint64_t tokenId,
virtual ~SandboxManagerKitMock() = default;
virtual int32_t CheckPersistPolicy(uint32_t tokenId,
const std::vector<PolicyInfo> &policy,
std::vector<bool> &result) = 0;
virtual int32_t PersistPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result) = 0;
virtual int32_t UnPersistPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result) = 0;
virtual int32_t StartAccessingPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result) = 0;
virtual int32_t StopAccessingPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result) = 0;
static inline std::shared_ptr<SandboxManagerKit> sandboxManagerKit = nullptr;
static inline std::shared_ptr<SandboxManagerKitMock> sandboxManagerKitMock = nullptr;
};
class SandboxMock : public SandboxManagerKit {
class SandboxMock : public SandboxManagerKitMock {
public:
MOCK_METHOD3(CheckPersistPolicy,
int32_t(uint64_t tokenId, const std::vector<PolicyInfo> &policy, std::vector<bool> &result));
int32_t(uint32_t tokenId, const std::vector<PolicyInfo> &policy, std::vector<bool> &result));
MOCK_METHOD2(PersistPolicy, int32_t(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result));
MOCK_METHOD2(UnPersistPolicy, int32_t(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result));
MOCK_METHOD2(StartAccessingPolicy, int32_t(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result));
MOCK_METHOD2(StopAccessingPolicy, int32_t(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result));
};
} // namespace OHOS::AccessControl::SandboxManager
}
}
#endif
#endif

View File

@ -12,31 +12,36 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef SANDBOX_MANAGER
#include "sandbox_mock.h"
namespace OHOS {
using namespace AppFileService;
using namespace OHOS::AccessControl::SandboxManager;
int32_t CheckPersistPolicy(uint64_t tokenId, const std::vector<PolicyInfo> &policy, std::vector<bool> &result)
int32_t SandboxManagerKit::CheckPersistPolicy(uint32_t tokenId,
const std::vector<PolicyInfo> &policy, std::vector<bool> &result)
{
return SandboxManagerKit::sandboxManagerKit->CheckPersistPolicy(tokenId, policy, result);
return SandboxManagerKitMock::sandboxManagerKitMock->CheckPersistPolicy(tokenId, policy, result);
}
int32_t PersistPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result)
int32_t SandboxManagerKit::PersistPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result)
{
return SandboxManagerKit::sandboxManagerKit->PersistPolicy(policy, result);
return SandboxManagerKitMock::sandboxManagerKitMock->PersistPolicy(policy, result);
}
int32_t UnPersistPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result)
int32_t SandboxManagerKit::UnPersistPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result)
{
return SandboxManagerKit::sandboxManagerKit->UnPersistPolicy(policy, result);
return SandboxManagerKitMock::sandboxManagerKitMock->UnPersistPolicy(policy, result);
}
int32_t StartAccessingPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result)
int32_t SandboxManagerKit::StartAccessingPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result)
{
return SandboxManagerKit::sandboxManagerKit->StartAccessingPolicy(policy, result);
return SandboxManagerKitMock::sandboxManagerKitMock->StartAccessingPolicy(policy, result);
}
int32_t StopAccessingPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result)
int32_t SandboxManagerKit::StopAccessingPolicy(const std::vector<PolicyInfo> &policy, std::vector<uint32_t> &result)
{
return SandboxManagerKit::sandboxManagerKit->StopAccessingPolicy(policy, result);
}
return SandboxManagerKitMock::sandboxManagerKitMock->StopAccessingPolicy(policy, result);
}
}
#endif