test_uri.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #ifndef _TEST_SIPURI_H
  23. #define _TEST_SIPURI_H
  24. const char* uris[] = {
  25. //== SIP:
  26. "sip:123.com",
  27. "sip:open-ims.test",
  28. "sip:pcscf.open-ims.test:4060;lr;transport=udp",
  29. "sip:2233392625@sip2sip.info",
  30. "sip:alice@iatlanta.com;p1=23",
  31. "sip:*666*@atlanta.com",
  32. "sip:#66#@atlanta.com", // should fail: # must be replaced with %23
  33. "sip:alice:secretword@atlanta.com",
  34. "sip:alice:secretword@atlanta.com:65535;transport=tcp",
  35. "sip:+1-212-555-1212:1234@gateway.com;user=phone",
  36. "sip:alice@192.0.2.4:5060",
  37. "sip:alice@[1111::aaa:bbb:ccc:ddd]:5060",
  38. "sip:atlanta.com",
  39. "sip:alice@[1111::aaa:bbb:ccc:ddd]",
  40. "sip:alice@[1111::aaa:bbb:ccc:ddd]:5060;user=phone",
  41. "sip:alice@1111::aaa:bbb:ccc:ddd", // should fail
  42. "sip:alice@[::127]",
  43. "sip:ss2.biloxi.example.com;lr",// FIXME
  44. "sip:atlanta.com;method=REGISTER?to=alice%40atlanta.com",
  45. "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15",
  46. "sip:alice@atlanta.com;comp=sigcomp",
  47. "sip:atlanta.com;method=REGISTER?to=alice%40atlanta.com",
  48. "sip:alice@atlanta.com?subject=project%20x&priority=urgent",
  49. //== SIPS:
  50. "sips:alice@atlanta.com",
  51. "sips:alice:secretword@atlanta.com;transport=tcp",
  52. "sips:+1-212-555-1212:1234@gateway.com;user=phone",
  53. "sips:alice@192.0.2.4",
  54. "sips:atlanta.com;method=REGISTER?to=alice%40atlanta.com",
  55. "sips:alice@atlanta.com;maddr=239.255.255.1;ttl=15",
  56. "sips:alice@atlanta.com;comp=sigcomp",
  57. "sips:atlanta.com;method=REGISTER?to=alice%40atlanta.com",
  58. "sips:alice@atlanta.com?subject=project%20x&priority=urgent",
  59. //== TEL:
  60. "tel:+1-201-555-0123",
  61. "tel:7042;phone-context=example.com;ff=ff",
  62. "tel:863-1234;phone-context=+1-914-555",
  63. "tel:#666#",
  64. };
  65. void test_uri_tostring(const tsip_uri_t *uri)
  66. {
  67. char* ret = tsip_uri_tostring(uri, 1, 1);
  68. TSK_DEBUG_INFO("uri_to_string=%s", ret);
  69. TSK_FREE(ret);
  70. }
  71. void test_uri_parser()
  72. {
  73. int i;
  74. tsk_list_item_t *item = 0;
  75. for(i=0; i<sizeof(uris)/sizeof(const char*); i++) {
  76. tsip_uri_t *uri = tsip_uri_parse(uris[i], tsk_strlen(uris[i]));
  77. printf("\n== Parsing {{ %s }} ==\n\n", uris[i]);
  78. if(uri) {
  79. printf("scheme: %s\n", uri->scheme);
  80. printf("user-name: %s\n", uri->user_name);
  81. printf("password: %s\n", uri->password);
  82. printf("host: %s\n", uri->host);
  83. printf("port: %d\n", uri->port);
  84. printf("host-type: %s\n", uri->host_type == host_ipv4 ? "IPv4" : (uri->host_type == host_ipv6 ? "IPv6" : (uri->host_type == host_hostname ? "HOSTNAME" : "UNKNOWN")) );
  85. printf("---PARAMS---\n");
  86. /* dump all parameters */
  87. tsk_list_foreach(item, uri->params) {
  88. tsk_param_t* param = item->data;
  89. printf("-->%s=%s\n", param->name, param->value);
  90. }
  91. printf("Is-secure: %s\n", TSIP_URI_IS_SECURE(uri) ? "YES" : "NO");
  92. test_uri_tostring(uri);
  93. }
  94. else {
  95. printf("INVALID SIP URI.\n");
  96. }
  97. printf("\n\n");
  98. getchar();
  99. TSK_OBJECT_SAFE_FREE(uri);
  100. }
  101. }
  102. struct test_uri_bundle {
  103. const char* uri1;
  104. const char* uri2;
  105. unsigned match:1;
  106. };
  107. // From RFC 3261 - 19.1.4 URI Comparison
  108. struct test_uri_bundle test_uri_bundles[] = {
  109. /* Match */
  110. { "sip:%61lice@atlanta.com;transport=TCP", "sip:alice@AtLanTa.CoM;Transport=tcp", 1 },
  111. { "sip:carol@chicago.com", "sip:carol@chicago.com;newparam=5", 1 },
  112. { "sip:carol@chicago.com", "sip:carol@chicago.com;security=on", 1 },
  113. { "sip:carol@chicago.com;newparam=5", "sip:carol@chicago.com;security=on", 1 },
  114. { "sip:biloxi.com;transport=tcp;method=REGISTER?to=sip:bob%40biloxi.com", "sip:biloxi.com;method=REGISTER;transport=tcp?to=sip:bob%40biloxi.com", 1 },
  115. { "sip:alice@atlanta.com?subject=project%20x&priority=urgent", "sip:alice@atlanta.com?priority=urgent&subject=project%20x", 1 },
  116. /* Do not match */
  117. { "SIP:ALICE@AtLanTa.CoM;Transport=udp", "sip:alice@AtLanTa.CoM;Transport=UDP", 0 }, /* different usernames */
  118. { "sip:bob@biloxi.com", "sip:bob@biloxi.com:5060", 0 }, /* can resolve to different ports */
  119. { "sip:bob@biloxi.com", "sip:bob@biloxi.com;transport=udp", 0 }, /* can resolve to different transports */
  120. { "sip:bob@biloxi.com", "sip:bob@biloxi.com:6000;transport=tcp", 0 }, /* can resolve to different port and transports */
  121. { "sip:carol@chicago.com", "sip:carol@chicago.com?Subject=next%20meeting", 0 }, /* different header component */
  122. { "sip:bob@phone21.boxesbybob.com", "sip:bob@192.0.2.4", 0 }, /* even though that's what phone21.boxesbybob.com resolves to*/
  123. };
  124. void test_uri_cmp()
  125. {
  126. size_t i;
  127. for(i=0; i< sizeof(test_uri_bundles)/sizeof(struct test_uri_bundle); i++) {
  128. tsip_uri_t *uri1 = tsip_uri_parse(test_uri_bundles[i].uri1, tsk_strlen(test_uri_bundles[i].uri1));
  129. tsip_uri_t *uri2 = tsip_uri_parse(test_uri_bundles[i].uri2, tsk_strlen(test_uri_bundles[i].uri2));
  130. if(tsk_object_cmp(uri1, uri2) && test_uri_bundles[i].match) {
  131. TSK_DEBUG_ERROR("URI Comparison failed.");
  132. }
  133. else {
  134. TSK_DEBUG_INFO("URI Comparison ok.");
  135. }
  136. TSK_OBJECT_SAFE_FREE(uri1);
  137. TSK_OBJECT_SAFE_FREE(uri2);
  138. }
  139. }
  140. void test_uri()
  141. {
  142. test_uri_parser();
  143. test_uri_cmp();
  144. }
  145. #endif /* _TEST_SIPURI_H */