Review Board 1.7.16


Various unit test API updates

Review Request #493 - Created Feb. 8, 2010 and submitted

Russell Bryant
trunk
Reviewers
asterisk-dev
dvossel
Asterisk
This is a bit of a mix of changes to the test API that were a part of what I worked on during part of a long flight.  Here is a breakdown of changes included here:

1) It occurred to me that the difference in usage between the error ast_str and the ast_test_update_status() usage has turned out to be a bit ambiguous in practice.  In a lot of cases, the same message was being sent to both.  In other cases, it was only sent to one or the other.  My opinion now is that in every case, I think it makes sense to do both; we should output it to the CLI as well as save it off for logging purposes.

This change results in most of the changes in this diff, since it required changes to all existing unit tests.  It also allowed for some simplifications of unit test API implementation code.

2) Update ast_test_status_update() to include the file, function, and line number for the code providing the update.

3) There are some formatting tweaks here and there.  Hopefully they aren't too distracting for code review purposes.  Reviewboard's diff viewer seems to do a pretty good job of pointing out when something is a whitespace change.

4) I moved the md5_test and sha1_test into the test_utils module.  It seemed like a better approach since these tests are so tiny.

5) I changed the number of nodes used in heap_test_2 from 1 million to 100 thousand.  The only reason for this was to reduce the time it took for this test to run.

6) Remove an unused function prototype that was at the bottom of utils.h.

7) Simplify test_insert() using the LIST_INSERT_SORTALPHA() macro.  The one minor difference in behavior is that it no longer checks for a test registered with the same name.

8) Expand the code in test_alloc() to provide specific error messages for each failure case, to clearly inform developers if they forget to set the name, summary, description, etc.

9) Tweak the output of the "test show registered" CLI command.  I swapped the name and category to have the category first.  It seemed more natural since that is the sort key.

10) Don't output the status ast_str in the "test show results" CLI command.  This is going to tend to be pretty verbose, so just leave that for the detailed test logs (test generate results).

I have run the tests in the test suite to make sure they all still pass.  I have also run through the CLI commands to verify that I get the expected output.
/trunk/channels/sip/config_parser.c
Revision 245626 New Change
[20] 260 lines
[+20] [+] AST_TEST_DEFINE(sip_parse_register_line_test)
261
		reg->configured_expiry != default_expiry ||
261
		reg->configured_expiry != default_expiry ||
262
		reg->portno != STANDARD_SIP_PORT    ||
262
		reg->portno != STANDARD_SIP_PORT    ||
263
		reg->callid_valid != FALSE          ||
263
		reg->callid_valid != FALSE          ||
264
		reg->ocseq != INITIAL_CSEQ) {
264
		reg->ocseq != INITIAL_CSEQ) {
265

    
   
265

   
266
		ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple config failed\n");
266
		ast_test_status_update(test, "Test 1: simple config failed\n");
267
		res = AST_TEST_FAIL;
267
		res = AST_TEST_FAIL;
268
	}
268
	}
269
	ast_string_field_free_memory(reg);
269
	ast_string_field_free_memory(reg);
270
	ast_free(reg);
270
	ast_free(reg);
271

    
   
271

   
[+20] [20] 16 lines
[+20] AST_TEST_DEFINE(sip_parse_register_line_test)
288
		reg->configured_expiry != default_expiry ||
288
		reg->configured_expiry != default_expiry ||
289
		reg->portno != STANDARD_SIP_PORT    ||
289
		reg->portno != STANDARD_SIP_PORT    ||
290
		reg->callid_valid != FALSE          ||
290
		reg->callid_valid != FALSE          ||
291
		reg->ocseq != INITIAL_CSEQ) {
291
		reg->ocseq != INITIAL_CSEQ) {
292

    
   
292

   
293
		ast_str_append(&args->ast_test_error_str, 0, "Test 2: add secret failed\n");
293
		ast_test_status_update(test,  "Test 2: add secret failed\n");
294
		res = AST_TEST_FAIL;
294
		res = AST_TEST_FAIL;
295
	}
295
	}
296
	ast_string_field_free_memory(reg);
296
	ast_string_field_free_memory(reg);
297
	ast_free(reg);
297
	ast_free(reg);
298

    
   
298

   
[+20] [20] 16 lines
[+20] AST_TEST_DEFINE(sip_parse_register_line_test)
315
		reg->configured_expiry != default_expiry ||
