Commit 54934aae authored by Slava Monich's avatar Slava Monich

[connman] Added tests for new counters and history. JB#36051, JB#43750

parent ddfd9fa0
......@@ -75,6 +75,9 @@ unit/test-sailfish_wakeup_timer
unit/test-service
unit/test-vpn-settings
unit/test-sailfish_iptables_ext
unit/test-sailfish_datacounter
unit/test-sailfish_datacounters
unit/test-sailfish_datahistory
unit/test-firewall
unit/test-globalproxy
unit/test-iptables
......
......@@ -412,6 +412,66 @@ noinst_PROGRAMS += unit/test-sailfish_wakeup_timer
TESTS += unit/test-sailfish_wakeup_timer
endif
if SAILFISH_COUNTERS
unit_test_sailfish_datacounter_CPPFLAGS = -I$(builddir)/src \
@GLIBUTIL_CFLAGS@ $(AM_CPPFLAGS)
unit_test_sailfish_datacounter_CFLAGS = $(COVERAGE_OPT) $(AM_CFLAGS) \
-DSTATS_SHORT_WRITE_PERIOD_SEC=1 \
-DSTATS_LONG_WRITE_PERIOD_SEC=3 \
-Ddatacounters_time_now=test_time_now \
-Dgutil_time_notify_new=test_time_notify_new \
-Dgutil_time_notify_ref=test_time_notify_ref \
-Dgutil_time_notify_unref=test_time_notify_unref \
-Dgutil_time_notify_add_handler=test_time_notify_add_handler \
-Dgutil_time_notify_remove_handler=test_time_notify_remove_handler
unit_test_sailfish_datacounter_LDADD = @GLIBUTIL_LIBS@ @GLIB_LIBS@ -ldl
unit_test_sailfish_datacounter_SOURCES = unit/test-sailfish_datacounter.c \
unit/test_timenotify.c src/sailfish_datacounter.c \
src/sailfish_datacounters_util.c src/inotify.c src/storage.c \
src/wakeup_timer.c src/log.c $(backtrace_sources)
noinst_PROGRAMS += unit/test-sailfish_datacounter
TESTS += unit/test-sailfish_datacounter
unit_test_sailfish_datacounters_CPPFLAGS = -I$(builddir)/src \
@GLIBUTIL_CFLAGS@ $(AM_CPPFLAGS)
unit_test_sailfish_datacounters_CFLAGS = $(COVERAGE_OPT) $(AM_CFLAGS) \
-Ddatacounters_time_now=test_time_now \
-Dgutil_time_notify_new=test_time_notify_new \
-Dgutil_time_notify_ref=test_time_notify_ref \
-Dgutil_time_notify_unref=test_time_notify_unref \
-Dgutil_time_notify_add_handler=test_time_notify_add_handler \
-Dgutil_time_notify_remove_handler=test_time_notify_remove_handler
unit_test_sailfish_datacounters_LDADD = @GLIBUTIL_LIBS@ @GLIB_LIBS@ -ldl
unit_test_sailfish_datacounters_SOURCES = unit/test-sailfish_datacounters.c \
unit/test_timenotify.c src/sailfish_datacounters.c \
src/sailfish_datacounter.c src/sailfish_datacounters_util.c \
src/inotify.c src/storage.c src/wakeup_timer.c src/log.c \
$(backtrace_sources)
noinst_PROGRAMS += unit/test-sailfish_datacounters
TESTS += unit/test-sailfish_datacounters
unit_test_sailfish_datahistory_CPPFLAGS = -I$(builddir)/src \
@GLIBUTIL_CFLAGS@ $(AM_CPPFLAGS)
unit_test_sailfish_datahistory_CFLAGS = $(COVERAGE_OPT) $(AM_CFLAGS) \
-DSTATS_SHORT_WRITE_PERIOD_SEC=1 \
-DSTATS_LONG_WRITE_PERIOD_SEC=3 \
-Ddatacounters_time_now=test_time_now \
-Dgutil_time_notify_new=test_time_notify_new \
-Dgutil_time_notify_ref=test_time_notify_ref \
-Dgutil_time_notify_unref=test_time_notify_unref \
-Dgutil_time_notify_add_handler=test_time_notify_add_handler \
-Dgutil_time_notify_remove_handler=test_time_notify_remove_handler
unit_test_sailfish_datahistory_LDADD = @GLIBUTIL_LIBS@ @GLIB_LIBS@ -ldl
unit_test_sailfish_datahistory_SOURCES = unit/test-sailfish_datahistory.c \
unit/test_timenotify.c src/sailfish_datacounter.c \
src/sailfish_datacounters_util.c src/sailfish_datahistory.c \
src/sailfish_datahistory_memory.c src/sailfish_datahistory_file.c \
src/inotify.c src/storage.c src/wakeup_timer.c src/log.c \
$(backtrace_sources)
noinst_PROGRAMS += unit/test-sailfish_datahistory
TESTS += unit/test-sailfish_datahistory
endif
if WISPR
noinst_PROGRAMS += tools/wispr
......
......@@ -14,6 +14,10 @@ test-iptables \
test-sailfish_access \
test-sailfish_iptables_ext \
test-sailfish_wakeup_timer \
test-sailfish_datacounter \
test-sailfish_datacounters \
test-sailfish_datahistory \
test-sailfish_wakeup_timer \
test-vpn-settings"
pushd `dirname $0` > /dev/null
......
This diff is collapsed.
/*
* Connection Manager
*
* Copyright (C) 2016-2018 Jolla Ltd. All rights reserved.
* Copyright (C) 2016-2018 Slava Monich <slava.monich@jolla.com>
*
* 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 "sailfish_datacounters.h"
#include "connman.h"
#include <gutil_strv.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#define TEST_SUITE "datacounters"
#define TEST_(t) "/" TEST_SUITE "/" t
static const char *test_ident = "test";
static const char *test_ident2 = "test2";
static const char *test_counter = "home";
static const char *test_counter2 = "roaming";
static char *test_tmp_dir = "home";
static gint64 time_override_utc = 0;
GDateTime *test_time_now()
{
return time_override_utc ?
g_date_time_new_from_unix_utc(time_override_utc) :
g_date_time_new_now_utc();
}
static char *test_service_dir()
{
return g_strconcat(STORAGEDIR, "/", test_ident, NULL);
}
static char *test_counter_file()
{
return g_strconcat(STORAGEDIR, "/", test_ident, "/stats.",
test_counter, NULL);
}
static char *test_counter_file2()
{
return g_strconcat(STORAGEDIR, "/", test_ident, "/stats.",
test_counter2, NULL);
}
static void test_remove_counter_file()
{
char *file = test_counter_file();
remove(file);
g_free(file);
}
static void test_remove_counter_file2()
{
char *file = test_counter_file2();
remove(file);
g_free(file);
}
static void test_remove_counter_files()
{
test_remove_counter_file();
test_remove_counter_file2();
}
/* ==== /datacounters/null ==== */
static void test_datacounters_null()
{
struct datacounters *counters;
/* API calls should survive all kinds of NULL arguments */
g_assert(!datacounters_new(NULL));
g_assert(!datacounters_ref(NULL));
datacounters_unref(NULL);
g_assert(!datacounters_get_counter(NULL, NULL));
g_assert(!datacounters_get_counter(NULL, test_counter));
datacounters_reset_all_counters(NULL);
g_assert(!datacounters_add_counters_handler(NULL, NULL, NULL));
datacounters_remove_handler(NULL, 0);
datacounters_remove_handlers(NULL, NULL, 0);
/* Create and immediately deallocate the second one */
counters = datacounters_new(test_ident);
datacounters_unref(datacounters_new(test_ident2));
/* NULL arguments other that the object pointer */
datacounters_remove_handler(counters, 0);
g_assert(!datacounters_get_counter(counters, NULL));
g_assert(!datacounters_add_counters_handler(counters, NULL, NULL));
datacounters_remove_handler(counters, 0);
datacounters_remove_handlers(counters, NULL, 0);
datacounters_unref(counters);
}
/* ==== /datacounters/basic ==== */
static void test_datacounters_cb(struct datacounters *counters, void *arg)
{
int *count = arg;
(*count)++;
}
static void test_datacounters_basic_reset_cb(struct datacounter *dc, void *arg)
{
int *count = arg;
(*count)++;
}
static void test_datacounters_basic()
{
int reset_count = 0;
int counters_changed_count = 0;
struct datacounters *c = datacounters_new(test_ident);
gulong dcid, cid = datacounters_add_counters_handler(c,
test_datacounters_cb, &counters_changed_count);
struct datacounter *dc = datacounters_get_counter(c, test_counter);
struct datacounter *dc2 = datacounters_get_counter(c, test_counter2);
/* Both datacounters_new calls actually return the same object */
g_assert(datacounters_new(test_ident) == c);
datacounters_unref(c);
/* Only two counters should have been created */
g_assert(counters_changed_count == 2);
counters_changed_count = 0;
/* Same goes for datacounters_get_counter */
g_assert(datacounters_get_counter(c, test_counter) == dc);
datacounter_unref(dc);
/* Reset all counters (actually only one) */
dcid = datacounter_add_reset_handler(dc,
test_datacounters_basic_reset_cb, &reset_count);
datacounters_reset_all_counters(c);
datacounter_remove_handler(dc, dcid);
g_assert(reset_count == 1);
reset_count = 0;
datacounters_remove_handler(c, cid);
datacounters_unref(c);
datacounter_unref(dc);
datacounter_unref(dc2);
test_remove_counter_files();
}
/* Common */
static void test_init(const char *pname)
{
char *template = g_strconcat(pname, "_XXXXXX", NULL);
char *service_dir;
test_tmp_dir = g_dir_make_tmp(template, NULL);
__connman_log_init(pname, g_test_verbose() ? "*" : NULL, FALSE, FALSE,
pname, CONNMAN_VERSION);
__connman_inotify_init();
__connman_storage_init(test_tmp_dir, 0755, 0644);
service_dir = test_service_dir();
mkdir(STORAGEDIR, 0755);
mkdir(service_dir, 0755);
g_free(service_dir);
g_free(template);
}
static void test_cleanup()
{
char *service_dir = test_service_dir();
test_remove_counter_files();
remove(service_dir);
remove(STORAGEDIR);
remove(test_tmp_dir);
__connman_log_cleanup(FALSE);
__connman_storage_cleanup();
__connman_inotify_cleanup();
g_free(service_dir);
g_free(test_tmp_dir);
test_tmp_dir = NULL;
}
int main(int argc, char *argv[])
{
int ret;
char *pname = g_path_get_basename(argv[0]);
g_test_init(&argc, &argv, NULL);
test_init(pname);
g_test_add_func(TEST_("null"), test_datacounters_null);
g_test_add_func(TEST_("basic"), test_datacounters_basic);
ret = g_test_run();
test_cleanup();
g_free(pname);
return ret;
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 8
* indent-tabs-mode: t
* End:
*/
This diff is collapsed.
/*
* Connection Manager
*
* Copyright (C) 2016-2018 Jolla Ltd. All rights reserved.
* Copyright (C) 2016-2018 Slava Monich <slava.monich@jolla.com>
*
* 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 "test_timenotify.h"
#include <glib-object.h>
typedef struct gutil_time_notify {
GObject object;
} TestTimeNotify;
enum test_time_notify_signal {
SIGNAL_TIME_CHANGED,
SIGNAL_COUNT
};
#define SIGNAL_TIME_CHANGED_NAME "time-changed"
static guint test_time_notify_signals[SIGNAL_COUNT] = { 0 };
typedef GObjectClass TestTimeNotifyClass;
G_DEFINE_TYPE(TestTimeNotify, test_time_notify, G_TYPE_OBJECT)
#define PARENT_CLASS test_time_notify_parent_class
#define TEST_TIME_NOTIFY_TYPE (test_time_notify_get_type())
#define TEST_TIME_NOTIFY(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
TEST_TIME_NOTIFY_TYPE, TestTimeNotify))
TestTimeNotify *test_time_notify_new()
{
/* There's no need to have more than one instance. */
static TestTimeNotify *instance = NULL;
if (instance) {
test_time_notify_ref(instance);
} else {
instance = g_object_new(TEST_TIME_NOTIFY_TYPE, 0);
g_object_add_weak_pointer(G_OBJECT(instance),
(gpointer*)(&instance));
}
return instance;
}
TestTimeNotify *test_time_notify_ref(TestTimeNotify *self)
{
if (G_LIKELY(self)) {
g_object_ref(TEST_TIME_NOTIFY(self));
}
return self;
}
void test_time_notify_unref(TestTimeNotify *self)
{
if (G_LIKELY(self)) {
g_object_unref(TEST_TIME_NOTIFY(self));
}
}
gulong test_time_notify_add_handler(TestTimeNotify *self,
GUtilTimeNotifyFunc fn, void *arg)
{
return (G_LIKELY(self) && G_LIKELY(fn)) ? g_signal_connect(self,
SIGNAL_TIME_CHANGED_NAME, G_CALLBACK(fn), arg) : 0;
}
void test_time_notify_remove_handler(TestTimeNotify *self, gulong id)
{
if (G_LIKELY(self) && G_LIKELY(id)) {
g_signal_handler_disconnect(self, id);
}
}
void test_time_notify_signal(TestTimeNotify *self)
{
if (G_LIKELY(self)) {
g_signal_emit(self, test_time_notify_signals
[SIGNAL_TIME_CHANGED], 0);
}
}
static void test_time_notify_init(TestTimeNotify *self)
{
}
static void test_time_notify_class_init(TestTimeNotifyClass *klass)
{
test_time_notify_signals[SIGNAL_TIME_CHANGED] =
g_signal_new(SIGNAL_TIME_CHANGED_NAME,
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL, G_TYPE_NONE, 0);
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 8
* indent-tabs-mode: t
* End:
*/
/*
* Connection Manager
*
* Copyright (C) 2016 Jolla Ltd. All rights reserved.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* 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.
*/
#ifndef TEST_TIME_NOTIFY_H
#define TEST_TIME_NOTIFY_H
#include <gutil_timenotify.h>
void test_time_notify_signal(struct gutil_time_notify *test);
#endif /* TEST_TIME_NOTIFY_H */
/*
* Local Variables:
* mode: C
* c-basic-offset: 8
* indent-tabs-mode: t
* 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