summaryrefslogtreecommitdiff
path: root/test/nsoption.c
blob: 5874c94b6ce032779829f8472dc81cf5f71e92d5 (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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
 * Copyright 2016 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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 <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * Tests for user option processing
 */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <check.h>

#include "utils/errors.h"
#include "utils/log.h"
#include "utils/nsoption.h"

const char *test_choices_path = "test/data/Choices";
const char *test_choices_short_path = "test/data/Choices-short";
const char *test_choices_all_path = "test/data/Choices-all";
const char *test_choices_full_path = "test/data/Choices-full";
const char *test_choices_missing_path = "test/data/Choices-missing";

/**
 * generate test output filenames
 */
static char *testnam(char *out)
{
	static int count = 0;
	static char name[64];
	snprintf(name, 64, "/tmp/nsoptiontest%d", count);
	count++;
	return name;
}

static nserror gui_options_init_defaults(struct nsoption_s *defaults)
{
	/* Set defaults for absent option strings */
	nsoption_setnull_charp(ca_bundle, strdup("NetSurf:Resources.ca-bundle"));
	nsoption_setnull_charp(cookie_file, strdup("NetSurf:Cookies"));
	nsoption_setnull_charp(cookie_jar, strdup("Cookies"));

	if (nsoption_charp(ca_bundle) == NULL ||
	    nsoption_charp(cookie_file) == NULL ||
	    nsoption_charp(cookie_jar) == NULL) {
		return NSERROR_BAD_PARAMETER;
	}
	return NSERROR_OK;
}


/**
 * compare two files contents
 */
static int cmp(const char *f1, const char *f2)
{
	int res = 0;
	FILE *fp1;
	FILE *fp2;
	int ch1;
	int ch2;

	fp1 = fopen(f1, "r");
	if (fp1 == NULL) {
		return -1;
	}
	fp2 = fopen(f2, "r");
	if (fp2 == NULL) {
		fclose(fp1);
		return -1;
	}

	while (res == 0) {
		ch1 = fgetc(fp1);
		ch2 = fgetc(fp2);

		if (ch1 != ch2) {
			res = 1;
		}

		if (ch1 == EOF) {
			break;
		}
	}

	fclose(fp1);
	fclose(fp2);
	return res;
}

/** option create fixture */
static void nsoption_create(void)
{
	nserror res;

	res = nsoption_init(NULL, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);
}

/** option create fixture for format case */
static void nsoption_format_create(void)
{
	nserror res;

	res = nsoption_init(NULL, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	/* read from file */
	res = nsoption_read(test_choices_path, NULL);
	ck_assert_int_eq(res, NSERROR_OK);
}

/** option teardown fixture */
static void nsoption_teardown(void)
{
	nserror res;

	res = nsoption_finalise(NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);
}


/**
 * Test full options session from start to finish
 */
START_TEST(nsoption_session_test)
{
	nserror res;
	int argc = 2;
	char arg1[] = "nsoption";
	char arg2[] = "--http_proxy_host=fooo";
	char *argv[] = { arg1, arg2, NULL};
	char *outnam;

	res = nsoption_init(gui_options_init_defaults, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	/* read from file */
	res = nsoption_read(test_choices_path, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	/* overlay commandline */
	res = nsoption_commandline(&argc, &argv[0], NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	/* change a string option */
	nsoption_set_charp(http_proxy_host, strdup("bar"));

	/* change an uint option */
	nsoption_set_uint(disc_cache_size, 42);

	/* change a colour */
	nsoption_set_colour(sys_colour_ActiveBorder, 0x00d0000d);

	/* write options out */
	outnam = testnam(NULL);
	res = nsoption_write(outnam, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	/* check for the correct answer */
	ck_assert_int_eq(cmp(outnam, test_choices_full_path), 0);

	/* remove test output */
	unlink(outnam);

	res = nsoption_finalise(NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

}
END_TEST

static TCase *nsoption_session_case_create(void)
{
	TCase *tc;
	tc = tcase_create("Full session");

	tcase_add_test(tc, nsoption_session_test);

	return tc;
}



struct format_test_vec_s {
	int opt_idx;
	const char *res_html;
	const char *res_text;
};

struct format_test_vec_s format_test_vec[] = {
	{
		NSOPTION_http_proxy,
		"<tr><th>http_proxy</th><td>boolean</td><td>default</td><td>false</td></tr>",
		"http_proxy:0"
	},
	{
		NSOPTION_enable_javascript,
		"<tr><th>enable_javascript</th><td>boolean</td><td>user</td><td>true</td></tr>",
		"enable_javascript:1"
	},
	{
		NSOPTION_http_proxy_port,
		"<tr><th>http_proxy_port</th><td>integer</td><td>default</td><td>8080</td></tr>",
		"http_proxy_port:8080"
	},
	{
		NSOPTION_http_proxy_host,
		"<tr><th>http_proxy_host</th><td>string</td><td>default</td><td><span class=\"null-content\">NULL</span></td></tr>",
		"http_proxy_host:"
	},
	{
		NSOPTION_cookie_file,
		"<tr><th>cookie_file</th><td>string</td><td>user</td><td>/home/vince/.netsurf/Cookies</td></tr>",
		"cookie_file:/home/vince/.netsurf/Cookies"
	},
	{
		NSOPTION_disc_cache_size,
		"<tr><th>disc_cache_size</th><td>unsigned integer</td><td>default</td><td>1073741824</td></tr>",
		"disc_cache_size:1073741824"
	},
	{
		NSOPTION_sys_colour_ActiveBorder,
		"<tr><th>sys_colour_ActiveBorder</th><td>colour</td><td>default</td><td><span style=\"background-color: #d3d3d3; color: #000000; font-family:Monospace; \">#D3D3D3</span></td></tr>",
		"sys_colour_ActiveBorder:d3d3d3"
	},
};

/**
 * Test formatting of html output
 */
START_TEST(nsoption_format_html_test)
{
	int ret;
	char buffer[1024];
	struct format_test_vec_s *tst = &format_test_vec[_i];

	ret = nsoption_snoptionf(buffer, sizeof buffer, tst->opt_idx,
		"<tr><th>%k</th><td>%t</td><td>%p</td><td>%V</td></tr>");
	ck_assert_int_gt(ret, 0);
	ck_assert_str_eq(buffer, tst->res_html);
}
END_TEST

/**
 * Test formatting of text output
 */
START_TEST(nsoption_format_text_test)
{
	int ret;
	char buffer[1024];
	struct format_test_vec_s *tst = &format_test_vec[_i];

	ret = nsoption_snoptionf(buffer, sizeof buffer, tst->opt_idx,
				 "%k:%v");
	ck_assert_int_gt(ret, 0);
	ck_assert_str_eq(buffer, tst->res_text);
}
END_TEST

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

static TCase *nsoption_format_case_create(void)
{
	TCase *tc;
	tc = tcase_create("Formatted output");

	/* ensure options are initialised and finalised for every test */
	tcase_add_unchecked_fixture(tc,
				    nsoption_format_create,
				    nsoption_teardown);

	tcase_add_loop_test(tc,
			    nsoption_format_html_test,
			    0, NELEMS(format_test_vec));

	tcase_add_loop_test(tc,
			    nsoption_format_text_test,
			    0, NELEMS(format_test_vec));

	return tc;
}


/**
 * Test dumping option file
 */
START_TEST(nsoption_dump_test)
{
	nserror res;
	char *outnam;
	FILE *fp;

	res = nsoption_read(test_choices_path, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	outnam = testnam(NULL);

	fp = fopen(outnam, "w");
	res = nsoption_dump(fp, NULL);
	fclose(fp);

	ck_assert_int_eq(res, NSERROR_OK);

	ck_assert_int_eq(cmp(outnam, test_choices_all_path), 0);

	unlink(outnam);
}
END_TEST

/**
 * Test writing option file
 */
START_TEST(nsoption_write_test)
{
	nserror res;
	char *outnam;

	res = nsoption_read(test_choices_path, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	outnam = testnam(NULL);

	res = nsoption_write(outnam, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	ck_assert_int_eq(cmp(outnam, test_choices_short_path), 0);

	unlink(outnam);
}
END_TEST

/**
 * Test reading option file
 */
START_TEST(nsoption_read_test)
{
	nserror res;
	res = nsoption_read(test_choices_path, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	ck_assert(nsoption_charp(homepage_url) != NULL);
	ck_assert_str_eq(nsoption_charp(homepage_url), "about:welcome");
}
END_TEST


/**
 * Test reading missing option file
 */
START_TEST(nsoption_read_missing_test)
{
	nserror res;
	res = nsoption_read(test_choices_missing_path, NULL);
	ck_assert_int_eq(res, NSERROR_NOT_FOUND);
}
END_TEST

/**
 * Test commandline string value setting
 */
START_TEST(nsoption_commandline_test)
{
	nserror res;
	int argc = 4;
	char arg1[] = "nsoption";
	char arg2[] = "--http_proxy_host=fooo";
	char arg3[] = "--http_proxy_port";
	char arg4[] = "not-option";
	char *argv[] = { arg1, arg2, arg3, arg4, NULL};

	/* commandline */
	res = nsoption_commandline(&argc, &argv[0], NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	ck_assert(nsoption_charp(http_proxy_host) != NULL);
	ck_assert_str_eq(nsoption_charp(http_proxy_host), "fooo");
}
END_TEST

static TCase *nsoption_case_create(void)
{
	TCase *tc;
	tc = tcase_create("File operations");

	/* ensure options are initialised and finalised for every test */
	tcase_add_unchecked_fixture(tc,
				    nsoption_create,
				    nsoption_teardown);

	tcase_add_test(tc, nsoption_commandline_test);
	tcase_add_test(tc, nsoption_read_test);
	tcase_add_test(tc, nsoption_read_missing_test);
	tcase_add_test(tc, nsoption_write_test);
	tcase_add_test(tc, nsoption_dump_test);

	return tc;
}


/**
 * Test finalisation without init
 */
START_TEST(nsoption_api_fini_no_init_test)
{
	nserror res;

	/* attempt to finalise without init */
	res = nsoption_finalise(NULL, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);
}
END_TEST

/**
 * Test read without path
 */
START_TEST(nsoption_api_read_no_path_test)
{
	nserror res;

	/* read with no path or init */
	res = nsoption_read(NULL, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);
}
END_TEST

/**
 * Test read without init
 */
START_TEST(nsoption_api_read_no_init_test)
{
	nserror res;

	/* read with path but no init */
	res = nsoption_read(test_choices_path, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);
}
END_TEST

/**
 * Test write without path
 */
START_TEST(nsoption_api_write_no_path_test)
{
	nserror res;

	/* write with no path or init */
	res = nsoption_write(NULL, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);
}
END_TEST

/**
 * Test write without init
 */
START_TEST(nsoption_api_write_no_init_test)
{
	nserror res;

	/* write with path but no init */
	res = nsoption_write(test_choices_path, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);

}
END_TEST

/**
 * Test dump without path
 */
START_TEST(nsoption_api_dump_no_path_test)
{
	nserror res;

	/* write with no path or init */
	res = nsoption_dump(NULL, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);
}
END_TEST

/**
 * Test dump without init
 */
START_TEST(nsoption_api_dump_no_init_test)
{
	nserror res;
	FILE *outf;

	outf = tmpfile();
	ck_assert(outf != NULL);

	/* write with path but no init */
	res = nsoption_dump(outf, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);

	fclose(outf);
}
END_TEST

/**
 * Test commandline without args
 */
START_TEST(nsoption_api_commandline_no_args_test)
{
	nserror res;
	int argc = 2;
	char arg1[] = "nsoption";
	char arg2[] = "--http_proxy_host=fooo";
	char *argv[] = { arg1, arg2, NULL};

	/* commandline with no argument count or init */
	res = nsoption_commandline(NULL, &argv[0], NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);

	/* commandline with no argument vector or init */
	res = nsoption_commandline(&argc, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);
}
END_TEST

/**
 * Test commandline without init
 */
START_TEST(nsoption_api_commandline_no_init_test)
{
	nserror res;
	int argc = 2;
	char arg1[] = "nsoption";
	char arg2[] = "--http_proxy_host=fooo";
	char *argv[] = { arg1, arg2, NULL};

	/* write with path but no init */
	res = nsoption_commandline(&argc, &argv[0], NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);
}
END_TEST


/**
 * Test default initialisation and repeated finalisation
 */
START_TEST(nsoption_api_fini_twice_test)
{
	nserror res;
	res = nsoption_init(NULL, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	res = nsoption_finalise(NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	res = nsoption_finalise(NULL, NULL);
	ck_assert_int_eq(res, NSERROR_BAD_PARAMETER);
}
END_TEST


/**
 * Test default initialisation and finalisation
 */
START_TEST(nsoption_api_init_def_test)
{
	nserror res;
	res = nsoption_init(NULL, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);

	res = nsoption_finalise(NULL, NULL);
	ck_assert_int_eq(res, NSERROR_OK);
}
END_TEST

/**
 * Test default initialisation and finalisation with parameters
 */
START_TEST(nsoption_api_init_param_test)
{
	nserror res;
	res = nsoption_init(NULL, &nsoptions, &nsoptions_default);
	ck_assert_int_eq(res, NSERROR_OK);

	res = nsoption_finalise(nsoptions, nsoptions_default);
	ck_assert_int_eq(res, NSERROR_OK);
}
END_TEST

static nserror failing_init_cb(struct nsoption_s *defaults)
{
	return NSERROR_INIT_FAILED;
}

/**
 * Test default initialisation with failing callback
 */
START_TEST(nsoption_api_init_failcb_test)
{
	nserror res;
	res = nsoption_init(failing_init_cb, NULL, NULL);
	ck_assert_int_eq(res, NSERROR_INIT_FAILED);
}
END_TEST

/**
 * Test snoptionf format
 */
START_TEST(nsoption_api_snoptionf_badfmt_test)
{
	int ret;
	ret = nsoption_snoptionf(NULL, 0, -1, NULL);
	ck_assert_int_eq(ret, -1);
}
END_TEST

/**
 * Test snoptionf range
 */
START_TEST(nsoption_api_snoptionf_param_test)
{
	int ret;

	ret = nsoption_snoptionf(NULL, 0, NSOPTION_LISTEND, "");
	ck_assert_int_eq(ret, -1);
}
END_TEST

/**
 * Test snoptionf with no initialisation
 */
START_TEST(nsoption_api_snoptionf_no_init_test)
{
	int ret;
	ret = nsoption_snoptionf(NULL, 0, 0, "");
	ck_assert_int_eq(ret, -1);
}
END_TEST


static TCase *nsoption_api_case_create(void)
{
	TCase *tc;
	tc = tcase_create("API checks");

	tcase_add_test(tc, nsoption_api_fini_no_init_test);
	tcase_add_test(tc, nsoption_api_read_no_path_test);
	tcase_add_test(tc, nsoption_api_read_no_init_test);
	tcase_add_test(tc, nsoption_api_write_no_path_test);
	tcase_add_test(tc, nsoption_api_write_no_init_test);
	tcase_add_test(tc, nsoption_api_dump_no_path_test);
	tcase_add_test(tc, nsoption_api_dump_no_init_test);
	tcase_add_test(tc, nsoption_api_commandline_no_args_test);
	tcase_add_test(tc, nsoption_api_commandline_no_init_test);
	tcase_add_test(tc, nsoption_api_init_def_test);
	tcase_add_test(tc, nsoption_api_fini_twice_test);
	tcase_add_test(tc, nsoption_api_init_param_test);
	tcase_add_test(tc, nsoption_api_init_failcb_test);
	tcase_add_test(tc, nsoption_api_snoptionf_no_init_test);
	tcase_add_test(tc, nsoption_api_snoptionf_badfmt_test);
	tcase_add_test(tc, nsoption_api_snoptionf_param_test);

	return tc;
}


static Suite *nsoption_suite_create(void)
{
	Suite *s;
	s = suite_create("User options");

	suite_add_tcase(s, nsoption_api_case_create());
	suite_add_tcase(s, nsoption_case_create());
	suite_add_tcase(s, nsoption_format_case_create());
	suite_add_tcase(s, nsoption_session_case_create());

	return s;
}

int main(int argc, char **argv)
{
	int number_failed;
	SRunner *sr;

	sr = srunner_create(nsoption_suite_create());

	srunner_run_all(sr, CK_ENV);

	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}