积分305 / 贡献0

提问7答案被采纳4文章45

[经验分享] 内核系统调用hats测试用例编写指南 原创

润开鸿_闻飞 显示全部楼层 发表于 2024-9-13 14:05:37

一、规范要求说明

每个接口用例设计需要满足以下要求:

  1. 必须符合华为代码规范

    OpenHarmony代码规范指导

  2. 接口用例编写去通过man手册查看其说明

  3. 接口用例编写前阅读对应接口的ltp用例,尽可能不要重复

    ltp仓路径

  4. 每个接口基本用例必须覆盖每个参数,且参数值满足正向输入和异常输入

  5. 每个接口至少提供一种场景用例覆盖

  6. 每个测试用例必须提供用例说明报告以及代码中提供英文注释

  7. 提交的代码必须能够通过gitee的代码检查和运行

二、开发流程

2.1 流程图

kernel-xts.jpg

2.2 流程说明

  • 拿到接口后需通过man手册查看接口功能和参数说明
  • 阅读ltp用例,尽可能不要重复
  • 设计、编写并测试用例
  • 提交代码到gitee
  • 创建issue并提交pr到OpenHarmony组织
  • 发起构建请求
    • 构建失败需检查失败原因,如果用例原因需修改用例重新提交
  • 构建成功后
    • 编写测试用例报告
    • 找接口人review
    • review成功则直接合入
    • review失败则重新提交

2.3 OpenHarmony代码提交流程

详细流程参见:

OpenHarmony代码提交流程

  • 注册gitee账号:https://gitee.com

    • 实名认证
    • 邮箱设置为runkaihong邮箱
  • 邮箱签署DCO:https://dco.openharmony.cn/sign

  • fork OpenHamrony 组织的xts_hats仓代码到个人账号下

    xts仓路径

  • 将编写的用例提交

  • 在OpenHamrony的xts_hats创建issue

  • 提交PR并关联issue

  • 在PR中评论区输入:start build

  • 等构建完成后查看是否有报错

三、测试用例demo编写

3.1 测试报告编写模板

image.png

3.2 demo用例编写

以accept4接口为例,实现一个demo用例的过程

链接参考:

accept4接口为例

3.2.1 添加HatsAccept4Test模块

accept4接口为网络通信中使用,当前归类到net,其他接口同样要区分其类别,如果不清楚可讨论划分 根据accept4属于net类,因此在test/xts/hats/kernel/syscalls目录下创建net目录,然后在net目录下创建accept4目录,同步修改各自的BUILD.gn test/xts/hats/kernel/syscalls/BUILD.gn文件修改

group("HatsSyscallTest") {
  testonly = true
  deps = [
    "fileio:HatsFileIoTest",
    "net:HatsNetTest", # 添加net模块
  ]
}

test/xts/hats/kernel/syscalls/net/BUILD.gn 文件修改

# Copyright (C) 2024 HiHope Open Source Organization.
# 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/ohos_var.gni")
import("//test/xts/hats/build.gni")

group("HatsNetTest") {
  testonly = true
  deps = [ "accept4:HatsAccept4Test" ]
}

格式化BUILD.gn

cat test/xts/hats/kernel/syscalls/BUILD.gn | prebuilts/build-tools/linux-x86/bin/gn format --stdin > FORMAT_RESULT.gn;cp -f FORMAT_RESULT.gn test/xts/hats/kernel/syscalls/BUILD.gn;rm FORMAT_RESULT.gn

cat test/xts/hats/kernel/syscalls/net/BUILD.gn | prebuilts/build-tools/linux-x86/bin/gn format --stdin > FORMAT_RESULT.gn;cp -f FORMAT_RESULT.gn test/xts/hats/kernel/syscalls/net/BUILD.gn;rm FORMAT_RESULT.gn

3.2.2 添加accept4测试用例代码

在test/xts/hats/kernel/syscalls/net/accept4目录下添加以下文件内容

BUILD.gn文件

# Copyright (C) 2024 HiHope Open Source Organization.
# 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("//test/xts/tools/build/suite.gni")
module_output_path = "hats/syscalls/net" # 注意net替换成自己的模块

ohos_moduletest_suite("HatsAccept4Test") {
  module_out_path = module_output_path
  sources = [ "./Accept4ApiTest.cpp" ]
  deps = [ "//third_party/googletest:gtest_main" ]
  include_dirs = [ "include" ]
  cflags = [ "-Wno-error" ]
  external_deps = [ "c_utils:utils" ]
  subsystem_name = "kernel"
  part_name = "hats"
}

格式化BUILD.gn

