Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for a module automatically generated from a Protocol Buffer. #1162

Merged
merged 13 commits into from
Nov 13, 2019
Merged
27 changes: 22 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,41 @@ matrix:
- os: linux
dist: xenial
sudo: required
env: CONFIGFLAGS="CFLAGS=-m64 --enable-cuckoo --enable-magic --enable-address-sanitizer"
env: CONFIGFLAGS="CFLAGS=-m64 --enable-cuckoo --enable-magic --enable-pb-tests --enable-address-sanitizer"
# The certificate for scan.coverity.com is too new and is not recognized by
# wget. This command adds the certificate to /etc/ssl/certs/ca-certificates.crt
# See: https://github.com/travis-ci/travis-ci/issues/6142
before_install: echo -n | openssl s_client -connect scan.coverity.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | sudo tee -a /etc/ssl/certs/ca-certificates.crt
install: |
sudo apt-get update
sudo apt-get install -y autoconf automake libtool libjansson-dev libmagic-dev libssl-dev
sudo apt-get install -y \
autoconf \
automake \
libtool \
libjansson-dev \
libmagic-dev \
libssl-dev \
protobuf-compiler \
protobuf-c-compiler \
libprotobuf-c-dev

# Build for 64-bit Linux with profiling enabled
- os: linux
dist: xenial
sudo: required
env: CONFIGFLAGS="CFLAGS=-m64 --enable-cuckoo --enable-magic --enable-address-sanitizer --enable-profiling"
env: CONFIGFLAGS="CFLAGS=-m64 --enable-cuckoo --enable-magic --enable-pb-tests -enable-address-sanitizer --enable-profiling"
install: |
sudo apt-get update
sudo apt-get install -y autoconf automake libtool libjansson-dev libmagic-dev libssl-dev
sudo apt-get install -y \
autoconf \
automake \
libtool \
libjansson-dev \
libmagic-dev \
libssl-dev \
protobuf-compiler \
protobuf-c-compiler \
libprotobuf-c-dev

# Build for 32-bit Linux
- os: linux
Expand Down Expand Up @@ -84,7 +102,6 @@ script: |
*--host=*mingw*) ;;
*) make check ;;
esac
set +e

after_failure: if [ -e test-suite.log ]; then cat test-suite.log; fi

Expand Down
6 changes: 6 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ test_dotnet_SOURCES = tests/test-dotnet.c tests/util.c
test_dotnet_LDADD = libyara/.libs/libyara.a
endif

if PB_TESTS_MODULE
check_PROGRAMS+=test-pb
test_pb_SOURCES = tests/test-pb.c tests/util.c
test_pb_LDADD = libyara/.libs/libyara.a
endif

# man pages
man1_MANS = yara.man yarac.man

Expand Down
21 changes: 20 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,31 @@ AC_ARG_ENABLE([debug-dex],
CFLAGS="$CFLAGS -DDEBUG_DEX_MODULE"
fi])

AC_ARG_ENABLE([pb-tests],
[AS_HELP_STRING([--enable-pb-tests], [enable protobuf test module])],
[if test x$enableval = xyes; then
build_pb_tests_module=true
AC_CHECK_PROG(PROTOC, protoc, protoc)
AS_IF([test "x${PROTOC}" == "x"],
[AC_MSG_ERROR([protobuf compiler "protoc" not found])])
# AC_CHECK_PROG(PROTOC_GEN_YARA, protoc-gen-yara, protoc-gen-yara)
# AS_IF([test "x${PROTOC_GEN_YARA}" == "x"],
# [AC_MSG_ERROR([please install https://github.com/VirusTotal/protoc-gen-yara])])
PKG_CHECK_MODULES(PROTOBUF_C, libprotobuf-c >= 1.0.0)
AC_CHECK_LIB(protobuf-c, protobuf_c_message_unpack,,
AC_MSG_ERROR([please install libprotobuf-c library]))
CFLAGS="$CFLAGS -DPB_TESTS_MODULE"
PC_REQUIRES_PRIVATE="$PC_REQUIRES_PRIVATE libprotobuf-c"
fi])

