syslog_test.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
  4. *
  5. * Logging function tests for CONFIG_LOG_SYSLOG=y.
  6. *
  7. * Invoke the test with: ./u-boot -d arch/sandbox/dts/test.dtb
  8. */
  9. /* Override CONFIG_LOG_MAX_LEVEL */
  10. #define LOG_DEBUG
  11. #include <common.h>
  12. #include <asm/global_data.h>
  13. #include <dm/device.h>
  14. #include <hexdump.h>
  15. #include <test/log.h>
  16. #include <test/test.h>
  17. #include <test/suites.h>
  18. #include <test/ut.h>
  19. #include <asm/eth.h>
  20. #include "syslog_test.h"
  21. DECLARE_GLOBAL_DATA_PTR;
  22. int sb_log_tx_handler(struct udevice *dev, void *packet, unsigned int len)
  23. {
  24. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  25. struct sb_log_env *env = priv->priv;
  26. /* uts is updated by the ut_assert* macros */
  27. struct unit_test_state *uts = env->uts;
  28. char *buf = packet;
  29. struct ethernet_hdr *eth_hdr = packet;
  30. struct ip_udp_hdr *ip_udp_hdr;
  31. /* Check Ethernet header */
  32. ut_asserteq_mem(&eth_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
  33. ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
  34. /* Check IP header */
  35. buf += sizeof(struct ethernet_hdr);
  36. ip_udp_hdr = (struct ip_udp_hdr *)buf;
  37. ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
  38. ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
  39. ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
  40. ut_asserteq(UDP_HDR_SIZE + strlen(env->expected) + 1,
  41. ntohs(ip_udp_hdr->udp_len));
  42. /* Check payload */
  43. buf += sizeof(struct ip_udp_hdr);
  44. ut_asserteq_mem(env->expected, buf,
  45. ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
  46. /* Signal that the callback function has been executed */
  47. env->expected = NULL;
  48. return 0;
  49. }
  50. int syslog_test_setup(struct unit_test_state *uts)
  51. {
  52. ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), true));
  53. return 0;
  54. }
  55. int syslog_test_finish(struct unit_test_state *uts)
  56. {
  57. ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), false));
  58. return 0;
  59. }
  60. /**
  61. * log_test_syslog_err() - test log_err() function
  62. *
  63. * @uts: unit test state
  64. * Return: 0 = success
  65. */
  66. static int log_test_syslog_err(struct unit_test_state *uts)
  67. {
  68. int old_log_level = gd->default_log_level;
  69. struct sb_log_env env;
  70. ut_assertok(syslog_test_setup(uts));
  71. gd->log_fmt = LOGF_TEST;
  72. gd->default_log_level = LOGL_INFO;
  73. env_set("ethact", "eth@10002000");
  74. env_set("log_hostname", "sandbox");
  75. env.expected = "<3>sandbox uboot: log_test_syslog_err() "
  76. "testing log_err\n";
  77. env.uts = uts;
  78. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  79. /* Used by ut_assert macros in the tx_handler */
  80. sandbox_eth_set_priv(0, &env);
  81. log_err("testing %s\n", "log_err");
  82. /* Check that the callback function was called */
  83. sandbox_eth_set_tx_handler(0, NULL);
  84. gd->default_log_level = old_log_level;
  85. gd->log_fmt = log_get_default_format();
  86. ut_assertok(syslog_test_finish(uts));
  87. return 0;
  88. }
  89. LOG_TEST(log_test_syslog_err);
  90. /**
  91. * log_test_syslog_warning() - test log_warning() function
  92. *
  93. * @uts: unit test state
  94. * Return: 0 = success
  95. */
  96. static int log_test_syslog_warning(struct unit_test_state *uts)
  97. {
  98. int old_log_level = gd->default_log_level;
  99. struct sb_log_env env;
  100. ut_assertok(syslog_test_setup(uts));
  101. gd->log_fmt = LOGF_TEST;
  102. gd->default_log_level = LOGL_INFO;
  103. env_set("ethact", "eth@10002000");
  104. env_set("log_hostname", "sandbox");
  105. env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
  106. "testing log_warning\n";
  107. env.uts = uts;
  108. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  109. /* Used by ut_assert macros in the tx_handler */
  110. sandbox_eth_set_priv(0, &env);
  111. log_warning("testing %s\n", "log_warning");
  112. sandbox_eth_set_tx_handler(0, NULL);
  113. /* Check that the callback function was called */
  114. ut_assertnull(env.expected);
  115. gd->default_log_level = old_log_level;
  116. gd->log_fmt = log_get_default_format();
  117. ut_assertok(syslog_test_finish(uts));
  118. return 0;
  119. }
  120. LOG_TEST(log_test_syslog_warning);
  121. /**
  122. * log_test_syslog_notice() - test log_notice() function
  123. *
  124. * @uts: unit test state
  125. * Return: 0 = success
  126. */
  127. static int log_test_syslog_notice(struct unit_test_state *uts)
  128. {
  129. int old_log_level = gd->default_log_level;
  130. struct sb_log_env env;
  131. ut_assertok(syslog_test_setup(uts));
  132. gd->log_fmt = LOGF_TEST;
  133. gd->default_log_level = LOGL_INFO;
  134. env_set("ethact", "eth@10002000");
  135. env_set("log_hostname", "sandbox");
  136. env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
  137. "testing log_notice\n";
  138. env.uts = uts;
  139. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  140. /* Used by ut_assert macros in the tx_handler */
  141. sandbox_eth_set_priv(0, &env);
  142. log_notice("testing %s\n", "log_notice");
  143. sandbox_eth_set_tx_handler(0, NULL);
  144. /* Check that the callback function was called */
  145. ut_assertnull(env.expected);
  146. gd->default_log_level = old_log_level;
  147. gd->log_fmt = log_get_default_format();
  148. ut_assertok(syslog_test_finish(uts));
  149. return 0;
  150. }
  151. LOG_TEST(log_test_syslog_notice);
  152. /**
  153. * log_test_syslog_info() - test log_info() function
  154. *
  155. * @uts: unit test state
  156. * Return: 0 = success
  157. */
  158. static int log_test_syslog_info(struct unit_test_state *uts)
  159. {
  160. int old_log_level = gd->default_log_level;
  161. struct sb_log_env env;
  162. ut_assertok(syslog_test_setup(uts));
  163. gd->log_fmt = LOGF_TEST;
  164. gd->default_log_level = LOGL_INFO;
  165. env_set("ethact", "eth@10002000");
  166. env_set("log_hostname", "sandbox");
  167. env.expected = "<6>sandbox uboot: log_test_syslog_info() "
  168. "testing log_info\n";
  169. env.uts = uts;
  170. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  171. /* Used by ut_assert macros in the tx_handler */
  172. sandbox_eth_set_priv(0, &env);
  173. log_info("testing %s\n", "log_info");
  174. sandbox_eth_set_tx_handler(0, NULL);
  175. /* Check that the callback function was called */
  176. ut_assertnull(env.expected);
  177. gd->default_log_level = old_log_level;
  178. gd->log_fmt = log_get_default_format();
  179. ut_assertok(syslog_test_finish(uts));
  180. return 0;
  181. }
  182. LOG_TEST(log_test_syslog_info);
  183. /**
  184. * log_test_syslog_debug() - test log_debug() function
  185. *
  186. * @uts: unit test state
  187. * Return: 0 = success
  188. */
  189. static int log_test_syslog_debug(struct unit_test_state *uts)
  190. {
  191. int old_log_level = gd->default_log_level;
  192. struct sb_log_env env;
  193. ut_assertok(syslog_test_setup(uts));
  194. gd->log_fmt = LOGF_TEST;
  195. gd->default_log_level = LOGL_DEBUG;
  196. env_set("ethact", "eth@10002000");
  197. env_set("log_hostname", "sandbox");
  198. env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
  199. "testing log_debug\n";
  200. env.uts = uts;
  201. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  202. /* Used by ut_assert macros in the tx_handler */
  203. sandbox_eth_set_priv(0, &env);
  204. log_debug("testing %s\n", "log_debug");
  205. sandbox_eth_set_tx_handler(0, NULL);
  206. /* Check that the callback function was called */
  207. ut_assertnull(env.expected);
  208. gd->default_log_level = old_log_level;
  209. gd->log_fmt = log_get_default_format();
  210. ut_assertok(syslog_test_finish(uts));
  211. return 0;
  212. }
  213. LOG_TEST(log_test_syslog_debug);