test_parser.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (C) 2014 Mamadou DIOP.
  2. *
  3. * This file is part of Open Source Doubango Framework.
  4. *
  5. * DOUBANGO is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DOUBANGO is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with DOUBANGO.
  17. *
  18. */
  19. #ifndef TEST_TINYBFCP_PARSER_H
  20. #define TEST_TINYBFCP_PARSER_H
  21. #include "tinybfcp.h"
  22. #define SEND_BUFF(buff_ptr, buff_size) \
  23. { \
  24. struct sockaddr_storage addr_to; \
  25. tnet_socket_t* socket = tnet_socket_create(kLocalIP, kLocalPort, tnet_socket_type_udp_ipv4); \
  26. tnet_sockaddr_init(kRemoteIP, kRemotePort, tnet_socket_type_udp_ipv4, &addr_to); \
  27. tnet_sockfd_sendto(socket->fd, (const struct sockaddr *)&addr_to, (buff_ptr), (buff_size)); \
  28. TSK_OBJECT_SAFE_FREE(socket); \
  29. } \
  30. uint8_t __parse_buff_write_ptr[1200];
  31. static const tsk_size_t __parse_buff_write_size = sizeof(__parse_buff_write_ptr)/sizeof(__parse_buff_write_ptr[0]);
  32. uint8_t __parse_buff_read_ptr[1200];
  33. static const tsk_size_t __parse_buff_read_size = sizeof(__parse_buff_read_ptr)/sizeof(__parse_buff_read_ptr[0]);
  34. static int test_parser_buff_cmp(const uint8_t* pc_buf1_ptr, tsk_size_t n_buff1_size, const uint8_t* pc_buf2_ptr, tsk_size_t n_buff2_size)
  35. {
  36. int ret;
  37. tsk_size_t u;
  38. if (!pc_buf1_ptr || !pc_buf2_ptr || (n_buff1_size != n_buff2_size)) {
  39. return -1;
  40. }
  41. for (u = 0; u < n_buff1_size; ++u) {
  42. if ((ret = (pc_buf1_ptr[u] - pc_buf2_ptr[u]))) {
  43. return ret;
  44. }
  45. }
  46. return 0;
  47. }
  48. static int test_parser_add_user_info(tbfcp_pkt_t* p_pkt)
  49. {
  50. tbfcp_attr_octetstring_t* p_attr = tsk_null;
  51. if (!p_pkt) {
  52. TSK_DEBUG_ERROR("Invalid parameter");
  53. return -1;
  54. }
  55. BAIL_IF_ERR(tbfcp_attr_octetstring_create(tbfcp_attribute_type_USER_DISPLAY_NAME, kBfcpFieldMNo, kUserDisplayName, tsk_strlen(kUserDisplayName), &p_attr));
  56. BAIL_IF_ERR(tbfcp_pkt_add_attr(p_pkt, (tbfcp_attr_t**)&p_attr));
  57. BAIL_IF_ERR(tbfcp_attr_octetstring_create(tbfcp_attribute_type_USER_URI, kBfcpFieldMNo, kUserUri, tsk_strlen(kUserUri), &p_attr));
  58. BAIL_IF_ERR(tbfcp_pkt_add_attr(p_pkt, (tbfcp_attr_t**)&p_attr));
  59. return 0;
  60. bail:
  61. TSK_OBJECT_SAFE_FREE(p_attr);
  62. return -1;
  63. }
  64. static void test_parser_Hello()
  65. {
  66. tbfcp_pkt_t* p_pkt = tsk_null;
  67. tsk_size_t n_written_bytes, n_read_bytes;
  68. (n_read_bytes);
  69. BAIL_IF_ERR(tbfcp_pkt_create_Hello(kConfId, kTransacId, kUserId, &p_pkt));
  70. BAIL_IF_ERR(test_parser_add_user_info(p_pkt));
  71. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_write_ptr, __parse_buff_write_size, &n_written_bytes));
  72. //SEND_BUFF(__parse_buff_write_ptr, n_written_bytes);
  73. TSK_OBJECT_SAFE_FREE(p_pkt);
  74. BAIL_IF_ERR(tbfcp_pkt_read(__parse_buff_write_ptr, n_written_bytes, &p_pkt));
  75. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_read_ptr, __parse_buff_read_size, &n_read_bytes));
  76. //SEND_BUFF(__parse_buff_read_ptr, n_read_bytes);
  77. BAIL_IF_ERR(test_parser_buff_cmp(__parse_buff_write_ptr, n_written_bytes, __parse_buff_read_ptr, n_read_bytes));
  78. TSK_DEBUG_INFO("test_parser_Hello...OK");
  79. bail:
  80. TSK_OBJECT_SAFE_FREE(p_pkt);
  81. }
  82. static void test_parser_HelloAck()
  83. {
  84. tbfcp_pkt_t* p_pkt = tsk_null;
  85. tsk_size_t n_written_bytes, n_read_bytes;
  86. (n_read_bytes);
  87. BAIL_IF_ERR(tbfcp_pkt_create_HelloAck_2(kConfId, kTransacId, kUserId, &p_pkt));
  88. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_write_ptr, __parse_buff_write_size, &n_written_bytes));
  89. //SEND_BUFF(__parse_buff_write_ptr, n_written_bytes);
  90. TSK_OBJECT_SAFE_FREE(p_pkt);
  91. BAIL_IF_ERR(tbfcp_pkt_read(__parse_buff_write_ptr, n_written_bytes, &p_pkt));
  92. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_read_ptr, __parse_buff_read_size, &n_read_bytes));
  93. //SEND_BUFF(__parse_buff_read_ptr, n_read_bytes);
  94. BAIL_IF_ERR(test_parser_buff_cmp(__parse_buff_write_ptr, n_written_bytes, __parse_buff_read_ptr, n_read_bytes));
  95. TSK_DEBUG_INFO("test_parser_HelloAck...OK");
  96. bail:
  97. TSK_OBJECT_SAFE_FREE(p_pkt);
  98. }
  99. static void test_parser_FloorRequest()
  100. {
  101. tbfcp_pkt_t* p_pkt = tsk_null;
  102. tsk_size_t n_written_bytes, n_read_bytes;
  103. (n_read_bytes);
  104. BAIL_IF_ERR(tbfcp_pkt_create_FloorRequest_2(kConfId, kTransacId, kUserId, kFloorId, &p_pkt));
  105. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_write_ptr, __parse_buff_write_size, &n_written_bytes));
  106. //SEND_BUFF(__parse_buff_write_ptr, n_written_bytes);
  107. TSK_OBJECT_SAFE_FREE(p_pkt);
  108. BAIL_IF_ERR(tbfcp_pkt_read(__parse_buff_write_ptr, n_written_bytes, &p_pkt));
  109. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_read_ptr, __parse_buff_read_size, &n_read_bytes));
  110. //SEND_BUFF(__parse_buff_read_ptr, n_read_bytes);
  111. BAIL_IF_ERR(test_parser_buff_cmp(__parse_buff_write_ptr, n_written_bytes, __parse_buff_read_ptr, n_read_bytes));
  112. TSK_DEBUG_INFO("test_parser_FloorRequest...OK");
  113. bail:
  114. TSK_OBJECT_SAFE_FREE(p_pkt);
  115. }
  116. static void test_parser_FloorRequestStatus()
  117. {
  118. tbfcp_pkt_t* p_pkt = tsk_null;
  119. tbfcp_attr_grouped_t* p_floor_req_info = tsk_null;
  120. tbfcp_attr_grouped_t* p_overal_req_status = tsk_null;
  121. tbfcp_attr_octetstring16_t* p_req_status = tsk_null;
  122. tbfcp_attr_grouped_t* p_floor_req_status = tsk_null;
  123. tsk_size_t n_written_bytes, n_read_bytes;
  124. static uint8_t ReqStatusOctesString16[2] = { kReqStatus, kQueuePosition };
  125. (n_read_bytes);
  126. // PKT$FLOOR_REQUEST_INFORMATION"OVERALL_REQUEST_STATUS'REQUEST_STATUS',FLOOR_REQUEST_STATUS"$
  127. BAIL_IF_ERR(tbfcp_pkt_create_FloorRequestStatus(kConfId, kTransacId, kUserId, &p_pkt));
  128. BAIL_IF_ERR(tbfcp_attr_grouped_create_u16(tbfcp_attribute_type_FLOOR_REQUEST_INFORMATION, kBfcpFieldMNo, kFloorReqId, &p_floor_req_info));
  129. BAIL_IF_ERR(tbfcp_attr_grouped_create_u16(tbfcp_attribute_type_OVERALL_REQUEST_STATUS, kBfcpFieldMNo, kFloorReqId, &p_overal_req_status));
  130. BAIL_IF_ERR(tbfcp_attr_octetstring16_create(tbfcp_attribute_type_REQUEST_STATUS, kBfcpFieldMNo, ReqStatusOctesString16, &p_req_status));
  131. BAIL_IF_ERR(tbfcp_attr_grouped_add_attr(p_overal_req_status, (tbfcp_attr_t**)&p_req_status));
  132. BAIL_IF_ERR(tbfcp_attr_grouped_add_attr(p_floor_req_info,(tbfcp_attr_t**) &p_overal_req_status));
  133. BAIL_IF_ERR(tbfcp_attr_grouped_create_u16(tbfcp_attribute_type_FLOOR_REQUEST_STATUS, kBfcpFieldMNo, kFloorId, &p_floor_req_status));
  134. BAIL_IF_ERR(tbfcp_attr_grouped_add_attr(p_floor_req_info, (tbfcp_attr_t**)&p_floor_req_status));
  135. BAIL_IF_ERR(tbfcp_pkt_add_attr(p_pkt, (tbfcp_attr_t**)&p_floor_req_info));
  136. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_write_ptr, __parse_buff_write_size, &n_written_bytes));
  137. //SEND_BUFF(__parse_buff_write_ptr, n_written_bytes);
  138. TSK_OBJECT_SAFE_FREE(p_pkt);
  139. BAIL_IF_ERR(tbfcp_pkt_read(__parse_buff_write_ptr, n_written_bytes, &p_pkt));
  140. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_read_ptr, __parse_buff_read_size, &n_read_bytes));
  141. //SEND_BUFF(__parse_buff_read_ptr, n_read_bytes);
  142. BAIL_IF_ERR(test_parser_buff_cmp(__parse_buff_write_ptr, n_written_bytes, __parse_buff_read_ptr, n_read_bytes));
  143. TSK_DEBUG_INFO("test_parser_FloorRequestStatus...OK");
  144. bail:
  145. TSK_OBJECT_SAFE_FREE(p_pkt);
  146. TSK_OBJECT_SAFE_FREE(p_floor_req_info);
  147. TSK_OBJECT_SAFE_FREE(p_overal_req_status);
  148. TSK_OBJECT_SAFE_FREE(p_req_status);
  149. TSK_OBJECT_SAFE_FREE(p_floor_req_status);
  150. }
  151. static void test_parser_FloorRelease()
  152. {
  153. tbfcp_pkt_t* p_pkt = tsk_null;
  154. tsk_size_t n_written_bytes, n_read_bytes;
  155. BAIL_IF_ERR(tbfcp_pkt_create_FloorRelease_2(kConfId, kTransacId, kUserId, kFloorReqId, &p_pkt));
  156. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_write_ptr, __parse_buff_write_size, &n_written_bytes));
  157. //SEND_BUFF(__parse_buff_write_ptr, n_written_bytes);
  158. TSK_OBJECT_SAFE_FREE(p_pkt);
  159. BAIL_IF_ERR(tbfcp_pkt_read(__parse_buff_write_ptr, n_written_bytes, &p_pkt));
  160. BAIL_IF_ERR(tbfcp_pkt_write_with_padding(p_pkt, __parse_buff_read_ptr, __parse_buff_read_size, &n_read_bytes));
  161. //SEND_BUFF(__parse_buff_read_ptr, n_read_bytes);
  162. BAIL_IF_ERR(test_parser_buff_cmp(__parse_buff_write_ptr, n_written_bytes, __parse_buff_read_ptr, n_read_bytes));
  163. TSK_DEBUG_INFO("test_parser_FloorRelease...OK");
  164. bail:
  165. TSK_OBJECT_SAFE_FREE(p_pkt);
  166. }
  167. static void test_parser()
  168. {
  169. test_parser_Hello();
  170. test_parser_HelloAck();
  171. test_parser_FloorRequest();
  172. test_parser_FloorRequestStatus();
  173. test_parser_FloorRelease();
  174. }
  175. #endif /* TEST_TINYBFCP_PARSER_H */