新增ReportSecurityInfoAsync接口用于异步上送安全事件

Signed-off-by: jinfangxin <jinfangxin@huawei.com>
Change-Id: I2661d41e1083b399656a24bbc325da101e07ae1b
This commit is contained in:
jinfangxin 2024-11-18 21:35:45 +08:00
parent 899e74b196
commit f35119e624
11 changed files with 105 additions and 29 deletions

View File

@ -23,6 +23,30 @@
#include "sg_collect_client.h"
namespace OHOS::Security::SecurityGuard {
namespace {
int32_t ReportSecurityEvent(const std::shared_ptr<EventInfo> &info, bool isSync)
{
if (info == nullptr) {
return BAD_PARAM;
}
sptr<IDataCollectManager> proxy = SgCollectClient::GetInstance().GetProxy();
if (proxy == nullptr) {
return NULL_OBJECT;
}
int64_t eventId = info->GetEventId();
std::string version = info->GetVersion();
std::string content = info->GetContent();
std::string date = SecurityGuardUtils::GetDate();
int32_t ret = proxy->RequestDataSubmit(eventId, version, date, content, isSync);
if (ret != SUCCESS) {
SGLOGE("RequestSecurityInfo error, ret=%{public}d", ret);
return ret;
}
return SUCCESS;
}
}
SgCollectClient &SgCollectClient::GetInstance()
{
static SgCollectClient instance;
@ -80,24 +104,12 @@ void SgCollectClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remo
int32_t NativeDataCollectKit::ReportSecurityInfo(const std::shared_ptr<EventInfo> &info)
{
if (info == nullptr) {
return BAD_PARAM;
}
sptr<IDataCollectManager> proxy = SgCollectClient::GetInstance().GetProxy();
if (proxy == nullptr) {
return NULL_OBJECT;
}
return ReportSecurityEvent(info, true);
}
int64_t eventId = info->GetEventId();
std::string version = info->GetVersion();
std::string content = info->GetContent();
std::string date = SecurityGuardUtils::GetDate();
int32_t ret = proxy->RequestDataSubmit(eventId, version, date, content);
if (ret != SUCCESS) {
SGLOGE("RequestSecurityInfo error, ret=%{public}d", ret);
return ret;
}
return SUCCESS;
int32_t NativeDataCollectKit::ReportSecurityInfoAsync(const std::shared_ptr<EventInfo> &info)
{
return ReportSecurityEvent(info, false);
}
int32_t NativeDataCollectKit::SecurityGuardConfigUpdate(int32_t fd, const std::string &name)
@ -118,7 +130,7 @@ int32_t NativeDataCollectKit::SecurityGuardConfigUpdate(int32_t fd, const std::s
} // namespace OHOS::Security::SecurityGuard
static int32_t ReportSecurityInfoImpl(const struct EventInfoSt *info)
static int32_t ReportSecurityInfoImpl(const struct EventInfoSt *info, bool isSync)
{
if (info == nullptr || info->contentLen >= CONTENT_MAX_LEN || info->version == nullptr) {
return OHOS::Security::SecurityGuard::BAD_PARAM;
@ -133,7 +145,11 @@ static int32_t ReportSecurityInfoImpl(const struct EventInfoSt *info)
}
std::string content(reinterpret_cast<const char *>(tmp));
auto eventInfo = std::make_shared<OHOS::Security::SecurityGuard::EventInfo>(eventId, version, content);
return OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(eventInfo);
if (isSync) {
return OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(eventInfo);
} else {
return OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfoAsync(eventInfo);
}
}
#ifdef __cplusplus
@ -142,7 +158,12 @@ extern "C" {
int32_t ReportSecurityInfo(const struct EventInfoSt *info)
{
return ReportSecurityInfoImpl(info);
return ReportSecurityInfoImpl(info, true);
}
int32_t ReportSecurityInfoAsync(const struct EventInfoSt *info)
{
return ReportSecurityInfoImpl(info, false);
}
int32_t SecurityGuardConfigUpdate(int32_t fd, const char *fileName)

View File

@ -32,6 +32,7 @@ using namespace OHOS::Security::SecurityGuardTest;
extern "C" {
#endif
int32_t ReportSecurityInfo(const struct EventInfoSt *info);
int32_t ReportSecurityInfoAsync(const struct EventInfoSt *info);
#ifdef __cplusplus
}
#endif
@ -202,6 +203,28 @@ HWTEST_F(DataCollectKitTest, ReportSecurityInfo006, TestSize.Level1)
EXPECT_EQ(ret, SecurityGuard::BAD_PARAM);
}
/**
* @tc.name: ReportSecurityInfoAsync001
* @tc.desc: ReportSecurityInfoAsync with right param
* @tc.type: FUNC
* @tc.require: SR000H96L5
*/
HWTEST_F(DataCollectKitTest, ReportSecurityInfoAsync001, TestSize.Level1)
{
static int64_t eventId = 1011009000;
static std::string version = "0";
static std::string content = "{\"cred\":0,\"extra\":\"\",\"status\":0}";
EventInfoSt info;
info.eventId = eventId;
info.version = version.c_str();
(void) memset_s(info.content, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
errno_t rc = memcpy_s(info.content, CONTENT_MAX_LEN, content.c_str(), content.length());
EXPECT_TRUE(rc == EOK);
info.contentLen = static_cast<uint32_t>(content.length());
int ret = ReportSecurityInfoAsync(&info);
EXPECT_EQ(ret, SecurityGuard::SUCCESS);
}
/**
* @tc.name: ReleaseProxy001
* @tc.desc: SgCollectClient ReleaseProxy

View File

@ -25,7 +25,7 @@ DataCollectManagerProxy::DataCollectManagerProxy(const sptr<IRemoteObject> &impl
}
int32_t DataCollectManagerProxy::RequestDataSubmit(int64_t eventId, std::string &version,
std::string &time, std::string &content)
std::string &time, std::string &content, bool isSync)
{
SGLOGD("enter DataCollectManagerProxy RequestDataSubmit");
SGLOGD("eventId=%{public}" PRId64 ", version=%{public}s", eventId, version.c_str());
@ -40,7 +40,7 @@ int32_t DataCollectManagerProxy::RequestDataSubmit(int64_t eventId, std::string
data.WriteString(time);
data.WriteString(content);
MessageOption option = { MessageOption::TF_SYNC };
MessageOption option = { isSync ? MessageOption::TF_SYNC : MessageOption::TF_ASYNC };
sptr<IRemoteObject> remote = Remote();
if (remote == nullptr) {
SGLOGE("Remote error");
@ -51,8 +51,10 @@ int32_t DataCollectManagerProxy::RequestDataSubmit(int64_t eventId, std::string
SGLOGE("ret=%{public}d", ret);
return ret;
}
ret = reply.ReadInt32();
SGLOGD("reply=%{public}d", ret);
if (isSync) {
ret = reply.ReadInt32();
SGLOGD("reply=%{public}d", ret);
}
return ret;
}

View File

@ -28,6 +28,7 @@
extern "C" {
#endif
int32_t ReportSecurityInfo(const struct EventInfoSt *info);
int32_t ReportSecurityInfoAsync(const struct EventInfoSt *info);
int32_t SecurityGuardConfigUpdate(int32_t fd, const char *fileName);
#ifdef __cplusplus
@ -38,6 +39,7 @@ namespace OHOS::Security::SecurityGuard {
class NativeDataCollectKit {
public:
static int32_t ReportSecurityInfo(const std::shared_ptr<EventInfo> &info);
static int32_t ReportSecurityInfoAsync(const std::shared_ptr<EventInfo> &info);
static int32_t SecurityGuardConfigUpdate(int32_t fd, const std::string &fileName);
};

View File

@ -46,7 +46,7 @@ public:
};
virtual int32_t RequestDataSubmit(int64_t eventId, std::string &version, std::string &time,
std::string &content) = 0;
std::string &content, bool isSync = true) = 0;
virtual int32_t RequestRiskData(std::string &devId, std::string &eventList,
const sptr<IRemoteObject> &callback) = 0;
virtual int32_t Subscribe(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,

View File

@ -30,7 +30,8 @@ public:
explicit DataCollectManagerProxy(const sptr<IRemoteObject> &impl);
~DataCollectManagerProxy() override = default;
int32_t RequestRiskData(std::string &devId, std::string &eventList, const sptr<IRemoteObject> &callback) override;
int32_t RequestDataSubmit(int64_t eventId, std::string &version, std::string &time, std::string &content) override;
int32_t RequestDataSubmit(int64_t eventId, std::string &version, std::string &time,
std::string &content, bool isSync = true) override;
int32_t Subscribe(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
const sptr<IRemoteObject> &callback) override;
int32_t Unsubscribe(const sptr<IRemoteObject> &callback) override;

View File

@ -166,9 +166,10 @@ void DataCollectManagerService::DumpEventInfo(int fd, int64_t eventId)
}
int32_t DataCollectManagerService::RequestDataSubmit(int64_t eventId, std::string &version, std::string &time,
std::string &content)
std::string &content, bool isSync)
{
SGLOGD("enter DataCollectManagerService RequestDataSubmit");
SGLOGD("isSync: %{public}s", isSync ? "true" : "false");
int32_t ret = IsApiHasPermission("RequestDataSubmit");
if (ret != SUCCESS) {
return ret;

View File

@ -37,7 +37,8 @@ public:
void OnStart() override;
void OnStop() override;
int Dump(int fd, const std::vector<std::u16string>& args) override;
int32_t RequestDataSubmit(int64_t eventId, std::string &version, std::string &time, std::string &content) override;
int32_t RequestDataSubmit(int64_t eventId, std::string &version, std::string &time,
std::string &content, bool isSync = true) override;
int32_t RequestRiskData(std::string &devId, std::string &eventList, const sptr<IRemoteObject> &callback) override;
int32_t Subscribe(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
const sptr<IRemoteObject> &callback) override;

View File

@ -36,7 +36,8 @@ public:
void OnStart() override;
void OnStop() override;
int Dump(int fd, const std::vector<std::u16string>& args) override;
int32_t RequestDataSubmit(int64_t eventId, std::string &version, std::string &time, std::string &content) override;
int32_t RequestDataSubmit(int64_t eventId, std::string &version, std::string &time,
std::string &content, bool isSync = true) override;
int32_t RequestRiskData(std::string &devId, std::string &eventList, const sptr<IRemoteObject> &callback) override;
int32_t Subscribe(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
const sptr<IRemoteObject> &callback) override;

View File

@ -463,6 +463,26 @@ HWTEST_F(SecurityGuardDataCollectSaTest, RequestDataSubmit_Success02, TestSize.L
EXPECT_EQ(result, SUCCESS);
}
HWTEST_F(SecurityGuardDataCollectSaTest, RequestDataSubmit_Success03, TestSize.Level1)
{
int64_t eventId = 1;
std::string version = "1.0";
std::string time = "2022-01-01";
std::string content = "content";
EXPECT_CALL(*(AccessToken::AccessTokenKit::GetInterface()), VerifyAccessToken)
.WillOnce(Return(AccessToken::PermissionState::PERMISSION_GRANTED));
EXPECT_CALL(*(AccessToken::AccessTokenKit::GetInterface()), GetTokenType)
.WillOnce(Return(AccessToken::TypeATokenTypeEnum::TOKEN_HAP));
EXPECT_CALL(*(AccessToken::TokenIdKit::GetInterface()), IsSystemAppByFullTokenID)
.WillOnce(Return(true));
EXPECT_CALL(*(DataFormat::GetInterface()), CheckRiskContent).WillOnce(Return(true));
EXPECT_CALL(DatabaseManager::GetInstance(), InsertEvent).WillRepeatedly(Return(SUCCESS));
DataCollectManagerService service(DATA_COLLECT_MANAGER_SA_ID, true);
int32_t result = service.RequestDataSubmit(eventId, version, time, content, false);
EXPECT_EQ(result, SUCCESS);
}
HWTEST_F(SecurityGuardDataCollectSaTest, RequestRiskData01, TestSize.Level1)
{
std::string devId = "devId";

View File

@ -25,6 +25,10 @@ public:
{
return {};
};
static int32_t ReportSecurityInfoAsync(const std::shared_ptr<EventInfo> &info)
{
return {};
};
};
} // namespace OHOS::Security::SecurityGuard