315
		reg->configured_expiry != default_expiry ||
316
		reg->portno != STANDARD_SIP_PORT    ||
316
		reg->portno != STANDARD_SIP_PORT    ||
317
		reg->callid_valid != FALSE          ||
317
		reg->callid_valid != FALSE          ||
318
		reg->ocseq != INITIAL_CSEQ) {
318
		reg->ocseq != INITIAL_CSEQ) {
319

    
   
319

   
320
		ast_str_append(&args->ast_test_error_str, 0, "Test 3: add userdomain and authuser failed\n");
320
		ast_test_status_update(test, "Test 3: add userdomain and authuser failed\n");
321
		res = AST_TEST_FAIL;
321
		res = AST_TEST_FAIL;
322
	}
322
	}
323
	ast_string_field_free_memory(reg);
323
	ast_string_field_free_memory(reg);
324
	ast_free(reg);
324
	ast_free(reg);
325

    
   
325

   
[+20] [20] 16 lines
[+20] AST_TEST_DEFINE(sip_parse_register_line_test)
342
		reg->configured_expiry != default_expiry ||
342
		reg->configured_expiry != default_expiry ||
343
		reg->portno != STANDARD_SIP_PORT    ||
343
		reg->portno != STANDARD_SIP_PORT    ||
344
		reg->callid_valid != FALSE          ||
344
		reg->callid_valid != FALSE          ||
345
		reg->ocseq != INITIAL_CSEQ) {
345
		reg->ocseq != INITIAL_CSEQ) {
346

    
   
346

   
347
		ast_str_append(&args->ast_test_error_str, 0, "Test 4: add callback extension failed\n");
347
		ast_test_status_update(test, "Test 4: add callback extension failed\n");
348
		res = AST_TEST_FAIL;
348
		res = AST_TEST_FAIL;
349
	}
349
	}
350
	ast_string_field_free_memory(reg);
350
	ast_string_field_free_memory(reg);
351
	ast_free(reg);
351
	ast_free(reg);
352

    
   
352

   
[+20] [20] 16 lines
[+20] AST_TEST_DEFINE(sip_parse_register_line_test)
369
		reg->configured_expiry != default_expiry ||
369
		reg->configured_expiry != default_expiry ||
370
		reg->portno != STANDARD_SIP_PORT    ||
370
		reg->portno != STANDARD_SIP_PORT    ||
371
		reg->callid_valid != FALSE          ||
371
		reg->callid_valid != FALSE          ||
372
		reg->ocseq != INITIAL_CSEQ) {
372
		reg->ocseq != INITIAL_CSEQ) {
373

    
   
373

   
374
		ast_str_append(&args->ast_test_error_str, 0, "Test 5: add transport failed\n");
374
		ast_test_status_update(test, "Test 5: add transport failed\n");
375
		res = AST_TEST_FAIL;
375
		res = AST_TEST_FAIL;
376
	}
376
	}
377
	ast_string_field_free_memory(reg);
377
	ast_string_field_free_memory(reg);
378
	ast_free(reg);
378
	ast_free(reg);
379

    
   
379

   
[+20] [20] 16 lines
[+20] AST_TEST_DEFINE(sip_parse_register_line_test)
396
		reg->configured_expiry != 111 ||
396
		reg->configured_expiry != 111 ||
397
		reg->portno != STANDARD_TLS_PORT    ||
397
		reg->portno != STANDARD_TLS_PORT    ||
398
		reg->callid_valid != FALSE          ||
398
		reg->callid_valid != FALSE          ||
399
		reg->ocseq != INITIAL_CSEQ) {
399
		reg->ocseq != INITIAL_CSEQ) {
400

    
   
400

   
401
		ast_str_append(&args->ast_test_error_str, 0, "Test 6: change to tls transport and add expiry failed\n");
401
		ast_test_status_update(test, "Test 6: change to tls transport and add expiry failed\n");
402
		res = AST_TEST_FAIL;
402
		res = AST_TEST_FAIL;
403
	}
403
	}
404
	ast_string_field_free_memory(reg);
404
	ast_string_field_free_memory(reg);
405
	ast_free(reg);
405
	ast_free(reg);
406

    
   
406

   
[+20] [20] 16 lines
[+20] AST_TEST_DEFINE(sip_parse_register_line_test)
423
		reg->configured_expiry != 111 ||
423
		reg->configured_expiry != 111 ||
