未验证 提交 6b72f626 编写于 作者: O openharmony_ci 提交者: Gitee

!621 增加相对应的测试的代码,便于编译对应版本的程序

Merge pull request !621 from Mupceet/collect_test_0517
......@@ -15,6 +15,7 @@ declare_args() {
param_feature_watcher = true
param_feature_deviceinfo = true
param_test = true
control_test = false
param_begetctl_liteos_support = false
enable_ohos_startup_init_lite_use_thirdparty_mbedtls = true
enable_ohos_startup_init_lite_use_posix_file_api = false
......
......@@ -51,7 +51,8 @@
"//base/startup/init_lite/services/init/module_engine:libinit_module_engine",
"//base/startup/init_lite/services/init/module_engine:libinit_stub_empty",
"//base/startup/init_lite/device_info:device_info_group",
"//base/startup/init_lite/interfaces/innerkits/sandbox:libsandbox"
"//base/startup/init_lite/interfaces/innerkits/sandbox:libsandbox",
"//base/startup/init_lite/test/exec_test:exectest"
],
"inner_kits": [
{
......
# 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.
import("//base/startup/init_lite/begetd.gni")
import("//build/ohos.gni")
ohos_executable("client") {
sources = [ "client.c" ]
include_dirs = [
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//third_party/bounds_checking_function/include",
]
deps = [
"//base/startup/init_lite/interfaces/innerkits/socket:libsocket_static",
"//base/startup/init_lite/services/log:init_log",
"//third_party/bounds_checking_function:libsec_static",
]
install_images = [ "system" ]
install_enable = true
part_name = "init"
}
ohos_executable("fd_holder_test") {
sources = [ "fd_holder_test.c" ]
defines = [ "INIT_AGENT" ]
deps = [ "//base/startup/init_lite/interfaces/innerkits:libbegetutil" ]
include_dirs = [ "//base/startup/init_lite/interfaces/innerkits/include" ]
install_images = [ "system" ]
install_enable = true
part_name = "init"
}
ohos_prebuilt_etc("fd_holder_test.cfg") {
source = "//base/startup/init_lite/test/exec_test/fd_holder_test.cfg"
part_name = "init"
module_install_dir = "etc/init"
}
ohos_executable("server") {
sources = [ "server.c" ]
include_dirs = [
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//third_party/bounds_checking_function/include",
]
deps = [
"//base/startup/init_lite/interfaces/innerkits/socket:libsocket_static",
"//base/startup/init_lite/services/log:init_log",
"//third_party/bounds_checking_function:libsec_static",
]
install_images = [ "system" ]
install_enable = true
part_name = "init"
}
group("exectest") {
if (control_test) {
deps = [
":client",
":fd_holder_test",
":fd_holder_test.cfg",
":server",
]
}
}
/*
* 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 <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "init_log.h"
#include "init_socket.h"
#include "securec.h"
#define BUFFER_LENGTH 15
#define ARG_COUNT 2
int main(int argc, char* argv[])
{
if (argc < ARG_COUNT) {
INIT_LOGE("Client need an argument");
return 0;
}
int sockFd = socket(PF_UNIX, SOCK_DGRAM, 0);
if (sockFd < 0) {
INIT_LOGE("Failed to create client socket");
return -1;
}
struct sockaddr_un addr;
bzero(&addr, sizeof(struct sockaddr_un));
addr.sun_family = PF_UNIX;
size_t addrLen = sizeof(addr.sun_path);
int ret = 0;
if (strcmp(argv[1], "server") == 0) {
ret = snprintf_s(addr.sun_path, addrLen, addrLen - 1, "/dev/unix/socket/serversock");
INIT_ERROR_CHECK(ret >= 0, return -1, "Failed to format addr");
if (connect(sockFd, (struct sockaddr *)&addr, sizeof(addr))) {
INIT_LOGE("Failed to connect socket: %d", errno);
return -1;
}
} else {
INIT_LOGE("input error, invalid server name");
return -1;
}
ret = write(sockFd, argv[ARG_COUNT], strlen(argv[ARG_COUNT]));
if (ret < 0) {
INIT_LOGE("Failed to write, errno = %d", errno);
}
close(sockFd);
return 0;
}
\ No newline at end of file
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "beget_ext.h"
#include "fd_holder.h"
#define BUFFER_LENGTH 5
#define FD_COUNT 2
#define ENV_FD_HOLD_PREFIX "OHOS_FD_HOLD_"
static void SaveFds(const char *serviceName, int argc, char **argv)
{
int fds[10];
int i;
for (i = 0; i < argc; i++) {
BEGET_LOGI("Opening %s \n", argv[i]);
fds[i] = open(argv[i], O_APPEND | O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fds[i] < 0) {
BEGET_LOGI("Failed to open file %s\n", argv[i]);
break;
}
(void)write(fds[i], "hello", BUFFER_LENGTH);
}
int fdCount = i;
BEGET_LOGI("fdCount = %d\n", fdCount);
if (fdCount <= 0) {
BEGET_LOGE("Invalid fds for hold, abort\n");
return;
}
int ret = ServiceSaveFd(serviceName, fds, (size_t)fdCount);
if (ret < 0) {
BEGET_LOGE("Request init save fds failed\n");
} else {
BEGET_LOGI("Request init save fds done\n");
}
}
int main(int argc, char **argv)
{
size_t outfdCount = 0;
int *fds = ServiceGetFd("fd_holder_test", &outfdCount);
if (fds == NULL) {
BEGET_LOGE("Cannot get fds, maybe first time\n");
} else {
for (size_t i = 0; i < outfdCount; i++) {
int fd = fds[i];
if (write(fd, "world", BUFFER_LENGTH) < 0) {
BEGET_LOGE("Failed to write content to fd: %d, err = %d", fd, errno);
}
close(fd);
}
free(fds);
outfdCount = 0;
}
char *files[] = {"/data/test/1", "/data/test/2"};
SaveFds("fd_holder_test", FD_COUNT, (char **)files);
while (1) {
pause();
}
return 0;
}
{
"jobs" : [{
"name" : "post-fs-data",
"cmds" : [
"mkdir /data/test",
"start fd_holder_test"
]
}
],
"services" : [{
"name" : "fd_holder_test",
"path" : ["/system/bin/fd_holder_test", "save", "fd_holder_test", "/data/test/1", "/data/test/2"],
"uid" : "root",
"gid" : "system",
"once" : 1
}
]
}
\ No newline at end of file
/*
* 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 <errno.h>
#include "init_log.h"
#include "init_socket.h"
#include "securec.h"
#define MAX_BUFFER_SIZE 1024
int main(int argc, char* argv[])
{
int ret = 0;
int sockFd = GetControlSocket("serversock");
if (sockFd < 0) {
INIT_LOGE("Failed to get server socket");
return -1;
}
char buffer[MAX_BUFFER_SIZE] = { 0 };
while (1) {
ret = read(sockFd, buffer, sizeof(buffer) - 1);
if (ret > 0) {
INIT_LOGI("Read message: %s", buffer);
(void)memset_s(buffer, MAX_BUFFER_SIZE, 0, MAX_BUFFER_SIZE);
}
}
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册