OpenHarmony开发者自测试框架初学指南

OH开发者自测试框架初学指南

      • 前言
      • 前置环境搭建
        • wsl2安装
        • wsl2迁移
        • 源码下载
        • 编译环境
      • 测试框架应用
        • 环境搭建
          • libreadline-dev安装
          • python库安装
          • nfs-kernel-server
          • 验证
        • windows环境准备
          • vscode
          • usbipd
      • 生成测试用例
        • 更改BUILD.gn的配置项
        • 关于fuzz配置编写
        • fuzz用例编译
        • fuzz用例执行
      • 后记

前言

本文是关于开发者自测试框架的初学者使用教程,其中会包括一些笔者在自行搭建测试过程中遇到的一些问题的解决方案。在阅读这篇文章之前,可以先参考以下的两篇文章,这两篇文章里面包含了有关前置环境搭建的全部主流程。

源码下载及编译环境搭建

wsl迁移

本篇也会涉及上面两篇文章中对所涉及到的内容,并对其中可能会遇到的问题提供解决方案。

前置的默认:开发者已经拥有一个安装好OH系统的开发板

前置环境搭建

建议:在使用OH测试框架之前一定要搭建好OH系统的编译环境

wsl2安装

win+R运行appwiz.cpl命令,然后点击启用或关闭Windows功能,选中适用于Linux的Windows子系统虚拟机平台两项启用

在microsoft store当中搜索Ubuntu,选择Ubuntu20.04进行安装。安装后直接打开wsl,对用户名和密码进行设置。

Enter new UNIX username: [yourname]
New password: [yourpassword]

进入ubuntu终端后,重新设置root账户的密码

sudo passwd

然后执行sudo dpkg-reconfigure dash,选择No

Ubuntu已经自带了Python3.8版本所以不需要安装额外的python,只需要安装pip即可。

sudo apt-get -y update
sudo apt-get install python3-pip

随即退出wsl,回到windows当中在cmd当中运行命令(将wsl版本切换至2)

wsl.exe --set-version Ubuntu-20.04 2
wsl2迁移

如果不想C盘在之后wsl运行过程中被占用太多存储空间,需要使用wsl迁移的方式,将wsl迁移至非系统盘。

具体参考wsl迁移

源码下载

再次进入wsl当中,切换软件源。

使用sudo vim /etc/apt/sources.list,将其中的内容清空把下面的内容复制进去使用:wq写入并保存。

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

从vim当中退出来之后,在bash中使用如下命令更新软件数据索引和软件升级。

sudo apt-get update
sudo apt-get upgrade

然后安装git和git-lfs

sudo apt install git-all
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs
git lfs install

配置用户信息

git config --global user.name "yourname"
git config --global user.email "your-email-address"
git config --global credential.helper store

执行如下命令安装repo

mkdir ~/bin
curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 -o ~/bin/repo 
chmod a+x ~/bin/repo
pip3 install -i https://repo.huaweicloud.com/repository/pypi/simple requests

拉取主干源码,使用如下的方式(个人推荐如下方式比较方便不用配置ssh)

repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify
repo sync -c
repo forall -c 'git lfs pull'
编译环境

使用如下软件安装命令实现编译环境的安装。

sudo apt-get update
sudo apt-get install binutils binutils-dev git git-lfs gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib gcc-arm-linux-gnueabi libc6-dev-i386 libc6-dev-amd64 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip m4 bc gnutls-bin python3.8 python3-pip ruby genext2fs device-tree-compiler make libffi-dev e2fsprogs pkg-config perl openssl libssl-dev libelf-dev libdwarf-dev u-boot-tools mtd-utils cpio doxygen liblz4-tool openjdk-8-jre gcc g++ texinfo dosfstools mtools default-jre default-jdk libncurses5 apt-utils wget scons python3.8-distutils tar rsync git-core libxml2-dev lib32z-dev grsync xxd libglib2.0-dev libpixman-1-dev kmod jfsutils reiserfsprogs xfsprogs squashfs-tools pcmciautils quota ppp libtinfo-dev libtinfo5 libncurses5-dev libncursesw5 libstdc++6 gcc-arm-none-eabi vim ssh locales libxinerama-dev libxcursor-dev libxrandr-dev libxi-dev

PS:上述步骤容易在第二步正式安装软件的时候失败,其原因是安装的所有库当中存在的相互依赖的关系,可以分批次安装,然后逐步找到出现问题的依赖包。
比如:

