ipa_nat_test.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2014, 2018-2019 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. /*
  30. * ===========================================================================
  31. *
  32. * INCLUDE FILES FOR MODULE
  33. *
  34. * ===========================================================================
  35. */
  36. #include <stdio.h>
  37. #include <stdint.h>
  38. #include <stdbool.h>
  39. #include <stdlib.h>
  40. #include <time.h>
  41. #include <netinet/in.h> /* for proto definitions */
  42. #include "ipa_nat_drv.h"
  43. #include "ipa_nat_drvi.h"
  44. #undef array_sz
  45. #define array_sz(a) \
  46. ( sizeof(a)/sizeof(a[0]) )
  47. #define u32 uint32_t
  48. #define u16 uint16_t
  49. #define u8 uint8_t
  50. #define RAN_ADDR rand_ip_addr()
  51. #define RAN_PORT rand_ip_port()
  52. static inline u32 rand_ip_addr()
  53. {
  54. static char buf[64];
  55. snprintf(
  56. buf, sizeof(buf),
  57. "%u.%u.%u.%u",
  58. (rand() % 254) + 1,
  59. rand() % 255,
  60. rand() % 255,
  61. (rand() % 254) + 1);
  62. return (u32) inet_addr(buf);
  63. }
  64. static inline u16 rand_ip_port()
  65. {
  66. return (u16) ((rand() % 60535) + 5000);
  67. }
  68. /*============ Preconditions to run NAT Test cases =========*/
  69. #define IPA_NAT_TEST_PRE_COND_TE 20
  70. #define CHECK_ERR(x) \
  71. if ( x ) { \
  72. IPAERR("Abrupt end of %s with " \
  73. "err: %d at line: %d\n", \
  74. __FUNCTION__, x, __LINE__); \
  75. return -1; \
  76. }
  77. #define CHECK_ERR_TBL_STOP(x, th) \
  78. if ( th ) { \
  79. int _ter_ = ipa_nat_validate_ipv4_table(th); \
  80. if ( _ter_ ) { \
  81. if ( sep ) { \
  82. ipa_nat_del_ipv4_tbl(th); \
  83. } \
  84. IPAERR("Abrupt end of %s with " \
  85. "err: %d at line: %d\n", \
  86. __FUNCTION__, _ter_, __LINE__); \
  87. return -1; \
  88. } \
  89. } \
  90. if ( x ) { \
  91. if ( th ) { \
  92. ipa_nat_dump_ipv4_table(th); \
  93. if( sep ) { \
  94. ipa_nat_del_ipv4_tbl(th); \
  95. } \
  96. } \
  97. IPAERR("Abrupt end of %s with " \
  98. "err: %d at line: %d\n", \
  99. __FUNCTION__, x, __LINE__); \
  100. return -1; \
  101. }
  102. #define CHECK_ERR_TBL_ACTION(x, th, action) \
  103. if ( th ) { \
  104. int _ter_ = ipa_nat_validate_ipv4_table(th); \
  105. if ( _ter_ ) { \
  106. IPAERR("ipa_nat_validate_ipv4_table() failed " \
  107. "in: %s at line: %d\n", \
  108. __FUNCTION__, __LINE__); \
  109. action; \
  110. } \
  111. } \
  112. if ( x ) { \
  113. if ( th ) { \
  114. ipa_nat_dump_ipv4_table(th); \
  115. } \
  116. IPAERR("error: %d in %s at line: %d\n", \
  117. x, __FUNCTION__, __LINE__); \
  118. action; \
  119. }
  120. typedef int (*NatTestFunc)(
  121. const char*, u32, int, u32, int, void*);
  122. typedef struct
  123. {
  124. const char* func_name;
  125. int num_ents_trigger;
  126. int test_hold_time_in_secs;
  127. NatTestFunc func;
  128. } NatTests;
  129. #undef NAT_TEST_ENTRY
  130. #define NAT_TEST_ENTRY(f, n, ht) \
  131. {#f, (n), (ht), f}
  132. #define NAT_DEBUG
  133. int ipa_nat_validate_ipv4_table(u32);
  134. int ipa_nat_testREG(const char*, u32, int, u32, int, void*);
  135. int ipa_nat_test000(const char*, u32, int, u32, int, void*);
  136. int ipa_nat_test001(const char*, u32, int, u32, int, void*);
  137. int ipa_nat_test002(const char*, u32, int, u32, int, void*);
  138. int ipa_nat_test003(const char*, u32, int, u32, int, void*);
  139. int ipa_nat_test004(const char*, u32, int, u32, int, void*);
  140. int ipa_nat_test005(const char*, u32, int, u32, int, void*);
  141. int ipa_nat_test006(const char*, u32, int, u32, int, void*);
  142. int ipa_nat_test007(const char*, u32, int, u32, int, void*);
  143. int ipa_nat_test008(const char*, u32, int, u32, int, void*);
  144. int ipa_nat_test009(const char*, u32, int, u32, int, void*);
  145. int ipa_nat_test010(const char*, u32, int, u32, int, void*);
  146. int ipa_nat_test011(const char*, u32, int, u32, int, void*);
  147. int ipa_nat_test012(const char*, u32, int, u32, int, void*);
  148. int ipa_nat_test013(const char*, u32, int, u32, int, void*);
  149. int ipa_nat_test014(const char*, u32, int, u32, int, void*);
  150. int ipa_nat_test015(const char*, u32, int, u32, int, void*);
  151. int ipa_nat_test016(const char*, u32, int, u32, int, void*);
  152. int ipa_nat_test017(const char*, u32, int, u32, int, void*);
  153. int ipa_nat_test018(const char*, u32, int, u32, int, void*);
  154. int ipa_nat_test019(const char*, u32, int, u32, int, void*);
  155. int ipa_nat_test020(const char*, u32, int, u32, int, void*);
  156. int ipa_nat_test021(const char*, u32, int, u32, int, void*);
  157. int ipa_nat_test022(const char*, u32, int, u32, int, void*);
  158. int ipa_nat_test023(const char*, u32, int, u32, int, void*);
  159. int ipa_nat_test024(const char*, u32, int, u32, int, void*);
  160. int ipa_nat_test025(const char*, u32, int, u32, int, void*);
  161. int ipa_nat_test999(const char*, u32, int, u32, int, void*);