2009-07-16 17:39:16 +00:00
|
|
|
/*
|
|
|
|
* Check: a unit test framework for C
|
|
|
|
* Copyright (C) 2001, 2002 Arien Malec
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
2016-12-09 09:48:11 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2009-07-16 17:39:16 +00:00
|
|
|
*/
|
|
|
|
|
2016-12-09 09:48:11 +00:00
|
|
|
#include "libcompat/libcompat.h"
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-12-14 11:54:32 +00:00
|
|
|
#include "internal-check.h"
|
2009-07-16 17:39:16 +00:00
|
|
|
#include "check_list.h"
|
|
|
|
#include "check_impl.h"
|
|
|
|
#include "check_str.h"
|
|
|
|
#include "check_print.h"
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void srunner_fprint_summary (FILE * file, SRunner * sr,
|
|
|
|
enum print_output print_mode);
|
|
|
|
static void srunner_fprint_results (FILE * file, SRunner * sr,
|
|
|
|
enum print_output print_mode);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
|
|
|
srunner_print (SRunner * sr, enum print_output print_mode)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
srunner_fprint (stdout, sr, print_mode);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
|
|
|
srunner_fprint (FILE * file, SRunner * sr, enum print_output print_mode)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (print_mode == CK_ENV) {
|
2009-10-12 11:49:35 +00:00
|
|
|
print_mode = get_env_printmode ();
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
srunner_fprint_summary (file, sr, print_mode);
|
|
|
|
srunner_fprint_results (file, sr, print_mode);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
srunner_fprint_summary (FILE * file, SRunner * sr, enum print_output print_mode)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-12-17 20:09:48 +00:00
|
|
|
#if ENABLE_SUBUNIT
|
|
|
|
if (print_mode == CK_SUBUNIT)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
if (print_mode >= CK_MINIMAL) {
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = sr_stat_str (sr);
|
|
|
|
fprintf (file, "%s\n", str);
|
2009-10-12 11:49:35 +00:00
|
|
|
free (str);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
srunner_fprint_results (FILE * file, SRunner * sr, enum print_output print_mode)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
List *resultlst;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2009-12-17 20:09:48 +00:00
|
|
|
#if ENABLE_SUBUNIT
|
|
|
|
if (print_mode == CK_SUBUNIT)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
resultlst = sr->resultlst;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
for (check_list_front (resultlst); !check_list_at_end (resultlst);
|
|
|
|
check_list_advance (resultlst)) {
|
|
|
|
TestResult *tr = (TestResult *) check_list_val (resultlst);
|
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
tr_fprint (file, tr, print_mode);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2013-07-23 19:39:53 +00:00
|
|
|
fprint_xml_esc (FILE * file, const char *str)
|
|
|
|
{
|
2016-12-09 09:48:11 +00:00
|
|
|
/* The valid XML characters are as follows:
|
|
|
|
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
|
|
|
* Characters that are outside of ASCII must be encoded. Further, the
|
|
|
|
* following special characters:
|
|
|
|
* " ' < > &
|
|
|
|
* must be encoded. We assume that the incoming string may be a multibyte
|
|
|
|
* character.
|
|
|
|
*/
|
2013-07-23 19:39:53 +00:00
|
|
|
|
2016-12-09 09:48:11 +00:00
|
|
|
for (; *str != '\0'; str++) {
|
|
|
|
char next = *str;
|
|
|
|
|
|
|
|
/* handle special characters that must be escaped */
|
|
|
|
if (next == '"' || next == '\'' || next == '<' || next == '>'
|
|
|
|
|| next == '&') {
|
|
|
|
switch (next) {
|
|
|
|
case '"':
|
|
|
|
fputs (""", file);
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
fputs ("'", file);
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
fputs ("<", file);
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
fputs (">", file);
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
fputs ("&", file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* printable ASCII */
|
|
|
|
else if (next >= ' ' && next <= '~') {
|
|
|
|
fputc (next, file);
|
|
|
|
}
|
|
|
|
/* Non-printable character */
|
|
|
|
else if (next == 0x9 || next == 0xA || next == 0xD || next >= 0x20) {
|
|
|
|
fprintf (file, "&#x%X;", next);
|
2013-07-23 19:39:53 +00:00
|
|
|
}
|
2016-12-09 09:48:11 +00:00
|
|
|
/* If it did not get printed, it is not a valid XML character */
|
2013-07-23 19:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
void
|
|
|
|
tr_fprint (FILE * file, TestResult * tr, enum print_output print_mode)
|
|
|
|
{
|
|
|
|
if (print_mode == CK_ENV) {
|
|
|
|
print_mode = get_env_printmode ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((print_mode >= CK_VERBOSE && tr->rtype == CK_PASS) ||
|
|
|
|
(tr->rtype != CK_PASS && print_mode >= CK_NORMAL)) {
|
|
|
|
char *trstr = tr_str (tr);
|
|
|
|
|
|
|
|
fprintf (file, "%s\n", trstr);
|
|
|
|
free (trstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2009-12-17 20:09:48 +00:00
|
|
|
tr_xmlprint (FILE * file, TestResult * tr,
|
|
|
|
enum print_output print_mode CK_ATTRIBUTE_UNUSED)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
char result[10];
|
2014-11-15 11:53:32 +00:00
|
|
|
char *path_name = NULL;
|
|
|
|
char *file_name = NULL;
|
|
|
|
char *slash = NULL;
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
switch (tr->rtype) {
|
2009-10-12 11:49:35 +00:00
|
|
|
case CK_PASS:
|
2014-11-15 11:53:32 +00:00
|
|
|
snprintf (result, sizeof (result), "%s", "success");
|
2009-10-12 11:49:35 +00:00
|
|
|
break;
|
|
|
|
case CK_FAILURE:
|
2014-11-15 11:53:32 +00:00
|
|
|
snprintf (result, sizeof (result), "%s", "failure");
|
2009-10-12 11:49:35 +00:00
|
|
|
break;
|
|
|
|
case CK_ERROR:
|
2014-11-15 11:53:32 +00:00
|
|
|
snprintf (result, sizeof (result), "%s", "error");
|
2009-10-12 11:49:35 +00:00
|
|
|
break;
|
|
|
|
case CK_TEST_RESULT_INVALID:
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
break;
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
if (tr->file) {
|
|
|
|
slash = strrchr (tr->file, '/');
|
|
|
|
if (slash == NULL) {
|
|
|
|
slash = strrchr (tr->file, '\\');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slash == NULL) {
|
|
|
|
path_name = strdup (".");
|
|
|
|
file_name = tr->file;
|
|
|
|
} else {
|
|
|
|
path_name = strdup (tr->file);
|
|
|
|
path_name[slash - tr->file] = 0; /* Terminate the temporary string. */
|
|
|
|
file_name = slash + 1;
|
|
|
|
}
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
fprintf (file, " <test result=\"%s\">\n", result);
|
2014-11-15 11:53:32 +00:00
|
|
|
fprintf (file, " <path>%s</path>\n",
|
|
|
|
(path_name == NULL ? "" : path_name));
|
|
|
|
fprintf (file, " <fn>%s:%d</fn>\n",
|
|
|
|
(file_name == NULL ? "" : file_name), tr->line);
|
2009-10-12 11:49:35 +00:00
|
|
|
fprintf (file, " <id>%s</id>\n", tr->tname);
|
|
|
|
fprintf (file, " <iteration>%d</iteration>\n", tr->iter);
|
2014-11-15 11:53:32 +00:00
|
|
|
fprintf (file, " <duration>%d.%06d</duration>\n",
|
|
|
|
tr->duration < 0 ? -1 : tr->duration / US_PER_SEC,
|
|
|
|
tr->duration < 0 ? 0 : tr->duration % US_PER_SEC);
|
2013-07-23 19:39:53 +00:00
|
|
|
fprintf (file, " <description>");
|
|
|
|
fprint_xml_esc (file, tr->tcname);
|
|
|
|
fprintf (file, "</description>\n");
|
|
|
|
fprintf (file, " <message>");
|
|
|
|
fprint_xml_esc (file, tr->msg);
|
|
|
|
fprintf (file, "</message>\n");
|
2009-10-12 11:49:35 +00:00
|
|
|
fprintf (file, " </test>\n");
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
free (path_name);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
enum print_output
|
|
|
|
get_env_printmode (void)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
char *env = getenv ("CK_VERBOSITY");
|
2014-11-15 11:53:32 +00:00
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
if (env == NULL)
|
|
|
|
return CK_NORMAL;
|
|
|
|
if (strcmp (env, "silent") == 0)
|
|
|
|
return CK_SILENT;
|
|
|
|
if (strcmp (env, "minimal") == 0)
|
|
|
|
return CK_MINIMAL;
|
|
|
|
if (strcmp (env, "verbose") == 0)
|
|
|
|
return CK_VERBOSE;
|
|
|
|
return CK_NORMAL;
|
|
|
|
}
|