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