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