424
		reg->portno != 1234    ||
424
		reg->portno != 1234    ||
425
		reg->callid_valid != FALSE          ||
425
		reg->callid_valid != FALSE          ||
426
		reg->ocseq != INITIAL_CSEQ) {
426
		reg->ocseq != INITIAL_CSEQ) {
427

    
   
427

   
428
		ast_str_append(&args->ast_test_error_str, 0, "Test 7, change transport to tcp, add custom port, and add peer failed.\n");
428
		ast_test_status_update(test, "Test 7, change transport to tcp, add custom port, and add peer failed.\n");
429
		res = AST_TEST_FAIL;
429
		res = AST_TEST_FAIL;
430
	}
430
	}
431
	ast_string_field_free_memory(reg);
431
	ast_string_field_free_memory(reg);
432
	ast_free(reg);
432
	ast_free(reg);
433

    
   
433

   
[+20] [20] 16 lines
[+20] AST_TEST_DEFINE(sip_parse_register_line_test)
450
		reg->configured_expiry != 111 ||
450
		reg->configured_expiry != 111 ||
451
		reg->portno != 1234    ||
451
		reg->portno != 1234    ||
452
		reg->callid_valid != FALSE          ||
452
		reg->callid_valid != FALSE          ||
453
		reg->ocseq != INITIAL_CSEQ) {
453
		reg->ocseq != INITIAL_CSEQ) {
454

    
   
454

   
455
		ast_str_append(&args->ast_test_error_str, 0, "Test 8, remove transport failed.\n");
455
		ast_test_status_update(test, "Test 8, remove transport failed.\n");
456
		res = AST_TEST_FAIL;
456
		res = AST_TEST_FAIL;
457
	}
457
	}
458
	ast_string_field_free_memory(reg);
458
	ast_string_field_free_memory(reg);
459
	ast_free(reg);
459
	ast_free(reg);
460

    
   
460

   
461
	/* ---Test reg 9, missing domain, expected to fail --- */
461
	/* ---Test reg 9, missing domain, expected to fail --- */
462
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
462
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
463
		goto alloc_fail;
463
		goto alloc_fail;
464
	} else if (!sip_parse_register_line(reg, default_expiry, reg9, 1)) {
464
	} else if (!sip_parse_register_line(reg, default_expiry, reg9, 1)) {
465

    
   
465
		ast_test_status_update(test,
466
		ast_str_append(&args->ast_test_error_str, 0, "Test 9, missing domain, expected to fail but did not.\n");
466
				"Test 9, missing domain, expected to fail but did not.\n");
467
		res = AST_TEST_FAIL;
467
		res = AST_TEST_FAIL;
468
	}
468
	}
469
	ast_string_field_free_memory(reg);
469
	ast_string_field_free_memory(reg);
470
	ast_free(reg);
470
	ast_free(reg);
471

    
   
471

   
472
	/* ---Test reg 10,  missing user, expected to fail --- */
472
	/* ---Test reg 10,  missing user, expected to fail --- */
473
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
473
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
474
		goto alloc_fail;
474
		goto alloc_fail;
475
	} else if (!sip_parse_register_line(reg, default_expiry, reg10, 1)) {
475
	} else if (!sip_parse_register_line(reg, default_expiry, reg10, 1)) {
476

    
   
476
		ast_test_status_update(test,
477
		ast_str_append(&args->ast_test_error_str, 0, "Test 10, missing user expected to fail but did not\n");
477
				"Test 10, missing user expected to fail but did not\n");
478
		res = AST_TEST_FAIL;
478
		res = AST_TEST_FAIL;
479
	}
479
	}
480
	ast_string_field_free_memory(reg);
480
	ast_string_field_free_memory(reg);
481
	ast_free(reg);
481
	ast_free(reg);
482

    
   
482

   
483
	/* ---Test reg 11, no registry object, expected to fail--- */
483
	/* ---Test reg 11, no registry object, expected to fail--- */
484
	if (!sip_parse_register_line(NULL, default_expiry, reg1, 1)) {
484
	if (!sip_parse_register_line(NULL, default_expiry, reg1, 1)) {
485

    
   
485
		ast_test_status_update(test,
486
		ast_str_append(&args->ast_test_error_str, 0, "Test 11, no registery object, expected to fail but did not.\n");
486
				"Test 11, no registery object, expected to fail but did not.\n");
487
		res = AST_TEST_FAIL;
487
		res = AST_TEST_FAIL;
488
	}
488
	}
