Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/freetds/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ static inline int tds_thread_is_current(tds_thread_id th)
# define tds_cond_init tds_raw_cond_init
# define tds_cond_destroy tds_raw_cond_destroy
# define tds_cond_signal tds_raw_cond_signal
# if !ENABLE_EXTRA_CHECKS
# if !ENABLE_EXTRA_CHECKS || !defined(TDS_HAVE_MUTEX)
# define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
# define tds_mutex tds_raw_mutex
# define tds_mutex_lock tds_raw_mutex_lock
Expand Down
64 changes: 64 additions & 0 deletions include/freetds/utils/test_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef _tdsguard_afBM6E9n8CuIFSBHNNblq5
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add copyright lines.

#define _tdsguard_afBM6E9n8CuIFSBHNNblq5

/*
* Base header for FreeTDS unit tests, even those just covering helpers
* from the utils and replacements trees. Should be included first
* (possibly via a common.h) to be certain of preceding <assert.h>.
*/

/* Ensure assert is always active. */
#undef NDEBUG
#ifdef assert
# error "Include test_base.h (or common.h) earlier"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test turns out to be too strong for some tests. The problem is that some tests include, at the beginning, a C file and that's supposed to be the first thing they do (all some linking trick). Would be enough to check that NDEBUG is not defined instead ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already adjusted these tests to start with appropriate #include directives (common.h for tds tests, test_base.h for replacements tests), which AFAICT should be fine. I do see compilation errors for this branch, oops, but due to different formal considerations I'll need to address -- old MSVC versions complain about a C99ism I accidentally put in the tds read_login_info, and newer versions complain about a missed change to collations.c that somehow slipped through the cracks.

At any rate, if this particular construct is a problem, I reckon

#if defined(assert)  &&  defined(NDEBUG)
#  error "..."
#endif
#undef NDEBUG
/* ... */

would still be one in at least some cases.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds reasonable. Specifically it fails to build due to some duplicate definition. One due to some MingW headers, another due to some potential Windows macros. But mostly, the trick of including the C file to avoid exporting symbols works well if you include the C file as soon as possible.

I'll do the change. Possibly also some new tests came out, I'll add required changes if needed.

#endif

#include <config.h>

#include <freetds/bool.h>
#include <freetds/macros.h>

/*
* Tests should define test_main in lieu of main so that they can be
* configured to suppress automation-unfriendly crash dialog boxes on
* Windows. To that end, they can use the TEST_MAIN macro, which cleanly
* avoids warnings for the tests that ignore their arguments (but still
* provides the details under conventional names for the remainder).
*/
int test_main(int argc, char **argv);

#define TEST_MAIN() int test_main(int argc TDS_UNUSED, char **argv TDS_UNUSED)


typedef struct
{
char SERVER[512];
char DATABASE[512];
char USER[512];
char PASSWORD[512];
char DRIVER[1024]; /* ODBC-only */
char CHARSET[512];
int maxlength;
bool fverbose;
bool initialized;
bool tried_env;
} COMMON_PWD;

extern COMMON_PWD common_pwd;

#define DEFAULT_PWD_PATH "../../../PWD"

void reset_login_info(COMMON_PWD * common_pwd);

/*
* Both return the path used (favoring $TDSPWDFILE in the absence of
* tried_env) on success or NULL on failure (silently in the case of
* try_read_login_info), and defer to any existing settings from
* e.g. the command line.
*/
const char * read_login_info_base(COMMON_PWD * common_pwd,
const char * default_path);
const char * try_read_login_info_base(COMMON_PWD * common_pwd,
const char * default_path);

#endif /* _tdsguard_afBM6E9n8CuIFSBHNNblq5 */
3 changes: 2 additions & 1 deletion src/apps/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ include_directories(..)
foreach(target defncopy)
add_executable(a_${target} EXCLUDE_FROM_ALL ${target}.c)
set_target_properties(a_${target} PROPERTIES OUTPUT_NAME ${target})
target_link_libraries(a_${target} replacements tdsutils ${lib_NETWORK} ${lib_BASE})
target_link_libraries(a_${target} tds_test_base replacements tdsutils
${lib_NETWORK} ${lib_BASE})
add_test(NAME a_${target} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND a_${target})
add_dependencies(check a_${target})
endforeach(target)
4 changes: 3 additions & 1 deletion src/apps/unittests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ check_PROGRAMS = $(TESTS)
defncopy_SOURCES = defncopy.c

AM_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir)/.. -I../ -DFREETDS_TOPDIR=\"$(top_srcdir)\"
LDADD = ../../replacements/libreplacements.la $(LTLIBICONV) $(NETWORK_LIBS)
LDADD = ../../utils/unittests/libtds_test_base.a \
../../replacements/libreplacements.la $(LTLIBICONV) \
$(NETWORK_LIBS)
EXTRA_DIST = CMakeLists.txt

54 changes: 9 additions & 45 deletions src/apps/unittests/defncopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
* This tests execute some command using tsql and defncopy to check behaviour
*/

#undef NDEBUG
#include <config.h>
#include <freetds/utils/test_base.h>

#include <stdio.h>
#include <stdlib.h>
Expand All @@ -48,49 +47,14 @@
#include <freetds/bool.h>
#include <freetds/macros.h>

static char USER[512];
static char SERVER[512];
static char PASSWORD[512];
static char DATABASE[512];

/* content of output file, from command executed */
static char *output;

