From d67957ab2a4673f98ae39333acb8b5b9f7e2a552 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Tue, 4 Nov 2014 14:47:13 +0100 Subject: [PATCH] Add log collection tester suite --- .gitignore | 2 + build/android/liblinphone_tester.mk | 1 + .../LibLinphoneTester-native.vcxproj | 1 + coreapi/linphonecore.c | 2 +- tester/Makefile.am | 1 + tester/liblinphone_tester.h | 1 + tester/log_collection_tester.c | 145 ++++++++++++++++++ tester/tester.c | 1 + 8 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 tester/log_collection_tester.c diff --git a/.gitignore b/.gitignore index d5a842ef1..85a6aed1d 100644 --- a/.gitignore +++ b/.gitignore @@ -76,3 +76,5 @@ tester/tmp.db .DS_Store Linphone.app *.dmg +tester/linphone*.log +tester/linphone_log.txt diff --git a/build/android/liblinphone_tester.mk b/build/android/liblinphone_tester.mk index b23a381bd..5ca3732be 100644 --- a/build/android/liblinphone_tester.mk +++ b/build/android/liblinphone_tester.mk @@ -14,6 +14,7 @@ common_SRC_FILES := \ tester.c \ remote_provisioning_tester.c \ quality_reporting_tester.c \ + log_collection_tester.c \ transport_tester.c \ player_tester.c diff --git a/build/wp8/LibLinphoneTester-native/LibLinphoneTester-native.vcxproj b/build/wp8/LibLinphoneTester-native/LibLinphoneTester-native.vcxproj index 0512be517..021ab6328 100644 --- a/build/wp8/LibLinphoneTester-native/LibLinphoneTester-native.vcxproj +++ b/build/wp8/LibLinphoneTester-native/LibLinphoneTester-native.vcxproj @@ -100,6 +100,7 @@ + diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 207310c99..661604ab0 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -454,7 +454,7 @@ static int prepare_log_collection_file_to_upload(const char *filename) { ortp_mutex_lock(&liblinphone_log_collection_mutex); output_filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", filename); - output_file = COMPRESS_OPEN(output_filename, "a"); + output_file = COMPRESS_OPEN(output_filename, "w"); if (output_file == NULL) goto error; input_filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", "linphone1.log"); input_file = fopen(input_filename, "r"); diff --git a/tester/Makefile.am b/tester/Makefile.am index d8877119d..7fb08bb0f 100644 --- a/tester/Makefile.am +++ b/tester/Makefile.am @@ -22,6 +22,7 @@ liblinphonetester_la_SOURCES = tester.c \ stun_tester.c \ remote_provisioning_tester.c \ quality_reporting_tester.c \ + log_collection_tester.c \ transport_tester.c \ player_tester.c diff --git a/tester/liblinphone_tester.h b/tester/liblinphone_tester.h index 18990f313..a1ad60ec0 100644 --- a/tester/liblinphone_tester.h +++ b/tester/liblinphone_tester.h @@ -58,6 +58,7 @@ extern test_suite_t flexisip_test_suite; extern test_suite_t stun_test_suite; extern test_suite_t remote_provisioning_test_suite; extern test_suite_t quality_reporting_test_suite; +extern test_suite_t log_collection_test_suite; extern test_suite_t transport_test_suite; extern test_suite_t player_test_suite; diff --git a/tester/log_collection_tester.c b/tester/log_collection_tester.c new file mode 100644 index 000000000..d8b9dd13d --- /dev/null +++ b/tester/log_collection_tester.c @@ -0,0 +1,145 @@ +/* + belle-sip - SIP (RFC3261) library. + Copyright (C) 2010 Belledonne Communications SARL + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + 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. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include "CUnit/Basic.h" +#include "linphonecore.h" +#include "private.h" +#include "liblinphone_tester.h" + +extern char *strptime(char*, char*, struct tm*); + +LinphoneCoreManager* setup(bool_t enable_logs) { + linphone_core_enable_log_collection(enable_logs); + LinphoneCoreManager *marie; + int timeout_ms = 3000; + + // linphone_core_set_log_collection_size(10); + marie = linphone_core_manager_new( "marie_rc"); + // wait a few seconds to generate some traffic + while (timeout_ms > 0){ + linphone_core_iterate(marie->lc); + ms_usleep(100000); //100 ms sleep + timeout_ms -= 100; + // Generate some logs + ms_message("Time left: %d", timeout_ms); + } + return marie; +} + +time_t check_file(char * filepath, bool_t remove_file) { + time_t time_curr = -1; + if (filepath != NULL) { + int line_count = 0; + FILE *file = fopen(filepath, "r"); + char *line = NULL; + size_t line_size = 256; + struct tm tm_curr; + time_t time_prev = -1; + + // 1) expect to find folder name in filename path + CU_ASSERT_PTR_NOT_NULL(strstr(filepath, liblinphone_tester_writable_dir_prefix)); + + // 2) check file contents + while (getline(&line, &line_size, file) != -1) { + // a) there should be at least 100 lines + ++line_count; + + // b) logs should be ordered by date (format: 2014-11-04 15:22:12:606) + if (strlen(line) > 24) { + char date[24] = {'\0'}; + memcpy(date, line, 23); + if (strptime(date, "%Y-%m-%d %H:%M:%S", &tm_curr) != NULL) { + time_curr = mktime(&tm_curr); + CU_ASSERT_TRUE(time_curr >= time_prev); + time_prev = time_curr; + } + } + } + CU_ASSERT(line_count > 100); + free(line); + fclose(file); + if (remove_file) { + remove(filepath); + } + ms_free(filepath); + } + // return latest time in file + return time_curr; +} + +static OrtpLogLevel old_log_level; +// static LinphoneLogCollectionState old_collection_state; +static int collect_init() { + old_log_level = ortp_get_log_level_mask(); + // old_collection_state = liblinphone_log_collection_enabled; + // CU_ASSERT_FALSE("Fixme: // old_collection_state = liblinphone_log_collection_enabled;"); + + // if we want some logs, we must force them... even if user dont want to! + linphone_core_set_log_level(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL); + linphone_core_set_log_collection_path(liblinphone_tester_writable_dir_prefix); + + return 0; +} + +static int collect_cleanup() { + linphone_core_set_log_level(old_log_level); + // liblinphone_log_collection_enabled = old_collection_state; + // CU_ASSERT_FALSE("Fixme: // liblinphone_log_collection_enabled = old_collection_state;"); + + return 0; +} + +static void collect_files_disabled() { + LinphoneCoreManager* marie = setup(FALSE); + CU_ASSERT_PTR_NULL(linphone_core_compress_log_collection(marie->lc)); + linphone_core_manager_destroy(marie); +} + +static void collect_files_filled() { + LinphoneCoreManager* marie = setup(TRUE); + char * filepath = linphone_core_compress_log_collection(marie->lc); + CU_ASSERT_PTR_NOT_NULL(filepath); + CU_ASSERT_EQUAL(ms_time(0), check_file(filepath, FALSE)); + linphone_core_manager_destroy(marie); +} + +static void collect_files_small_size() { + LinphoneCoreManager* marie = setup(TRUE); + // linphone_core_set_log_collection_size(10); + char * filepath= linphone_core_compress_log_collection(marie->lc); + CU_ASSERT_PTR_NOT_NULL(filepath); + CU_ASSERT_EQUAL(ms_time(0), check_file(filepath, TRUE)); + linphone_core_manager_destroy(marie); +} + +test_t log_collection_tests[] = { + { "No file when disabled", collect_files_disabled}, + { "Collect files filled when enabled", collect_files_filled}, + { "Logs collected into small file", collect_files_small_size}, +}; + +test_suite_t log_collection_test_suite = { + "LogCollection", + collect_init, + collect_cleanup, + sizeof(log_collection_tests) / sizeof(log_collection_tests[0]), + log_collection_tests +}; + diff --git a/tester/tester.c b/tester/tester.c index 97b45713d..c157fe49b 100644 --- a/tester/tester.c +++ b/tester/tester.c @@ -387,6 +387,7 @@ void liblinphone_tester_init(void) { add_test_suite(&flexisip_test_suite); add_test_suite(&remote_provisioning_test_suite); add_test_suite(&quality_reporting_test_suite); + add_test_suite(&log_collection_test_suite); add_test_suite(&transport_test_suite); add_test_suite(&player_test_suite); }