Commit a0a8ea66 authored by Andreas Schneider's avatar Andreas Schneider

tests: Add test as cmocka test.

parent 48d46cac
......@@ -49,3 +49,9 @@ configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
# check subdirectories
add_subdirectory(src)
if (WITH_TESTING)
find_package(CMocka REQUIRED)
include(AddCMockaTest)
add_subdirectory(tests)
endif (WITH_TESTING)
option(WITH_ZLIB "Build with ZLIB support" ON)
option(WITH_SSH1 "Build with SSH1 support" OFF)
option(WITH_SFTP "Build with SFTP support" ON)
option(WITH_SERVER "Build with SSH server support" ON)
option(WITH_STATIC_LIB "Build with a static library" OFF)
option(WITH_DEBUG_CRYPTO "Build with cryto debug output" OFF)
option(WITH_DEBUG_CALLTRACE "Build with calltrace debug output" ON)
option(WITH_GCRYPT "Compile against libgcrypt" OFF)
option(WITH_PCAP "Compile with Pcap generation support" ON)
option(WITH_INTERNAL_DOC "Compile doxygen internal documentation" OFF)
option(WITH_TESTING "Build with unit tests" OFF)
option(WITH_CLIENT_TESTING "Build with client tests; requires a running sshd" OFF)
option(WITH_BENCHMARKS "Build benchmarks tools" OFF)
if (WITH_ZLIB)
set(WITH_LIBZ ON)
else (WITH_ZLIB)
set(WITH_LIBZ OFF)
endif (WITH_ZLIB)
if(WITH_BENCHMARKS)
set(WITH_TESTING ON)
endif(WITH_BENCHMARKS)
if (WITH_TESTING)
set(WITH_STATIC_LIB ON)
endif (WITH_TESTING)
# - ADD_CHECK_TEST(test_name test_source linklib1 ... linklibN)
# Copyright (c) 2007 Daniel Gollub <dgollub@suse.de>
# Copyright (c) 2007-2010 Andreas Schneider <asn@cynapses.org>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
enable_testing()
include(CTest)
if(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW)
set(CMAKE_C_FLAGS_PROFILING "-g -O0 -Wall -W -Wshadow -Wunused-variable -Wunused-parameter -Wunused-function -Wunused -Wno-system-headers -Wwrite-strings -fprofile-arcs -ftest-coverage" CACHE STRING "Profiling Compiler Flags")
set(CMAKE_SHARED_LINKER_FLAGS_PROFILING " -fprofile-arcs -ftest-coverage" CACHE STRING "Profiling Linker Flags")
set(CMAKE_MODULE_LINKER_FLAGS_PROFILING " -fprofile-arcs -ftest-coverage" CACHE STRING "Profiling Linker Flags")
set(CMAKE_EXEC_LINKER_FLAGS_PROFILING " -fprofile-arcs -ftest-coverage" CACHE STRING "Profiling Linker Flags")
endif(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW)
function (ADD_CMOCKA_TEST _testName _testSource)
add_executable(${_testName} ${_testSource})
target_link_libraries(${_testName} ${ARGN})
add_test(${_testName} ${CMAKE_CURRENT_BINARY_DIR}/${_testName})
endfunction (ADD_CMOCKA_TEST)
# - Try to find CMocka
# Once done this will define
#
# CMOCKA_ROOT_DIR - Set this variable to the root installation of CMocka
#
# Read-Only variables:
# CMOCKA_FOUND - system has CMocka
# CMOCKA_INCLUDE_DIR - the CMocka include directory
# CMOCKA_LIBRARIES - Link these to use CMocka
# CMOCKA_DEFINITIONS - Compiler switches required for using CMocka
#
#=============================================================================
# Copyright (c) 2011-2012 Andreas Schneider <asn@cryptomilk.org>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
#
find_path(CMOCKA_INCLUDE_DIR
NAMES
cmocka.h
PATHS
${CMOCKA_ROOT_DIR}/include
)
find_library(CMOCKA_LIBRARY
NAMES
cmocka
PATHS
${CMOCKA_ROOT_DIR}/include
)
if (CMOCKA_LIBRARY)
set(CMOCKA_LIBRARIES
${CMOCKA_LIBRARIES}
${CMOCKA_LIBRARY}
)
endif (CMOCKA_LIBRARY)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(CMocka DEFAULT_MSG CMOCKA_LIBRARIES CMOCKA_INCLUDE_DIR)
# show the CMOCKA_INCLUDE_DIR and CMOCKA_LIBRARIES variables only in the advanced view
mark_as_advanced(CMOCKA_INCLUDE_DIR CMOCKA_LIBRARIES)
project(tests C)
include_directories(
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${CMOCKA_INCLUDE_DIR}
)
add_cmocka_test(testsuite testsuite.c ${CMOCKA_LIBRARY})
/*
Unix SMB/CIFS implementation.
local testing of the socket wrapper
Copyright (C) Jelmer Vernooij 2007
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "system/network.h"
#include "../socket_wrapper/socket_wrapper.h"
#include "torture/torture.h"
static char *old_dir = NULL;
static char *old_iface = NULL;
static void backup_env(void)
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
static void setup(void **state)
{
old_dir = getenv("SOCKET_WRAPPER_DIR");
old_iface = getenv("SOCKET_WRAPPER_DEFAULT_IFACE");
char test_tmpdir[256];
const char *p;
(void) state; /* unused */
snprintf(test_tmpdir, sizeof(test_tmpdir), "/tmp/test_socket_wrapper_XXXXXX");
p = mkdtemp(test_tmpdir);
assert_non_null(p);
setenv("SOCKET_WRAPPER_DIR", p, 1);
setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "11", 1);
}
static void restore_env(void)
static void teardown(void **state)
{
if (old_dir == NULL)
unsetenv("SOCKET_WRAPPER_DIR");
else
setenv("SOCKET_WRAPPER_DIR", old_dir, 1);
if (old_iface == NULL)
unsetenv("SOCKET_WRAPPER_DEFAULT_IFACE");
else
setenv("SOCKET_WRAPPER_DEFAULT_IFACE", old_iface, 1);
char remove_cmd[256];
const char *swrap_dir = getenv("SOCKET_WRAPPER_DIR");
(void) state; /* unused */
if (swrap_dir != NULL) {
snprintf(remove_cmd, sizeof(remove_cmd), "rm -rf %s", swrap_dir);
}
system(remove_cmd);
}
static bool test_socket_wrapper_dir(struct torture_context *tctx)
#if 0
static void test_socket_wrapper_dir(void **state)
{
backup_env();
setenv("SOCKET_WRAPPER_DIR", "foo", 1);
torture_assert_str_equal(tctx, socket_wrapper_dir(), "foo", "setting failed");
assert_string_equal(socket_wrapper_dir(), "foo");
setenv("SOCKET_WRAPPER_DIR", "./foo", 1);
torture_assert_str_equal(tctx, socket_wrapper_dir(), "foo", "setting failed");
assert_string_equal(socket_wrapper_dir(), "foo");
unsetenv("SOCKET_WRAPPER_DIR");
torture_assert_str_equal(tctx, socket_wrapper_dir(), NULL, "resetting failed");
assert_non_null(socket_wrapper_dir());
restore_env();
return true;
}
#endif
static bool test_swrap_socket(struct torture_context *tctx)
static void test_swrap_socket(void **state)
{
backup_env();
setenv("SOCKET_WRAPPER_DIR", "foo", 1);
(void) state; /* unused */
torture_assert_int_equal(tctx, swrap_socket(1337, 1337, 0), -1, "unknown address family fails");
torture_assert_int_equal(tctx, errno, EAFNOSUPPORT, "correct errno set");
torture_assert_int_equal(tctx, swrap_socket(AF_INET, 1337, 0), -1, "unknown type fails");
torture_assert_int_equal(tctx, errno, EPROTONOSUPPORT, "correct errno set");
torture_assert_int_equal(tctx, swrap_socket(AF_INET, SOCK_DGRAM, 10), -1, "unknown protocol fails");
torture_assert_int_equal(tctx, errno, EPROTONOSUPPORT, "correct errno set");
assert_int_equal(socket(1337, 1337, 0), -1);
assert_int_equal(errno, EAFNOSUPPORT);
restore_env();
assert_int_equal(socket(AF_INET, 1337, 0), -1);
assert_int_equal(errno, EPROTONOSUPPORT);
return true;
assert_int_equal(socket(AF_INET, SOCK_DGRAM, 10), -1);
assert_int_equal(errno, EPROTONOSUPPORT);
}
#if 0
unsigned int socket_wrapper_default_iface(void);
static bool test_socket_wrapper_default_iface(struct torture_context *tctx)
{
......@@ -91,15 +84,16 @@ static bool test_socket_wrapper_default_iface(struct torture_context *tctx)
restore_env();
return true;
}
#endif
struct torture_suite *torture_local_socket_wrapper(TALLOC_CTX *mem_ctx)
{
struct torture_suite *suite = torture_suite_create(mem_ctx,
"socket-wrapper");
int main(void) {
int rc;
const UnitTest tests[] = {
unit_test_setup_teardown(test_swrap_socket, setup, teardown),
};
torture_suite_add_simple_test(suite, "socket_wrapper_dir", test_socket_wrapper_dir);
torture_suite_add_simple_test(suite, "socket", test_swrap_socket);
torture_suite_add_simple_test(suite, "socket_wrapper_default_iface", test_socket_wrapper_default_iface);
rc = run_tests(tests);
return suite;
return rc;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment