ExceptionTests.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 "TestsUtils.h"
  30. #include "RoutingDriverWrapper.h"
  31. #include "HeaderInsertion.h"
  32. #include "Filtering.h"
  33. #include "IPAFilteringTable.h"
  34. #include <string.h>
  35. using namespace IPA;
  36. class IPAExceptionTestFixture: public TestBase {
  37. public:
  38. IPAExceptionTestFixture() :
  39. m_uBufferSize(0) {
  40. memset(m_aBuffer, 0, sizeof(m_aBuffer));
  41. m_testSuiteName.push_back("Exception");
  42. }
  43. virtual bool AddRules() = 0;
  44. virtual bool ModifyPackets() = 0;
  45. virtual bool TestLogic() = 0;
  46. bool Setup() {
  47. ConfigureScenario(PHASE_SEVEN_TEST_CONFIGURATION);
  48. m_producer.Open(INTERFACE0_TO_IPA_DATA_PATH,
  49. INTERFACE0_FROM_IPA_DATA_PATH);
  50. m_Consumer1.Open(INTERFACE1_TO_IPA_DATA_PATH,
  51. INTERFACE1_FROM_IPA_DATA_PATH);
  52. m_Consumer2.Open(INTERFACE2_TO_IPA_DATA_PATH,
  53. INTERFACE2_FROM_IPA_DATA_PATH);
  54. m_Consumer3.Open(INTERFACE3_TO_IPA_DATA_PATH,
  55. INTERFACE3_FROM_IPA_DATA_PATH);
  56. m_Exceptions.Open(INTERFACE_TO_IPA_EXCEPTION_PATH,
  57. INTERFACE_FROM_IPA_EXCEPTION_PATH);
  58. if (!m_Routing.DeviceNodeIsOpened()) {
  59. LOG_MSG_ERROR(
  60. "Routing block is not ready for immediate commands!\n");
  61. return false;
  62. }
  63. if (!m_Filtering.DeviceNodeIsOpened()) {
  64. LOG_MSG_ERROR(
  65. "Filtering block is not ready for immediate commands!\n");
  66. return false;
  67. }
  68. if (!m_HeaderInsertion.DeviceNodeIsOpened())
  69. {
  70. LOG_MSG_ERROR("Header Insertion block is not ready for immediate commands!\n");
  71. return false;
  72. }
  73. m_HeaderInsertion.Reset();
  74. return true;
  75. } // Setup()
  76. bool Run() {
  77. m_uBufferSize = BUFF_MAX_SIZE;
  78. LOG_MSG_STACK("Entering Function");
  79. // Configure the system by adding Routing / Filtering / HDR
  80. if (!AddRules()) {
  81. LOG_MSG_ERROR("Failed adding Routing / Filtering / HDR.");
  82. return false;
  83. }
  84. // Load input data (IP packet) from file
  85. if (!LoadDefaultPacket(m_eIP, m_aBuffer, m_uBufferSize)) {
  86. LOG_MSG_ERROR("Failed default Packet");
  87. return false;
  88. }
  89. if (!ModifyPackets()) {
  90. LOG_MSG_ERROR("Failed to modify packets.");
  91. return false;
  92. }
  93. if (!TestLogic()) {
  94. LOG_MSG_ERROR("Test failed, Input and expected output mismatch.");
  95. return false;
  96. }
  97. LOG_MSG_STACK("Leaving Function (Returning True)");
  98. return true;
  99. } // Run()
  100. bool Teardown() {
  101. m_producer.Close();
  102. m_Consumer1.Close();
  103. m_Consumer2.Close();
  104. m_Consumer3.Close();
  105. m_Exceptions.Close();
  106. return true;
  107. } // Teardown()
  108. ~IPAExceptionTestFixture() {
  109. }
  110. static RoutingDriverWrapper m_Routing;
  111. static Filtering m_Filtering;
  112. static HeaderInsertion m_HeaderInsertion;
  113. InterfaceAbstraction m_producer;
  114. InterfaceAbstraction m_Consumer1;
  115. InterfaceAbstraction m_Consumer2;
  116. InterfaceAbstraction m_Consumer3;
  117. InterfaceAbstraction m_Exceptions;
  118. protected:
  119. static const size_t BUFF_MAX_SIZE = 1024;
  120. static const uint8_t MAX_HEADER_SIZE = 64; // 64Bytes - Max Header Length
  121. enum ipa_ip_type m_eIP;
  122. uint8_t m_aBuffer[BUFF_MAX_SIZE]; // Input file \ IP packet
  123. size_t m_uBufferSize;
  124. };
  125. RoutingDriverWrapper IPAExceptionTestFixture::m_Routing;
  126. Filtering IPAExceptionTestFixture::m_Filtering;
  127. HeaderInsertion IPAExceptionTestFixture::m_HeaderInsertion;
  128. //----------------------------------------------------------------------------------------------------------------------------------------/
  129. // Test001: Test that when a packet with (IPVer != 4) && (IPVer Ver != 6) , an exception packet is created and received & exception_pipe /
  130. //----------------------------------------------------------------------------------------------------------------------------------------/
  131. class IPAExceptionPacketTest001: public IPAExceptionTestFixture {
  132. public:
  133. IPAExceptionPacketTest001() {
  134. m_name = "IPAExceptionPacketTest001";
  135. m_description = "\
  136. IPA Exception Test 001 - Test that when a packet with (IPVer != 4) && (IPVer Ver != 6) , an exception packet is created and received & exception_pipe \
  137. Test Generates send NUM_OF_EXCEPTION_PKTS packets with IP Version changing from 0 to 9.\
  138. First IP Version == 4, hence it is not considered as exception (same goes for IP Ver == 6) \
  139. ";
  140. m_eIP = IPA_IP_v4;
  141. Register(*this);
  142. }
  143. virtual bool AddRules() {
  144. // Clear All Rules
  145. bool bRetVal = true;
  146. LOG_MSG_STACK("Entering Function");
  147. const char bypass0[20] = "Bypass0";
  148. struct ipa_ioc_get_rt_tbl sRoutingTable;
  149. IPAFilteringTable cFilterTable;
  150. struct ipa_flt_rule_add sFilterRuleEntry;
  151. uint32_t nRTTableHdl=0;
  152. memset(&sRoutingTable, 0, sizeof(sRoutingTable));
  153. LOG_MSG_STACK("Entering Function");
  154. if (!CreateBypassRoutingTable(&m_Routing, m_eIP, bypass0, IPA_CLIENT_TEST2_CONS,
  155. 0,&nRTTableHdl)) {
  156. LOG_MSG_ERROR("CreateBypassRoutingTable Failed\n");
  157. bRetVal = false;
  158. goto bail;
  159. }
  160. LOG_MSG_INFO("CreateBypassRoutingTable completed successfully");
  161. sRoutingTable.ip = m_eIP;
  162. strlcpy(sRoutingTable.name, bypass0, sizeof(sRoutingTable.name));
  163. if (!m_Routing.GetRoutingTable(&sRoutingTable)) {
  164. LOG_MSG_ERROR(
  165. "m_routing.GetRoutingTable(&sRoutingTable=0x%p) Failed.", &sRoutingTable);
  166. bRetVal = false;
  167. goto bail;
  168. }
  169. // Creating Filtering Rules
  170. cFilterTable.Init(m_eIP,IPA_CLIENT_TEST_PROD,true,1);
  171. LOG_MSG_INFO("Creation of filtering table completed successfully");
  172. // Configuring Filtering Rule No.1
  173. cFilterTable.GeneratePresetRule(0,sFilterRuleEntry);
  174. sFilterRuleEntry.at_rear = true;
  175. sFilterRuleEntry.flt_rule_hdl = -1; // return Value
  176. sFilterRuleEntry.status = -1; // return value
  177. sFilterRuleEntry.rule.action = IPA_PASS_TO_ROUTING;
  178. sFilterRuleEntry.rule.rt_tbl_hdl = nRTTableHdl;
  179. if (
  180. ((uint8_t)-1 == cFilterTable.AddRuleToTable(sFilterRuleEntry)) ||
  181. !m_Filtering.AddFilteringRule(cFilterTable.GetFilteringTable())
  182. )
  183. {
  184. LOG_MSG_ERROR ("Adding Rule (0) to Filtering block Failed.");
  185. bRetVal = false;
  186. goto bail;
  187. } else
  188. {
  189. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", cFilterTable.ReadRuleFromTable(0)->flt_rule_hdl,cFilterTable.ReadRuleFromTable(0)->status);
  190. }
  191. bail:
  192. LOG_MSG_STACK(
  193. "Leaving Function (Returning %s)", bRetVal?"True":"False");
  194. return bRetVal;
  195. } // AddRules()
  196. virtual bool ModifyPackets() {
  197. m_eIP = IPA_IP_v6;
  198. AddRules(); // Need to add Routing / Filtering rules for IPv6 as well.
  199. return true;
  200. } // ModifyPacktes ()
  201. virtual bool TestLogic() {
  202. int i = 0, nIPVer = 0;;
  203. memset(m_aExpectedBuffer, 0, sizeof(m_aExpectedBuffer));
  204. m_aExpectedBuffer[2] = 0x0b;
  205. m_aExpectedBuffer[3] = 0x80;
  206. memcpy(m_aExpectedBuffer+8, m_aBuffer, m_uBufferSize);
  207. m_aExpectedBufSize = m_uBufferSize+8;
  208. for (i=0;i<NUM_OF_EXCEPTION_PKTS;i++)
  209. {
  210. LOG_MSG_INFO("Packet %d\n",i);
  211. nIPVer = i+4 % 10;
  212. m_aBuffer[0] = (m_aBuffer[0] & 0x0F)+0x10*nIPVer;// Change to Invalid IP version
  213. m_aExpectedBuffer[8] = (m_aExpectedBuffer[8] & 0x0F)+0x10*nIPVer;
  214. if (4 == nIPVer || 6 == nIPVer)
  215. {
  216. if (!SendReceiveAndCompare(&m_producer, m_aBuffer, m_uBufferSize,
  217. &m_Consumer1, m_aExpectedBuffer+8, m_aExpectedBufSize-8))
  218. {
  219. LOG_MSG_ERROR("SendReceiveAndCompare failed. IPVer = %d",nIPVer);
  220. return false;
  221. }
  222. } else
  223. {
  224. if (!SendReceiveAndCompare(&m_producer, m_aBuffer, m_uBufferSize,
  225. &m_Exceptions, m_aExpectedBuffer, m_aExpectedBufSize))
  226. {
  227. LOG_MSG_ERROR("SendReceiveAndCompare failed. IPVer = %d",nIPVer);
  228. return false;
  229. }
  230. }
  231. }
  232. return true;
  233. }
  234. private:
  235. static const int NUM_OF_EXCEPTION_PKTS = 9;
  236. uint8_t m_aExpectedBuffer[BUFF_MAX_SIZE];
  237. size_t m_aExpectedBufSize;
  238. };
  239. //------------------------------------------------------------------------------------------------------------------------------------------/
  240. // Test003: Test that when Filtering Routes the Packet to the Exception Pipe, an exception packet is created and received & exception_pipe /
  241. //------------------------------------------------------------------------------------------------------------------------------------------/
  242. class IPAExceptionPacketTest003: public IPAExceptionTestFixture {
  243. public:
  244. IPAExceptionPacketTest003() {
  245. m_name = "IPAExceptionPacketTest003";
  246. m_description = "\
  247. IPA Exception Test 003 - Test that when Filtering Routes the Packet to the Exception Pipe, an exception packet is created and received & exception_pipe \
  248. Test Generates a Filtering Table that routes all packets to the Exception Pipe. \
  249. and verify that the packet is recieved @ the Exception Pipe. \
  250. ";
  251. m_eIP = IPA_IP_v4;
  252. Register(*this);
  253. }
  254. virtual bool AddRules() {
  255. // Clear All Rules
  256. bool bRetVal = true;
  257. LOG_MSG_STACK("Entering Function");
  258. const char bypass0[20] = "Bypass0";
  259. struct ipa_ioc_get_rt_tbl sRoutingTable;
  260. IPAFilteringTable cFilterTable;
  261. struct ipa_flt_rule_add sFilterRuleEntry;
  262. uint32_t nRTTableHdl=0;
  263. memset(&sRoutingTable, 0, sizeof(sRoutingTable));
  264. LOG_MSG_STACK("Entering Function");
  265. if (!CreateBypassRoutingTable(&m_Routing, m_eIP, bypass0, IPA_CLIENT_TEST2_CONS,
  266. 0,&nRTTableHdl)) {
  267. LOG_MSG_ERROR("CreateBypassRoutingTable Failed\n");
  268. bRetVal = false;
  269. goto bail;
  270. }
  271. LOG_MSG_INFO("CreateBypassRoutingTable completed successfully");
  272. sRoutingTable.ip = m_eIP;
  273. strlcpy(sRoutingTable.name, bypass0, sizeof(sRoutingTable.name));
  274. if (!m_Routing.GetRoutingTable(&sRoutingTable)) {
  275. LOG_MSG_ERROR(
  276. "m_routing.GetRoutingTable(&sRoutingTable=0x%p) Failed.", &sRoutingTable);
  277. bRetVal = false;
  278. goto bail;
  279. }
  280. // Creating Filtering Rules
  281. cFilterTable.Init(m_eIP,IPA_CLIENT_TEST_PROD,true,1);
  282. LOG_MSG_INFO("Creation of filtering table completed successfully");
  283. // Configuring Filtering Rule No.1
  284. cFilterTable.GeneratePresetRule(0,sFilterRuleEntry);
  285. sFilterRuleEntry.at_rear = true;
  286. sFilterRuleEntry.flt_rule_hdl = -1; // return Value
  287. sFilterRuleEntry.status = -1; // return value
  288. sFilterRuleEntry.rule.action = IPA_PASS_TO_EXCEPTION;
  289. sFilterRuleEntry.rule.rt_tbl_hdl = nRTTableHdl;
  290. if (
  291. ((uint8_t)-1 == cFilterTable.AddRuleToTable(sFilterRuleEntry)) ||
  292. !m_Filtering.AddFilteringRule(cFilterTable.GetFilteringTable())
  293. )
  294. {
  295. LOG_MSG_ERROR ("Adding Rule (0) to Filtering block Failed.");
  296. bRetVal = false;
  297. goto bail;
  298. } else
  299. {
  300. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", cFilterTable.ReadRuleFromTable(0)->flt_rule_hdl,cFilterTable.ReadRuleFromTable(0)->status);
  301. }
  302. bail:
  303. LOG_MSG_STACK(
  304. "Leaving Function (Returning %s)", bRetVal?"True":"False");
  305. return bRetVal;
  306. } // AddRules()
  307. virtual bool ModifyPackets() {
  308. return true;
  309. } // ModifyPacktes ()
  310. virtual bool TestLogic() {
  311. memset(m_aExpectedBuffer, 0, sizeof(m_aExpectedBuffer));
  312. m_aExpectedBuffer[2] = 0x0b;
  313. m_aExpectedBuffer[3] = 0x20;
  314. memcpy(m_aExpectedBuffer+8, m_aBuffer, m_uBufferSize);
  315. m_aExpectedBufSize = m_uBufferSize+8;
  316. if (!SendReceiveAndCompare(&m_producer, m_aBuffer, m_uBufferSize,
  317. &m_Exceptions, m_aExpectedBuffer, m_aExpectedBufSize))
  318. {
  319. LOG_MSG_ERROR("SendReceiveAndCompare failed.");
  320. return false;
  321. }
  322. return true;
  323. }
  324. private:
  325. uint8_t m_aExpectedBuffer[BUFF_MAX_SIZE];
  326. size_t m_aExpectedBufSize;
  327. };
  328. //-----------------------------------------------------------------------------------------------------------------------------------------/
  329. // Test006: Test that when a packet with Internet Header Length < 5 Arrives, an exception packet is created and received & exception_pipe /
  330. //-----------------------------------------------------------------------------------------------------------------------------------------/
  331. class IPAExceptionPacketTest006: public IPAExceptionTestFixture {
  332. public:
  333. IPAExceptionPacketTest006() {
  334. m_name = "IPAExceptionPacketTest006"
  335. m_description = "\
  336. IPA Exception Test 006 - Test that when a packet with Internet Header Length < 5 Arrives, an exception packet is created and received & exception_pipe \
  337. Test Generates a Packet with Internet Header Length (IHL == 4). \
  338. and verifies that the packet is recieved @ the Exception Pipe. \
  339. ";
  340. m_eIP = IPA_IP_v4;
  341. Register(*this);
  342. }
  343. virtual bool AddRules() {
  344. // Clear All Rules
  345. bool bRetVal = true;
  346. LOG_MSG_STACK("Entering Function");
  347. const char bypass0[20] = "Bypass0";
  348. struct ipa_ioc_get_rt_tbl sRoutingTable;
  349. IPAFilteringTable cFilterTable;
  350. struct ipa_flt_rule_add sFilterRuleEntry;
  351. uint32_t nRTTableHdl=0;
  352. memset(&sRoutingTable, 0, sizeof(sRoutingTable));
  353. LOG_MSG_STACK("Entering Function");
  354. if (!CreateBypassRoutingTable(&m_Routing, m_eIP, bypass0, IPA_CLIENT_TEST2_CONS,
  355. 0,&nRTTableHdl)) {
  356. LOG_MSG_ERROR("CreateBypassRoutingTable Failed\n");
  357. bRetVal = false;
  358. goto bail;
  359. }
  360. LOG_MSG_INFO("CreateBypassRoutingTable completed successfully");
  361. sRoutingTable.ip = m_eIP;
  362. strlcpy(sRoutingTable.name, bypass0, sizeof(sRoutingTable.name));
  363. if (!m_Routing.GetRoutingTable(&sRoutingTable)) {
  364. LOG_MSG_ERROR(
  365. "m_routing.GetRoutingTable(&sRoutingTable=0x%p) Failed.", &sRoutingTable);
  366. bRetVal = false;
  367. goto bail;
  368. }
  369. // Creating Filtering Rules
  370. cFilterTable.Init(m_eIP,IPA_CLIENT_TEST_PROD,true,1);
  371. LOG_MSG_INFO("Creation of filtering table completed successfully");
  372. // Configuring Filtering Rule No.1
  373. cFilterTable.GeneratePresetRule(0,sFilterRuleEntry);
  374. sFilterRuleEntry.at_rear = true;
  375. sFilterRuleEntry.flt_rule_hdl = -1; // return Value
  376. sFilterRuleEntry.status = -1; // return value
  377. sFilterRuleEntry.rule.action = IPA_PASS_TO_ROUTING;
  378. sFilterRuleEntry.rule.rt_tbl_hdl = nRTTableHdl;
  379. if (
  380. ((uint8_t)-1 == cFilterTable.AddRuleToTable(sFilterRuleEntry)) ||
  381. !m_Filtering.AddFilteringRule(cFilterTable.GetFilteringTable())
  382. )
  383. {
  384. LOG_MSG_ERROR ("Adding Rule (0) to Filtering block Failed.");
  385. bRetVal = false;
  386. goto bail;
  387. } else
  388. {
  389. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", cFilterTable.ReadRuleFromTable(0)->flt_rule_hdl,cFilterTable.ReadRuleFromTable(0)->status);
  390. }
  391. bail:
  392. LOG_MSG_STACK(
  393. "Leaving Function (Returning %s)", bRetVal?"True":"False");
  394. return bRetVal;
  395. } // AddRules()
  396. virtual bool ModifyPackets() {
  397. m_aBuffer[0] =(m_aBuffer[0] & 0xF0)+0x04;// Change the IHL to 4
  398. return true;
  399. } // ModifyPacktes ()
  400. virtual bool TestLogic() {
  401. memset(m_aExpectedBuffer, 0, sizeof(m_aExpectedBuffer));
  402. m_aExpectedBuffer[2] = 0x0b;
  403. m_aExpectedBuffer[3] = 0x04;
  404. memcpy(m_aExpectedBuffer+8, m_aBuffer, m_uBufferSize);
  405. m_aExpectedBufSize = m_uBufferSize+8;
  406. if (!SendReceiveAndCompare(&m_producer, m_aBuffer, m_uBufferSize,
  407. &m_Exceptions, m_aExpectedBuffer, m_aExpectedBufSize))
  408. {
  409. LOG_MSG_ERROR("SendReceiveAndCompare failed.");
  410. return false;
  411. }
  412. return true;
  413. }
  414. private:
  415. uint8_t m_aExpectedBuffer[BUFF_MAX_SIZE];
  416. size_t m_aExpectedBufSize;
  417. };
  418. static IPAExceptionPacketTest001 ipaExceptionPacketTest001;
  419. static IPAExceptionPacketTest002 ipaExceptionPacketTest002;
  420. static IPAExceptionPacketTest003 ipaExceptionPacketTest003;
  421. static IPAExceptionPacketTest006 ipaExceptionPacketTest006;