ExceptionsTests.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "RoutingDriverWrapper.h"
  30. #include "HeaderInsertion.h"
  31. #include "Filtering.h"
  32. #include "IPAFilteringTable.h"
  33. #include "TestsUtils.h"
  34. #include "ExceptionsTestFixture.h"
  35. #include "IPv4Packet.h"
  36. #include <string.h>
  37. #define MAX_SENT_BUFFER_SIZE 1500
  38. #define MAX_RECEIVE_BUFFER_SIZE 1500
  39. #define VALIDATE_WITH_MSG_AND_RETVAL(bRetVal,msg) \
  40. if (false == bRetVal){ \
  41. LOG_MSG_ERROR(msg); \
  42. return false; \
  43. }
  44. using namespace IPA;
  45. ///////////////////////////////////////////////////////////////////////////////
  46. class ExceptionsTestNonIpPacket: public ExceptionsTestFixture {
  47. public:
  48. //The packet size to be sent
  49. size_t m_nPacketSize;
  50. //A buffer to hold the non-IP(V4/V6) packet
  51. Byte *m_pSendBuffer;
  52. ///////////////////////////////////////////////////////////////////////////
  53. //Set the tests name and description
  54. ExceptionsTestNonIpPacket() :
  55. m_nPacketSize(0), m_pSendBuffer(NULL) {
  56. m_name = "ExceptionsTestNonIpPacket";
  57. m_description =
  58. "Create a non-IP packet(version!=4 && version !=6) and \
  59. expect exception from Filter block";
  60. }
  61. ///////////////////////////////////////////////////////////////////////////
  62. virtual bool Run() {
  63. bool bRetVal = true;
  64. Byte *pReceiveBuffer = new Byte[MAX_RECEIVE_BUFFER_SIZE];
  65. //Send the non-IPV4/IPV6 packet to the IPA
  66. LOG_MSG_DEBUG("Send the non-IPV4/IPV6 packet to the IPA");
  67. size_t nBytesSent = m_USB1ToIpaPipe.Send(m_pSendBuffer, m_nPacketSize);
  68. if (nBytesSent != m_nPacketSize) {
  69. LOG_MSG_ERROR("Not all data was sent into the IPA");
  70. return false;
  71. }
  72. //Read from the exception pipe(from IPA to A5) - try to read as much as we can
  73. size_t nBytesRead = m_IpaToA5ExceptionPipe.Receive(pReceiveBuffer,
  74. MAX_RECEIVE_BUFFER_SIZE);
  75. if (nBytesRead != nBytesSent) {
  76. LOG_MSG_ERROR("Not all data was read:");
  77. print_buff(pReceiveBuffer, nBytesRead);
  78. return false;
  79. }
  80. //check the exception packet against the one that we sent
  81. bRetVal = !memcmp(m_pSendBuffer, pReceiveBuffer, nBytesSent);
  82. if (false == bRetVal) {
  83. LOG_MSG_ERROR("Received packet is not equal, Received:");
  84. print_buff(pReceiveBuffer, nBytesRead);
  85. LOG_MSG_ERROR("Received packet is not equal, Sent:");
  86. print_buff(m_pSendBuffer, m_nPacketSize);
  87. return false;
  88. }
  89. return true;
  90. }
  91. ///////////////////////////////////////////////////////////////////////////
  92. //build the non-IP packet
  93. virtual bool Setup() {
  94. bool bRetVal = true;
  95. m_pSendBuffer = new Byte[MAX_SENT_BUFFER_SIZE];
  96. //Load some default IPV4 packet and save its size
  97. m_nPacketSize = MAX_SENT_BUFFER_SIZE; //This parameter is In/Out
  98. bRetVal = LoadDefaultPacket(IPA_IP_v4, m_pSendBuffer, m_nPacketSize);
  99. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Load failed");
  100. //Set the version field to non-IPV4/IPV6(version = 5)
  101. m_pSendBuffer[0] &= 0x0F;
  102. m_pSendBuffer[0] |= 0x50;
  103. //initialize Pipes
  104. bRetVal = m_USB1ToIpaPipe.Init();
  105. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Pipe Initialization failed");
  106. bRetVal = m_IpaToA5ExceptionPipe.Init();
  107. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Pipe Initialization failed");
  108. return true;
  109. }
  110. ///////////////////////////////////////////////////////////////////////////
  111. virtual bool Teardown() {
  112. bool bRetVal = true;
  113. delete[] m_pSendBuffer;
  114. m_USB1ToIpaPipe.Destroy();
  115. m_IpaToA5ExceptionPipe.Destroy();
  116. return bRetVal;
  117. }
  118. ///////////////////////////////////////////////////////////////////////////
  119. };
  120. //ExceptionTestNoneIpPacket
  121. ///////////////////////////////////////////////////////////////////////////////
  122. ///////////////////////////////////////////////////////////////////////////////
  123. ///////////////////////////////////////////////////////////////////////////////
  124. ///////////////////////////////////////////////////////////////////////////////
  125. class ExceptionsTestFragmentedException: public ExceptionsTestFixture {
  126. public:
  127. //The packet size to be sent
  128. size_t m_nPacketSize;
  129. //A buffer to hold the non-IP(V4/V6) packet
  130. Byte *m_pSendBuffer;
  131. Byte *m_pReceiveBuffer;
  132. ///////////////////////////////////////////////////////////////////////////
  133. //Set the tests name and description
  134. ExceptionsTestFragmentedException():m_nPacketSize(0), m_pSendBuffer(NULL),
  135. m_pReceiveBuffer(NULL){
  136. m_name = "ExceptionsTestFragmentedException";
  137. m_description =
  138. "Send IP packet with MF set, create global Filter rule \
  139. that will hit it as Exception";
  140. }
  141. ///////////////////////////////////////////////////////////////////////////
  142. virtual bool Run() {
  143. bool bRetVal = true;
  144. //configuring the Filter block to catch the fragmented packet:
  145. ConfigureFilterGlobalRuleForMF();
  146. //Send the non-IPV4/IPV6 packet to the IPA
  147. LOG_MSG_DEBUG("Send the IP packet with the MF bit set(size = %d)", m_nPacketSize);
  148. size_t nBytesSent = m_USB1ToIpaPipe.Send(m_pSendBuffer, m_nPacketSize);
  149. if (nBytesSent != m_nPacketSize) {
  150. LOG_MSG_ERROR("Not all data was sent into the IPA(only %d)", nBytesSent);
  151. return false;
  152. }
  153. //Read from the exception pipe(from IPA to A5) - try to read as much as we can
  154. size_t nBytesRead = m_IpaToA5ExceptionPipe.Receive(m_pReceiveBuffer,
  155. MAX_RECEIVE_BUFFER_SIZE);
  156. if (nBytesRead != nBytesSent) {
  157. LOG_MSG_ERROR("Not all data was read:");
  158. print_buff(m_pReceiveBuffer, nBytesRead);
  159. return false;
  160. }
  161. //check the exception packet against the one that we sent
  162. bRetVal = !memcmp(m_pSendBuffer, m_pReceiveBuffer, nBytesSent);
  163. if (false == bRetVal) {
  164. LOG_MSG_ERROR("Received packet is not equal, Received:");
  165. print_buff(m_pReceiveBuffer, nBytesRead);
  166. LOG_MSG_ERROR("Received packet is not equal, Sent:");
  167. print_buff(m_pSendBuffer, m_nPacketSize);
  168. return false;
  169. }
  170. return true;
  171. }
  172. ///////////////////////////////////////////////////////////////////////////
  173. //build the non-IP packet
  174. virtual bool Setup() {
  175. bool bRetVal = true;
  176. m_pReceiveBuffer = new Byte[MAX_RECEIVE_BUFFER_SIZE];
  177. m_pSendBuffer = new Byte[MAX_RECEIVE_BUFFER_SIZE];
  178. //Load some default TCP packet
  179. TCPPacket tcpPacket;
  180. //Set the MF bit
  181. tcpPacket.SetMF(true);
  182. //copy the packet to the send buffer
  183. m_nPacketSize = tcpPacket.GetSize();
  184. tcpPacket.ToNetworkByteStream(m_pSendBuffer);
  185. //initialize Pipes
  186. bRetVal = m_USB1ToIpaPipe.Init();
  187. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Pipe Initialization failed");
  188. bRetVal = m_IpaToA5ExceptionPipe.Init();
  189. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Pipe Initialization failed");
  190. return true;
  191. }
  192. ///////////////////////////////////////////////////////////////////////////
  193. virtual bool Teardown() {
  194. bool bRetVal = true;
  195. delete[] m_pSendBuffer;
  196. m_USB1ToIpaPipe.Destroy();
  197. m_IpaToA5ExceptionPipe.Destroy();
  198. return bRetVal;
  199. }
  200. ///////////////////////////////////////////////////////////////////////////
  201. void ConfigureFilterGlobalRuleForMF(){
  202. //struct ipa_ioc_add_flt_rule *pRuleTable;
  203. //Allocate memory for a table with one rule.
  204. //Instruct the Driver to write this table(with its one rule) to the HW
  205. //Continue from here - build the rule to catch the fragmented packet
  206. }
  207. ///////////////////////////////////////////////////////////////////////////
  208. };
  209. //ExceptionsTestFragmentedException
  210. ///////////////////////////////////////////////////////////////////////////////
  211. ///////////////////////////////////////////////////////////////////////////////
  212. ///////////////////////////////////////////////////////////////////////////////
  213. class ExceptionsTestNonTCPUDP: public ExceptionsTestFixture {
  214. };
  215. ///////////////////////////////////////////////////////////////////////////////
  216. //Classes instances:
  217. static ExceptionsTestNonIpPacket exceptionsTestNonIpPacket;
  218. static ExceptionsTestFragmentedException exceptionsTestFragmentedException;
  219. ///////////////////////////////////////////////////////////////////////////////
  220. ////////////// EOF ////////
  221. ///////////////////////////////////////////////////////////////////////////////