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