syslog_test.c 6.6 KB

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