From d015817150443cce74966a1caf4f1a0c66c64941 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Mon, 21 Oct 2024 11:36:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9sensor=E5=B7=AE=E5=BC=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: Id0d015b99fef69f9666b745e08fb908f953d2a74 --- bundle.json | 1 - frameworks/native/src/sensor_agent_proxy.cpp | 2 +- interfaces/kits/c/oh_sensor_type.h | 1 - .../createdatachannelstub_fuzzer.cpp | 10 +++++-- .../createsocketchannelstub_fuzzer.cpp | 10 +++++-- .../destroydatachannelstub_fuzzer.cpp | 10 +++++-- .../destroysocketchannelstub_fuzzer.cpp | 10 +++++-- .../sensordisablestub_fuzzer.cpp | 22 ++++++++++++-- .../sensorenablestub_fuzzer.cpp | 29 +++++++++++++++++-- .../unittest/interfaces/js/sensor/config.json | 2 +- .../interfaces/kits/sensor_native_test.cpp | 8 +++++ 11 files changed, 83 insertions(+), 22 deletions(-) diff --git a/bundle.json b/bundle.json index 96956292..bce0a20d 100644 --- a/bundle.json +++ b/bundle.json @@ -67,7 +67,6 @@ "//base/sensors/sensor/test/fuzztest/interfaces:fuzztest", "//base/sensors/sensor/test/unittest/interfaces/inner_api:unittest", "//base/sensors/sensor/test/fuzztest/services:fuzztest", - "//base/sensors/sensor/test/fuzztest/services:fuzztest", "//base/sensors/sensor/test/unittest/coverage:unittest" ] } diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index eae2a02e..53b1715e 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -296,7 +296,7 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * unsubscribeMap_.erase(sensorId); } if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) { - PrintSensorData::GetInstance().SavePrintUserInfo(user->callback); + PrintSensorData::GetInstance().RemovePrintUserInfo(user->callback); } return OHOS::Sensors::SUCCESS; } diff --git a/interfaces/kits/c/oh_sensor_type.h b/interfaces/kits/c/oh_sensor_type.h index 82fcd02f..0641a9c0 100644 --- a/interfaces/kits/c/oh_sensor_type.h +++ b/interfaces/kits/c/oh_sensor_type.h @@ -327,7 +327,6 @@ int32_t OH_SensorEvent_GetAccuracy(Sensor_Event* sensorEvent, Sensor_Accuracy *a * SENSOR_TYPE_AMBIENT_LIGHT: data[0], indicating the ambient light intensity, in lux. Since api version 12, * two additional data will be returned, where data[1] indicating the color temperature, in kelvin; data[2] * indicating the infrared luminance, in cd/m2. - * SENSOR_TYPE_AMBIENT_LIGHT: data[0], indicating the ambient light intensity, in lux. * SENSOR_TYPE_MAGNETIC_FIELD: data[0], data[1], and data[2], indicating the magnetic field strength around * the x, y, and z axes of the device, respectively, in μT. * SENSOR_TYPE_BAROMETER: data[0], indicating the atmospheric pressure, in hPa. diff --git a/test/fuzztest/services/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.cpp b/test/fuzztest/services/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.cpp index 5394ff1b..f3f85d1b 100644 --- a/test/fuzztest/services/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.cpp +++ b/test/fuzztest/services/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.cpp @@ -35,6 +35,7 @@ namespace { constexpr size_t U32_AT_SIZE = 4; auto g_service = SensorDelayedSpSingleton::GetInstance(); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +static sptr g_remote = new (std::nothrow) IPCObjectStub(); } // namespace void SetUpTestCase() @@ -66,7 +67,10 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) SetUpTestCase(); MessageParcel datas; datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); - datas.WriteBuffer(data, size); + if (g_remote == nullptr) { + return false; + } + datas.WriteRemoteObject(g_remote); datas.RewindRead(0); MessageParcel reply; MessageOption option; @@ -74,8 +78,8 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) datas, reply, option); return true; } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { diff --git a/test/fuzztest/services/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.cpp b/test/fuzztest/services/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.cpp index 9664deb1..59717834 100644 --- a/test/fuzztest/services/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.cpp +++ b/test/fuzztest/services/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.cpp @@ -35,6 +35,7 @@ namespace { constexpr size_t U32_AT_SIZE = 4; auto g_service = SensorDelayedSpSingleton::GetInstance(); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +static sptr g_remote = new (std::nothrow) IPCObjectStub(); } // namespace void SetUpTestCase() @@ -66,7 +67,10 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) SetUpTestCase(); MessageParcel datas; datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); - datas.WriteBuffer(data, size); + if (g_remote == nullptr) { + return false; + } + datas.WriteRemoteObject(g_remote); datas.RewindRead(0); MessageParcel reply; MessageOption option; @@ -74,8 +78,8 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) datas, reply, option); return true; } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { diff --git a/test/fuzztest/services/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.cpp b/test/fuzztest/services/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.cpp index c7b17b2f..48284574 100644 --- a/test/fuzztest/services/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.cpp +++ b/test/fuzztest/services/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.cpp @@ -35,6 +35,7 @@ namespace { constexpr size_t U32_AT_SIZE = 4; auto g_service = SensorDelayedSpSingleton::GetInstance(); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +static sptr g_remote = new (std::nothrow) IPCObjectStub(); } // namespace void SetUpTestCase() @@ -66,7 +67,10 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) SetUpTestCase(); MessageParcel datas; datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); - datas.WriteBuffer(data, size); + if (g_remote == nullptr) { + return false; + } + datas.WriteRemoteObject(g_remote); datas.RewindRead(0); MessageParcel reply; MessageOption option; @@ -74,8 +78,8 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) datas, reply, option); return true; } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { diff --git a/test/fuzztest/services/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.cpp b/test/fuzztest/services/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.cpp index e7c20b3b..92d43754 100644 --- a/test/fuzztest/services/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.cpp +++ b/test/fuzztest/services/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.cpp @@ -35,6 +35,7 @@ namespace { constexpr size_t U32_AT_SIZE = 4; auto g_service = SensorDelayedSpSingleton::GetInstance(); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +static sptr g_remote = new (std::nothrow) IPCObjectStub(); } // namespace void SetUpTestCase() @@ -66,7 +67,10 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) SetUpTestCase(); MessageParcel datas; datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); - datas.WriteBuffer(data, size); + if (g_remote == nullptr) { + return false; + } + datas.WriteRemoteObject(g_remote); datas.RewindRead(0); MessageParcel reply; MessageOption option; @@ -74,8 +78,8 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) datas, reply, option); return true; } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { diff --git a/test/fuzztest/services/sensordisablestub_fuzzer/sensordisablestub_fuzzer.cpp b/test/fuzztest/services/sensordisablestub_fuzzer/sensordisablestub_fuzzer.cpp index 4d84342e..f5ca6e0d 100644 --- a/test/fuzztest/services/sensordisablestub_fuzzer/sensordisablestub_fuzzer.cpp +++ b/test/fuzztest/services/sensordisablestub_fuzzer/sensordisablestub_fuzzer.cpp @@ -37,6 +37,20 @@ auto g_service = SensorDelayedSpSingleton::GetInstance(); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; } // namespace +template +size_t GetObject(T &object, const uint8_t *data, size_t size) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + errno_t ret = memcpy_s(&object, objectSize, data, objectSize); + if (ret != EOK) { + return 0; + } + return objectSize; +} + void SetUpTestCase() { const char **perms = new (std::nothrow) const char *[2]; @@ -66,7 +80,9 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) SetUpTestCase(); MessageParcel datas; datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); - datas.WriteBuffer(data, size); + int32_t sensorId = 0; + GetObject(sensorId, data, size); + datas.WriteInt32(sensorId); datas.RewindRead(0); MessageParcel reply; MessageOption option; @@ -74,8 +90,8 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) datas, reply, option); return true; } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { diff --git a/test/fuzztest/services/sensorenablestub_fuzzer/sensorenablestub_fuzzer.cpp b/test/fuzztest/services/sensorenablestub_fuzzer/sensorenablestub_fuzzer.cpp index 6b5743b6..28b34adb 100644 --- a/test/fuzztest/services/sensorenablestub_fuzzer/sensorenablestub_fuzzer.cpp +++ b/test/fuzztest/services/sensorenablestub_fuzzer/sensorenablestub_fuzzer.cpp @@ -37,6 +37,20 @@ auto g_service = SensorDelayedSpSingleton::GetInstance(); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; } // namespace +template +size_t GetObject(T &object, const uint8_t *data, size_t size) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + errno_t ret = memcpy_s(&object, objectSize, data, objectSize); + if (ret != EOK) { + return 0; + } + return objectSize; +} + void SetUpTestCase() { const char **perms = new (std::nothrow) const char *[2]; @@ -66,7 +80,16 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) SetUpTestCase(); MessageParcel datas; datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); - datas.WriteBuffer(data, size); + size_t startPos = 0; + int32_t sensorId = 0; + startPos += GetObject(sensorId, data + startPos, size - startPos); + datas.WriteInt32(sensorId); + int64_t samplingPeriod = 0; + startPos += GetObject(samplingPeriod, data + startPos, size - startPos); + datas.WriteInt64(samplingPeriod); + int64_t maxReportDelay = 0; + GetObject(maxReportDelay, data + startPos, size - startPos); + datas.WriteInt64(maxReportDelay); datas.RewindRead(0); MessageParcel reply; MessageOption option; @@ -74,8 +97,8 @@ bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) datas, reply, option); return true; } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { diff --git a/test/unittest/interfaces/js/sensor/config.json b/test/unittest/interfaces/js/sensor/config.json index 435079f9..675fc7e8 100644 --- a/test/unittest/interfaces/js/sensor/config.json +++ b/test/unittest/interfaces/js/sensor/config.json @@ -58,7 +58,7 @@ "deviceType": [ "default", "tablet", - "2inl" + "2in1" ], "distro": { "deliveryWithInstall": true, diff --git a/test/unittest/interfaces/kits/sensor_native_test.cpp b/test/unittest/interfaces/kits/sensor_native_test.cpp index 00fcb060..f2125c7d 100644 --- a/test/unittest/interfaces/kits/sensor_native_test.cpp +++ b/test/unittest/interfaces/kits/sensor_native_test.cpp @@ -674,6 +674,14 @@ HWTEST_F(SensorAgentTest, OH_SensorSubscriber_SetCallback_002, TestSize.Level1) ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); } +HWTEST_F(SensorAgentTest, OH_SensorSubscriber_SetCallback_003, TestSize.Level1) +{ + SEN_HILOGI("OH_SensorSubscriber_SetCallback_003 in"); + g_user = OH_Sensor_CreateSubscriber(); + int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl); + ASSERT_EQ(ret, SENSOR_SUCCESS); +} + HWTEST_F(SensorAgentTest, OH_SensorSubscriber_GetCallback_001, TestSize.Level1) { SEN_HILOGI("OH_SensorSubscriber_GetCallback_001 in"); -- Gitee