diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 5a175be21419441dd1c75a3cef2b693b9dfae6c5..b22fbfbcfc776c1367ef407dfae82d1ded51a9b8 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -30,6 +30,7 @@ ohos_unittest("SensorAgentTest") { "$SUBSYSTEM_DIR/sensor/utils/include", "$SUBSYSTEM_DIR/sensor/interfaces/native/include", "$SUBSYSTEM_DIR/sensor/test/unittest/common/include", + "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", ] deps = [ @@ -73,11 +74,46 @@ ohos_unittest("SensorAlgorithmTest") { ] } +ohos_unittest("SensorPowerTest") { + module_out_path = module_output_path + + sources = [ + "$SUBSYSTEM_DIR/sensor/test/unittest/common/src/system_info.cpp", + "unittest/sensor_power_test.cpp", + "unittest/sensor_status_callback.cpp", + ] + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "$SUBSYSTEM_DIR/sensor/utils/include", + "$SUBSYSTEM_DIR/sensor/interfaces/native/include", + "$SUBSYSTEM_DIR/sensor/test/unittest/common/include", + "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", + "unittest/sensor_status_callback.h", + ] + + deps = [ + "$SUBSYSTEM_DIR/sensor/interfaces/native:sensor_ndk_target", + "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + ###########################end########################### group("unittest") { testonly = true deps = [ ":SensorAgentTest", ":SensorAlgorithmTest", + ":SensorPowerTest", ] } diff --git a/interfaces/native/test/fuzztest/sensoragent_fuzzer/BUILD.gn b/interfaces/native/test/fuzztest/sensoragent_fuzzer/BUILD.gn index c9c1069ebd312da8d8f0fac90b5f9f8014252750..99cbfd475dc8983550800e720177189917f43e88 100644 --- a/interfaces/native/test/fuzztest/sensoragent_fuzzer/BUILD.gn +++ b/interfaces/native/test/fuzztest/sensoragent_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-2023Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -14,6 +14,8 @@ import("//build/config/features.gni") import("//build/ohos.gni") import("//build/test.gni") + +SUBSYSTEM_DIR = "//base/sensors" module_output_path = "sensors/sensor" ohos_fuzztest("SensorAgentFuzzTest") { @@ -25,6 +27,7 @@ ohos_fuzztest("SensorAgentFuzzTest") { include_dirs = [ "//base/sensors/sensor/interfaces/native/test/fuzztest/sensoragent_fuzzer", "//base/sensors/sensor/interfaces/native/include", + "$SUBSYSTEM_DIR/sensor/frameworks/native/sensor/include", ] cflags = [ diff --git a/interfaces/native/test/unittest/sensor_power_test.cpp b/interfaces/native/test/unittest/sensor_power_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dca2d009cf2e6fda77773fe56f69aa83f7f5b02c --- /dev/null +++ b/interfaces/native/test/unittest/sensor_power_test.cpp @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sensor_power_test.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +#include "sensor_agent.h" +#include "sensor_status_callback.h" +#include "sensors_errors.h" +#include "system_info.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorPowerTest" }; +constexpr int32_t sensorId {1}; +constexpr int32_t invalidValue { -1 }; +} // namespace + +class SensorPowerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +int32_t process_pid = 0; +int32_t g_callback_pid = 0; +int32_t g_callback_sensorId = 0; +bool g_callback_isActive = false; +int64_t g_callback_samplingPeriodNs = 0; +int64_t g_callback_maxReportDelayNs = 0; + +void SensorPowerTest::SetUpTestCase() +{ + const char **perms = new const char *[1]; + perms[0] = "ohos.permission.ACCELEROMETER"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "SensorPowerTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + int res = SetSelfTokenID(tokenId); + ASSERT_EQ(res, 0); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; + + SYSTEM_INFO::CpuInfo cpuInfo; + const std::string process_name = "SensorPowerTest"; + process_pid = cpuInfo.GetTaskPidFile(process_name); + SEN_HILOGI("Current process pid is %{public}d", process_pid); + ASSERT_NE(process_pid, 0); +} + +void SensorPowerTest::TearDownTestCase() {} + +void SensorPowerTest::SetUp() {} + +void SensorPowerTest::TearDown() {} + +void SensorDataCallbackImpl(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("SensorEvent is null"); + return; + } + float *sensorData = (float *)event[0].data; + SEN_HILOGI("SensorId:%{public}d, version:%{public}d,dataLen:%{public}d,data:%{public}f", + event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); +} + +HWTEST_F(SensorPowerTest, SuspendSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("SuspendSensorsTest_001 in"); + int32_t ret = SuspendSensors(invalidValue); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, ResumeSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("ResumeSensorsTest_001 in"); + int32_t ret = ResumeSensors(invalidValue); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, GetAppSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("GetAppSensorsTest_001 in"); + AppSensorInfo *appSensorInfos {nullptr}; + int32_t count { 0 }; + int32_t ret = GetAppSensors(invalidValue, &appSensorInfos, &count); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, RegisterCallbackTest_001, TestSize.Level1) +{ + SEN_HILOGI("RegisterCallbackTest_001 in"); + sptr sensorCallback = nullptr; + int32_t ret = RegisterCallback(sensorCallback); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_001, TestSize.Level1) +{ + SEN_HILOGI("SensorPowerTest_001 in"); + sptr sensorCallback = new (std::nothrow) SensorStatusCallback(); + int32_t ret = RegisterCallback(sensorCallback); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + SensorUser user; + user.callback = SensorDataCallbackImpl; + ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + if (g_callback_pid == process_pid) { + SEN_HILOGI("Callback: pid:%{public}d, sensorId:%{public}d, isActive:%{public}d,\ + samplingPeriodNs:%{public}lld, maxReportDelayNs:%{public}lld", + g_callback_pid, g_callback_sensorId, g_callback_isActive, + g_callback_samplingPeriodNs, g_callback_maxReportDelayNs); + } + + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_002, TestSize.Level1) +{ + SEN_HILOGI("SensorPowerTest_002 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + AppSensorInfo *appSensorInfos {nullptr}; + int32_t count { 0 }; + ret = GetAppSensors(process_pid, &appSensorInfos, &count); + for (int32_t i = 0; i < count; ++i) { + AppSensorInfo *curAppSensor = appSensorInfos + i; + SEN_HILOGI("i:%{public}d, pid:%{public}d, sensorId:%{public}d, isActive:%{public}d,\ + samplingPeriodNs:%{public}lld, maxReportDelayNs:%{public}lld", + i, curAppSensor->pid, curAppSensor->sensorId, curAppSensor->isActive, + curAppSensor->samplingPeriodNs, curAppSensor->maxReportDelayNs); + } + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_003, TestSize.Level1) +{ + SEN_HILOGI("SensorPowerTest_003 in"); + sptr sensorCallback = new (std::nothrow) SensorStatusCallback(); + int32_t ret = RegisterCallback(sensorCallback); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + SensorUser user; + user.callback = SensorDataCallbackImpl; + ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + if (g_callback_pid == process_pid) { + SEN_HILOGI("Callback: pid:%{public}d, sensorId:%{public}d, isActive:%{public}d,\ + samplingPeriodNs:%{public}lld, maxReportDelayNs:%{public}lld", + g_callback_pid, g_callback_sensorId, g_callback_isActive, + g_callback_samplingPeriodNs, g_callback_maxReportDelayNs); + ret = SuspendSensors(g_callback_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + ret = ResumeSensors(g_callback_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + } + + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_004, TestSize.Level1) +{ + SEN_HILOGI("SensorPowerTest_004 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + AppSensorInfo *appSensorInfos {nullptr}; + int32_t count { 0 }; + ret = GetAppSensors(process_pid, &appSensorInfos, &count); + for (int32_t i = 0; i < count; ++i) { + AppSensorInfo *curAppSensor = appSensorInfos + i; + SEN_HILOGI("i:%{public}d, pid:%{public}d, sensorId:%{public}d, isActive:%{public}d,\ + samplingPeriodNs:%{public}lld, maxReportDelayNs:%{public}lld", + i, curAppSensor->pid, curAppSensor->sensorId, curAppSensor->isActive, + curAppSensor->samplingPeriodNs, curAppSensor->maxReportDelayNs); + } + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = SuspendSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + + ret = ResumeSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} +} // namespace Sensors +} // namespace OHOS diff --git a/interfaces/native/test/unittest/sensor_power_test.h b/interfaces/native/test/unittest/sensor_power_test.h new file mode 100644 index 0000000000000000000000000000000000000000..121430a5aab4e4b36bf472dc0fb10fe1e555e375 --- /dev/null +++ b/interfaces/native/test/unittest/sensor_power_test.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_POWER_TEST_H +#define SENSOR_POWER_TEST_H + +#include + +namespace OHOS { +namespace Sensors { + extern int32_t g_callback_pid; + extern int32_t g_callback_sensorId; + extern bool g_callback_isActive; + extern int64_t g_callback_samplingPeriodNs; + extern int64_t g_callback_maxReportDelayNs; + +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_POWER_TEST_H \ No newline at end of file diff --git a/interfaces/native/test/unittest/sensor_status_callback.cpp b/interfaces/native/test/unittest/sensor_status_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f34f238470828746735d2a7093d7937b2956696b --- /dev/null +++ b/interfaces/native/test/unittest/sensor_status_callback.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sensor_status_callback.h" + +#include "hisysevent.h" + +#include "sensor_power_test.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +void SensorStatusCallback::OnSensorChanged(const AppSensorInfo &appSensorInfo) +{ + g_callback_pid = appSensorInfo.pid; + g_callback_sensorId = appSensorInfo.sensorId; + g_callback_isActive = appSensorInfo.isActive; + g_callback_samplingPeriodNs = appSensorInfo.samplingPeriodNs; + g_callback_maxReportDelayNs = appSensorInfo.maxReportDelayNs; +} +} // namespace Sensors +} // namespace OHOS diff --git a/interfaces/native/test/unittest/sensor_status_callback.h b/interfaces/native/test/unittest/sensor_status_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..ef266600d804379e8b65d61128a70ae670bd3574 --- /dev/null +++ b/interfaces/native/test/unittest/sensor_status_callback.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_STATUS_CALLBACK_H +#define SENSOR_STATUS_CALLBACK_H + +#include "sensor_status_callback_stub.h" + +namespace OHOS { +namespace Sensors { +class SensorStatusCallback : public SensorStatusCallbackStub { +public: + void OnSensorChanged(const AppSensorInfo &appSensorInfo) override; +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_STATUS_CALLBACK_H \ No newline at end of file