log_collection_tester.c 9.68 KB
Newer Older
1
/*
2 3
	Linphone
	Copyright (C) 2014  Belledonne Communications SARL
4

5 6 7 8
	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.
9

10 11 12 13
	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.
14

15 16
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18
*/

Ghislain MARY's avatar
Ghislain MARY committed
19 20
#ifndef _XOPEN_SOURCE
	#define _XOPEN_SOURCE 700 // To have definition of strptime, snprintf and getline
21
#endif
22
#include <time.h>
23
#include "linphone/core.h"
24
#include "liblinphone_tester.h"
Benjamin REIS's avatar
Benjamin REIS committed
25
#include "tester_utils.h"
26

27 28 29 30
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif

31 32 33
#ifdef __ANDROID__
#include "android/api-level.h"
#endif
34

35
/*getline is POSIX 2008, not available on many systems.*/
36 37

/*It is declared since NDK14 unified headers, that can be detected by the presence of __ANDROID_API_O__ define*/
Ghislain MARY's avatar
Ghislain MARY committed
38
#if (defined(__ANDROID__) &&  __ANDROID_API__ < 18) || defined(_WIN32) || defined(__QNX__)
39
/* This code is public domain -- Will Hartung 4/9/09 */
40
static ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	char *bufptr = NULL;
	char *p = bufptr;
	size_t size;
	int c;

	if (lineptr == NULL) {
		return -1;
	}
	if (stream == NULL) {
		return -1;
	}
	if (n == NULL) {
		return -1;
	}
	bufptr = *lineptr;
	size = *n;

	c = fgetc(stream);
	if (c == EOF) {
		return -1;
	}
	if (bufptr == NULL) {
		bufptr = malloc(128);
		if (bufptr == NULL) {
			return -1;
		}
		size = 128;
	}
	p = bufptr;
	while(c != EOF) {
71
		size_t curpos = p-bufptr;
72

73
		if (curpos > (size - 1)) {
74 75
			size = size + 128;
			bufptr = realloc(bufptr, size);
76
			p = bufptr + curpos;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
			if (bufptr == NULL) {
				return -1;
			}
		}
		*p++ = c;
		if (c == '\n') {
			break;
		}
		c = fgetc(stream);
	}

	*p++ = '\0';
	*lineptr = bufptr;
	*n = size;

92
	return (ssize_t)(p - bufptr) - 1;
93 94 95
}
#endif

96
static LinphoneLogCollectionState old_collection_state;
97
static void collect_init(void)  {
98
	old_collection_state = linphone_core_log_collection_enabled();
99
	linphone_core_set_log_collection_path(bc_tester_get_writable_dir_prefix());
100 101
}

102
static void collect_cleanup(LinphoneCoreManager *marie)  {
103 104 105 106 107 108
	linphone_core_manager_destroy(marie);

	linphone_core_enable_log_collection(old_collection_state);
	linphone_core_reset_log_collection();
}

109
static LinphoneCoreManager* setup(LinphoneLogCollectionState log_collection_state)  {
110
	LinphoneCoreManager *marie;
111 112
	int timeout = 300;

113
	collect_init();
114
	linphone_core_enable_log_collection(log_collection_state);
115

116
	marie = linphone_core_manager_new2("marie_rc", 0);
117
	// wait a few seconds to generate some traffic
118 119 120 121
	while (--timeout){
		// Generate some logs - error logs because we must ensure that
		// even if user did not enable logs, we will see them
		ms_error("(test error)Timeout in %d...", timeout);
122 123 124 125
	}
	return marie;
}

126
#if HAVE_ZLIB
127

128
/*returns uncompressed log file*/
129
static FILE* gzuncompress(const char* filepath) {
130 131
		gzFile file = gzopen(filepath, "rb");
		FILE *output = NULL;
132
		FILE *ret;
133
		char *newname = ms_strdup_printf("%s.txt", filepath);
134 135
		char buffer[512]={0};
		output = fopen(newname, "wb");
136
		while (gzread(file, buffer, 511) > 0) {
137 138
			fputs(buffer, output);
			memset(buffer, 0, strlen(buffer));
139
		}
140
		fclose(output);
141
		BC_ASSERT_EQUAL(gzclose(file), Z_OK, int, "%d");
142
		ret=fopen(newname, "rb");
143
		ms_free(newname);
144
		return ret;
145 146 147
}
#endif

