Commit b631e501 authored by Slava Monich's avatar Slava Monich

[mms-lib] Added log option parsing test

parent 36aec507
......@@ -91,7 +91,7 @@ RELEASE_DEFS =
WARNINGS = -Wall
DEFINES = $(RESIZE_DEFINES)
INCLUDES = -I$(SRC_DIR) -I$(INCLUDE_DIR)
CFLAGS += -fPIC $(WARNINGS) $(DEFINES) $(INCLUDES) $(RESIZE_CFLAGS) \
CFLAGS += -fPIC $(WARNINGS) $(INCLUDES) $(RESIZE_CFLAGS) \
$(shell pkg-config --cflags $(PKGS)) -MMD
ifndef KEEP_SYMBOLS
......@@ -102,8 +102,12 @@ ifneq ($(KEEP_SYMBOLS),0)
RELEASE_FLAGS += -g
endif
DEBUG_CFLAGS = $(DEBUG_FLAGS) $(DEBUG_DEFS) $(CFLAGS)
RELEASE_CFLAGS = $(RELEASE_FLAGS) $(RELEASE_DEFS) $(CFLAGS)
ifneq ($(MMS_LOG_SYSLOG),)
DEFINES += -DMMS_LOG_SYSLOG=$(MMS_LOG_SYSLOG)
endif
DEBUG_CFLAGS = $(DEFINES) $(DEBUG_FLAGS) $(DEBUG_DEFS) $(CFLAGS)
RELEASE_CFLAGS = $(DEFINES) $(RELEASE_FLAGS) $(RELEASE_DEFS) $(CFLAGS)
DEBUG_CPPFLAGS = -Wno-psabi $(RESIZE_CPPFLAGS) $(DEBUG_CFLAGS)
RELEASE_CPPFLAGS = -Wno-psabi $(RESIZE_CPPFLAGS) $(RELEASE_CFLAGS)
......
......@@ -103,11 +103,18 @@ mms_logv(
const char* format,
va_list va);
#ifndef MMS_LOG_SYSLOG
# ifdef unix
# define MMS_LOG_SYSLOG 1
# else
# define MMS_LOG_SYSLOG 0
# endif
#endif /* MMS_LOG_SYSLOG */
extern const char MMS_LOG_TYPE_STDOUT[];
extern const char MMS_LOG_TYPE_GLIB[];
extern const char MMS_LOG_TYPE_CUSTOM[];
#ifdef unix
# define MMS_LOG_SYSLOG
#if MMS_LOG_SYSLOG
extern const char MMS_LOG_TYPE_SYSLOG[];
#endif
......@@ -116,7 +123,7 @@ extern const char MMS_LOG_TYPE_SYSLOG[];
const char* format, va_list va)
MMS_DEFINE_LOG_FN(mms_log_stdout);
MMS_DEFINE_LOG_FN(mms_log_glib);
#ifdef MMS_LOG_SYSLOG
#if MMS_LOG_SYSLOG
MMS_DEFINE_LOG_FN(mms_log_syslog);
#endif
......
......@@ -53,7 +53,7 @@ static const struct _mms_log_level {
const char MMS_LOG_TYPE_STDOUT[] = "stdout";
const char MMS_LOG_TYPE_GLIB[] = "glib";
const char MMS_LOG_TYPE_CUSTOM[] = "custom";
#ifdef MMS_LOG_SYSLOG
#if MMS_LOG_SYSLOG
const char MMS_LOG_TYPE_SYSLOG[] = "syslog";
#endif
......@@ -104,7 +104,7 @@ mms_log_stdout(
}
/* Formards output to syslog */
#ifdef MMS_LOG_SYSLOG
#if MMS_LOG_SYSLOG
#include <syslog.h>
void
mms_log_syslog(
......@@ -324,7 +324,7 @@ mms_log_set_type(
const char* type,
const char* default_name)
{
#ifdef MMS_LOG_SYSLOG
#if MMS_LOG_SYSLOG
if (!strcasecmp(type, MMS_LOG_TYPE_SYSLOG)) {
if (mms_log_func != mms_log_syslog) {
openlog(NULL, LOG_PID | LOG_CONS, LOG_USER);
......@@ -352,7 +352,7 @@ const char*
mms_log_get_type()
{
return (mms_log_func == mms_log_stdout) ? MMS_LOG_TYPE_STDOUT :
#ifdef MMS_LOG_SYSLOG
#if MMS_LOG_SYSLOG
(mms_log_func == mms_log_syslog) ? MMS_LOG_TYPE_SYSLOG :
#endif /* MMS_LOG_SYSLOG */
(mms_log_func == mms_log_glib) ? MMS_LOG_TYPE_STDOUT :
......
......@@ -65,7 +65,8 @@ MMS_LIB_DIR = ../..
MMS_LIB_BUILD_DIR = $(MMS_LIB_DIR)/build
MMS_LIB_DEBUG_LIB = $(MMS_LIB_BUILD_DIR)/debug/$(MMS_LIB_LIB)
MMS_LIB_RELEASE_LIB = $(MMS_LIB_BUILD_DIR)/release/$(MMS_LIB_LIB)
MMS_LIB_MAKE = $(MAKE) --no-print-directory -C $(MMS_LIB_DIR) GCOV=$(GCOV)
MMS_LIB_MAKE = $(MAKE) --no-print-directory -C $(MMS_LIB_DIR) \
GCOV=$(GCOV) MMS_LOG_SYSLOG=0
#
# Tools and flags
......@@ -77,13 +78,12 @@ DEBUG_FLAGS = $(BASE_FLAGS) -g
RELEASE_FLAGS = $(BASE_FLAGS) -O2
DEBUG_DEFS = -DDEBUG
RELEASE_DEFS =
WARNINGS = -Wall
LIBS = $(shell pkg-config --libs $(LIB_PKGS)) -lmagic -ljpeg $(RESIZE_LIBS)
CFLAGS = -fPIE $(shell pkg-config --cflags $(PKGS)) -I$(MMS_LIB_DIR)/include \
-I$(MMS_LIB_DIR)/src -I$(COMMON_DIR) -MMD
CFLAGS = -Wall -fPIE $(shell pkg-config --cflags $(PKGS)) \
-I$(MMS_LIB_DIR)/include -I$(MMS_LIB_DIR)/src -I$(COMMON_DIR) -MMD
DEBUG_CFLAGS = $(WARNINGS) $(DEBUG_FLAGS) $(DEBUG_DEFS) $(CFLAGS)
RELEASE_CFLAGS = $(WARNINGS) $(RELEASE_FLAGS) $(RELEASE_DEFS) $(CFLAGS)
DEBUG_CFLAGS = $(DEBUG_FLAGS) $(DEBUG_DEFS) $(CFLAGS)
RELEASE_CFLAGS = $(RELEASE_FLAGS) $(RELEASE_DEFS) $(CFLAGS)
DEBUG_LIBS = $(MMS_LIB_DEBUG_LIB) $(LIBS)
RELEASE_LIBS = $(MMS_LIB_RELEASE_LIB) $(LIBS)
......
......@@ -3,7 +3,7 @@
# This script requires lcov to be installed
#
TESTS="media_type mms_codec delivery_ind read_ind read_report resize retrieve retrieve_cancel retrieve_no_proxy send"
TESTS="media_type mms_codec mms_log delivery_ind read_ind read_report resize retrieve retrieve_cancel retrieve_no_proxy send"
FLAVOR="release"
pushd `dirname $0` > /dev/null
......
# -*- Mode: makefile-gmake -*-
EXE = test_mms_log
SRC = $(EXE).c
include ../common/Makefile
/*
* Copyright (C) 2014 Jolla Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
*/
#include "mms_log.h"
#define RET_OK (0)
#define RET_ERR (1)
typedef struct test_desc {
const char* name;
MMSLogModule** mods;
int nmods;
const int* results; /* nmods elements */
int default_level;
char** opts;
int nopts;
gboolean test_success;
} TestDesc;
const MMS_LOG_MODULE_DEFINE_(log_defaults,NULL);
/* Test1: default level + 2 modules */
MMS_LOG_MODULE_DEFINE_(mod1a, "test-a");
MMS_LOG_MODULE_DEFINE_(mod1b, "test-b");
static MMSLogModule* mods1[] = { &mod1a, &mod1b };
static const int results1[] = { MMS_LOGLEVEL_DEBUG, MMS_LOGLEVEL_INFO };
static const char* opts1[] = { "verbose", "test-a:debug", "test-b:info" };
G_STATIC_ASSERT(G_N_ELEMENTS(mods1) == G_N_ELEMENTS(results1));
/* Test2: wrong module name */
MMS_LOG_MODULE_DEFINE_(mod2, "test-a");
static MMSLogModule* mods2[] = { &mod2 };
static const int results2[] = { MMS_LOGLEVEL_GLOBAL };
static const char* opts2[] = { "test-b:debug" };
/* Test3: one option overriding another */
MMS_LOG_MODULE_DEFINE_(mod3, "test");
static MMSLogModule* mods3[] = { &mod3 };
static const int results3[] = { MMS_LOGLEVEL_DEBUG };
static const char* opts3[] = { "test:verbose", "test:debug" };
G_STATIC_ASSERT(G_N_ELEMENTS(mods3) == G_N_ELEMENTS(results3));
/* Test4: log levels as numbers */
MMS_LOG_MODULE_DEFINE_(mod4a, "a");
MMS_LOG_MODULE_DEFINE_(mod4b, "b");
MMS_LOG_MODULE_DEFINE_(mod4c, "c");
MMS_LOG_MODULE_DEFINE_(mod4d, "d");
MMS_LOG_MODULE_DEFINE_(mod4e, "e");
MMS_LOG_MODULE_DEFINE_(mod4f, "f");
static MMSLogModule* mods4[] = { &mod4a,&mod4b,&mod4c,&mod4d,&mod4e,&mod4f };
static const int results4[] = { MMS_LOGLEVEL_NONE, MMS_LOGLEVEL_ERR,
MMS_LOGLEVEL_WARN, MMS_LOGLEVEL_INFO, MMS_LOGLEVEL_DEBUG,
MMS_LOGLEVEL_VERBOSE };
static const char* opts4[] = { "a:0", "b:1", "c:2", "d:3", "e:4", "f:5" };
G_STATIC_ASSERT(G_N_ELEMENTS(mods4) == G_N_ELEMENTS(results4));
/* Test5: invalid number */
MMS_LOG_MODULE_DEFINE_(mod5, "test");
static MMSLogModule* mods5[] = { &mod5 };
static const int results5[] = { MMS_LOGLEVEL_GLOBAL };
static const char* opts5[] = { "test-b:66" };
#define ARRAY(a) (a), G_N_ELEMENTS(a)
static const TestDesc log_tests[] = {
{ "Test1", ARRAY(mods1), results1, MMS_LOGLEVEL_VERBOSE,
(char**)ARRAY(opts1), TRUE },
{ "Test2", ARRAY(mods2), results2, MMS_LOGLEVEL_DEFAULT,
(char**)ARRAY(opts2), FALSE },
{ "Test3", ARRAY(mods3), results3, MMS_LOGLEVEL_DEFAULT,
(char**)ARRAY(opts3), TRUE },
{ "Test4", ARRAY(mods4), results4, MMS_LOGLEVEL_DEFAULT,
(char**)ARRAY(opts4), TRUE },
{ "Test5", ARRAY(mods5), results5, MMS_LOGLEVEL_DEFAULT,
(char**)ARRAY(opts5), FALSE },
};
static
gboolean
run_test(
const TestDesc* test)
{
int i;
gboolean ok = FALSE;
const int prev_default_level = mms_log_default.level;
int test_default_level;
mms_log_default.level = MMS_LOGLEVEL_DEFAULT;
for (i=0; i<test->nmods; i++) {
test->mods[i]->level = MMS_LOGLEVEL_GLOBAL;
}
for (i=0; i<test->nopts; i++) {
const char* opt = test->opts[i];
GError* error = NULL;
if (!mms_log_parse_option(opt, test->mods, test->nmods, &error)) {
g_error_free(error);
break;
}
}
test_default_level = mms_log_default.level;
mms_log_default.level = prev_default_level;
if (test_default_level == test->default_level && i == test->nopts) {
char* desc = mms_log_description(test->mods, test->nmods);
MMS_DEBUG("%s", desc);
g_free(desc);
ok = TRUE;
for (i=0; i<test->nmods; i++) {
const int expect = test->results[i];
const MMSLogModule* mod = test->mods[i];
if (mod->level != expect) {
ok = FALSE;
if (test->test_success) {
MMS_ERR("%s mismatch: %d vs %d", mod->name,
mod->level, expect);
}
}
}
if (ok && !test->test_success) {
MMS_ERR("%s is expected to fail", test->name);
ok = FALSE;
}
} else if (!test->test_success) {
/* Test is expected to fail */
ok = TRUE;
} else if (test_default_level != test->default_level) {
MMS_ERR("%s default log level mismatch: %d vs %d", test->name,
test_default_level, test->default_level);
} else {
MMS_ERR("%s error parsing %s", test->name, test->opts[i]);
}
MMS_INFO("%s: %s", ok ? "OK" : "FAILED", test->name);
return ok;
}
static
gboolean
run_named_test(
const char* name,
const TestDesc* tests,
int count)
{
int i;
for (i=0; i<count; i++) {
if (!strcmp(tests[i].name, name)) {
return run_test(tests + i);
}
}
MMS_ERR("Unknown test: %s", name);
return FALSE;
}
static
int
run_tests(
const TestDesc* tests,
int count)
{
int i, ret = RET_OK;
for (i=0; i<count; i++) {
if (!run_test(tests + i)) {
ret = RET_ERR;
}
}
return ret;
}
int main(int argc, char* argv[])
{
int ret = RET_OK;
mms_log_set_type(MMS_LOG_TYPE_STDOUT, "test_mms_log");
mms_log_stdout_timestamp = FALSE;
mms_log_default.level = MMS_LOGLEVEL_INFO;
if (argc == 1) {
MMS_DEBUG("log type %s", mms_log_get_type());
ret = run_tests(log_tests, G_N_ELEMENTS(log_tests));
} else {
int i;
for (i=1; i<argc; i++) {
if (!run_named_test(argv[i], log_tests, G_N_ELEMENTS(log_tests))) {
ret = RET_ERR;
}
}
}
return ret;
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
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