From f81dd109d18434cdc7d2ab2cdfff014edeb8848f Mon Sep 17 00:00:00 2001 From: FondMemoryVVV Date: Fri, 22 Jul 2022 15:23:30 +0800 Subject: [PATCH] add toolchain Signed-off-by: FondMemoryVVV --- toolchain/BUILD.gn | 90 +++++++++++++++ toolchain/libc-test/BUILD.gn | 54 +++++++++ toolchain/libc-test/Test.json | 32 ++++++ toolchain/libc-test/include/getfiles.cpp | 44 ++++++++ toolchain/libc-test/include/runtest.h | 25 +++++ toolchain/libc-test/include/setrlim.cpp | 44 ++++++++ toolchain/libc-test/src/toolchaintest.cpp | 131 ++++++++++++++++++++++ toolchain/libc-test/tar_files.py | 62 ++++++++++ 8 files changed, 482 insertions(+) create mode 100644 toolchain/BUILD.gn create mode 100644 toolchain/libc-test/BUILD.gn create mode 100644 toolchain/libc-test/Test.json create mode 100644 toolchain/libc-test/include/getfiles.cpp create mode 100644 toolchain/libc-test/include/runtest.h create mode 100644 toolchain/libc-test/include/setrlim.cpp create mode 100644 toolchain/libc-test/src/toolchaintest.cpp create mode 100644 toolchain/libc-test/tar_files.py diff --git a/toolchain/BUILD.gn b/toolchain/BUILD.gn new file mode 100644 index 000000000..411eb9498 --- /dev/null +++ b/toolchain/BUILD.gn @@ -0,0 +1,90 @@ +# Copyright (C) 2021 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/ohos_var.gni") +import("//test/xts/tools/build/suite.gni") + +group("toolchain") { + testonly = true + if (is_standard_system) { + deps = [ + #":tar_testcases", + ":tar_dllib", + ] + } +} + +action("tar_testcases") { + testonly = true + deps = [ + "libc-test:ActToolChainTest", + "//third_party/musl:libctest", + ] + project_dir = rebase_path(".") + print("project_dir-58:", project_dir) + project_dird = rebase_path("tar_files.py", ".", root_out_dir) + print("project_dird-60:", project_dird) + + project_dirf = project_dir + "/" + project_dird + print("project_dirf-64:", project_dirf) + + test_path = string_replace(project_dirf, "/tar_files.py", "") + script = rebase_path("//test/xts/acts/toolchain/libc-test/tar_files.py") + + _outputs = [ "$target_out_dir/libc-test.tar" ] + outputs = _outputs + + input_path = rebase_path("$test_path/tests/unittest/libc-test") + output_path = rebase_path("$test_path/suites/acts/testcases/libc-test.tar") + + print("root_build_dir-49", root_build_dir) + args = [ + "--input_path", + input_path, + "--output_path", + output_path, + "--temp_path", + "./libc-test", + ] +} + +action("tar_dllib") { + testonly = true + deps = [ ":tar_testcases" ] + project_dir = rebase_path(".") + print("project_dir-58:", project_dir) + project_dird = rebase_path("tar_files.py", ".", root_out_dir) + print("project_dird-60:", project_dird) + + project_dirf = project_dir + "/" + project_dird + print("project_dirf-64:", project_dirf) + + dllib_path = string_replace(project_dirf, "/tar_files.py", "") + script = rebase_path("//test/xts/acts/toolchain/libc-test/tar_files.py") + + _outputs = [ "$target_out_dir/libc-test-lib.tar" ] + outputs = _outputs + + input_path = rebase_path("$dllib_path/musl/libc-test-lib") + output_path = + rebase_path("$dllib_path/suites/acts/testcases/libc-test-lib.tar") + + print("root_build_dir-49", root_build_dir) + args = [ + "--input_path", + input_path, + "--output_path", + output_path, + "--temp_path", + "./libc-test-lib", + ] +} diff --git a/toolchain/libc-test/BUILD.gn b/toolchain/libc-test/BUILD.gn new file mode 100644 index 000000000..6005a9e89 --- /dev/null +++ b/toolchain/libc-test/BUILD.gn @@ -0,0 +1,54 @@ +# Copyright (C) 2021 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("//test/xts/tools/build/suite.gni") + +#module_output_path = "hits/huks_standard" +######################################################## +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//utils/native/base/include", + "//third_party/bounds_checking_function/include", + "//third_party/musl/porting/linux/user/include", + "//test/xts/acts/security_lite/huks/common/include", + ] +} + +######################################################## +ohos_moduletest_suite("ActToolChainTest") { + configs = [ ":module_private_config" ] + cflags_cc = [ "-DHILOG_ENABLE" ] + defines = [ "_STANDARD_SYSTEM_" ] + + sources = [ + "include/getfiles.cpp", + "include/setrlim.cpp", + "src/toolchaintest.cpp", + ] + + include_dirs = [ + "//utils/native/base/include", + "//third_party/bounds_checking_function/include", + "//third_party/musl/porting/linux/user/include/", + "//third_party/musl/porting/linux/user/src/sched", + "/third_party/musl/libc-test/src/commom", + "./include", + ] + + deps = [ + "//third_party/bounds_checking_function:libsec_static", + "//utils/native/base:utils", + ] +} diff --git a/toolchain/libc-test/Test.json b/toolchain/libc-test/Test.json new file mode 100644 index 000000000..7949e491e --- /dev/null +++ b/toolchain/libc-test/Test.json @@ -0,0 +1,32 @@ +{ + "kits": [ + { + "push": [ + "ActToolChainTest->/data/local/tmp/ActToolChainTest", + "libc-test.tar->/data/local/tmp/libc-test.tar", + "libc-test-lib.tar->/data/local/tmp/libc-test-lib.tar" + ], + "type": "PushKit", + "post-push": [ + "mkdir /tmp", + "mkdir /dev/shm", + "mkdir /src", + "mkdir /src/functional", + "tar -xf /data/local/tmp/libc-test.tar -C /data/local/tmp/", + "tar -xf /data/local/tmp/libc-test-lib.tar -C /data/local/tmp/", + "chmod a+x -R /data/local/tmp/libc-test-lib", + "cp /data/local/tmp/libc-test-lib/{libdlopen_dso.so,libdlclose_reset_dso.so,libtls_get_new-dtv_dso.so} /", + "cp /data/local/tmp/libc-test-lib/{libtls_align_dso.so,libtls_init_dso.so} /src/functional/", + "chmod a+x -R /data/local/tmp/libc-test" + ] + } + ], + "driver": { + "native-test-timeout": "120000", + "type": "CppTest", + "module-name": "ActToolChainTest", + "runtime-hint": "1s", + "native-test-device-path": "/data/local/tmp" + }, + "description": "Configuration for ActToolChainTest Tests" +} \ No newline at end of file diff --git a/toolchain/libc-test/include/getfiles.cpp b/toolchain/libc-test/include/getfiles.cpp new file mode 100644 index 000000000..2c7554c72 --- /dev/null +++ b/toolchain/libc-test/include/getfiles.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2022 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 +#include +#include +#include + +#include "runtest.h" + +void GetFileNames(std::string path, std::vector& filenames) +{ + DIR *pDir; + struct dirent* ptr; + std::string p; + if (!(pDir = opendir(path.c_str()))) { + std::cout << "Folder doesn't Exist!" << std::endl; + return; + } + while ((ptr = readdir(pDir)) != nullptr) { + if (ptr->d_type == DT_DIR) { + if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { + GetFileNames(path + "/" + ptr->d_name, filenames); + } + } else { + if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { + filenames.push_back(path + "/" + ptr->d_name); + } + } + } + closedir(pDir); +} \ No newline at end of file diff --git a/toolchain/libc-test/include/runtest.h b/toolchain/libc-test/include/runtest.h new file mode 100644 index 000000000..5ba4aef43 --- /dev/null +++ b/toolchain/libc-test/include/runtest.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2022 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. + */ +#ifndef TOOLCHAIN_RUNTEST_H +#define TOOLCHAIN_RUNTEST_H + +#include +#include +#include + +int t_setrlim(int r, long lim); +void GetFileNames(std::string path, std::vector& filenames); + +#endif // TOOLCHAIN_LIBC_TEST_INCLUDE_RUNTEST_H_ diff --git a/toolchain/libc-test/include/setrlim.cpp b/toolchain/libc-test/include/setrlim.cpp new file mode 100644 index 000000000..63b4ea340 --- /dev/null +++ b/toolchain/libc-test/include/setrlim.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 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 +#include +#include +#include + +#include "runtest.h" + +int t_setrlim(int r, long lim) +{ + struct rlimit rl; + + if (getrlimit(r, &rl)) { + printf("getrlimit %d: %s\n", r, strerror(errno)); + return -1; + } + if (lim > rl.rlim_max) { + return -1; + } + if (lim == rl.rlim_max && lim == rl.rlim_cur) { + return 0; + } + rl.rlim_max = lim; + rl.rlim_cur = lim; + if (setrlimit(r, &rl)) { + printf("setrlimit(%d, %ld): %s\n", r, lim, strerror(errno)); + return -1; + } + return 0; +} + diff --git a/toolchain/libc-test/src/toolchaintest.cpp b/toolchain/libc-test/src/toolchaintest.cpp new file mode 100644 index 000000000..71cc5e887 --- /dev/null +++ b/toolchain/libc-test/src/toolchaintest.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2022 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gtest/gtest.h" +#include "runtest.h" + +using namespace std; +using namespace testing::ext; +class ToolChainTest : public testing::Test {}; + +volatile int t_status = 0; + +static void handler(int s) +{ +} + +static int start(char *wrap, const char *argvs) +{ + int pid, space_size = 100*1024; + + pid = fork(); + if (pid == 0) { + t_setrlim(RLIMIT_STACK, space_size); + int exe = execl(argvs, "strptime", nullptr); + printf("exe:%d %s exec failed: %s\n", exe, argvs, strerror(errno)); + exit(1); + } + return pid; +} + +static int runtests(const char *argvs) +{ + char wrap[] = ""; + int timeoutsec = 5, timeout = 0; + int status, pid; + sigset_t set; + void (*retfunc)(int); + + sigemptyset(&set); + sigaddset(&set, SIGCHLD); + sigprocmask(SIG_BLOCK, &set, nullptr); + retfunc = signal(SIGCHLD, handler); + if (retfunc == SIG_ERR) { + printf("signal triggering failed:%s\n", strerror(errno)); + } + pid = start(wrap, argvs); + if (pid == -1) { + printf("%s fork failed: %s\n", argvs, strerror(errno)); + printf("FAIL %s [internal]\n", argvs); + return -1; + } + struct timespec tp; + tp.tv_sec = timeoutsec; + tp.tv_nsec = 0; + if (sigtimedwait(&set, nullptr, &tp) == -1) { + if (errno == EAGAIN) { + timeout = 1; + } else { + printf("%s sigtimedwait failed: %s\n", argvs, strerror(errno)); + } + if (kill(pid, SIGKILL) == -1) { + printf("%s kill failed: %s\n", argvs, strerror(errno)); + } + } + if (waitpid(pid, &status, 0) != pid) { + printf("%s waitpid failed: %s\n", argvs, strerror(errno)); + printf("FAIL %s [internal]\n", argvs); + return -1; + } + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) == 0) { + return t_status; + } + printf("FAIL %s [status %d]\n", argvs, WEXITSTATUS(status)); + } else if (timeout) { + printf("FAIL %s [timed out]\n", argvs); + } else if (WIFSIGNALED(status)) { + printf("FAIL %s [signal %s]\n", argvs, strsignal(WTERMSIG(status))); + } else { + printf("FAIL %s [unknown]\n", argvs); + } + return 1; +} + +/** + * @tc.name : ToolChainTest.LibcTest + * @tc.desc : start test + * @tc.level : Level 2 + */ +HWTEST_F(ToolChainTest, LibcTest, Function | MediumTest | Level3) +{ + int ret; + vector temp; + string filepath = "/data/local/tmp/libc-test/src"; + GetFileNames(filepath, temp); + for (size_t i = 0; i < temp.size(); i++) { + if ((temp[i].find("stat", filepath.length()-1) != -1) || + (temp[i].find("sem_close-unmap", filepath.length()-1) != -1) || + (temp[i].find("runtest", filepath.length()-1) != -1)) { + continue; + } + ret = runtests(temp[i].c_str()); + EXPECT_EQ(0, ret) << "test " << temp[i] << " failed" << endl; + } +} diff --git a/toolchain/libc-test/tar_files.py b/toolchain/libc-test/tar_files.py new file mode 100644 index 000000000..1f52fc237 --- /dev/null +++ b/toolchain/libc-test/tar_files.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +Copyright (c) 2020-2021 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 os +import argparse +import tarfile +import shutil + +copyFileCounts = 0 + +def copyFiles(sourceDir, targetDir): + global copyFileCounts + for f in os.listdir(sourceDir): + sourceF = os.path.join(sourceDir, f) + targetF = os.path.join(targetDir, f) + if os.path.isfile(sourceF): + if not os.path.exists(targetDir): + os.makedirs(targetDir) + copyFileCounts += 1 + if not os.path.exists(targetF) or (os.path.exists(targetF) and (os.path.getsize(targetF) != os.path.getsize(sourceF))): + open(targetF, "wb").write(open(sourceF, "rb").read()) + if os.path.isdir(sourceF): + copyFiles(sourceF, targetF) + +def make_targz_one_by_one(output_filename, source_dir): + tar = tarfile.open(output_filename,"w") + for root,dir,files in os.walk(source_dir): + for file in files: + pathfile = os.path.join(root, file) + tar.add(pathfile) + tar.close() + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='manual to this script') + parser.add_argument("--input_path", type=str, default="0") + parser.add_argument("--output_path", type=str, default="0") + parser.add_argument("--temp_path", type=str, default="0") + args = parser.parse_args() + print(args.input_path) + print(args.output_path) + print(args.temp_path) + + copyFiles(args.input_path, args.temp_path) + make_targz_one_by_one(args.output_path, args.temp_path) + + shutil.rmtree(args.temp_path) #delete middle files -- GitLab