static bool
read_login_info(void)
{
FILE *in = NULL;
char line[512];
char *s1, *s2;

s1 = getenv("TDSPWDFILE");
if (s1 && s1[0])
in = fopen(s1, "r");
if (!in)
in = fopen("../../../PWD", "r");
if (!in) {
fprintf(stderr, "Can not open PWD file\n\n");
return false;
}

while (fgets(line, sizeof(line), in)) {
s1 = strtok(line, "=");
s2 = strtok(NULL, "\n");
if (!s1 || !s2) {
continue;
}
if (!strcmp(s1, "UID")) {
strcpy(USER, s2);
} else if (!strcmp(s1, "SRV")) {
strcpy(SERVER, s2);
} else if (!strcmp(s1, "PWD")) {
strcpy(PASSWORD, s2);
} else if (!strcmp(s1, "DB")) {
strcpy(DATABASE, s2);
}
}
fclose(in);
return true;
reset_login_info(&common_pwd);
return read_login_info_base(&common_pwd, DEFAULT_PWD_PATH) != NULL;
}

static void
Expand Down Expand Up @@ -212,14 +176,14 @@ static char *
add_server(char *dest, char *const dest_end)
{
dest = add_string(dest, dest_end, " -S ");
dest = quote_arg(dest, dest_end, SERVER);
dest = quote_arg(dest, dest_end, common_pwd.SERVER);
dest = add_string(dest, dest_end, " -U ");
dest = quote_arg(dest, dest_end, USER);
dest = quote_arg(dest, dest_end, common_pwd.USER);
dest = add_string(dest, dest_end, " -P ");
dest = quote_arg(dest, dest_end, PASSWORD);
if (DATABASE[0]) {
dest = quote_arg(dest, dest_end, common_pwd.PASSWORD);
if (common_pwd.DATABASE[0]) {
dest = add_string(dest, dest_end, " -D ");
dest = quote_arg(dest, dest_end, DATABASE);
dest = quote_arg(dest, dest_end, common_pwd.DATABASE);
}
return dest;
}
Expand Down Expand Up @@ -413,7 +377,7 @@ test_weird_index_names(void)
tsql(clean);
}

int main(void)
TEST_MAIN()
{
FILE *f;

Expand Down
8 changes: 6 additions & 2 deletions src/ctlib/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ foreach(target t0001 t0002 t0003 t0004
add_executable(c_${target} EXCLUDE_FROM_ALL ${target}.c)
set_target_properties(c_${target} PROPERTIES OUTPUT_NAME ${target})
if (target STREQUAL "all_types")
target_link_libraries(c_${target} c_common ct-static t_common tds replacements tdsutils ${lib_NETWORK} ${lib_BASE})
target_link_libraries(c_${target} c_common ct-static
t_common tds_test_base tds replacements
tdsutils ${lib_NETWORK} ${lib_BASE})
else()
target_link_libraries(c_${target} c_common ct replacements tdsutils ${lib_NETWORK} ${lib_BASE})
target_link_libraries(c_${target} c_common tds_test_base ct
replacements tdsutils ${lib_NETWORK}
${lib_BASE})
endif()
add_test(NAME c_${target} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND c_${target})
add_dependencies(check c_${target})
Expand Down
4 changes: 3 additions & 1 deletion src/ctlib/unittests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ AM_LDFLAGS = -no-fast-install
else
AM_LDFLAGS = -no-install -L../.libs -R "$(abs_builddir)/../.libs"
endif
LDADD = libcommon.a ../libct.la ../../replacements/libreplacements.la $(LTLIBICONV)
LDADD = libcommon.a ../../utils/unittests/libtds_test_base.a \
../libct.la ../../replacements/libreplacements.la \
$(LTLIBICONV)
CLEANFILES = tdsdump.out
EXTRA_DIST = CMakeLists.txt
3 changes: 1 addition & 2 deletions src/ctlib/unittests/all_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ static void test_type(TDSSOCKET *tds TDS_UNUSED, TDSCOLUMN *col)
tds_free_results(bindinfo);
}

int
main(void)
TEST_MAIN()
{
TDSCONTEXT *tds_ctx;
TDSSOCKET *tds;
Expand Down
3 changes: 1 addition & 2 deletions src/ctlib/unittests/array_bind.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "common.h"

/* Testing: array binding of result set */
int
main(void)
TEST_MAIN()
{
CS_CONTEXT *ctx;
CS_CONNECTION *conn;
Expand Down
3 changes: 1 addition & 2 deletions src/ctlib/unittests/blk_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ do_bind(CS_BLKDESC * blkdesc, int colnum, CS_INT host_format, CS_INT host_type,
}


int
main(void)
TEST_MAIN()
{
CS_CONTEXT *ctx;
CS_CONNECTION *conn;
Expand Down
3 changes: 1 addition & 2 deletions src/ctlib/unittests/blk_in2.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ hoge_blkin(CS_CONNECTION * con, CS_BLKDESC * blk, char *table, char *data)
check_call(blk_done, (blk, CS_BLK_ALL, &row));
}

int
main(void)
TEST_MAIN()
{
CS_CONTEXT *ctx;
CS_CONNECTION *conn;
Expand Down
3 changes: 1 addition & 2 deletions src/ctlib/unittests/blk_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include <bkpublic.h>

/* Testing: array binding of result set */
int
main(void)
TEST_MAIN()
{
CS_CONTEXT *ctx;
CS_CONNECTION *conn;
Expand Down
6 changes: 2 additions & 4 deletions src/ctlib/unittests/cancel.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ catch_alrm(int sig_num TDS_UNUSED)
}

/* Testing: Test asynchronous ct_cancel() */
int
main(void)
TEST_MAIN()
{
CS_CONTEXT *ctx;
CS_CONNECTION *conn;
Expand Down Expand Up @@ -190,8 +189,7 @@ do_fetch(CS_COMMAND * cmd, int *cnt)

#else

int
main(void)
TEST_MAIN()
{
return 0;
}
Expand Down
Loading