!501 静默访问权限校验添加测试用例

Merge pull request !501 from wangjiaqiang/master
This commit is contained in:
openharmony_ci 2024-04-13 14:49:21 +00:00 committed by Gitee
commit a05ff951c8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 325 additions and 13 deletions

View File

@ -24,6 +24,8 @@ let DDL_TBL_CREATE = "CREATE TABLE IF NOT EXISTS "
+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY, number INTEGER)";
let DDL_TABLE_USER_SQL = "CREATE TABLE IF NOT EXISTS user (userId INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName TEXT, age INTEGER , balance DOUBLE NOT NULL)";
let DDL_TABLE_BOOK_SQL = "CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, userId INTEGER, FOREIGN KEY (userId) REFERENCES user (userId) ON UPDATE NO ACTION ON DELETE CASCADE)";
let DDL_TABLE_PERMISSION1_SQL = "CREATE TABLE IF NOT EXISTS permission1 (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY, number INTEGER)";
let DDL_TABLE_PERMISSION2_SQL = "CREATE TABLE IF NOT EXISTS permission2 (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY, number INTEGER)";
let rdbStore;
@ -38,6 +40,8 @@ extends Extension {
await rdbStore.executeSql(DDL_TBL_CREATE, []);
await rdbStore.executeSql(DDL_TABLE_USER_SQL, []);
await rdbStore.executeSql(DDL_TABLE_BOOK_SQL, []);
await rdbStore.executeSql(DDL_TABLE_PERMISSION1_SQL, []);
await rdbStore.executeSql(DDL_TABLE_PERMISSION2_SQL, []);
console.log('[ttt] [DataShareTest] DataShareExtAbility executeSql multiple tables done');
let err = {"code":0};
callback(err);

View File

@ -71,6 +71,24 @@
"name": "dataProperties",
"resource": "$profile:test"
}
},
{
"uri": "datashareproxy://com.acts.datasharetest/entry/DB00/permission1",
"requiredReadPermission":"ohos.permission.GET_BUNDLE_INFO",
"requiredWritePermission":"ohos.permission.WRITE_CALL_LOG",
"metadata": {
"name": "dataProperties",
"resource": "$profile:permission1"
}
},
{
"uri": "datashareproxy://com.acts.datasharetest/entry/DB00/permission2",
"requiredReadPermission":"ohos.permission.GET_BUNDLE_INFO",
"requiredWritePermission":"ohos.permission.WRITE_CALL_LOG",
"metadata": {
"name": "dataProperties",
"resource": "$profile:permission2"
}
}
]
}

View File

@ -0,0 +1,19 @@
{
"path": "DB00/permission1",
"type": "rdb",
"tableConfig":[
{
"uri":"*",
"crossUserMode":1
},
{
"uri":"datashare://com.acts.datasharetest/entry/DB00/permission1",
"crossUserMode":1
},
{
"uri":"datashareproxy://com.acts.datasharetest/entry/DB00/permission1",
"crossUserMode":1
}
],
"isSilentProxyEnable":true
}

View File

@ -0,0 +1,19 @@
{
"path": "DB00/permission2",
"type": "rdb",
"tableConfig":[
{
"uri":"*",
"crossUserMode":1
},
{
"uri":"datashare://com.acts.datasharetest/entry/DB00/permission1",
"crossUserMode":1
},
{
"uri":"datashareproxy://com.acts.datasharetest/entry/DB00/permission1",
"crossUserMode":1
}
],
"isSilentProxyEnable":true
}

View File

