OpenHarmony内核系统调用hats测试用例编写指南

一、规范要求说明

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

  1. 必须符合华为代码规范:OpenHarmony 代码规范指导
  2. 接口用例编写去通过 man 手册查看其说明
  3. 接口用例编写前阅读对应接口的 ltp 用例,尽可能不要重复
  4. 每个接口基本用例必须覆盖每个参数,且参数值满足正向输入和异常输入
  5. 每个接口至少提供一种场景用例覆盖
  6. 每个测试用例必须提供用例说明报告以及代码中提供英文注释
  7. 提交的代码必须能够通过 gitee 的代码检查和运行

二、开发流程

2.1 流程图

2.2 流程说明

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

三、测试用例 demo 编写

3.1 测试报告编写模板

3.2 demo 用例编写

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

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 文件修改

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

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

ini 复制代码
# 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

bash 复制代码
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
DD一下: 欢迎大家关注公众号<程序猿百晓生>,可以了解到以下知识点。
erlang 复制代码
`欢迎大家关注公众号<程序猿百晓生>,可以了解到以下知识点。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......

3.2.2 添加 accept4 测试用例代码

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

BUILD.gn 文件

ini 复制代码
# 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

bash 复制代码
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 文件

ini 复制代码
/*
 * 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 文件

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

bash 复制代码
# 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 遍以上

ini 复制代码
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遍
相关推荐
鸿蒙布道师1 小时前
鸿蒙NEXT开发数值工具类(TS)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
塞尔维亚大汉6 小时前
鸿蒙南向开发 ——轻量系统芯片移植指南(二)
物联网·嵌入式·harmonyos
触角010100017 小时前
STM32看门狗原理与应用详解:独立看门狗 vs 窗口看门狗(上) | 零基础入门STM32第九十四步
驱动开发·stm32·单片机·嵌入式硬件·物联网
hmywillstronger8 小时前
【ESP32】ESP32物联网应用:MQTT控制与状态监测
java·物联网·struts
90后的晨仔8 小时前
鸿蒙ArkTS是如何实现并发的?
harmonyos
鸿蒙布道师10 小时前
鸿蒙NEXT开发日期工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
HMSCore11 小时前
在应用内购票、寄件时,如何一键填充所需信息?
harmonyos
HarmonyOS_SDK12 小时前
在应用内购票、寄件时,如何一键填充所需信息?
harmonyos
枫叶丹413 小时前
【HarmonyOS Next之旅】DevEco Studio使用指南(十一)
华为·harmonyos·deveco studio·harmonyos next