sudo apt-get install binutils binutils-dev git git-lfs gnupg flex bison gperf build-essential zip curl
sudo apt-get install zlib1g-dev gcc-multilib g++-multilib gcc-arm-linux-gnueabi libc6-dev-i386 libc6-dev-amd64 lib32ncurses5-dev
sudo apt-get install x11proto-core-dev libx11-dev lib32z1-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip m4 
sudo apt-get install bc gnutls-bin python3.8 python3-pip ruby genext2fs device-tree-compiler make libffi-dev
sudo apt-get install e2fsprogs pkg-config perl openssl libssl-dev libelf-dev libdwarf-dev u-boot-tools mtd-utils 
sudo apt-get install cpio doxygen liblz4-tool openjdk-8-jre gcc g++ texinfo dosfstools mtools default-jre 
sudo apt-get install default-jdk libncurses5 apt-utils wget scons python3.8-distutils tar rsync git-core 
sudo apt-get install libxml2-dev lib32z-dev grsync xxd libglib2.0-dev libpixman-1-dev kmod jfsutils 
sudo apt-get install reiserfsprogs xfsprogs squashfs-tools pcmciautils quota ppp libtinfo-dev libtinfo5 
sudo apt-get install libncurses5-dev libncursesw5 libstdc++6 gcc-arm-none-eabi vim ssh locales libxinerama-dev libxcursor-dev libxrandr-dev libxi-dev

PS:如果仍存在问题,则定点找到某一句安装语句,对该语句的安装依赖进行拆分逐步找到出现问题的依赖,最后安装该依赖。我个人的操作经验是libc6-dev-amd64依赖库出了问题,把这个库最后安装就行了
然后去源码目录下找到prebuilts_download.sh并执行

cd build
bash prebuilts_download.sh

然后去安装下载hdc连接工具为后面的操作铺路,从每日构建当中下载

每日构建地址:http://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist

在上述网中下载ohos-sdk,下载之后放到wsl当中,解压之后,把/ohos-sdk/linux/toolchains目录添加到系统变量当中

vim /etc/bashrc
//在/etc/bashrc当中写下(具体的绝对路径按实际情况为准)
export PATH=$PATH:/tmp/ohos-sdk/linux/toolchains

测试框架应用

主流程在社区当中,这里笔者对其中可能出现的问题给出了解决方案

主流程地址

环境搭建
libreadline-dev安装

首先打开wsl,安装Linux拓展组件readline,安装命令如下

sudo apt-get install libreadline-dev
python库安装

使用pip安装setuptools,paramiko,rsa,pyserial

pip3 install setuptools
pip3 install paramiko
pip3 install rsa
pip3 install pyserial

PS:如果在使用pip安装库出现AttributeError: module 'lib' has no attribute 'X509_V_FLAG_CB_ISSUER_CHECK'的错误时,这是由于pip版本与pyopenssl版本不匹配所造成的问题,解决方案如下

# 卸载pip
sudo apt-get remove python3-pip
# 下载pip
wget https://files.pythonhosted.org/packages/4b/30/e15b806597e67057e07a5acdc135216ccbf76a5f1681a324533b61066b0b/pip-22.2.2.tar.gz
# 解压
tar xf pip-22.2.2.tar.gz
# 安装
cd pip-22.2.2
python3 setup.py install
# 查询版本
pip3 --version
pip 22.2.2 from /usr/local/lib/python3.8/dist-packages/pip-22.2.2-py3.8.egg/pip (python 3.8)
#安装匹配的openssl
pip3 install pyOpenSSL --upgrade
nfs-kernel-server

随后安装nfs-kernel-server(如果使用的是标准设备则不需要这一步)

sudo apt install nfs-kernel-server

PS:这里需要初始化nfs-kernel-server,请参考如下步骤

//安装nfs-kernel-server
sudo apt-get install nfs-kernel-server
//创建共享目录
sudo mkdir /nfs_share
sudo chown nobody:nogroup /nfs_share
sudo chmod 777 /nfs_share

vim /etc/exports
编辑/etc/exports文件,添加以下内容:
/nfs_share *(rw,sync,no_subtree_check)

//启动nfs服务
service nfs-kernel-server start
验证

进入源码目录下的test/testfwk/developer_test,赋予start.sh执行权限

chmod a+x start.sh

启动测试框架

./start.sh

出现>>>的字样便是成功

windows环境准备

后续步骤回到windows当中进行操作

vscode

使用vscode安装wsl的拓展,并重启。

usbipd

该软件是用于解决wsl无法识别开发板的usb问题

wsl下运行

sudo apt install linux-tools-5.8.0-63-generic
sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip `ls /usr/lib/linux-tools/*/usbip | tail -n1` 20

win下运行,第二个命令的运行结果会列出一系列设备,记录设备名为HDC device的BUSID

winget install usbipd
usbipd wsl list(查看信息)

win下管理员运行,将刚才记录的BUSID添到如下命令当中

usbipd wsl attach --distribution Ubuntu-20.04 --busid [BUSID]

PS:attach当过程中可能会出现断联的情况,可以多试几次,重新插拔usb接口

生成测试用例

回到wsl当中来到OpenHarmony源码目录的test/testfwk/developer_test的子目录下

启动测试框架

./start.sh

向框架的控制台输入

gen -t FUZZ -fn calculator_fuzzer -dp base/account/os_account/test/fuzztest/osaccount

测试用例的生成目录为

/openharmony/OpenHarmony-v4.0-Release/OpenHarmony/base/account/os_account/test/fuzztest/osaccount/calculator_fuzzer

