syslog_test.c 8.1 KB

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