148
static time_t get_current_time(void) {
149 150
	struct timeval tp;
	struct tm *lt;
151
#ifndef _WIN32
152 153 154 155 156 157
	struct tm tmbuf;
#endif
	time_t tt;
	ortp_gettimeofday(&tp,NULL);
	tt = (time_t)tp.tv_sec;

158
#ifdef _WIN32
159 160 161 162 163 164 165
	lt = localtime(&tt);
#else
	lt = localtime_r(&tt,&tmbuf);
#endif
	return mktime(lt);
}

166
static time_t check_file(LinphoneCoreManager* mgr)  {
167

168
	time_t cur_time = get_current_time();
169
	char*    filepath = linphone_core_compress_log_collection();
170
	time_t  log_time = -1;
171
	uint32_t timediff = 0;
172
	FILE *file = NULL;
173

174
	BC_ASSERT_PTR_NOT_NULL(filepath);
175

176 177 178 179
	if (filepath != NULL) {
		int line_count = 0;
		char *line = NULL;
		size_t line_size = 256;
180
#ifndef _WIN32
181
		struct tm tm_curr = {0};
182
		time_t time_prev = 0;
183
#endif
184

185 186 187 188
#if HAVE_ZLIB
		// 0) if zlib is enabled, we must decompress the file first
		file = gzuncompress(filepath);
#else
189
		file = fopen(filepath, "rb");
190
#endif
191
		BC_ASSERT_PTR_NOT_NULL(file);
192
		if (!file) return 0;
193
		// 1) expect to find folder name in filename path
194
		BC_ASSERT_PTR_NOT_NULL(strstr(filepath, bc_tester_get_writable_dir_prefix()));
195 196 197

		// 2) check file contents
		while (getline(&line, &line_size, file) != -1) {
198
			// a) there should be at least 25 lines
199
			++line_count;
200
#ifndef _WIN32
201 202 203 204
			// 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);
205 206
				/*reset tm_curr to reset milliseconds and below fields*/
				memset(&tm_curr, 0, sizeof(struct tm));
207
				if (strptime(date, "%Y-%m-%d %H:%M:%S", &tm_curr) != NULL) {
208 209
					tm_curr.tm_isdst = -1; // LOL
					log_time = mktime(&tm_curr);
210
					BC_ASSERT_GREATER(log_time , time_prev, long int, "%ld");
211
					time_prev = log_time;
212 213
				}
			}
214
#endif
215
		}
216
		BC_ASSERT_GREATER(line_count , 25, int, "%d");
217 218 219
		free(line);
		fclose(file);
		ms_free(filepath);
220

221

222
		timediff = labs((long int)log_time - (long int)cur_time);
223
#ifndef _WIN32
224
		BC_ASSERT_LOWER(timediff, 1, unsigned, "%u");
225
		if( !(timediff <= 1) ){
226 227 228 229 230 231 232 233 234
			char buffers[2][128] = {{0}};
			strftime(buffers[0], sizeof(buffers[0]), "%Y-%m-%d %H:%M:%S", localtime(&log_time));
			strftime(buffers[1], sizeof(buffers[1]), "%Y-%m-%d %H:%M:%S", localtime(&cur_time));

			ms_error("log_time: %ld (%s), cur_time: %ld (%s) timediff: %u"
				, (long int)log_time, buffers[0]
				, (long int)cur_time, buffers[1]
				, timediff
			);
235
		}
236
#else
237
		(void)timediff;
238 239
		ms_warning("strptime() not available for this platform, test is incomplete.");
#endif
240
	}
241
	// return latest time in file
242
	return log_time;
243 244
}

245
static void collect_files_disabled(void)  {
246
	LinphoneCoreManager* marie = setup(LinphoneLogCollectionDisabled);
247
	BC_ASSERT_PTR_NULL(linphone_core_compress_log_collection());
248
	collect_cleanup(marie);
249 250
}

251
static void collect_files_filled(void) {
252
	LinphoneCoreManager* marie = setup(LinphoneLogCollectionEnabled);
253
	check_file(marie);
254
	collect_cleanup(marie);
255 256
}

