Commit 2acf1eb5 authored by Michael Meyling's avatar Michael Meyling

[trackrdrd] test_util has now functions for checking stdout and stderr....

[trackrdrd] test_util has now functions for checking stdout and stderr. Already used in test_config.c
parent c5118848
......@@ -29,5 +29,31 @@
*
*/
/**
* \file config_common.h
* \brief Config handling interface
* \details Common functions to get configuration data
*
* This header defines the functions the plugin uses to get its
* configuration data.
*/
/**
* Function type for adding key value pairs to config data.
*
* @param lval key
* @param rval value
* @return 0 on success, an error value on failure
*/
typedef int conf_add_f(const char *lval, const char *rval);
/**
* Set config values.
*
* @param file config path we are reading
* @param func pointer for config add function.
* @return 0 on success, an value less than zero if we had a problem
* reading from one file, parsing a line or adding a config value.
*/
int CONF_ReadFile(const char *file, conf_add_f *func);
......@@ -47,6 +47,7 @@
#define DEFAULT_USER "nobody"
#define DEFAULT_PID_FILE "/var/run/trackrdrd.pid"
#define DEFAULT_RESTART_PAUSE 1
#define DEFAULT_HASH_MAX_PROBES 10
#define confdump(str,val) \
i += sprintf(verbose_buffer + i, str"\n", (val))
......@@ -100,7 +101,7 @@ saveConfig(const char * fname)
fp = fopen( fname, "w" );
if ( fp == NULL )
{
printf("Cannot open %s for writing ", fname );
perror(fname);
return 4;
}
content = getConfigContent();
......@@ -124,10 +125,15 @@ static char
CONF_Init();
VMASSERT(!strcmp(DEFAULT_USER, config.user_name),
"Default user name expected: \"%s\", but found: \"%s\"", DEFAULT_USER, config.user_name);
"Default user name expected: \"%s\", but found: \"%s\"",
DEFAULT_USER, config.user_name);
VMASSERT(!strcmp(DEFAULT_PID_FILE, config.pid_file),
"Default pid file name expected: \"%s\", but found: \"%s\"", DEFAULT_PID_FILE, config.user_name);
"Default pid file name expected: \"%s\", but found: \"%s\"",
DEFAULT_PID_FILE, config.user_name);
MASSERT(DEFAULT_RESTART_PAUSE == config.restart_pause);
VMASSERT(DEFAULT_HASH_MAX_PROBES == config.hash_max_probes,
"Default hash_max_probes expected: \"%i\", but found: \"%i\"",
DEFAULT_HASH_MAX_PROBES, config.hash_max_probes);
return NULL;
}
......@@ -147,7 +153,7 @@ static char
strcpy(confNameNew, confName);
strcat(confNameNew, ".new");
saveConfig(confNameNew);
VMASSERT(TEST_compareFiles(confName, confNameNew),
VMASSERT(!TEST_compareFiles(confName, confNameNew),
"Files are not equal: \"%s\" and \"%s\"", confName, confNameNew);
// CONF_Dump();
......@@ -170,12 +176,37 @@ static const char
return NULL;
}
static char
*readAndFindError(const char * confName)
{
int err;
CONF_Init();
TEST_catchStderrStart();
err = CONF_ReadFile(confName, CONF_Add);
TEST_catchStderrEnd();
VMASSERT(err != 0, "No error code during reading config \"%s\": %i", confName, err);
err = TEST_stderrEquals("Error in trackrdrd_010.conf line 16 (Invalid argument): "\
"'unknown.module = /my/path/module.so'\n");
VMASSERT(err == 0, "stderr output other than expected: %i", err);
return NULL;
}
static const char
*test_CONF_ReadFile(void)
{
printf("... testing CONF_ReadFile\n");
returnIfNotNull(readAndFindError("trackrdrd_010.conf"));
return NULL;
}
static const char *
all_tests(void)
{
mu_run_test(test_CONF_Init);
mu_run_test(test_CONF_ReadDefault);
mu_run_test(test_CONF_ReadFile);
return NULL;
}
......
......@@ -37,29 +37,38 @@
/***** includes ***************************************************************/
#include "test_utils.h"
#include <unistd.h>
#include <fcntl.h>
/***** constants **************************************************************/
const char * FILE_NAME_STDOUT = "stdout.txt";
const char * FILE_NAME_STDERR = "stderr.txt";
/***** functions **************************************************************/
int TEST_compareFiles(const char * fname1, const char * fname2)
int
TEST_compareFiles(const char * fname1, const char * fname2)
{
FILE *fp1, *fp2;
int ch1, ch2;
int line = 1;
int col = 1;
verbose("comparing files %s : %s", fname1, fname2);
fp1 = fopen( fname1, "r" );
if ( fp1 == NULL )
{
printf("Cannot open %s for reading ", fname1 );
perror(fname1);
return -2;
}
fp2 = fopen( fname2, "r" ) ;
if (fp2 == NULL)
{
printf("Cannot open %s for reading ", fname2 );
fclose ( fp1 );
perror(fname2);
return -3;
}
do
......@@ -67,7 +76,7 @@ int TEST_compareFiles(const char * fname1, const char * fname2)
col++;
ch1 = getc( fp1 );
ch2 = getc( fp2 );
if ( ch1 == '\n' )
if ( ch1 == '\n' && ch2 == '\n')
{
line++;
col = 0;
......@@ -77,7 +86,118 @@ int TEST_compareFiles(const char * fname1, const char * fname2)
fclose ( fp2 );
if ( ch1 != ch2 ) {
printf(" files differ at line: %i col: %i \n", line, col);
return line;
} else {
return 0;
}
return ch1 == ch2;
}
int
TEST_compareFileWithString(const char * fname, const char * text)
{
FILE *fp1;
int ch1, ch2;
int line = 1;
int col = 1;
int i = 0;
verbose("comparing file %s with text", fname);
fp1 = fopen( fname, "r" );
if ( fp1 == NULL )
{
perror(fname);
return -2;
}
do
{
col++;
ch1 = getc( fp1 );
ch2 = *(text + i++);
if ( ch1 == '\n' && ch2 == '\n')
{
line++;
col = 0;
}
} while ((ch1 != EOF) && (ch2 != '\0') && (ch1 == ch2));
fclose ( fp1 );
if ( ch1 != EOF || ch2 != '\0' ) {
printf(" file differs at line: %i col: %i \n", line, col);
return line;
} else {
return 0;
}
}
int stdoutBak;
int stdoutNew;
fpos_t stdoutPos;
int
TEST_catchStdoutStart()
{
fflush(stdout);
fgetpos(stdout, &stdoutPos);
stdoutBak = dup(fileno(stdout));
stdoutNew = open(FILE_NAME_STDOUT, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | \
S_IROTH | S_IWOTH);
dup2(stdoutNew, fileno(stdout));
close(stdoutNew);
return(0);
}
int
TEST_catchStdoutEnd()
{
fflush(stdout);
dup2(stdoutBak, fileno(stdout));
close(stdoutBak);
clearerr(stdout);
fsetpos(stdout, &stdoutPos);
return(0);
}
int stderrBak, stderrNew;
fpos_t stderrPos;
int
TEST_catchStderrStart()
{
fflush(stderr);
fgetpos(stdout, &stderrPos);
stderrBak = dup(fileno(stderr));
stderrNew = open(FILE_NAME_STDERR, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | \
S_IROTH | S_IWOTH);
if (stderrNew < 0) {
perror(NULL);
return(-1);
}
dup2(stderrNew, fileno(stderr));
close(stderrNew);
return(0);
}
int
TEST_catchStderrEnd()
{
fflush(stderr);
dup2(stderrBak, fileno(stderr));
close(stderrBak);
clearerr(stderr);
fsetpos(stderr, &stderrPos);
return(0);
}
int
TEST_stdoutEquals(const char * text)
{
return TEST_compareFileWithString(FILE_NAME_STDOUT, text);
}
int
TEST_stderrEquals(const char * text)
{
return TEST_compareFileWithString(FILE_NAME_STDERR, text);
}
......@@ -47,18 +47,30 @@
/** debugging output */
#define VERBOSE 1
#undef VERBOSE
#ifdef VERBOSE
#define verbose(fmt, ...) \
do { printf("%8s:%4d:%20s:>>> "fmt"\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); } while (0)
do { printf("%8s:%4d:%20s:>>> "fmt"\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); } \
while (0)
#else
#define verbose(fmt, ...) do{ } while ( 0 )
#endif
#define returnIfNotNull(test) do { const char *msg = test; if (msg) return msg; } while (0)
/***** variables **************************************************************/
extern long global_line_pos; /* actual position in input line */
/***** constants **************************************************************/
/** file name for saving stdout */
// #define FILE_NAME_STDOUT = "stdout.txt";
extern const char * FILE_NAME_STDOUT;
/** file name for saving stderr */
//#define FILE_NAME_STDERR = "stderr.txt";
extern const char * FILE_NAME_STDERR;
/***** variables **************************************************************/
/***** structures *************************************************************/
......@@ -66,7 +78,66 @@ extern long global_line_pos; /* actual position in input line */
/***** functions **************************************************************/
/**
* Redirect stdout into new file FILE_NAME_STDOUT
*/
extern int TEST_catchStdoutStart(void);
/**
* Reset redirection of stdout and close resulting file FILE_NAME_STDOUT
*/
extern int TEST_catchStdoutEnd(void);
/**
* Redirect stderr into new file FILE_NAME_STDERR
*/
extern int TEST_catchStderrStart(void);
/**
* Reset redirection of stdout and close resulting file FILE_NAME_STDERR
*/
extern int TEST_catchStderrEnd(void);
/**
* Test if files have same content. If the comparison is not successful some info
* is written to stderr.
*
* @param fname1 first file.
* @param fname2 second file.
* @return 0 on success, a value < 0 if we had problems reading the files and a
* line number (starting with 1) if there was a difference in that line.
*/
extern int TEST_compareFiles(const char * fname1, const char * fname2);
/**
* Test if file contents equals given text.
*
* @param fname file name.
* @param text compare file contents with this text.
* @return 0 on success, a value < 0 if we had problems reading the file and a
* line number (starting with 1) if there was a difference in that line.
*/
extern int TEST_compareFileWithString(const char * fname, const char * text);
/**
* Test if previously saved stdout equals given text. See TEST_catchStdoutStart() and
* TEST_catchStdoutEnd().
*
* @param text compare stdout with this text.
* @return 0 on success, a value < 0 if we had problems reading the stdout file and a
* line number (starting with 1) if there was a difference in that line.
*/
extern int TEST_stdoutEquals(const char * text);
/**
* Test if previously saved stderr equals given text. See TEST_catchStderrStart() and
* TEST_catchStderrEnd().
*
* @param text compare stderr with this text.
* @return 0 on success, a value < 0 if we had problems reading the stderr file and a
* line number (starting with 1) if there was a difference in that line.
*/
extern int TEST_stderrEquals(const char * text);
#endif /* _TEST_UTILS_H */
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