[Concept,10/16] test: pxe: Add a few tests for pxe functions

Message ID 20260109015323.3411528-11-sjg@u-boot.org
State New
Headers
Series test: pxe: Add some decent tests for the PXE/extlinux parser |

Commit Message

Simon Glass Jan. 9, 2026, 1:53 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add some tests for:

- pxe_get_file_size(): missing env var, valid hex values, invalid format
- format_mac_pxe(): buffer too small, valid MAC format verification
- get_pxelinux_path(): path too long error

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 test/boot/pxe.c                  | 107 ++++++++++++++++++++++++++++++-
 test/py/tests/test_pxe_parser.py |   7 ++
 2 files changed, 113 insertions(+), 1 deletion(-)
  

Patch

diff --git a/test/boot/pxe.c b/test/boot/pxe.c
index 3ccb0c977e9..e6d95d07d41 100644
--- a/test/boot/pxe.c
+++ b/test/boot/pxe.c
@@ -13,11 +13,14 @@ 
 #include <fs_legacy.h>
 #include <linux/libfdt.h>
 #include <mapmem.h>
+#include <net-common.h>
 #include <pxe_utils.h>
 #include <test/test.h>
 #include <test/ut.h>
 
-/* Define test macro for pxe suite - no init function needed */
+/* Define test macros for pxe suite */
+#define PXE_TEST(_name, _flags) \
+	UNIT_TEST(_name, _flags, pxe)
 #define PXE_TEST_ARGS(_name, _flags, ...) \
 	UNIT_TEST_ARGS(_name, _flags, pxe, __VA_ARGS__)
 
@@ -483,3 +486,105 @@  static int pxe_test_errors_norun(struct unit_test_state *uts)
 PXE_TEST_ARGS(pxe_test_errors_norun, UTF_CONSOLE | UTF_MANUAL,
 	{ "fs_image", UT_ARG_STR },
 	{ "cfg_path", UT_ARG_STR });
+
+/**
+ * Test pxe_get_file_size() function
+ *
+ * This tests reading the filesize from the environment variable.
+ */
+static int pxe_test_get_file_size(struct unit_test_state *uts)
+{
+	ulong size;
+
+	/* Test with no filesize set - should return -ENOENT */
+	env_set("filesize", NULL);
+	ut_asserteq(-ENOENT, pxe_get_file_size(&size));
+
+	/* Test with valid hex filesize */
+	env_set("filesize", "1234");
+	ut_assertok(pxe_get_file_size(&size));
+	ut_asserteq(0x1234, size);
+
+	/* Test with larger value */
+	env_set("filesize", "abcdef");
+	ut_assertok(pxe_get_file_size(&size));
+	ut_asserteq(0xabcdef, size);
+
+	/* Test with invalid (non-hex) value */
+	env_set("filesize", "not_hex");
+	ut_asserteq(-EINVAL, pxe_get_file_size(&size));
+
+	/* Clean up */
+	env_set("filesize", NULL);
+
+	return 0;
+}
+PXE_TEST(pxe_test_get_file_size, 0);
+
+/**
+ * Test format_mac_pxe() function
+ *
+ * This tests MAC address formatting for PXE boot paths.
+ */
+static int pxe_test_format_mac(struct unit_test_state *uts)
+{
+	char buf[21];
+
+	/* Test with buffer too small */
+	ut_asserteq(-ENOSPC, format_mac_pxe(buf, 20));
+	ut_asserteq(-ENOSPC, format_mac_pxe(buf, 1));
+
+	/* Test with valid buffer - sandbox has an ethernet device */
+	ut_asserteq(1, format_mac_pxe(buf, sizeof(buf)));
+
+	/* Verify format: 01-xx-xx-xx-xx-xx-xx */
+	ut_asserteq(20, strlen(buf));
+	ut_asserteq('0', buf[0]);
+	ut_asserteq('1', buf[1]);
+	ut_asserteq('-', buf[2]);
+	ut_asserteq('-', buf[5]);
+	ut_asserteq('-', buf[8]);
+	ut_asserteq('-', buf[11]);
+	ut_asserteq('-', buf[14]);
+	ut_asserteq('-', buf[17]);
+
+	return 0;
+}
+PXE_TEST(pxe_test_format_mac, UTF_ETH_BOOTDEV);
+
+/**
+ * Test get_pxelinux_path() with path too long
+ *
+ * This tests the path length check in get_pxelinux_path().
+ */
+static int pxe_test_pxelinux_path_norun(struct unit_test_state *uts)
+{
+	const char *fs_image = ut_str(PXE_ARG_FS_IMAGE);
+	struct pxe_test_info info;
+	struct pxe_context ctx;
+	char path[600];
+
+	ut_assertnonnull(fs_image);
+	info.uts = uts;
+
+	/* Bind the filesystem image */
+	ut_assertok(run_commandf("host bind 0 %s", fs_image));
+
+	/* Set up the PXE context */
+	ut_assertok(pxe_setup_ctx(&ctx, pxe_test_getfile, &info, false, "/",
+				  false, false, NULL));
+
+	/* Create a path that's too long (> 512 - 13 for "pxelinux.cfg/") */
+	memset(path, 'a', sizeof(path) - 1);
+	path[sizeof(path) - 1] = '\0';
+
+	/* Should fail with -ENAMETOOLONG */
+	ut_asserteq(-ENAMETOOLONG, get_pxelinux_path(&ctx, path,
+						     PXE_LOAD_ADDR));
+
+	pxe_destroy_ctx(&ctx);
+
+	return 0;
+}
+PXE_TEST_ARGS(pxe_test_pxelinux_path_norun, UTF_CONSOLE | UTF_MANUAL,
+	{ "fs_image", UT_ARG_STR });
diff --git a/test/py/tests/test_pxe_parser.py b/test/py/tests/test_pxe_parser.py
index 2afe3a93b86..0d957c08219 100644
--- a/test/py/tests/test_pxe_parser.py
+++ b/test/py/tests/test_pxe_parser.py
@@ -420,3 +420,10 @@  class TestPxeParser:
         with ubman.log.section('Test PXE errors'):
             ubman.run_ut('pxe', 'pxe_test_errors',
                          fs_image=fs_img, cfg_path=cfg_path)
+
+    def test_pxe_pxelinux_path(self, ubman, pxe_image):
+        """Test get_pxelinux_path() path length checking"""
+        fs_img, cfg_path = pxe_image
+        with ubman.log.section('Test PXE pxelinux path'):
+            ubman.run_ut('pxe', 'pxe_test_pxelinux_path',
+                         fs_image=fs_img)