Add metrics.c test 02/240902/2
authorAgnieszka Baumann <a.baumann@samsung.com>
Fri, 7 Aug 2020 07:49:03 +0000 (09:49 +0200)
committerAgnieszka Baumann <a.baumann@samsung.com>
Wed, 12 Aug 2020 16:24:50 +0000 (18:24 +0200)
Change-Id: I1bd23d2b0dfe9ed834872d6e9c7c61ba849030b9

Makefile.am
src/tests/metrics.c

index ceffc67..551aa11 100644 (file)
@@ -628,7 +628,7 @@ src_tests_metrics_SOURCES = src/tests/metrics.c \
 src_tests_metrics_DEPENDENCIES = libdlogutil.la
 src_tests_metrics_LDADD = libdlogutil.la
 src_tests_metrics_CFLAGS = $(check_CFLAGS)
-src_tests_metrics_LDFLAGS = $(AM_LDFLAGS)
+src_tests_metrics_LDFLAGS = $(AM_LDFLAGS) -lpthread -Wl,--wrap=dlogutil_entry_get_tag,--wrap=dlogutil_entry_get_pid,--wrap=dlogutil_entry_get_priority,--wrap=free,--wrap=calloc,--wrap=strdup
 
 src_tests_deduplicate_test_SOURCES = src/tests/deduplicate_test.c src/libdlog/log.c src/libdlog/deduplicate.c src/shared/hash.c src/shared/logcommon.c src/shared/loglimiter.c src/libdlog/dynamic_config.c src/shared/logconfig.c src/shared/parsers.c src/shared/ptrs_list.c
 src_tests_deduplicate_test_CFLAGS = $(check_CFLAGS) -pthread
index 15282fd..a6d5db9 100644 (file)
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE. */
-
+// C
 #include <assert.h>
+#include <stdint.h>
+
+// Dlog
 #include <metrics.h>
 #include <queued_entry.h>
 
+static bool fail_dlogutil_get_tag = false;
+int __real_dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag);
+int __wrap_dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag)
+{
+       if (!fail_dlogutil_get_tag)
+               return  __real_dlogutil_entry_get_tag(entry, tag);
+
+       return 1;
+}
+
+static bool fail_dlogutil_get_pid = false;
+int __real_dlogutil_entry_get_pid(const dlogutil_entry_s *e, pid_t *pid);
+int __wrap_dlogutil_entry_get_pid(const dlogutil_entry_s *e, pid_t *pid)
+{
+       if (!fail_dlogutil_get_pid)
+               return  __real_dlogutil_entry_get_pid(e, pid);
+
+       return 1;
+}
+
+static bool fail_dlogutil_get_priority = false;
+int __real_dlogutil_entry_get_priority(const dlogutil_entry_s *e, log_priority *prio);
+int __wrap_dlogutil_entry_get_priority(const dlogutil_entry_s *e, log_priority *prio)
+{
+       if (!fail_dlogutil_get_priority)
+               return  __real_dlogutil_entry_get_priority(e, prio);
+
+       return 1;
+}
+
+uintptr_t sum_alloc;
+static bool count_pointers = false;
+
+void __real_free(void *ptr);
+void __wrap_free(void *ptr)
+{
+       if (count_pointers)
+               sum_alloc -= (uintptr_t) ptr;
+
+       __real_free(ptr);
+}
+
+void *__real_calloc(size_t nmemb, size_t size);
+void *__wrap_calloc(size_t nmemb, size_t size)
+{
+       void *tmp = __real_calloc(nmemb, size);
+
+       if (count_pointers)
+               sum_alloc += (uintptr_t) tmp;
+
+       return tmp;
+}
+
+char *__real_strdup(const char *s);
+char *__wrap_strdup(const char *s)
+{
+       void *tmp = __real_strdup(s);
+
+       if (count_pointers)
+               sum_alloc += (uintptr_t) tmp;
+
+       return tmp;
+}
+
 int main()
 {
        struct metrics *m = metrics_create();
        assert(m);
+       assert(0 == metrics_get_total(m));
 
        int count;
        struct metrics_info *info;
@@ -171,6 +239,7 @@ int main()
        assert(info[1].count[DLOG_DEBUG] == 0);
        assert(info[1].count[DLOG_VERBOSE] == 0);
        free(info);
+       assert(6 == metrics_get_total(m));
 
        const int PIDS = 1200;
 
@@ -182,6 +251,45 @@ int main()
        assert(count == PIDS + 3);
        free(info);
 
+       assert(1206 == metrics_get_total(m));
+
+       assert(metrics_add_log(m, &e0.header));
+       assert(1207 == metrics_get_total(m));
+
+       assert(metrics_add_log(m, &e2.header));
+       assert(1208 == metrics_get_total(m));
+
+       metrics_clear(m);
+       assert(0 == metrics_get_total(m));
+       info = metrics_get_info(m, &count);
+       assert(info == NULL);
+
+       fail_dlogutil_get_tag = true;
+       assert(!metrics_add_log(m, &e0.header));
+       fail_dlogutil_get_tag = false;
+
+       fail_dlogutil_get_pid = true;
+       assert(!metrics_add_log(m, &e0.header));
+       fail_dlogutil_get_pid = false;
+
+       fail_dlogutil_get_priority = true;
+       assert(!metrics_add_log(m, &e0.header));
+       fail_dlogutil_get_priority = false;
+
        metrics_destroy(m);
+
+       sum_alloc = 0;
+       count_pointers = true;
+
+       struct metrics *m2 = metrics_create();
+       assert(metrics_add_log(m2, &e0.header));
+       assert(metrics_add_log(m2, &e3.header));
+       metrics_destroy(m2);
+
+       count_pointers = false;
+       assert(0 == sum_alloc);
+
+       // it shouldn't crash.
+       metrics_destroy(NULL);
 }