489

    
   
489

   
490
	/* ---Test reg 11,  no registry line, expected to fail --- */
490
	/* ---Test reg 11,  no registry line, expected to fail --- */
491
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
491
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
492
		goto alloc_fail;
492
		goto alloc_fail;
493
	} else if (!sip_parse_register_line(reg, default_expiry, NULL, 1)) {
493
	} else if (!sip_parse_register_line(reg, default_expiry, NULL, 1)) {
494

    
   
494

   
495
		ast_str_append(&args->ast_test_error_str, 0, "Test 11, NULL register line expected to fail but did not.\n");
495
		ast_test_status_update(test,

    
   
496
				"Test 11, NULL register line expected to fail but did not.\n");
496
		res = AST_TEST_FAIL;
497
		res = AST_TEST_FAIL;
497
	}
498
	}
498
	ast_string_field_free_memory(reg);
499
	ast_string_field_free_memory(reg);
499
	ast_free(reg);
500
	ast_free(reg);
500

    
   
501

   
501

    
   
502

   
502
	return res;
503
	return res;
503

    
   
504

   
504
alloc_fail:
505
alloc_fail:
505
	ast_str_set(&args->ast_test_error_str, 0, "Out of memory. \n");
506
	ast_test_status_update(test, "Out of memory. \n");
506
	return res;
507
	return res;
507
}
508
}
508

    
   
509

   
509
int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport)
510
int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport)
510
{
511
{
[+20] [20] 71 lines
[+20] [+] AST_TEST_DEFINE(sip_parse_host_line_test)
582
	/* test 1, simple host */
583
	/* test 1, simple host */
583
	sip_parse_host(host1, 1, &host, &port, &transport);
584
	sip_parse_host(host1, 1, &host, &port, &transport);
584
	if (port != STANDARD_SIP_PORT ||
585
	if (port != STANDARD_SIP_PORT ||
585
		ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
586
			ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
586
		transport != SIP_TRANSPORT_UDP) {
587
			transport != SIP_TRANSPORT_UDP) {
587

    
   
588
		ast_test_status_update(test, "Test 1: simple host failed.\n");
588
		ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple host failed.\n");

   
589
		res = AST_TEST_FAIL;
589
		res = AST_TEST_FAIL;
590
	}
590
	}
591

    
   
591

   
592
	/* test 2, add tcp transport */
592
	/* test 2, add tcp transport */
593
	sip_parse_host(host2, 1, &host, &port, &transport);
593
	sip_parse_host(host2, 1, &host, &port, &transport);
594
	if (port != STANDARD_SIP_PORT ||
594
	if (port != STANDARD_SIP_PORT ||
595
		ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
595
			ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
596
		transport != SIP_TRANSPORT_TCP) {
596
			transport != SIP_TRANSPORT_TCP) {
597

    
   
597
		ast_test_status_update(test, "Test 2: tcp host failed.\n");
598
		ast_str_append(&args->ast_test_error_str, 0, "Test 2: tcp host failed.\n");

   
599
		res = AST_TEST_FAIL;
598
		res = AST_TEST_FAIL;
600
	}
599
	}
601

    
   
600

   
602
	/* test 3, add tls transport */
601
	/* test 3, add tls transport */
603
	sip_parse_host(host3, 1, &host, &port, &transport);
602
	sip_parse_host(host3, 1, &host, &port, &transport);
604
	if (port != STANDARD_TLS_PORT ||
603
	if (port != STANDARD_TLS_PORT ||
605
		ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
604
			ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
606
		transport != SIP_TRANSPORT_TLS) {
605
			transport != SIP_TRANSPORT_TLS) {
607

    
   
606
		ast_test_status_update(test, "Test 3: tls host failed. \n");
608
		ast_str_append(&args->ast_test_error_str, 0, "Test 3: tls host failed. \n");

   
609
		res = AST_TEST_FAIL;
607
		res = AST_TEST_FAIL;
610
	}
608
	}
611

    
   
609

   
612
	/* test 4, add custom port with tls */
610
	/* test 4, add custom port with tls */
613
	sip_parse_host(host4, 1, &host, &port, &transport);
611
	sip_parse_host(host4, 1, &host, &port, &transport);
614
	if (port != 1234 ||
612
	if (port != 1234 || ast_strlen_zero(host) ||
615
		ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
613
			strcmp(host, "10.10.10.10") ||
616
		transport != SIP_TRANSPORT_TLS) {
614
			transport != SIP_TRANSPORT_TLS) {
617

    
   
615
		ast_test_status_update(test, "Test 4: tls host with custom port failed.\n");
618
		ast_str_append(&args->ast_test_error_str, 0, "Test 4: tls host with custom port failed.\n");

   
619
		res = AST_TEST_FAIL;
616
		res = AST_TEST_FAIL;
620
	}
617
	}
621

    
   
618

   
622
	/* test 5, simple host with custom port */
619
	/* test 5, simple host with custom port */
623
	sip_parse_host(host5, 1, &host, &port, &transport);
620
	sip_parse_host(host5, 1, &host, &port, &transport);
624
	if (port != 1234 ||
621
	if (port != 1234 || ast_strlen_zero(host) ||
625
		ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
622
			strcmp(host, "10.10.10.10") ||
626
		transport != SIP_TRANSPORT_UDP) {
623
			transport != SIP_TRANSPORT_UDP) {
627

    
   
624
		ast_test_status_update(test, "Test 5: simple host with custom port failed.\n");
628
		ast_str_append(&args->ast_test_error_str, 0, "Test 5: simple host with custom port failed.\n");

   
629
		res = AST_TEST_FAIL;
625
		res = AST_TEST_FAIL;
630
	}
626
	}
631
	return res;

   
632

    
   
627

   
633
	/* test 6, expected failure with NULL input */
628
	/* test 6, expected failure with NULL input */
634
	if (sip_parse_host(NULL, 1, &host, &port, &transport)) {
629
	if (!sip_parse_host(NULL, 1, &host, &port, &transport)) {
635

    
   
630
		ast_test_status_update(test, "Test 6: expected error on NULL input did not occur.\n");
636
		ast_str_append(&args->ast_test_error_str, 0, "Test 6: expected error on NULL input did not occur.\n");

   
637
		res = AST_TEST_FAIL;
631
		res = AST_TEST_FAIL;
638
	}
632
	}

    
   
633

   
639
	return res;
634
	return res;
640

    
   
635

   
641
}
636
}
642

    
   