cat test/xts/hats/kernel/syscalls/net/accept4/BUILD.gn | prebuilts/build-tools/linux-x86/bin/gn format --stdin > FORMAT_RESULT.gn;cp -f FORMAT_RESULT.gn test/xts/hats/kernel/syscalls/net/accept4/BUILD.gn;rm FORMAT_RESULT.gn

Accept4ApiTest.cpp文件

/*
 * Copyright (C) 2024 HiHope Open Source Organization.
 * 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 <cerrno>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#include <arpa/inet.h>
#include <gtest/gtest.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "securec.h"

using namespace testing::ext;

class HatsAccept4Test : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
private:
};
void HatsAccept4Test::SetUp()
{
}
void HatsAccept4Test::TearDown()
{
}
void HatsAccept4Test::SetUpTestCase()
{
}
void HatsAccept4Test::TearDownTestCase()
{
}

static const int BAD_SOCKET_FD = -1;
static const int TEST_PORT = 8888;
static const char *TEST_LOCAL_IP = "127.0.0.1";
static const char *TEST_CLIENT_IP = "0.0.0.0";

enum AcceptType {
GET_NONE = 0,
GET_CLIENT_SOCKET_ADDR_TEST,
GET_CLIENT_SOCKET_ADDR_LEN_TEST,
};

static void SocketServiceStart(int *fd)
{
    int ret;
    int socketFd = -1;
    int32_t backLog = 2;
    int32_t optVal = 1;
    struct sockaddr_in serAddr = {
    .sin_family = AF_INET,
    .sin_port = htons(TEST_PORT),
    .sin_addr = {
    .s_addr = inet_addr(TEST_LOCAL_IP),
}
};

    socketFd = socket(AF_INET, SOCK_STREAM, 0);
    ASSERT_TRUE(socketFd > 0);

    ret = setsockopt(socketFd, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(optVal));
    ASSERT_TRUE(ret == 0);

    ret = bind(socketFd, reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(serAddr));
    ASSERT_TRUE(ret == 0);

    ret = listen(socketFd, backLog);
    ASSERT_EQ(ret, 0);

    *fd = socketFd;
}

static void ClientConnect(void)
{
    int ret;
    int socketFd = -1;
    struct sockaddr_in serAddr = {
    .sin_family = AF_INET,
    .sin_port = htons(TEST_PORT),
    .sin_addr = {
    .s_addr = inet_addr(TEST_LOCAL_IP),
}
};
    socketFd = socket(AF_INET, SOCK_STREAM, 0);
    EXPECT_TRUE(socketFd > 0);
    ret = connect(socketFd, reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(struct sockaddr_in));
    EXPECT_EQ(ret, 0);
    close(socketFd);
}

/*
 * @tc.number SUB_KERNEL_SYSCALL_ACCEPT4_0100
 * @tc.name Accept4ValidSockfdSuccess_0001
 * @tc.desc accept4 valid sockfd success.
*/
HWTEST_F(HatsAccept4Test, Accept4ValidSockfdSuccess_0001, Function | MediumTest | Level1)
{
    int pid = -1;
    int socketFd = -1;
    int acceptFd = -1;
    int status = 0;
    struct sockaddr_in cliAddr;
    socklen_t addrlen;
    SocketServiceStart(&socketFd);
    ASSERT_TRUE(socketFd > 0);
    if ((pid = fork()) == 0) {
        ClientConnect();
        exit(0);
    }
    acceptFd = accept4(socketFd, reinterpret_cast<struct sockaddr *>(&cliAddr), &addrlen, 0);
    EXPECT_TRUE(acceptFd > 0);

    close(acceptFd);
    close(socketFd);
    waitpid(pid, &status, 0);
    EXPECT_TRUE(status == 0);
}

/*
 * @tc.number SUB_KERNEL_SYSCALL_ACCEPT4_0200
 * @tc.name Accept4GetClientAddrSuccess_0002
 * @tc.desc accept4 get client addr success.
*/
HWTEST_F(HatsAccept4Test, Accept4GetClientAddrSuccess_0002, Function | MediumTest | Level1)
{
    int pid = -1;
    int acceptFd = -1;
    int socketFd = -1;
    int status = 0;
    struct sockaddr_in cliAddr = { 0 };
    socklen_t addrlen = 0;
    SocketServiceStart(&socketFd);
    ASSERT_TRUE(socketFd > 0);
    if ((pid = fork()) == 0) {
        ClientConnect();
        exit(0);
    }
    acceptFd = accept4(socketFd, reinterpret_cast<struct sockaddr *>(&cliAddr), &addrlen, 0);
    EXPECT_STREQ(inet_ntoa(cliAddr.sin_addr), TEST_CLIENT_IP);
    close(acceptFd);
    close(socketFd);
    waitpid(pid, &status, 0);
    EXPECT_TRUE(status == 0);
}

