syslog_test.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. DECLARE_GLOBAL_DATA_PTR;
  20. /**
  21. * struct sb_log_env - private data for sandbox ethernet driver
  22. *
  23. * This structure is used for the private data of the sandbox ethernet
  24. * driver.
  25. *
  26. * @expected: string expected to be written by the syslog driver
  27. * @uts: unit test state
  28. */
  29. struct sb_log_env {
  30. const char *expected;
  31. struct unit_test_state *uts;
  32. };
  33. /**
  34. * sb_log_tx_handler() - transmit callback function
  35. *
  36. * This callback function is invoked when a network package is sent using the
  37. * sandbox Ethernet driver. The private data of the driver holds a sb_log_env
  38. * structure with the unit test state and the expected UDP payload.
  39. *
  40. * The following checks are executed:
  41. *
  42. * * the Ethernet packet indicates a IP broadcast message
  43. * * the IP header is for a local UDP broadcast message to port 514
  44. * * the UDP payload matches the expected string
  45. *
  46. * After testing the pointer to the expected string is set to NULL to signal
  47. * that the callback function has been called.
  48. *
  49. * @dev: sandbox ethernet device
  50. * @packet: Ethernet packet
  51. * @len: length of Ethernet packet
  52. * Return: 0 = success
  53. */
  54. static int sb_log_tx_handler(struct udevice *dev, void *packet,
  55. unsigned int len)
  56. {
  57. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  58. struct sb_log_env *env = priv->priv;
  59. /* uts is updated by the ut_assert* macros */
  60. struct unit_test_state *uts = env->uts;
  61. char *buf = packet;
  62. struct ethernet_hdr *eth_hdr = packet;
  63. struct ip_udp_hdr *ip_udp_hdr;
  64. /* Check Ethernet header */
  65. ut_asserteq_mem(&eth_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
  66. ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
  67. /* Check IP header */
  68. buf += sizeof(struct ethernet_hdr);
  69. ip_udp_hdr = (struct ip_udp_hdr *)buf;
  70. ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
  71. ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
  72. ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
  73. ut_asserteq(UDP_HDR_SIZE + strlen(env->expected) + 1,
  74. ntohs(ip_udp_hdr->udp_len));
  75. /* Check payload */
  76. buf += sizeof(struct ip_udp_hdr);
  77. ut_asserteq_mem(env->expected, buf,
  78. ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
  79. /* Signal that the callback function has been executed */
  80. env->expected = NULL;
  81. return 0;
  82. }
  83. /**
  84. * syslog_test_log_err() - test log_err() function
  85. *
  86. * @uts: unit test state
  87. * Return: 0 = success
  88. */
  89. static int syslog_test_log_err(struct unit_test_state *uts)
  90. {
  91. int old_log_level = gd->default_log_level;
  92. struct sb_log_env env;
  93. gd->log_fmt = LOGF_DEFAULT;
  94. gd->default_log_level = LOGL_INFO;
  95. env_set("ethact", "eth@10002000");
  96. env_set("log_hostname", "sandbox");
  97. env.expected = "<3>sandbox uboot: syslog_test_log_err() "
  98. "testing log_err\n";
  99. env.uts = uts;
  100. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  101. /* Used by ut_assert macros in the tx_handler */
  102. sandbox_eth_set_priv(0, &env);
  103. log_err("testing %s\n", "log_err");
  104. /* Check that the callback function was called */
  105. sandbox_eth_set_tx_handler(0, NULL);
  106. gd->default_log_level = old_log_level;
  107. return 0;
  108. }
  109. LOG_TEST(syslog_test_log_err);
  110. /**
  111. * syslog_test_log_warning() - test log_warning() function
  112. *
  113. * @uts: unit test state
  114. * Return: 0 = success
  115. */
  116. static int syslog_test_log_warning(struct unit_test_state *uts)
  117. {
  118. int old_log_level = gd->default_log_level;
  119. struct sb_log_env env;
  120. gd->log_fmt = LOGF_DEFAULT;
  121. gd->default_log_level = LOGL_INFO;
  122. env_set("ethact", "eth@10002000");
  123. env_set("log_hostname", "sandbox");
  124. env.expected = "<4>sandbox uboot: syslog_test_log_warning() "
  125. "testing log_warning\n";
  126. env.uts = uts;
  127. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  128. /* Used by ut_assert macros in the tx_handler */
  129. sandbox_eth_set_priv(0, &env);
  130. log_warning("testing %s\n", "log_warning");
  131. sandbox_eth_set_tx_handler(0, NULL);
  132. /* Check that the callback function was called */
  133. ut_assertnull(env.expected);
  134. gd->default_log_level = old_log_level;
  135. return 0;
  136. }
  137. LOG_TEST(syslog_test_log_warning);
  138. /**
  139. * syslog_test_log_notice() - test log_notice() function
  140. *
  141. * @uts: unit test state
  142. * Return: 0 = success
  143. */
  144. static int syslog_test_log_notice(struct unit_test_state *uts)
  145. {
  146. int old_log_level = gd->default_log_level;
  147. struct sb_log_env env;
  148. gd->log_fmt = LOGF_DEFAULT;
  149. gd->default_log_level = LOGL_INFO;
  150. env_set("ethact", "eth@10002000");
  151. env_set("log_hostname", "sandbox");
  152. env.expected = "<5>sandbox uboot: syslog_test_log_notice() "
  153. "testing log_notice\n";
  154. env.uts = uts;
  155. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  156. /* Used by ut_assert macros in the tx_handler */
  157. sandbox_eth_set_priv(0, &env);
  158. log_notice("testing %s\n", "log_notice");
  159. sandbox_eth_set_tx_handler(0, NULL);
  160. /* Check that the callback function was called */
  161. ut_assertnull(env.expected);
  162. gd->default_log_level = old_log_level;
  163. return 0;
  164. }
  165. LOG_TEST(syslog_test_log_notice);
  166. /**
  167. * syslog_test_log_info() - test log_info() function
  168. *
  169. * @uts: unit test state
  170. * Return: 0 = success
  171. */
  172. static int syslog_test_log_info(struct unit_test_state *uts)
  173. {
  174. int old_log_level = gd->default_log_level;
  175. struct sb_log_env env;
  176. gd->log_fmt = LOGF_DEFAULT;
  177. gd->default_log_level = LOGL_INFO;
  178. env_set("ethact", "eth@10002000");
  179. env_set("log_hostname", "sandbox");
  180. env.expected = "<6>sandbox uboot: syslog_test_log_info() "
  181. "testing log_info\n";
  182. env.uts = uts;
  183. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  184. /* Used by ut_assert macros in the tx_handler */
  185. sandbox_eth_set_priv(0, &env);
  186. log_info("testing %s\n", "log_info");
  187. sandbox_eth_set_tx_handler(0, NULL);
  188. /* Check that the callback function was called */
  189. ut_assertnull(env.expected);
  190. gd->default_log_level = old_log_level;
  191. return 0;
  192. }
  193. LOG_TEST(syslog_test_log_info);
  194. /**
  195. * syslog_test_log_debug() - test log_debug() function
  196. *
  197. * @uts: unit test state
  198. * Return: 0 = success
  199. */
  200. static int syslog_test_log_debug(struct unit_test_state *uts)
  201. {
  202. int old_log_level = gd->default_log_level;
  203. struct sb_log_env env;
  204. gd->log_fmt = LOGF_DEFAULT;
  205. gd->default_log_level = LOGL_DEBUG;
  206. env_set("ethact", "eth@10002000");
  207. env_set("log_hostname", "sandbox");
  208. env.expected = "<7>sandbox uboot: syslog_test_log_debug() "
  209. "testing log_debug\n";
  210. env.uts = uts;
  211. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  212. /* Used by ut_assert macros in the tx_handler */
  213. sandbox_eth_set_priv(0, &env);
  214. log_debug("testing %s\n", "log_debug");
  215. sandbox_eth_set_tx_handler(0, NULL);
  216. /* Check that the callback function was called */
  217. ut_assertnull(env.expected);
  218. gd->default_log_level = old_log_level;
  219. return 0;
  220. }
  221. LOG_TEST(syslog_test_log_debug);
  222. /**
  223. * syslog_test_log_nodebug() - test logging level filter
  224. *
  225. * Verify that log_debug() does not lead to a log message if the logging level
  226. * is set to LOGL_INFO.
  227. *
  228. * @uts: unit test state
  229. * Return: 0 = success
  230. */
  231. static int syslog_test_log_nodebug(struct unit_test_state *uts)
  232. {
  233. int old_log_level = gd->default_log_level;
  234. struct sb_log_env env;
  235. gd->log_fmt = LOGF_DEFAULT;
  236. gd->default_log_level = LOGL_INFO;
  237. env_set("ethact", "eth@10002000");
  238. env_set("log_hostname", "sandbox");
  239. env.expected = "<7>sandbox uboot: syslog_test_log_nodebug() "
  240. "testing log_debug\n";
  241. env.uts = uts;
  242. sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
  243. /* Used by ut_assert macros in the tx_handler */
  244. sandbox_eth_set_priv(0, &env);
  245. log_debug("testing %s\n", "log_debug");
  246. sandbox_eth_set_tx_handler(0, NULL);
  247. /* Check that the callback function was not called */
  248. ut_assertnonnull(env.expected);
  249. gd->default_log_level = old_log_level;
  250. return 0;
  251. }
  252. LOG_TEST(syslog_test_log_nodebug);