mirror of
https://gitee.com/openharmony/sensors_sensor
synced 2024-12-04 20:58:50 +00:00
!617 Example Increase the sensor_basic_data_channel coverage rate
Merge pull request !617 from 白露/master
This commit is contained in:
commit
d56cc66af6
@ -67,7 +67,8 @@
|
|||||||
"//base/sensors/sensor/test/unittest/interfaces/kits:unittest",
|
"//base/sensors/sensor/test/unittest/interfaces/kits:unittest",
|
||||||
"//base/sensors/sensor/test/fuzztest/interfaces:fuzztest",
|
"//base/sensors/sensor/test/fuzztest/interfaces:fuzztest",
|
||||||
"//base/sensors/sensor/test/unittest/interfaces/inner_api:unittest",
|
"//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"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,8 @@
|
|||||||
#include "sensor_agent_type.h"
|
#include "sensor_agent_type.h"
|
||||||
#include "sensor_errors.h"
|
#include "sensor_errors.h"
|
||||||
|
|
||||||
|
namespace OHOS {
|
||||||
|
namespace Sensors {
|
||||||
using namespace OHOS::HiviewDFX;
|
using namespace OHOS::HiviewDFX;
|
||||||
using namespace OHOS::Security::AccessToken;
|
using namespace OHOS::Security::AccessToken;
|
||||||
using OHOS::Security::AccessToken::AccessTokenID;
|
using OHOS::Security::AccessToken::AccessTokenID;
|
||||||
@ -113,10 +115,12 @@ void SensorAgentFuzzTest(const uint8_t *data, size_t size)
|
|||||||
DeactivateSensor(sensorTypeId, &user);
|
DeactivateSensor(sensorTypeId, &user);
|
||||||
UnsubscribeSensor(sensorTypeId, &user);
|
UnsubscribeSensor(sensorTypeId, &user);
|
||||||
}
|
}
|
||||||
|
} // namespace Sensors
|
||||||
|
} // namespace OHOS
|
||||||
|
|
||||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
|
||||||
{
|
{
|
||||||
SensorAgentFuzzTest(data, size);
|
OHOS::Sensors::SensorAgentFuzzTest(data, size);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
48
test/unittest/coverage/BUILD.gn
Normal file
48
test/unittest/coverage/BUILD.gn
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
import("//build/test.gni")
|
||||||
|
import("./../../../sensor.gni")
|
||||||
|
|
||||||
|
ohos_unittest("SensorBasicDataChannelTest") {
|
||||||
|
module_out_path = "sensor/coverage"
|
||||||
|
|
||||||
|
sources = [
|
||||||
|
"$SUBSYSTEM_DIR/test/unittest/coverage/sensor_basic_data_channel_test.cpp",
|
||||||
|
]
|
||||||
|
|
||||||
|
include_dirs = [
|
||||||
|
"$SUBSYSTEM_DIR/utils/common/include",
|
||||||
|
"$SUBSYSTEM_DIR/interfaces/kits/c",
|
||||||
|
"$SUBSYSTEM_DIR/frameworks/native/include",
|
||||||
|
"$SUBSYSTEM_DIR/interfaces/inner_api",
|
||||||
|
]
|
||||||
|
|
||||||
|
deps = [
|
||||||
|
"$SUBSYSTEM_DIR/frameworks/native:libsensor_client",
|
||||||
|
"$SUBSYSTEM_DIR/frameworks/native:ohsensor",
|
||||||
|
"$SUBSYSTEM_DIR/utils/common:libsensor_utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
external_deps = [
|
||||||
|
"c_utils:utils",
|
||||||
|
"googletest:gmock",
|
||||||
|
"googletest:gtest_main",
|
||||||
|
"hilog:libhilog",
|
||||||
|
"ipc:ipc_single",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
group("unittest") {
|
||||||
|
testonly = true
|
||||||
|
deps = [ ":SensorBasicDataChannelTest" ]
|
||||||
|
}
|
149
test/unittest/coverage/sensor_basic_data_channel_test.cpp
Normal file
149
test/unittest/coverage/sensor_basic_data_channel_test.cpp
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024 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 <cinttypes>
|
||||||
|
#include <memory>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#include "message_parcel.h"
|
||||||
|
|
||||||
|
#include "sensor_errors.h"
|
||||||
|
#include "sensor_basic_data_channel.h"
|
||||||
|
|
||||||
|
#undef LOG_TAG
|
||||||
|
#define LOG_TAG "SensorBasicDataChannelTest"
|
||||||
|
|
||||||
|
namespace OHOS {
|
||||||
|
namespace Sensors {
|
||||||
|
using namespace testing::ext;
|
||||||
|
using namespace OHOS::HiviewDFX;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr int32_t INVALID_FD = 2;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
class SensorBasicDataChannelTest : public testing::Test {
|
||||||
|
public:
|
||||||
|
static void SetUpTestCase();
|
||||||
|
static void TearDownTestCase();
|
||||||
|
void SetUp();
|
||||||
|
void TearDown();
|
||||||
|
};
|
||||||
|
|
||||||
|
void SensorBasicDataChannelTest::SetUpTestCase() {}
|
||||||
|
|
||||||
|
void SensorBasicDataChannelTest::TearDownTestCase() {}
|
||||||
|
|
||||||
|
void SensorBasicDataChannelTest::SetUp() {}
|
||||||
|
|
||||||
|
void SensorBasicDataChannelTest::TearDown() {}
|
||||||
|
|
||||||
|
HWTEST_F(SensorBasicDataChannelTest, SensorBasicDataChannelTest_001, TestSize.Level1)
|
||||||
|
{
|
||||||
|
SEN_HILOGI("SensorBasicDataChannelTest_001 in");
|
||||||
|
|
||||||
|
SensorBasicDataChannel sensorChannel = SensorBasicDataChannel();
|
||||||
|
MessageParcel data;
|
||||||
|
int32_t ret = sensorChannel.CreateSensorBasicChannel();
|
||||||
|
ASSERT_EQ(ret, ERR_OK);
|
||||||
|
ret = sensorChannel.CreateSensorBasicChannel(data);
|
||||||
|
ASSERT_EQ(ret, ERR_OK);
|
||||||
|
|
||||||
|
ret = sensorChannel.SendToBinder(data);
|
||||||
|
ASSERT_EQ(ret, ERR_OK);
|
||||||
|
|
||||||
|
char buff[128] = {};
|
||||||
|
ret = sensorChannel.SendData(static_cast<void *>(buff), sizeof(buff));
|
||||||
|
ASSERT_EQ(ret, ERR_OK);
|
||||||
|
|
||||||
|
ret = sensorChannel.ReceiveData(static_cast<void *>(buff), sizeof(buff));
|
||||||
|
ASSERT_NE(ret, SENSOR_CHANNEL_RECEIVE_ADDR_ERR);
|
||||||
|
|
||||||
|
sensorChannel.DestroySensorBasicChannel();
|
||||||
|
}
|
||||||
|
|
||||||
|
HWTEST_F(SensorBasicDataChannelTest, CreateSensorBasicChannel_001, TestSize.Level1)
|
||||||
|
{
|
||||||
|
SEN_HILOGI("CreateSensorBasicChannel_001 in");
|
||||||
|
|
||||||
|
SensorBasicDataChannel sensorChannel = SensorBasicDataChannel();
|
||||||
|
MessageParcel data;
|
||||||
|
data.WriteFileDescriptor(INVALID_FD);
|
||||||
|
int32_t ret = sensorChannel.CreateSensorBasicChannel(data);
|
||||||
|
ASSERT_EQ(ret, ERR_OK);
|
||||||
|
|
||||||
|
ret = sensorChannel.CreateSensorBasicChannel(data);
|
||||||
|
ASSERT_EQ(ret, ERR_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
HWTEST_F(SensorBasicDataChannelTest, CreateSensorBasicChannel_002, TestSize.Level1)
|
||||||
|
{
|
||||||
|
SEN_HILOGI("CreateSensorBasicChannel_002 in");
|
||||||
|
SensorBasicDataChannel sensorChannel = SensorBasicDataChannel();
|
||||||
|
MessageParcel data;
|
||||||
|
int32_t ret = sensorChannel.CreateSensorBasicChannel(data);
|
||||||
|
ASSERT_EQ(ret, SENSOR_CHANNEL_READ_DESCRIPTOR_ERR);
|
||||||
|
}
|
||||||
|
|
||||||
|
HWTEST_F(SensorBasicDataChannelTest, SendToBinder_001, TestSize.Level1)
|
||||||
|
{
|
||||||
|
SEN_HILOGI("SendToBinder_001 in");
|
||||||
|
SensorBasicDataChannel sensorChannel = SensorBasicDataChannel();
|
||||||
|
MessageParcel data;
|
||||||
|
int32_t ret = sensorChannel.SendToBinder(data);
|
||||||
|
ASSERT_EQ(ret, SENSOR_CHANNEL_SENDFD_ERR);
|
||||||
|
}
|
||||||
|
|
||||||
|
HWTEST_F(SensorBasicDataChannelTest, SendData_001, TestSize.Level1)
|
||||||
|
{
|
||||||
|
SEN_HILOGI("SendData_001 in");
|
||||||
|
SensorBasicDataChannel sensorChannel = SensorBasicDataChannel();
|
||||||
|
char buff[128] = {};
|
||||||
|
int32_t ret = sensorChannel.SendData(static_cast<void *>(buff), sizeof(buff));
|
||||||
|
ASSERT_EQ(ret, SENSOR_CHANNEL_SEND_ADDR_ERR);
|
||||||
|
}
|
||||||
|
|
||||||
|
HWTEST_F(SensorBasicDataChannelTest, SendData_002, TestSize.Level1)
|
||||||
|
{
|
||||||
|
SEN_HILOGI("SendData_002 in");
|
||||||
|
SensorBasicDataChannel sensorChannel = SensorBasicDataChannel();
|
||||||
|
MessageParcel data;
|
||||||
|
data.WriteFileDescriptor(INVALID_FD);
|
||||||
|
int32_t ret = sensorChannel.CreateSensorBasicChannel(data);
|
||||||
|
ASSERT_EQ(ret, ERR_OK);
|
||||||
|
char buff[128] = {};
|
||||||
|
ret = sensorChannel.SendData(static_cast<void *>(buff), sizeof(buff));
|
||||||
|
ASSERT_EQ(ret, SENSOR_CHANNEL_SEND_DATA_ERR);
|
||||||
|
}
|
||||||
|
|
||||||
|
HWTEST_F(SensorBasicDataChannelTest, ReceiveData_001, TestSize.Level1)
|
||||||
|
{
|
||||||
|
SEN_HILOGI("ReceiveData_001 in");
|
||||||
|
SensorBasicDataChannel sensorChannel = SensorBasicDataChannel();
|
||||||
|
char buff[128] = {};
|
||||||
|
int32_t ret = sensorChannel.ReceiveData(static_cast<void *>(buff), sizeof(buff));
|
||||||
|
ASSERT_EQ(ret, SENSOR_CHANNEL_RECEIVE_ADDR_ERR);
|
||||||
|
|
||||||
|
sensorChannel.CreateSensorBasicChannel();
|
||||||
|
char *buff1 = nullptr;
|
||||||
|
ret = sensorChannel.ReceiveData(static_cast<void *>(buff1), sizeof(buff1));
|
||||||
|
ASSERT_EQ(ret, SENSOR_CHANNEL_RECEIVE_ADDR_ERR);
|
||||||
|
|
||||||
|
sensorChannel.DestroySensorBasicChannel();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Sensors
|
||||||
|
} // namespace OHOS
|
Loading…
Reference in New Issue
Block a user