Upload a generated file with curl after checking that an unknown target
is refused, then check its size and CRC32 in U-Boot. The test is skipped
unless the board environment provides env__net_httpd_file.

Signed-off-by: Vitaliy Sochnev <[email protected]>
---
 test/py/tests/test_net.py | 95 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/test/py/tests/test_net.py b/test/py/tests/test_net.py
index a2007c2fd..8c4c67f2d 100644
--- a/test/py/tests/test_net.py
+++ b/test/py/tests/test_net.py
@@ -72,6 +72,17 @@ For example:
         'timeout': 50000,
     }
 
+    # Details regarding a file that may be written to U-Boot using the httpd
+    # command. This variable may be omitted or set to None if httpd testing is
+    # not possible or desired. The test uses host-side curl to upload a
+    # generated file to U-Boot. The optional httpd_url entry may be used when
+    # the host must use a forwarded address instead of U-Boot's ipaddr value.
+    env__net_httpd_file = {
+        'addr': 0x10000000,
+        'size': 4096,
+        'timeout': 50000,
+    }
+
     # Details regarding a file that may be read from a NFS server. This 
variable
     # may be omitted or set to None if NFS testing is not possible or desired.
     env__net_nfs_readable_file = {
@@ -554,3 +565,87 @@ def test_net_tftpsrv(ubman):
 
     output = ubman.run_command("crc32 $fileaddr $filesize")
     assert crc in output
+
[email protected]("cmd_crc32")
[email protected]("cmd_httpd")
[email protected]("curl")
+def test_net_httpd(ubman):
+    """Test the httpd command.
+
+    A file is generated on the host and uploaded to U-Boot using HTTP after
+    an upload to an unknown target has been refused. The file is then
+    validated in U-Boot using its size and CRC32.
+
+    The details of the file to upload are provided by the boardenv_* file;
+    see the comment at the beginning of this file.
+    """
+
+    if not net_set_up:
+        pytest.skip("Network not initialized")
+
+    f = ubman.config.env.get("env__net_httpd_file", None)
+    if not f:
+        pytest.skip("No httpd file to write")
+
+    addr = f.get("addr", None)
+    if not addr:
+        addr = utils.find_ram_base(ubman)
+
+    timeout = f.get("timeout", ubman.p.timeout)
+    timeout_secs = max(1, (timeout + 999) // 1000)
+    size = f.get("size", 4096)
+    url = f.get("httpd_url", None)
+    data = bytes([i % 251 for i in range(size)])
+    crc = "%08x" % (zlib.crc32(data) & 0xffffffff)
+
+    ip = ubman.run_command("echo $ipaddr").strip()
+    if not ip:
+        pytest.skip("No U-Boot IP address")
+    if not url:
+        url = "http://%s"; % ip
+
+    def upload(target):
+        return utils.run_and_log(
+            ubman,
+            [
+                "curl",
+                "--silent",
+                "--max-time",
+                str(timeout_secs),
+                "--output",
+                "/dev/null",
+                "--write-out",
+                "%{http_code}",
+                "--header",
+                "Content-Type: application/octet-stream",
+                "--data-binary",
+                "@" + tmp.name,
+                "%s/upload/%s" % (url, target),
+            ],
+        ).strip()
+
+    ubman.run_command("setenv httpd_targets test")
+    with tempfile.NamedTemporaryFile() as tmp:
+        tmp.write(data)
+        tmp.flush()
+
+        done = False
+        with ubman.temporary_timeout(timeout):
+            try:
+                ubman.run_command("httpd %x" % addr, wait_for_prompt=False)
+                ubman.wait_for("Listening for HTTP uploads")
+                assert upload("unknown") == "400"
+                assert upload("test") == "200"
+                ubman.wait_for("Bytes transferred = %d" % size)
+                ubman.wait_for(ubman.prompt)
+                done = True
+            finally:
+                if not done:
+                    ubman.ctrlc()
+                    ubman.drain_console()
+
+    output = ubman.run_command("echo $httpd_target $filesize")
+    assert "test %x" % size in output
+
+    output = ubman.run_command("crc32 $fileaddr $filesize")
+    assert crc in output
-- 
2.55.0

Reply via email to