summaryrefslogtreecommitdiffstats
path: root/test/unittest/libunit.c
blob: b0be673a409e0939dafaaf55fda9c3550811ff9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/* vim: set textwidth=80 tabstop=4: */

/*
//      libunit.c
//      
//      Copyright 2010 Michael Rasmussen <mir@datanom.net>
//      
//      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 3 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, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
*/

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "caldav.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <glib.h>
#include <string.h>
#include <sys/time.h>
#include <string.h>

static const char* usage[] = 	{
"unittest is part of libcaldav for claws-mails "
"vcalendar plugin.\nCopyright (C) Michael Rasmussen, 2008.\n"
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 3 of the License, or\n"
"(at your option) any later version.\n"
"\nusage:\n\tunittest [Options]\n"
"\n\tOptions:\n"
"\t\t-h|-?\tusage\n"
"\t\t-a\tURL\n"
"\t\t-d\tEnable debug\n"
"\t\t-l\tDebug library\n"
"\t\t-p\tpassword\n"
"\t\t-u\tusername\n"
};

/*
 * Options can be given in one of three ways:
 * 1) Entered as options on the command line
 * 2) Stored in environment variables:
 *    - LIBCALDAV_UID -> username to use when connection to server
 *    - LIBCALDAV_PWD -> password, if any, to use when connection to server
 *    - LIBCALDAV_URL -> complete URL to server. prefixed with either
 *                       http:// or https://
 * 3) Stored in a file found in the same directory as the program:
 *    [server]
 * 	  password=Big secret
 *    username=username
 *    url=http[s]://bar.tld/foo
 */

#define CONFFILE "caldav-unit-settings.conf"

typedef struct {
	gchar* uid;
	gchar* pwd;
	gchar* url;
} settings;

typedef enum {
	PWD,
	UID,
	URL
} CONFIG;
	
static settings* SETTING = NULL;
gboolean DEBUG = FALSE;
gboolean DEBUG_LIB = FALSE;

void settings_free(settings** setting) {
	settings* s;
	
	if (! *setting)
		return;
	s = *setting;
	g_free(s->url);
	g_free(s->uid);
	g_free(s->pwd);
	g_free(s);
	*setting = s = NULL;
}

settings* parse_cmdline(int argc, char** argv) {
	char c;
	settings* setting;

	setting = g_new0(settings, 1);	
	while ((c = getopt(argc, argv, "ha:dlp:u:?")) != -1) {
		switch (c) {
			case 'h':
			case '?':
				fprintf(stdout, "%s", usage[0]);
				return 0;
			case 'a':
				setting->url = g_strdup(optarg);
				break;
			case 'd':
				DEBUG = TRUE;
				break;
			case 'l':
				DEBUG_LIB = TRUE;
				break;
			case 'p':
				setting->pwd = g_strdup(optarg);
				break;
			case 'u':
				setting->uid = g_strdup(optarg);
				break;
		}
	}
	if (argv[optind]) {
		fprintf(stderr, "No arguments after options expected\n");
		fprintf(stderr, "%s", usage[0]);
		settings_free(&setting);
		return NULL;
	}
	
	return setting;
}

void read_file(const gchar* file) {
	GKeyFile* keyfile;
	
	if (! SETTING) {
		SETTING = g_new0(settings, 1);
		keyfile = g_key_file_new();
		if (g_key_file_load_from_file(keyfile,
									  file,
									  G_KEY_FILE_KEEP_COMMENTS,
									  NULL)) {
			SETTING->pwd = g_key_file_get_string(keyfile,
												 "server",
												 "password",
												 NULL);
			SETTING->uid = g_key_file_get_string(keyfile,
												 "server",
												 "username",
												 NULL);
			SETTING->url = g_key_file_get_string(keyfile,
												 "server",
												 "url",
												 NULL);
		}
		g_key_file_free(keyfile);
	}
}