@ -14,6 +14,7 @@
*/
#include <gtest/gtest.h>
#include <unistd.h>
#include <vector>
#include "accesstoken_kit.h"
#include "data_ability_observer_stub.h"
@ -29,9 +30,14 @@ namespace DataShare {
using namespace testing::ext;
using namespace OHOS::Security::AccessToken;
constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
static int USER_100 = 100;
std::string DATA_SHARE_URI = "datashare:///com.acts.datasharetest";
std::string SLIENT_ACCESS_URI = "datashare:///com.acts.datasharetest/entry/DB00/TBL00?Proxy=true";
std::string SLIENT_REGISTER_URI = "datashare:///com.acts.datasharetest/entry/DB00/TBL02?Proxy=true";
std::string SLIENT_ACCESS_PERMISSION1_URI = "datashare:///com.acts.datasharetest/entry/DB00/permission1?Proxy=true";
std::string SLIENT_PROXY_PERMISSION1_URI = "datashareproxy://com.acts.datasharetest/entry/DB00/permission1";
std::string SLIENT_ACCESS_PERMISSION2_URI = "datashare:///com.acts.datasharetest/entry/DB00/permission2?Proxy=true";
std::string SLIENT_PROXY_PERMISSION2_URI = "datashareproxy://com.acts.datasharetest/entry/DB00/permission2";
std::string TBL_STU_NAME = "name";
std::string TBL_STU_AGE = "age";
std::shared_ptr<DataShare::DataShareHelper> g_slientAccessHelper;
@ -85,6 +91,34 @@ std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(int32_t system
return DataShare::DataShareHelper::Creator(remoteObj, uri);
}
std::vector<PermissionStateFull> GetPermissionStateFulls()
{
std::vector<PermissionStateFull> permissionStateFulls = {
{
.permissionName = "ohos.permission.WRITE_CONTACTS",
.isGeneral = true,
.resDeviceID = { "local" },
.grantStatus = { PermissionState::PERMISSION_GRANTED },
.grantFlags = { 1 }
},
{
.permissionName = "ohos.permission.WRITE_CALL_LOG",
.isGeneral = true,
.resDeviceID = { "local" },
.grantStatus = { PermissionState::PERMISSION_GRANTED },
.grantFlags = { 1 }
},
{
.permissionName = "ohos.permission.GET_BUNDLE_INFO",
.isGeneral = true,
.resDeviceID = { "local" },
.grantStatus = { PermissionState::PERMISSION_GRANTED },
.grantFlags = { 1 }
}
};
return permissionStateFulls;
}
void SlientAccessTest::SetUpTestCase(void)
{
LOG_INFO("SetUpTestCase invoked");
@ -99,6 +133,7 @@ void SlientAccessTest::SetUpTestCase(void)
.instIndex = 0,
.appIDDesc = "ohos.datashareclienttest.demo"
};
auto permStateList = GetPermissionStateFulls();
HapPolicyParams policy = {
.apl = APL_NORMAL,
.domain = "test.domain",
@ -114,20 +149,12 @@ void SlientAccessTest::SetUpTestCase(void)
.descriptionId = 1
}
},
.permStateList = {
{
.permissionName = "ohos.permission.test",
.isGeneral = true,
.resDeviceID = { "local" },
.grantStatus = { PermissionState::PERMISSION_GRANTED },
.grantFlags = { 1 }
}
}
.permStateList = permStateList
};
AccessTokenKit::AllocHapToken(info, policy);
auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID(
auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenIDEx(
info.userID, info.bundleName, info.instIndex);
SetSelfTokenID(testTokenId);
SetSelfTokenID(testTokenId.tokenIDEx);
g_slientAccessHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
ASSERT_TRUE(g_slientAccessHelper != nullptr);
@ -136,8 +163,8 @@ void SlientAccessTest::SetUpTestCase(void)
void SlientAccessTest::TearDownTestCase(void)
{
auto tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.datashareclienttest.demo", 0);
AccessTokenKit::DeleteToken(tokenId);
auto tokenId = AccessTokenKit::GetHapTokenIDEx(100, "ohos.datashareclienttest.demo", 0);
AccessTokenKit::DeleteToken(tokenId.tokenIDEx);
g_slientAccessHelper = nullptr;
}
@ -310,5 +337,230 @@ HWTEST_F(SlientAccessTest, SlientAccess_NoRegister_Test_002, TestSize.Level0)
EXPECT_EQ((retVal >= 0), true);
LOG_INFO("SlientAccess_NoRegister_Test_002::End");
}
HWTEST_F(SlientAccessTest, SlientAccess_Permission_Insert_Test_001, TestSize.Level0)
{
LOG_INFO("SlientAccess_Permission_Insert_Test_001::Start");
auto helper = g_slientAccessHelper;
Uri uri(SLIENT_ACCESS_URI);
DataShare::DataShareValuesBucket valuesBucket;
std::string value = "lisi";
valuesBucket.Put(TBL_STU_NAME, value);
int age = 25;
valuesBucket.Put(TBL_STU_AGE, age);
int retVal = helper->Insert(uri, valuesBucket);
EXPECT_EQ((retVal > 0), true);
LOG_INFO("SlientAccess_Permission_Insert_Test_001::End");
}
HWTEST_F(SlientAccessTest, SlientAccess_Permission_Insert_Test_003, TestSize.Level0)
{
LOG_INFO("SlientAccess_Permission_Insert_Test_003::Start");
auto helper = g_slientAccessHelper;
Uri uri(SLIENT_PROXY_PERMISSION1_URI);
DataShare::DataShareValuesBucket valuesBucket;
std::string value = "lisi";
valuesBucket.Put(TBL_STU_NAME, value);
int age = 25;
valuesBucket.Put(TBL_STU_AGE, age);
int retVal = helper->Insert(uri, valuesBucket);
EXPECT_EQ((retVal > 0), true);
LOG_INFO("SlientAccess_Permission_Insert_Test_003::End");
}
HWTEST_F(SlientAccessTest, SlientAccess_Permission_Update_Test_001, TestSize.Level0)
{
LOG_INFO("SlientAccess_Permission_Update_Test_001::Start");
auto helper = g_slientAccessHelper;
Uri uri(SLIENT_PROXY_PERMISSION1_URI);
DataShare::DataShareValuesBucket valuesBucket;
int value = 50;
valuesBucket.Put(TBL_STU_AGE, value);
DataShare::DataSharePredicates predicates;
std::string selections = TBL_STU_NAME + " = 'lisi'";
predicates.SetWhereClause(selections);
int retVal = helper->Update(uri, predicates, valuesBucket);
EXPECT_EQ((retVal > 0), true);
LOG_INFO("SlientAccess_Permission_Update_Test_001::End");
}
HWTEST_F(SlientAccessTest, SlientAccess_Permission_Query_Test_002, TestSize.Level0)
{
LOG_INFO("SlientAccess_Permission_Query_Test_002::Start");
auto helper = g_slientAccessHelper;
Uri uri(SLIENT_PROXY_PERMISSION2_URI);
DataShare::DataShareValuesBucket valuesBucket;
std::string value = "lisi";
valuesBucket.Put(TBL_STU_NAME, value);
int age = 25;
valuesBucket.Put(TBL_STU_AGE, age);
int retVal = helper->Insert(uri, valuesBucket);
EXPECT_EQ((retVal > 0), true);
DataShare::DataSharePredicates predicates;
predicates.EqualTo(TBL_STU_NAME, "lisi");
vector<string> columns;
DatashareBusinessError businessError;
auto resultSet = helper->Query(uri, predicates, columns, &businessError);
int result = 0;
if (resultSet != nullptr) {
resultSet->GetRowCount(result);
}
EXPECT_EQ(result, 1);
EXPECT_EQ(businessError.GetCode(), 0);
LOG_INFO("SlientAccess_Permission_Query_Test_002::End");
}
HWTEST_F(SlientAccessTest, SlientAccess_Permission_Delete_Test_001, TestSize.Level0)
{
LOG_INFO("SlientAccess_Permission_Delete_Test_001::Start");
auto helper = g_slientAccessHelper;
Uri uri(SLIENT_PROXY_PERMISSION2_URI);
DataShare::DataSharePredicates deletePredicates;
std::string selections = TBL_STU_NAME + " = 'lisi'";
deletePredicates.SetWhereClause(selections);
int retVal = helper->Delete(uri, deletePredicates);
EXPECT_EQ(retVal, 1);
LOG_INFO("SlientAccess_Permission_Delete_Test_001::End");
}
HWTEST_F(SlientAccessTest, SlientAccess_Permission_Insert_Test_002, TestSize.Level0)
{
LOG_INFO("SlientAccess_Permission_Insert_Test_002::Start");
HapInfoParams info = {
.userID = USER_100,
.bundleName = "ohos.permission.write.demo",
.instIndex = 0,
.isSystemApp = true,
.apiVersion = 8,
.appIDDesc = "ohos.permission.write.demo"
};
HapPolicyParams policy = {
.apl = APL_SYSTEM_CORE,
.domain = "test.domain",
.permStateList = {
{
.permissionName = "ohos.permission.WRITE_CONTACTS",
.isGeneral = true,
.resDeviceID = { "local" },
.grantStatus = { PermissionState::PERMISSION_GRANTED },
.grantFlags = { 1 }
}
}
};
AccessTokenKit::AllocHapToken(info, policy);
auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenIDEx(
info.userID, info.bundleName, info.instIndex);
SetSelfTokenID(testTokenId.tokenIDEx);
auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
Uri uri(SLIENT_ACCESS_PERMISSION1_URI);
DataShare::DataShareValuesBucket valuesBucket;
std::string value = "lisi";
valuesBucket.Put(TBL_STU_NAME, value);
int age = 25;
valuesBucket.Put(TBL_STU_AGE, age);
int retVal = helper->Insert(uri, valuesBucket);
EXPECT_EQ(retVal, -2);
helper = nullptr;
AccessTokenKit::DeleteToken(testTokenId.tokenIDEx);
LOG_INFO("SlientAccess_Permission_Insert_Test_002::End");
}
HWTEST_F(SlientAccessTest, SlientAccess_Permission_Update_Test_002, TestSize.Level0)
{
LOG_INFO("SlientAccess_Permission_Update_Test_002::Start");
HapInfoParams info = {
.userID = USER_100,
.bundleName = "ohos.permission.write.demo",
.instIndex = 0,
.isSystemApp = true,
.apiVersion = 8,
.appIDDesc = "ohos.permission.write.demo"
};
HapPolicyParams policy = {
.apl = APL_SYSTEM_CORE,
.domain = "test.domain",
.permStateList = {
{
.permissionName = "ohos.permission.WRITE_CONTACTS",
.isGeneral = true,
.resDeviceID = { "local" },
.grantStatus = { PermissionState::PERMISSION_GRANTED },
.grantFlags = { 1 }
}
}
};
AccessTokenKit::AllocHapToken(info, policy);
auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenIDEx(
info.userID, info.bundleName, info.instIndex);
SetSelfTokenID(testTokenId.tokenIDEx);
auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
Uri uri(SLIENT_ACCESS_PERMISSION1_URI);
DataShare::DataShareValuesBucket valuesBucket;
int value = 50;
valuesBucket.Put(TBL_STU_AGE, value);
DataShare::DataSharePredicates predicates;
std::string selections = TBL_STU_NAME + " = 'lisi'";
predicates.SetWhereClause(selections);
int retVal = helper->Update(uri, predicates, valuesBucket);
EXPECT_EQ(retVal, -2);
helper = nullptr;
AccessTokenKit::DeleteToken(testTokenId.tokenIDEx);
LOG_INFO("SlientAccess_Permission_Update_Test_002::End");
}
HWTEST_F(SlientAccessTest, SlientAccess_Permission_Query_Test_001, TestSize.Level0)
{
LOG_INFO("SlientAccess_Permission_Query_Test_001::Start");
HapInfoParams info = {
.userID = USER_100,
.bundleName = "ohos.permission.write.demo",
.instIndex = 0,
.isSystemApp = true,
.apiVersion = 8,
.appIDDesc = "ohos.permission.write.demo"
};
HapPolicyParams policy = {
.apl = APL_SYSTEM_CORE,
.domain = "test.domain",
.permStateList = {
{
.permissionName = "ohos.permission.WRITE_CONTACTS",
.isGeneral = true,
.resDeviceID = { "local" },
.grantStatus = { PermissionState::PERMISSION_GRANTED },
.grantFlags = { 1 }
}
}
};
AccessTokenKit::AllocHapToken(info, policy);
auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenIDEx(
info.userID, info.bundleName, info.instIndex);
SetSelfTokenID(testTokenId.tokenIDEx);
auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
Uri uri(SLIENT_ACCESS_PERMISSION2_URI);
DataShare::DataSharePredicates predicates;
predicates.EqualTo(TBL_STU_NAME, "lisi");
vector<string> columns;
DatashareBusinessError businessError;
auto resultSet = helper->Query(uri, predicates, columns, &businessError);
int result = 0;
if (resultSet != nullptr) {
resultSet->GetRowCount(result);
}
EXPECT_EQ(result, 0);
EXPECT_EQ(resultSet, nullptr);
EXPECT_EQ(businessError.GetCode(), -2);
helper = nullptr;
AccessTokenKit::DeleteToken(testTokenId.tokenIDEx);
LOG_INFO("SlientAccess_Permission_Query_Test_001::End");
}
} // namespace DataShare
} // namespace OHOS