257
static void collect_files_small_size(void)  {
258
	LinphoneCoreManager* marie = setup(LinphoneLogCollectionEnabled);
259
	linphone_core_set_log_collection_max_file_size(5000);
260
	check_file(marie);
261
	collect_cleanup(marie);
262 263
}

264
static void collect_files_changing_size(void)  {
265
	LinphoneCoreManager* marie = setup(LinphoneLogCollectionEnabled);
266 267
	int waiting = 100;

268
	check_file(marie);
269

270
	linphone_core_set_log_collection_max_file_size(5000);
271 272 273
	// Generate some logs
	while (--waiting) ms_error("(test error)Waiting %d...", waiting);

274
	check_file(marie);
275

276
	collect_cleanup(marie);
277
}
278 279 280
static void logCollectionUploadStateChangedCb(LinphoneCore *lc, LinphoneCoreLogCollectionUploadState state, const char *info) {

	stats* counters = get_stats(lc);
jehan's avatar
jehan committed
281 282 283
	ms_message("lc [%p], logCollectionUploadStateChanged to [%s], info [%s]",lc
																			,linphone_core_log_collection_upload_state_to_string(state)
																			,info);
284 285 286 287 288 289
	switch(state) {
		case LinphoneCoreLogCollectionUploadStateInProgress:
			counters->number_of_LinphoneCoreLogCollectionUploadStateInProgress++;
			break;
		case LinphoneCoreLogCollectionUploadStateDelivered:
			counters->number_of_LinphoneCoreLogCollectionUploadStateDelivered++;
290
			BC_ASSERT_GREATER((int)strlen(info), 0, int, "%d");
291 292 293 294 295 296
			break;
		case LinphoneCoreLogCollectionUploadStateNotDelivered:
			counters->number_of_LinphoneCoreLogCollectionUploadStateNotDelivered++;
			break;
	}
}
297
static void upload_collected_traces(void)  {
298
	if (transport_supported(LinphoneTransportTls)) {
299
		LinphoneCoreManager* marie = setup(LinphoneLogCollectionEnabled);
300
		int waiting = 100;
301 302 303 304
		LinphoneCoreCbs *cbs = linphone_factory_create_core_cbs(linphone_factory_get());
		linphone_core_cbs_set_log_collection_upload_state_changed(cbs, logCollectionUploadStateChangedCb);
		linphone_core_add_callbacks(marie->lc, cbs);
		linphone_core_cbs_unref(cbs);
305 306 307 308 309

		linphone_core_set_log_collection_max_file_size(5000);
		linphone_core_set_log_collection_upload_server_url(marie->lc,"https://www.linphone.org:444/lft.php");
		// Generate some logs
		while (--waiting) ms_error("(test error)Waiting %d...", waiting);
310
		ms_free(linphone_core_compress_log_collection());
311
		linphone_core_upload_log_collection(marie->lc);
312
		BC_ASSERT_TRUE(wait_for_until(marie->lc,marie->lc,&marie->stat.number_of_LinphoneCoreLogCollectionUploadStateDelivered,1, 10000));
313 314 315

		/*try 2 times*/
		waiting=100;
316
		linphone_core_reset_log_collection();
317
		while (--waiting) ms_error("(test error)Waiting %d...", waiting);
318
		ms_free(linphone_core_compress_log_collection());
319
		linphone_core_upload_log_collection(marie->lc);
320
		BC_ASSERT_TRUE(wait_for_until(marie->lc,marie->lc,&marie->stat.number_of_LinphoneCoreLogCollectionUploadStateDelivered,2, 10000));
321
		collect_cleanup(marie);
322
	}
323
}
324 325

test_t log_collection_tests[] = {
326 327 328 329
	TEST_NO_TAG("No file when disabled", collect_files_disabled),
	TEST_NO_TAG("Collect files filled when enabled", collect_files_filled),
	TEST_NO_TAG("Logs collected into small file", collect_files_small_size),
	TEST_NO_TAG("Logs collected when decreasing max size", collect_files_changing_size),
330
	TEST_NO_TAG("Upload collected traces", upload_collected_traces)
331 332
};

333
test_suite_t log_collection_test_suite = {"LogCollection", NULL, NULL, liblinphone_tester_before_each, liblinphone_tester_after_each,
334 335
										  sizeof(log_collection_tests) / sizeof(log_collection_tests[0]),
										  log_collection_tests};