/*
 * @tc.number SUB_KERNEL_SYSCALL_ACCEPT4_0300
 * @tc.name Accept4GetClientAddrSuccess_0003
 * @tc.desc accept4 get client addr len success.
*/
HWTEST_F(HatsAccept4Test, Accept4GetClientAddrSuccess_0003, Function | MediumTest | Level1)
{
    int pid = -1;
    int acceptFd = -1;
    int socketFd = -1;
    int status = 0;
    struct sockaddr_in cliAddr = { 0 };
    socklen_t addrlen;
    SocketServiceStart(&socketFd);
    ASSERT_TRUE(socketFd > 0);
    if ((pid = fork()) == 0) {
        ClientConnect();
        exit(0);
    }
    acceptFd = accept4(socketFd, reinterpret_cast<struct sockaddr *>(&cliAddr), &addrlen, 0);
    EXPECT_EQ(addrlen, sizeof(struct sockaddr));
    close(acceptFd);
    close(socketFd);
    waitpid(pid, &status, 0);
    EXPECT_TRUE(status == 0);
}

/*
 * @tc.number SUB_KERNEL_SYSCALL_ACCEPT4_0400
 * @tc.name Accept4InvalidFd_0004
 * @tc.desc accept4 use invalid socket fd, return -1, and set errno.
*/
HWTEST_F(HatsAccept4Test, Accept4InvalidFd_0004, Function | MediumTest | Level2)
{
    int ret = accept4(BAD_SOCKET_FD, nullptr, nullptr, 0);
    EXPECT_EQ(ret, -1);
    EXPECT_EQ(errno, EBADF);
    ret = accept4(STDIN_FILENO, nullptr, nullptr, 0);
    EXPECT_EQ(ret, -1);
    EXPECT_EQ(errno, ENOTSOCK);
}

Test.json文件

{
    "description": "Configuration for HatsAccept4Test Tests",
    "kits": [
        {
            "push": [
                "HatsAccept4Test->/data/local/tmp/HatsAccept4Test"
            ],
            "type": "PushKit"
        }
    ],
    "driver": {
        "native-test-timeout": "120000",
        "type": "CppTest",
        "module-name": "HatsAccept4Test",
        "runtime-hint": "1s",
        "native-test-device-path": "/data/local/tmp"
    }
}

3.2.3 注意事项

  • lisense必须是润和的,不得使用华为的抬头

    Copyright (C) 2024 HiHope Open Source Organization.

  • 用例的tc.number格式:

    SUB_KERNEL_SYSCALL_xxx_0100 SUB_KERNEL_SYSCALL_xxx_0200 step为是100

  • 用例的tc.name格式:

    接口名称+测试项+结果+_序列 例如: Accept4GetClientAddrSuccess_0002 接口名称:Accept4 测试项:GetClientAddr(获取客户端地址) 结果:Success 序列:0002

  • 如果需要判断errno,则必须在调用接口前给errno赋值0

    errno = 0;

  • 如果每个测试项测试之前都需要做相同的初始化,则将该部分代码放在SetUp中

  • 代码规范必须符合,C++采用的是驼峰风格

3.3 代码编译

3.3.1 rk3568全量代码编译

编译全量版本目的是为了将版本烧录到我们 rk3568开发板中,后面就不需要再全量编译了,每周需要更新一下代码,防止主线有更新

./build.sh --product-name rk3568 --ccache

3.3.2 编译hats用例代码

测试用例的输出路径:out/rk3568/suites/hats/testcases

# hats全模块编译
cd test/xts/hats
./build.sh product_name=rk3568 system_size=standard

# 单内核测试模块编译
./build.sh product_name=rk3568 system_size=standard target_subsystem=kernel

3.4 本地验证方法

将out/rk3568/suites/hats目录拷贝到windows下,注意不要放到中文目录下 然后运行run.bat,在出现的输入框内输入要测试的项目,上库前必须压测50遍以上

Successfully installed xdevice-ohos-0.0.0
[2024-09-05 19:54:22,502] [9108] [Main] [INFO] [*************** xDevice Test Framework 2.39.0.1042 Starting ***************]
>>> run -l HatsAccept4Test # 要测试的用例

>>> run -l HatsAccept4Test --repeat 50 # 重复测试50遍

>>> run hats --repeat 100 # 重复测试hats用例100遍

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部