AC_ARG_WITH([jemalloc],
[AS_HELP_STRING([--with-jemalloc],
[use jemalloc to debug heap-related issues])],
[if test x$withval = xyes; then
mallctl=mallctl
malloc_stats_print=malloc_stats_print
AC_CHECK_LIB(jemalloc, $jemalloc_prefix$mallctl,,
AC_MSG_ERROR([please install jemalloc library]))
AC_MSG_ERROR([please install jemalloc library]))
CFLAGS="$CFLAGS -DJEMALLOC -Dmallctl=$jemalloc_prefix$mallctl -Dmalloc_stats_print=$jemalloc_prefix$malloc_stats_print $no_std_allocator"
fi])

Expand Down Expand Up @@ -284,6 +301,7 @@ AS_IF([test "x$have_crypto" = "xno"],


AM_CONDITIONAL([GCC], [test "x$GCC" = xyes])
AM_CONDITIONAL([PROTOC], [test "x${PROTOC}" != "x"])
AM_CONDITIONAL([DEBUG], [test x$debug = xtrue])
AM_CONDITIONAL([POSIX], [test x$posix = xtrue])
AM_CONDITIONAL([PROFILING_ENABLED], [test x$profiling_enabled = xtrue])
Expand All @@ -294,6 +312,7 @@ AM_CONDITIONAL([MAGIC_MODULE], [test x$build_magic_module = xtrue])
AM_CONDITIONAL([HASH_MODULE], [test x$build_hash_module = xtrue])
AM_CONDITIONAL([DOTNET_MODULE], [test x$build_dotnet_module = xtrue])
AM_CONDITIONAL([MACHO_MODULE], [test x$build_macho_module = xtrue])
AM_CONDITIONAL([PB_TESTS_MODULE], [test x$build_pb_tests_module = xtrue])
AM_CONDITIONAL([DEX_MODULE], [test x$build_dex_module = xtrue])
AM_CONDITIONAL([DEBUG_DEX_MODULE], [test x$debug_dex_module = xtrue])

Expand Down
187 changes: 106 additions & 81 deletions libyara/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# Rules for generating YARA modules from .proto files. For each .proto file
# three files are generated: .c, .pb-c.c, and .pb-c.h. The .c file is generated
# by protoc-gen-yara and the other two by protoc-gen-c. This is done only if
# protoc is found, if not, files already in the repository are used instead
# of generating fresh ones from the .proto file.
#
if PROTOC
SUFFIXES = .proto .pb-c.c .pb-c.h .c
.proto.pb-c.c:
$(PROTOC) --c_out=$(builddir) $^ -Ipb/ -I=$(srcdir)

.proto.pb-c.h:
$(PROTOC) --c_out=$(builddir) $^ -Ipb/ -I=$(srcdir)

.proto.c:
$(PROTOC) --c_out=$(builddir) $^ -Ipb/ -I=$(srcdir)
endif

MODULES = modules/tests/tests.c

MODULES += modules/elf/elf.c
Expand Down Expand Up @@ -33,6 +51,11 @@ if DEX_MODULE
MODULES += modules/dex/dex.c
endif

if PB_TESTS_MODULE
MODULES += modules/pb_tests/pb_tests.c
MODULES += modules/pb_tests/pb_tests.pb-c.c
endif

#
# Add your modules here:
#
Expand Down Expand Up @@ -69,98 +92,100 @@ include_HEADERS = include/yara.h

yaraincludedir = $(includedir)/yara
yarainclude_HEADERS = \
include/yara/ahocorasick.h \
include/yara/arena.h \
include/yara/atoms.h \
include/yara/bitmask.h \
include/yara/compiler.h \
include/yara/error.h \
include/yara/exec.h \
include/yara/exefiles.h \
include/yara/filemap.h \
include/yara/hash.h \
include/yara/integers.h \
include/yara/libyara.h \
include/yara/limits.h \
include/yara/mem.h \
include/yara/modules.h \
include/yara/object.h \
include/yara/parser.h \
include/yara/proc.h \
include/yara/re.h \
include/yara/rules.h \
include/yara/scan.h \
include/yara/scanner.h \
include/yara/sizedstr.h \
include/yara/stack.h \
include/yara/stopwatch.h \
include/yara/stream.h \
include/yara/strutils.h \
include/yara/threading.h \
include/yara/types.h \
include/yara/utils.h
include/yara/ahocorasick.h \
include/yara/arena.h \
include/yara/atoms.h \
include/yara/bitmask.h \
include/yara/compiler.h \
include/yara/error.h \
include/yara/exec.h \
include/yara/exefiles.h \
include/yara/filemap.h \
include/yara/hash.h \
include/yara/integers.h \
include/yara/libyara.h \
include/yara/limits.h \
include/yara/mem.h \
include/yara/modules.h \
include/yara/object.h \
include/yara/parser.h \
include/yara/proc.h \
include/yara/re.h \
include/yara/rules.h \
include/yara/scan.h \
include/yara/scanner.h \
include/yara/sizedstr.h \
include/yara/stack.h \
include/yara/stopwatch.h \
include/yara/stream.h \
include/yara/strutils.h \
include/yara/threading.h \
include/yara/types.h \
include/yara/utils.h

noinst_HEADERS = \
crypto.h \
exception.h \
include/yara/dotnet.h \
include/yara/elf.h \
include/yara/endian.h \
include/yara/globals.h \
include/yara/hex_lexer.h \
include/yara/lexer.h \
include/yara/pe.h \
include/yara/pe_utils.h \
include/yara/re_lexer.h \
modules/module_list
crypto.h \
exception.h \
include/yara/dotnet.h \
include/yara/elf.h \
include/yara/endian.h \
include/yara/globals.h \
include/yara/hex_lexer.h \
include/yara/lexer.h \
include/yara/pe.h \
include/yara/pe_utils.h \
include/yara/re_lexer.h \
modules/module_list

dist_noinst_DATA = pb/yara.proto


lib_LTLIBRARIES = libyara.la

libyara_la_LDFLAGS = -version-number 3:11:0

BUILT_SOURCES = \
lexer.c \
hex_lexer.c \
re_lexer.c \
grammar.c \
hex_grammar.c \
re_grammar.c
lexer.c \
hex_lexer.c \
re_lexer.c \
grammar.c \
hex_grammar.c \
re_grammar.c

libyara_la_SOURCES = \
$(MODULES) \
grammar.y \
ahocorasick.c \
arena.c \
atoms.c \
bitmask.c \
compiler.c \
endian.c \
exec.c \
exefiles.c \
filemap.c \
hash.c \
hex_grammar.y \
hex_lexer.l \
lexer.l \
libyara.c \
mem.c \
modules.c \
object.c \
parser.c \
proc.c \
re.c \
re_grammar.y \
re_lexer.l \
rules.c \
scan.c \
scanner.c \
sizedstr.c \
stack.c \
stopwatch.c \
strutils.c \
stream.c \
threading.c
$(MODULES) \
grammar.y \
ahocorasick.c \
arena.c \
atoms.c \
bitmask.c \
compiler.c \
endian.c \
exec.c \
exefiles.c \
filemap.c \
hash.c \
hex_grammar.y \
hex_lexer.l \
lexer.l \
libyara.c \
mem.c \
modules.c \
object.c \
parser.c \
proc.c \
re.c \
re_grammar.y \
re_lexer.l \
rules.c \
scan.c \
scanner.c \
sizedstr.c \
stack.c \
stopwatch.c \
strutils.c \
stream.c \
threading.c


if USE_WINDOWS_PROC
Expand Down
2 changes: 1 addition & 1 deletion libyara/include/yara/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#define set_string(value, object, ...) \
set_sized_string(value, strlen(value), object, __VA_ARGS__)
set_sized_string(value, (value == NULL) ? 0 : strlen(value), object, __VA_ARGS__)


#define return_integer(integer) { \
Expand Down
4 changes: 4 additions & 0 deletions libyara/modules/module_list
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ MODULE(macho)
#ifdef DEX_MODULE
MODULE(dex)
#endif

#ifdef PB_TESTS_MODULE
MODULE(pb_tests)
#endif
Loading