HeaderRemovalTests.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2017 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "HeaderRemovalTests.h"
  30. #include "TestsUtils.h"
  31. #include <stdio.h>
  32. /////////////////////////////////////////////////////////////////////////////////
  33. static const unsigned int HEADER_REMOVAL_TEST_MAX_PACKET_BYTE_SIZE = 1024;
  34. /////////////////////////////////////////////////////////////////////////////////
  35. /////////////////////////////////////////////////////////////////////////////////
  36. /////////////////////////////////////////////////////////////////////////////////
  37. /////////////////////////////////////////////////////////////////////////////////
  38. HeaderRemovalTOSCheck::HeaderRemovalTOSCheck()
  39. {
  40. m_name = "HeaderRemovalTOSCheck";
  41. m_description = "HeaderRemovalTOSCheck: Remove the header from the A2NDUN pipe and check the TOS field of the IP packet";
  42. }
  43. /////////////////////////////////////////////////////////////////////////////////
  44. bool HeaderRemovalTOSCheck::Run()
  45. {
  46. bool bTestResult = true;
  47. Byte pPacketReceiveBuffer[HEADER_REMOVAL_TEST_MAX_PACKET_BYTE_SIZE] = {0};//This buffer will be used in order to store the received packet.
  48. unsigned int nMagicNumber = 0x12345678; //arbitrary number
  49. unsigned int nChannelID = 0xABCD;//arbitrary number
  50. unsigned int nA2NDUNPacketByteSize = 0;
  51. unsigned int nBytesSent = 0;
  52. int nBytesReceived = 0;
  53. Byte *pA2NDUNPacket = CreateA2NDUNPacket(nMagicNumber, nChannelID, IPV4_FILE_PATH, &nA2NDUNPacketByteSize);
  54. if(0 == pA2NDUNPacket) {
  55. LOG_MSG_ERROR("Cannot load file to memory, exiting");
  56. return false;
  57. }
  58. LOG_MSG_INFO("A2 Packet was successfully created (%d bytes)", nA2NDUNPacketByteSize);
  59. if ( false == SetIPATablesToPassAllToSpecificClient(IPA_CLIENT_TEST2_PROD, IPA_CLIENT_TEST_CONS)) {
  60. LOG_MSG_ERROR("SetIPATablesToPassAllToSpecificClient failed, exiting test case");
  61. bTestResult = false;
  62. goto bail;
  63. }
  64. LOG_MSG_INFO("All tables were configured in order to output the packet to the correct pipe");
  65. LOG_MSG_INFO("Sending packet into the A2NDUN pipe(%d bytes) and the Pipe will add an header",
  66. nA2NDUNPacketByteSize);
  67. nBytesSent = m_A2NDUNToIpaPipe.Send(pA2NDUNPacket, nA2NDUNPacketByteSize);
  68. if (nA2NDUNPacketByteSize != nBytesSent)
  69. {
  70. bTestResult = false;
  71. goto bail;
  72. }
  73. //Receive the raw IP packet(which is a 4 arbitrary bytes) without header removal by the Pipe
  74. LOG_MSG_INFO("Reading packet from the USB pipe");
  75. nBytesReceived = m_IpaToUsbPipe.Receive(pPacketReceiveBuffer, HEADER_REMOVAL_TEST_MAX_PACKET_BYTE_SIZE);
  76. //TODO Header Removal: at this point the success scenario is that data came to the correct pipe - change this to
  77. //packet memory compare after header insertion is enabled.
  78. if (0 == nBytesReceived)
  79. {
  80. bTestResult = false;
  81. goto bail;
  82. }
  83. LOG_MSG_INFO("Read buffer : ");
  84. //Print the output
  85. for (int i = 0 ; i < nBytesReceived ; i++)
  86. {
  87. printf("0x%02x", pPacketReceiveBuffer[i]);
  88. }
  89. LOG_MSG_INFO("End of Read buffer.");
  90. if(0 != memcmp((const void *)pPacketReceiveBuffer,
  91. (const void *)(pA2NDUNPacket + (nBytesSent - nBytesReceived)),
  92. nBytesReceived)) {
  93. LOG_MSG_ERROR("Memory contains don't match");
  94. bTestResult = false;
  95. goto bail;
  96. }
  97. /* fall through */
  98. bail:
  99. delete pA2NDUNPacket;
  100. return bTestResult;
  101. }
  102. HeaderRemovalMetaDataFiltering::HeaderRemovalMetaDataFiltering()
  103. {
  104. m_name = "HeaderRemovalMetaDataFiltering";
  105. m_description =
  106. "HeaderRemovalMetaDataFiltering: check meta data based filtering";
  107. }
  108. /////////////////////////////////////////////////////////////////////////////////
  109. bool HeaderRemovalMetaDataFiltering::Run()
  110. {
  111. bool bTestResult = true;
  112. Byte pPacketReceiveBuffer[HEADER_REMOVAL_TEST_MAX_PACKET_BYTE_SIZE] = {0};//This buffer will be used in order to store the received packet.
  113. unsigned int nMagicNumber = 0x12345678; //arbitrary number
  114. unsigned int nChannelID = 0xABCD;//arbitrary number
  115. unsigned int nA2NDUNPacketByteSize = 0;
  116. unsigned int nMetaData = 0;
  117. unsigned int nMetaDataMask = 0xFFFF;
  118. unsigned int nBytesSent = 0;
  119. int nBytesReceived = 0;
  120. Byte *pA2NDUNPacket = CreateA2NDUNPacket(nMagicNumber, nChannelID, IPV4_FILE_PATH, &nA2NDUNPacketByteSize);
  121. if(0 == pA2NDUNPacket) {
  122. LOG_MSG_ERROR("Cannot load file to memory, exiting");
  123. return false;
  124. }
  125. nMetaData = (nChannelID << 16) | (0xFFFF & nA2NDUNPacketByteSize);
  126. LOG_MSG_INFO("*************nMetaData == (0x%x)", nMetaData);
  127. LOG_MSG_INFO("A2 Packet was successfully created (%d bytes)", nA2NDUNPacketByteSize);
  128. SetRoutingTableToPassAllToSpecificClient(IPA_CLIENT_TEST_CONS);
  129. SetHeaderInsertionTableAddEmptyHeaderForTheClient(IPA_CLIENT_TEST_CONS);
  130. LOG_MSG_INFO("Configuring Filtering module...");
  131. if (false ==
  132. ConfigureFilteringBlockWithMetaDataEq(
  133. IPA_CLIENT_TEST_CONS,
  134. nMetaData,
  135. nMetaDataMask)) {
  136. bTestResult = false;
  137. goto bail;
  138. }
  139. LOG_MSG_INFO("Sending packet into the A2NDUN pipe(%d bytes) and the Pipe will add an header",
  140. nA2NDUNPacketByteSize);
  141. nBytesSent = m_A2NDUNToIpaPipe.Send(pA2NDUNPacket, nA2NDUNPacketByteSize);
  142. if (nA2NDUNPacketByteSize != nBytesSent) {
  143. bTestResult = false;
  144. goto bail;
  145. }
  146. //Receive the raw IP packet(which is a 4 arbitrary bytes) without header removal by the Pipe
  147. LOG_MSG_INFO("Reading packet from the USB pipe");
  148. nBytesReceived = m_IpaToUsbPipe.Receive(pPacketReceiveBuffer, HEADER_REMOVAL_TEST_MAX_PACKET_BYTE_SIZE);
  149. //TODO Header Removal: at this point the success scenario is that data came to the correct pipe - change this to
  150. //packet memory compare after header insertion is enabled.
  151. if (0 == nBytesReceived) {
  152. bTestResult = false;
  153. goto bail;
  154. }
  155. LOG_MSG_INFO("Read buffer : ");
  156. //Print the output
  157. for (int i = 0 ; i < nBytesReceived ; i++) {
  158. printf("0x%02x", pPacketReceiveBuffer[i]);
  159. }
  160. LOG_MSG_INFO("End of Read buffer.");
  161. if(0 != memcmp((const void *)pPacketReceiveBuffer,
  162. (const void *)(pA2NDUNPacket + (nBytesSent - nBytesReceived)),
  163. nBytesReceived)) {
  164. LOG_MSG_ERROR("Memory contains don't match");
  165. bTestResult = false;
  166. goto bail;
  167. }
  168. /* fall through */
  169. bail:
  170. delete pA2NDUNPacket;
  171. return bTestResult;
  172. }
  173. static HeaderRemovalTOSCheck headerRemovalTOSCheck;
  174. static HeaderRemovalMetaDataFiltering headerRemovalMetaDataFiltering;
  175. /////////////////////////////////////////////////////////////////////////////////
  176. /////////////////////////////////////////////////////////////////////////////////
  177. /////////////////////////////////////////////////////////////////////////////////