637

   
643
/*! \brief SIP test registration */
638
/*! \brief SIP test registration */
[+20] [20] 13 lines
/trunk/channels/sip/reqresp_parser.c
Revision 245626 New Change
 
/trunk/funcs/func_math.c
Revision 245626 New Change
 
/trunk/include/asterisk/test.h
Revision 245626 New Change
 
/trunk/include/asterisk/utils.h
Revision 245626 New Change
 
/trunk/main/test.c
Revision 245626 New Change
 
/trunk/tests/test_ast_format_str_reduce.c
Revision 245626 New Change
 
/trunk/tests/test_heap.c
Revision 245626 New Change
 
/trunk/tests/test_md5.c
Revision 245626 New Change
 
/trunk/tests/test_sched.c
Revision 245626 New Change
 
/trunk/tests/test_sha1.c
Revision 245626 New Change
 
/trunk/tests/test_skel.c
Revision 245626 New Change
 
/trunk/tests/test_substitution.c
Revision 245626 New Change
 
/trunk/tests/test_utils.c
Revision 245626 New Change
 
  1. /trunk/channels/sip/config_parser.c: Loading...
  2. /trunk/channels/sip/reqresp_parser.c: Loading...
  3. /trunk/funcs/func_math.c: Loading...
  4. /trunk/include/asterisk/test.h: Loading...
  5. /trunk/include/asterisk/utils.h: Loading...
  6. /trunk/main/test.c: Loading...
  7. /trunk/tests/test_ast_format_str_reduce.c: Loading...
  8. /trunk/tests/test_heap.c: Loading...
  9. /trunk/tests/test_md5.c: Loading...
  10. /trunk/tests/test_sched.c: Loading...
  11. /trunk/tests/test_sha1.c: Loading...
  12. /trunk/tests/test_skel.c: Loading...
  13. /trunk/tests/test_substitution.c: Loading...
  14. /trunk/tests/test_utils.c: Loading...

https://reviewboard.asterisk.org/ runs on a server provided by Digium, Inc. and uses bandwidth donated to the open source Asterisk community by API Digital Communications in Huntsville, AL USA.
Please report problems with this site to asteriskteam@digium.com.