const gchar* get_setting_from_file(CONFIG config) {
	gchar* cwd;
	gchar* path;
	gchar* pos;
	gchar* setting = NULL;
	int step;
	
	gchar* filename = g_get_prgname();
	if (filename[0] == '/') {
		/* absolute path */
		cwd = g_path_get_dirname(filename);
	}
	else if ((pos = strchr(filename, '/')) != NULL) {
		/* relative path */
		if (filename[0] == '.') {
			char* tmp = pos;
			while (tmp) {
				if (strncmp(tmp, "..", 2) == 0)
					step = 2;
				else
					step = 1;
				tmp = strchr(pos + step, '/');
				if (tmp)
					pos = tmp;
			}
			if (pos == NULL) {
				pos = strchr(filename, '/');
			}
		}
		path = g_get_current_dir();
		cwd = g_build_filename(path, pos + 1, NULL);
		g_free(path);
		if (g_file_test(cwd, G_FILE_TEST_IS_REGULAR)) {
			pos = strrchr(cwd, '/');
			if (pos) {
				path = g_strndup(cwd, pos - cwd);
				g_free(cwd);
				cwd = g_strdup(path);
				g_free(path);
			}
		}
	}
	else {
		/* find cwd in PATH */
		cwd = g_find_program_in_path(filename);
	}
	path = g_build_filename(cwd, CONFFILE, NULL);
	g_free(cwd);
	if (g_file_test(path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
		read_file(path);
		switch (config) {
			case UID: setting = SETTING->uid; break;
			case PWD: setting = SETTING->pwd; break;
			case URL: setting = SETTING->url; break;
		}
	}
	g_free(path);
	return setting;
}

gboolean find_missing(settings** setting) {
	settings* s = *setting;
	const char* env;
	gboolean error = FALSE;

	if (! s)
		return TRUE;
		
	if (! s->uid) {
		if ((env = getenv("LIBCALDAV_UID")) == NULL) {
			env = get_setting_from_file(UID);
			if (env == NULL)
				error = TRUE;
		}
		s->uid = g_strdup(env);
	}
	if (! s->pwd) {
		if ((env = getenv("LIBCALDAV_PWD")) == NULL) {
			env = get_setting_from_file(PWD);
			if (env == NULL)
				error = TRUE;
		}
		s->pwd = g_strdup(env);
	}
	if (! s->url) {
		if ((env = getenv("LIBCALDAV_URL")) == NULL) {
			env = get_setting_from_file(URL);
			if (env == NULL)
				error = TRUE;
		}
		s->url = g_strdup(env);
	}
	settings_free(&SETTING);
	return error;
}

time_t make_time_t(const char* time_elem) {
	struct tm datetime = {0,0,0,0,0,0,0,0,0,0,NULL};
	struct tm* tmp;
	gchar** elem;
	time_t t;

	t = time(NULL);
	tmp = localtime(&t);
	elem = g_strsplit(time_elem, "/", 3);
	datetime.tm_year = atoi(elem[0]) - 1900;
	datetime.tm_mon = atoi(elem[1]) - 1;
	datetime.tm_mday = atoi(elem[2]);
	g_strfreev(elem);
	t = mktime(&datetime);
	return t;
}

gboolean compare_freebusy(const gchar* s1, const gchar* s2) {
	const char* TOKEN = "FREEBUSY:";
	gchar* pos1;
	gchar* pos2;
	gchar* id1;

	if (! s1 && ! s2)
		return TRUE;
	if (! s1 || ! s2)
		return FALSE;
	pos1 = strstr(s2, TOKEN);
	if (! pos1)
		return FALSE;
	pos1 += strlen(TOKEN);
	pos2 = strchr(pos1, '\n');
	if (! pos2)
		return FALSE;
	if (*(pos2 - 1) == '\r')
		pos2--;
	id1 = g_strndup(pos1, pos2 - pos1);
	int res = strcmp(s1, id1);
	g_free(id1);
	return (res == 0);
}

gboolean compare_object(const gchar* TOKEN, const gchar* s1, const gchar* s2) {
	gchar* pos1;
	gchar* pos2;
	gchar* id1;
	gchar* id2;
	
	if (! s1 && ! s2)
		return TRUE;
	if (! s1 || ! s2)
		return FALSE;
	pos1 = strstr(s1, TOKEN);
	if (! pos1)
		return FALSE;
	pos1 += strlen(TOKEN) + 1;
	pos2 = strchr(pos1, '\n');
	if (! pos2)
		return FALSE;
	if (*(pos2 - 1) == '\r')
		pos2--;
	id1 = g_strndup(pos1, pos2 - pos1);
	pos2 = strstr(s2, TOKEN);
	if (! pos2) {
		g_free(id1);
		return FALSE;
	}
	pos2 += strlen(TOKEN) + 1;
	pos1 = strchr(pos2, '\n');	
	if (! pos1) {
		g_free(id1);
		return FALSE;
	}
	if (*(pos1 - 1) == '\r')
		pos1--;
	id2 = g_strndup(pos2, pos1 - pos2);
	int res = strcmp(id1, id2);
	g_free(id1);
	g_free(id2);
	return (res == 0);
}

void run_tests(settings* s) {
	runtime_info* info = caldav_get_runtime_info();
	response* resp = caldav_get_response();
	gchar** parts;
	gchar* url;
	gchar* object;
	
	if (DEBUG_LIB) {
	    info->options->debug = 1;
	    info->options->trace_ascii = 1;
	}
	parts = g_strsplit(s->url, "//", 2);
	url = g_strconcat(
		parts[0], "//", s->uid, ":", s->pwd, "@", parts[1], NULL);
	g_strfreev(parts);
	fprintf(stdout, "Test caldav_enabled_resource:\t\t\t");
	if (caldav_enabled_resource(url, info)) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "caldav enabled server\n");
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	fprintf(stdout, "Test caldav_get_displayname:\t\t\t");
	if (caldav_get_displayname(resp, url, info) == OK) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "Display Name: %s\n", resp->msg);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(resp->msg);
	resp->msg = NULL;
	fprintf(stdout, "Test caldav_get_server_options:\t\t\t");
	if ((parts = caldav_get_server_options(url, info)) != NULL) {
		fprintf(stdout, "OK\n");
		if (DEBUG) {
			char** tmp = parts;
			while (*tmp) {
				fprintf(stdout, "--> %s\n", *tmp++);
			}
		}
		g_strfreev(parts);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_file_get_contents("../ics/add.ics", &object, NULL, NULL);
	fprintf(stdout, "Test caldav_add_object:\t\t\t\t");
	if (caldav_add_object(object, url, info) == OK) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "Added successfully\n");
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	fprintf(stdout, "Test if object was added:\t\t\t");
	if (caldav_get_object(resp, make_time_t("2008/04/15"),
		make_time_t("2008/04/16"), url, info) == OK) {
		if (compare_object("UID", object, resp->msg))
			fprintf(stdout, "OK\n");
		else
			fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%s\n", resp->msg);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(resp->msg);
	resp->msg = NULL;
	g_free(object);
	fprintf(stdout, "Test FREEBUSY search the same day:\t\t");
	if (caldav_get_freebusy(resp, make_time_t("2008/04/15"),
		make_time_t("2008/04/16"), url, info) == OK) {
			if (compare_freebusy("20080415T151500Z/20080415T162500", resp->msg))
				fprintf(stdout, "OK\n");
			else
				fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%s\n", resp->msg);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(resp->msg);
	resp->msg = NULL;
	g_file_get_contents("../ics/modify.ics", &object, NULL, NULL);
	fprintf(stdout, "Test caldav_modify_object:\t\t\t");
	if (caldav_modify_object(object, url, info) == OK) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "Modified successfully\n");
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	fprintf(stdout, "Test if object was modified:\t\t\t");
	if (caldav_get_object(resp, make_time_t("2008/04/16"),
		make_time_t("2008/04/17"), url, info) == OK) {
		if (compare_object("DTEND", object, resp->msg))
			fprintf(stdout, "OK\n");
		else
			fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%s\n", resp->msg);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(resp->msg);
	resp->msg = NULL;
	fprintf(stdout, "Test caldav_getall_object:\t\t\t");
	if (caldav_getall_object(resp, url, info) == OK) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "%s\n", resp->msg);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	fprintf(stdout, "Test if object exists:\t\t\t\t");
	if (compare_object("UID", object, resp->msg))
		fprintf(stdout, "FAIL\n");
	else
		fprintf(stdout, "OK\n");
	g_free(resp->msg);
	resp->msg = NULL;
	g_free(object);
	g_file_get_contents("../ics/delete.ics", &object, NULL, NULL);
	fprintf(stdout, "Test caldav_delete_object:\t\t\t");
	if (caldav_delete_object(object, url, info) == OK) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "Deleted successfully\n");
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	fprintf(stdout, "Test if object was deleted:\t\t\t");
	if (caldav_get_object(resp, make_time_t("2008/04/16"),
		make_time_t("2008/04/17"), url, info) == OK) {
		if (compare_object("UID", object, resp->msg))
			fprintf(stdout, "FAIL\n");
		else
			fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "%s\n", (resp->msg) ? resp->msg : "No object found");
		g_free(resp->msg);
		resp->msg = NULL;
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(object);
	fprintf(stdout, "\nTesting without using locks\n");
	info->options->use_locking = 0;
	g_file_get_contents("../ics/add.ics", &object, NULL, NULL);
	fprintf(stdout, "Test caldav_add_object:\t\t\t\t");
	if (caldav_add_object(object, url, info) == OK) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "Added successfully\n");
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	fprintf(stdout, "Test if object was added:\t\t\t");
	if (caldav_get_object(resp, make_time_t("2008/04/15"),
		make_time_t("2008/04/16"), url, info) == OK) {
		if (compare_object("UID", object, resp->msg))
			fprintf(stdout, "OK\n");
		else
			fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%s\n", resp->msg);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(resp->msg);
	resp->msg = NULL;
	g_free(object);
/*	fprintf(stdout, "Test FREEBUSY search the same day:\t\t");
	if (caldav_get_freebusy(resp, make_time_t("2008/04/15"),
		make_time_t("2008/04/16"), url, info) == OK) {
			if (compare_freebusy("20080415T151500Z/20080415T162500", resp->msg))
				fprintf(stdout, "OK\n");
			else
				fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%s\n", resp->msg);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(resp->msg);
	resp->msg = NULL;*/
	g_file_get_contents("../ics/modify.ics", &object, NULL, NULL);
	fprintf(stdout, "Test caldav_modify_object:\t\t\t");
	if (caldav_modify_object(object, url, info) == OK) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "Modified successfully\n");
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	fprintf(stdout, "Test if object was modified:\t\t\t");
	if (caldav_get_object(resp, make_time_t("2008/04/16"),
		make_time_t("2008/04/17"), url, info) == OK) {
		if (compare_object("DTEND", object, resp->msg))
			fprintf(stdout, "OK\n");
		else
			fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%s\n", resp->msg);
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(resp->msg);
	resp->msg = NULL;
	g_free(object);
	g_file_get_contents("../ics/delete.ics", &object, NULL, NULL);
	fprintf(stdout, "Test caldav_delete_object:\t\t\t");
	if (caldav_delete_object(object, url, info) == OK) {
		fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "Deleted successfully\n");
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	fprintf(stdout, "Test if object was deleted:\t\t\t");
	if (caldav_get_object(resp, make_time_t("2008/04/16"),
		make_time_t("2008/04/17"), url, info) == OK) {
		if (compare_object("UID", object, resp->msg))
			fprintf(stdout, "FAIL\n");
		else
			fprintf(stdout, "OK\n");
		if (DEBUG) fprintf(stdout, "%s\n", (resp->msg) ? resp->msg : "No object found");
		g_free(resp->msg);
		resp->msg = NULL;
	}
	else {
		fprintf(stdout, "FAIL\n");
		if (DEBUG) fprintf(stdout, "%ld: %s\n", info->error->code, info->error->str);
	}
	g_free(url);
	caldav_free_response(&resp);
	caldav_free_runtime_info(&info);
}

int main(int argc, char** argv) {
	settings* setting;
	
	g_set_prgname(argv[0]);
	setting = parse_cmdline(argc, argv);
	if (find_missing(&setting)) {
		fprintf(stderr, "missing required information\n");
		fprintf(stdout, "%s", usage[0]);
		settings_free(&setting);
		return 1;
	}
	run_tests(setting);
	settings_free(&setting);
	return 0;
}