回到Windows当中启动VScode,点击远程资源管理器连接wsl

弹出工作目录选择界面之后选择OpenHarmony/base/account/os_account/目录为主工作目录(具体目录由开发者自己实际开发环境决定)

更改BUILD.gn的配置项

更改工作目录下的test/fuzztest/os_account/calculator_fuzzer/BUILD.gn填写相应的配置项。

#####################hydra-fuzz###################
import("//build/config/features.gni")
import("//build/test.gni")

##############################fuzztest##########################################
ohos_fuzztest("CalculatorFuzzTest") {
  module_out_path = "os_account/os_account"
  fuzz_config_file = "//base/account/os_account/test/fuzztest/osaccount/calculator_fuzzer"
  include_dirs = [
  ]
  cflags = ["-g","-O0","-Wno-unused-variable","-fno-omit-frame-pointer"]
  sources = [
    "calculator_fuzzer.cpp",
  ]
}
###############################################################################
group("fuzztest") {
  testonly = true
  deps = []
  deps += [
    # deps file
    ":CalculatorFuzzTest",
  ]
}
关于fuzz配置编写

更改同一目录下的project.xml文件,可以自行更改max_total_time,让用例运行时间缩短或增长

<fuzz_config>
  <fuzztest>
    
    <max_len>1000max_len>
    
    <max_total_time>30max_total_time>
    
    <rss_limit_mb>4096rss_limit_mb>
  fuzztest>
fuzz_config>
fuzz用例编译

在os_account/test/fuzztest/osaccount/的BUILD.gn中的最后一行添加

"calculator_fuzzer:fuzztest"如下所示,中间省略了一些其他配置

group("fuzztest") {
  testonly = true
  deps = []
  
  deps += [ 
  ......
  .....
  "calculator_fuzzer:fuzztest" ]
}

回到wsl当中,切换目录到OpenHarmony的源码主目录下,使用ls命令,其显示结果如下

root@DESKTOP-J061R7B:/openharmony# ls
applications  build     commonlibrary  docs     foundation  kernel          prebuilts      test
arkcompiler   build.py  developtools   domains  ide         napi_generator  productdefine  third_party
base          build.sh  device         drivers  interface   out             qemu-run       vendor

使用命令进行编译(需要wsl连接开发板)

./build.sh --product-name rk3568 --build-target CalculatorFuzzTest

PS:–product-name 必须要有,且要和开发者手上的板子型号一致,具体可以看社区介绍https://gitee.com/openharmony

fuzz用例执行

下载https://gitee.com/openharmony/testfwk_developer_test以及https://gitee.com/openharmony/testfwk_xdevice到本机(Windows下)。

创建一个名为test的文件夹(将上面下载的两个文件解压至该目录)并更改这两个文件夹的名字testfwk_developer_test->developer_test,testfwk_xdevice->xdevice。同时创建一个新的目录tests在test文件夹下

从wsl中找到编译好的测试用例文件,在\\wsl.localhost\Ubuntu-20.04\openharmony\out\rk3568\tests目录下(具体目录由开发者的实际情况决定)

将fuzztest和res两个文件夹复制出来,放到test目录下,最终结构如下

test下
2023/12/05  17:57    <DIR>          .
2023/12/05  17:57    <DIR>          ..
2023/12/05  17:57    <DIR>          developer_test
2023/12/05  17:57    <DIR>          tests
2023/12/05  17:57    <DIR>          xdevice
//tests下
2023/12/05  17:57    <DIR>          .
2023/12/05  17:57    <DIR>          ..
2023/12/05  17:57    <DIR>          fuzztest
2023/12/05  17:57    <DIR>          res

回到developer_test,双击start.bat启动测试框架,首先选择运行平台

Please select the current tested product form:
1. rk3568
2. ipcamera_hispark_aries
3. ipcamera_hispark_taurus
4. wifiiot_hispark_pegasus
default is [1] rk3568
1

然后在控制台输入如下命令即可运行用例

run -t FUZZ -ts CalculatorFuzzTest

输出的测试报告在developer_test\reports目录下

后记

该生成的测试用例是libfuzzer的基本测试用例,虽然名称是CalculatorFuzzTest但是测试的并不是计算器而是在calculator_fuzzer.cpp里面编写的代码段DoSomethingInterestingWithMyAPI,后续测试需要额外编写真正的测试用例。

namespace OHOS {
    bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
    {
        bool result = false;
        if (size >= FUZZ_DATA_LEN) {
            result = data[FUZZ_FST_DATA] == 'F' &&
                data[FUZZ_SND_DATA] == 'U' &&
                data[FUZZ_TRD_DATA] == 'Z' &&
                data[FUZZ_FTH_DATA] == 'Z';
        }
        return result;
    }
}

/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
    /* Run your code on data */
    OHOS::DoSomethingInterestingWithMyAPI(data, size);
    return 0;
}

你可能感兴趣的:(OpenHarmony,huawei,测试工具)