Skip to content

Commit

Permalink
cmocka:Add automated testing framework cmocka in testing
Browse files Browse the repository at this point in the history
Signed-off-by: yintao <yintao@xiaomi.com>
  • Loading branch information
yintao authored and xiaoxiang781216 committed Dec 21, 2022
1 parent 6725d48 commit 3fbb452
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 0 deletions.
29 changes: 29 additions & 0 deletions testing/cmocka/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config TESTING_CMOCKA
tristate "Enable libcmocka"
default n
---help---
Enable libcmocka and run all testcases

if TESTING_CMOCKA

config TESTING_CMOCKA_PROGNAME
string "Program name"
default "cmocka"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.

config TESTING_CMOCKA_PRIORITY
int "cmocka test task priority"
default 100

config TESTING_CMOCKA_STACKSIZE
int "cmocka test stack size"
default DEFAULT_TASK_STACKSIZE

endif
25 changes: 25 additions & 0 deletions testing/cmocka/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
############################################################################
# apps/testing/cmocka/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you 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.
#
############################################################################

ifneq ($(CONFIG_TESTING_CMOCKA),)
CONFIGURED_APPS += $(APPDIR)/testing/cmocka
CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/testing/cmocka/cmocka/include
CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/testing/cmocka/cmocka/include
endif
49 changes: 49 additions & 0 deletions testing/cmocka/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
############################################################################
# apps/testing/cmocka/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you 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 $(APPDIR)/Make.defs

CSRCS += $(wildcard cmocka/src/*.c)
CFLAGS += -Dprint_error=nx_print_error

PROGNAME = $(CONFIG_TESTING_CMOCKA_PROGNAME)
PRIORITY = $(CONFIG_TESTING_CMOCKA_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_CMOCKA_STACKSIZE)
MODULE = $(CONFIG_TESTING_CMOCKA)

MAINSRC = $(CURDIR)/cmocka_main.c

# Download and unpack tarball if no git repo found
ifeq ($(wildcard cmocka/.git),)
VERSION ?= 1.1.5
cmocka.zip:
$(Q) curl -L https://github.com/clibs/cmocka/archive/cmocka-$(VERSION).zip -o cmocka.zip
$(Q) unzip -o cmocka.zip
$(Q) mv cmocka-cmocka-$(VERSION) cmocka

context:: cmocka.zip

distclean::
$(call DELDIR, cmocka)
$(call DELFILE, cmocka.zip)

endif

include $(APPDIR)/Application.mk
125 changes: 125 additions & 0 deletions testing/cmocka/cmocka_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/****************************************************************************
* apps/testing/cmocka/cmocka_main.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>
#include <sched.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
#include <sys/wait.h>

#include <builtin/builtin.h>

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* cmocka_main
****************************************************************************/

int main(int argc, FAR char *argv[])
{
const char prefix[] = CONFIG_TESTING_CMOCKA_PROGNAME"_";
FAR const struct builtin_s *builtin;
int len = strlen(prefix);
FAR char *bypass[argc];
FAR char *cases[argc];
FAR char *skip[argc];
int num_bypass = 1;
int num_cases = 0;
int num_skip = 0;
int ret;
int i;
int j;

if (strlen(argv[0]) < len - 1 ||
strncmp(argv[0], prefix, len - 1))
{
return 0;
}

memset(cases, 0, sizeof(cases));
memset(skip, 0, sizeof(skip));
memset(bypass, 0, sizeof(bypass));

for (i = 1; i < argc; i++)
{
if (strcmp("--case", argv[i]) == 0)
{
cases[num_cases++] = argv[++i];
}
else if (strcmp("--skip", argv[i]) == 0)
{
skip[num_skip++] = argv[++i];
}
else
{
bypass[num_bypass++] = argv[i];
}
}

cmocka_set_skip_filter(NULL);
for (i = 0; skip[i]; i++)
{
cmocka_set_skip_filter(skip[i]);
}

for (i = 0; (builtin = builtin_for_index(i)) != NULL; i++)
{
if (builtin->main == NULL ||
strlen(builtin->name) < len ||
strncmp(builtin->name, prefix, len))
{
continue;
}

for (j = 0; cases[j]; j++)
{
if (strncmp(builtin->name + len,
cases[j], strlen(cases[j])) == 0)
{
break;
}
}

if (j && cases[j] == NULL)
{
continue;
}

bypass[0] = (FAR char *)builtin->name;
ret = exec_builtin(builtin->name, bypass, NULL, 0);
if (ret >= 0)
{
waitpid(ret, &ret, WUNTRACED);
}
}

return 0;
}

0 comments on commit 3fbb452

Please sign in to comment.