From patchwork Tue Nov 8 06:17:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Liu X-Patchwork-Id: 622632 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp2726664pvb; Mon, 7 Nov 2022 22:18:46 -0800 (PST) X-Google-Smtp-Source: AMsMyM4WKf7wkZ12dF+rQ/tAusBBUI4indVYSMNiqAAfRD9YW2P1QT0mWupXry/Aybhj6xDUL0+3 X-Received: by 2002:a17:907:1dd7:b0:7ae:41e1:cdfb with SMTP id og23-20020a1709071dd700b007ae41e1cdfbmr16952967ejc.58.1667888326035; Mon, 07 Nov 2022 22:18:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667888326; cv=none; d=google.com; s=arc-20160816; b=dQE/RgzOX6+zq/J3CC0RYWcWrupbxg2gHJ+feULp7KqGVSjuiuj6yZYi5FEjxU0AAG +uPV0dcC6yrBvwTWLVqed7aWtyXe9/5qRs+Y63z03a6+aTVTk/zY9y6H7VBraimU/rQl jrsfm5KBInmngppnaUTovY50pAPSBk8byHIl6ZmkGVmQTSS4xKI62JjhKohRJDm8SUoI 61MoJQ+RRD66apZ9qRbP5phlg97IMQwFSQb6lFay7mwARv1ERn5Y+IId6j2wvpAEo/oh xs36YNpl4s/pngAjMvI97H4KGXqXXKPZyTdJ11dJKeKAPYTwn02dha+P9JU1nytVFoJW XeuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=osxDeooOBCZuj5u29NDaKrzN5+DMle6pnEvzDf0qpyU=; b=eqHkpgBz0n9wEymrnKwIfZST1YqtnTlY9AFlyJjGaGu4lvumO3HgpIxDkwYJ4xx0DR Vw60pFzWmf9W/XLov4qXb+qMPHo3KbplkQpE2VmcKS6/LxrDDQSaRZDsRffh96tkRffL zUvJbFqZiQSLMjG6TMiBIDuXJtvw+T6dL6AFOG0NZbRVVBVODX21aN5jag2uc37kLmJJ ZELgVmyna1yzVZXVxPqABEUe6H83eVvlE6xWdpGiFimQjSLkHodrEp4kpck59ViePXBI +5RPZFrufE7odD+TTy49JNOKkLFurXeImDsXLSP4nA4vU0AcDMLUSbB/sjKpFR7kqRzK zQUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NOP0rtP1; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ho11-20020a1709070e8b00b007ae754729d1si3356560ejc.883.2022.11.07.22.18.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 22:18:46 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NOP0rtP1; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 27AC785059; Tue, 8 Nov 2022 07:18:18 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="NOP0rtP1"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4FD0D85020; Tue, 8 Nov 2022 07:17:55 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 54A5684FB5 for ; Tue, 8 Nov 2022 07:17:50 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=paul.liu@linaro.org Received: by mail-pf1-x433.google.com with SMTP id g62so12906914pfb.10 for ; Mon, 07 Nov 2022 22:17:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=osxDeooOBCZuj5u29NDaKrzN5+DMle6pnEvzDf0qpyU=; b=NOP0rtP1v+4jXGTYWqcrZ+ZlKnvSzOd8oOy4Z2gnVhqHJaPh0KlygSUZX+deOOuFvQ CxY0XFHG8p9lzTbexPhO1T4iJfKfNNmS3u1skpa5+dqT9UJ3rcnen9O60KlX3JFCwUl+ jGd1m4VOVcDeRiyN8LJv4VN7T9BXtQ8qCtaw1LPe0hh2jTEQwRX7RymhYe3UayOJtyps hqR7DCmJ+any2Af8AzRc392365J/72GpdDwF7Vt2X2KF/FMr6J52ehozlmNy3jyhWkpD BjsEYI3q4D7aV3uoRniMAmuCZrUMgyj7cKrjLDZRNcQcxd2KV0ST4ZmRQRZ5vADnHyrq t8bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=osxDeooOBCZuj5u29NDaKrzN5+DMle6pnEvzDf0qpyU=; b=BTFuaZDShSot0TjxDKe38aD6SbzdbjDhSg/H1tqCqclLbl+Hlg7u+rIx/nAc6fJjIa aCtL87cf3M/iF3KewW31eVFC/ANvamxOg7Yix5eucoinUQiMiv2Df+XIWgolwhddz4Pu VzWCbSDxNW3ZZKPXkkC1LdgYgmF6cv/x7Yf3MK+J8kqZev7+lps0oK5TWYFJuc8crPxc GrRwHVlx21SPfbxoXvJ5r2CQEOAiVYIVKAtmA4wIGrZjVTNTp8s1k0ZeuEETxxomo7fb 2ioyXIX0QnS/VhoxQx4eFNX+b5Nu1RsRGE7mnpX9/BrQgDNSmw/KdjLWauiQY4lJ0t1s YJdA== X-Gm-Message-State: ACrzQf1N1mz4tVX+24mswYhpOG+Y3Almi5IBlH/m5UFBICLTf/AGrocs T7FZZ4UhSVqpYxH/ZajyTXCK98CPsveGgw2/ X-Received: by 2002:a65:45ce:0:b0:46f:fcd2:a5ae with SMTP id m14-20020a6545ce000000b0046ffcd2a5aemr30368752pgr.149.1667888268403; Mon, 07 Nov 2022 22:17:48 -0800 (PST) Received: from localhost ([111.184.129.17]) by smtp.gmail.com with ESMTPSA id w129-20020a623087000000b0056ba6952e40sm5516814pfw.181.2022.11.07.22.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 22:17:47 -0800 (PST) From: "Ying-Chun Liu (PaulLiu)" X-Google-Original-From: "Ying-Chun Liu (PaulLiu)" To: u-boot@lists.denx.de Cc: "Ying-Chun Liu (PaulLiu)" , Christian Gmeiner , Joe Hershberger , Michal Simek , Ramon Fried , Simon Glass Subject: [PATCH v20 4/4] test: cmd: add test for wget command. Date: Tue, 8 Nov 2022 14:17:31 +0800 Message-Id: <20221108061731.1589327-5-paulliu@debian.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221108061731.1589327-1-paulliu@debian.org> References: <20221108061731.1589327-1-paulliu@debian.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean From: "Ying-Chun Liu (PaulLiu)" Simulate a TCP HTTP server's response for testing wget command. Signed-off-by: Ying-Chun Liu (PaulLiu) Cc: Christian Gmeiner Cc: Joe Hershberger Cc: Michal Simek Cc: Ramon Fried Cc: Simon Glass Reviewed-by: Simon Glass Reviewed-by: Ramon Fried --- test/cmd/Makefile | 1 + test/cmd/wget.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 test/cmd/wget.c diff --git a/test/cmd/Makefile b/test/cmd/Makefile index 6dd6e81875..bc961df3dc 100644 --- a/test/cmd/Makefile +++ b/test/cmd/Makefile @@ -20,3 +20,4 @@ ifdef CONFIG_SANDBOX obj-$(CONFIG_CMD_SETEXPR) += setexpr.o endif obj-$(CONFIG_CMD_TEMPERATURE) += temperature.o +obj-$(CONFIG_CMD_WGET) += wget.o diff --git a/test/cmd/wget.c b/test/cmd/wget.c new file mode 100644 index 0000000000..ed83fc94a5 --- /dev/null +++ b/test/cmd/wget.c @@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2022 Linaro + * + * (C) Copyright 2022 + * Ying-Chun Liu (PaulLiu) + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHIFT_TO_TCPHDRLEN_FIELD(x) ((x) << 4) +#define LEN_B_TO_DW(x) ((x) >> 2) + +static int sb_arp_handler(struct udevice *dev, void *packet, + unsigned int len) +{ + struct eth_sandbox_priv *priv = dev_get_priv(dev); + struct arp_hdr *arp = packet + ETHER_HDR_SIZE; + int ret = 0; + + if (ntohs(arp->ar_op) == ARPOP_REQUEST) { + priv->fake_host_ipaddr = net_read_ip(&arp->ar_spa); + + ret = sandbox_eth_recv_arp_req(dev); + if (ret) + return ret; + ret = sandbox_eth_arp_req_to_reply(dev, packet, len); + return ret; + } + + return -EPROTONOSUPPORT; +} + +static int sb_syn_handler(struct udevice *dev, void *packet, + unsigned int len) +{ + struct eth_sandbox_priv *priv = dev_get_priv(dev); + struct ethernet_hdr *eth = packet; + struct ip_tcp_hdr *tcp = packet + ETHER_HDR_SIZE; + struct ethernet_hdr *eth_send; + struct ip_tcp_hdr *tcp_send; + + /* Don't allow the buffer to overrun */ + if (priv->recv_packets >= PKTBUFSRX) + return 0; + + eth_send = (void *)priv->recv_packet_buffer[priv->recv_packets]; + memcpy(eth_send->et_dest, eth->et_src, ARP_HLEN); + memcpy(eth_send->et_src, priv->fake_host_hwaddr, ARP_HLEN); + eth_send->et_protlen = htons(PROT_IP); + tcp_send = (void *)eth_send + ETHER_HDR_SIZE; + tcp_send->tcp_src = tcp->tcp_dst; + tcp_send->tcp_dst = tcp->tcp_src; + tcp_send->tcp_seq = htonl(0); + tcp_send->tcp_ack = htonl(ntohl(tcp->tcp_seq) + 1); + tcp_send->tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE)); + tcp_send->tcp_flags = TCP_SYN | TCP_ACK; + tcp_send->tcp_win = htons(PKTBUFSRX * TCP_MSS >> TCP_SCALE); + tcp_send->tcp_xsum = 0; + tcp_send->tcp_ugr = 0; + tcp_send->tcp_xsum = tcp_set_pseudo_header((uchar *)tcp_send, + tcp->ip_src, + tcp->ip_dst, + TCP_HDR_SIZE, + IP_TCP_HDR_SIZE); + net_set_ip_header((uchar *)tcp_send, + tcp->ip_src, + tcp->ip_dst, + IP_TCP_HDR_SIZE, + IPPROTO_TCP); + + priv->recv_packet_length[priv->recv_packets] = + ETHER_HDR_SIZE + IP_TCP_HDR_SIZE; + ++priv->recv_packets; + + return 0; +} + +static int sb_ack_handler(struct udevice *dev, void *packet, + unsigned int len) +{ + struct eth_sandbox_priv *priv = dev_get_priv(dev); + struct ethernet_hdr *eth = packet; + struct ip_tcp_hdr *tcp = packet + ETHER_HDR_SIZE; + struct ethernet_hdr *eth_send; + struct ip_tcp_hdr *tcp_send; + void *data; + int pkt_len; + int payload_len = 0; + const char *payload1 = "HTTP/1.1 200 OK\r\n" + "Content-Length: 30\r\n\r\n\r\n" + "Hi\r\n"; + + /* Don't allow the buffer to overrun */ + if (priv->recv_packets >= PKTBUFSRX) + return 0; + + eth_send = (void *)priv->recv_packet_buffer[priv->recv_packets]; + memcpy(eth_send->et_dest, eth->et_src, ARP_HLEN); + memcpy(eth_send->et_src, priv->fake_host_hwaddr, ARP_HLEN); + eth_send->et_protlen = htons(PROT_IP); + tcp_send = (void *)eth_send + ETHER_HDR_SIZE; + tcp_send->tcp_src = tcp->tcp_dst; + tcp_send->tcp_dst = tcp->tcp_src; + data = (void *)tcp_send + IP_TCP_HDR_SIZE; + + if (ntohl(tcp->tcp_seq) == 1 && ntohl(tcp->tcp_ack) == 1) { + tcp_send->tcp_seq = htonl(ntohl(tcp->tcp_ack)); + tcp_send->tcp_ack = htonl(ntohl(tcp->tcp_seq) + 1); + payload_len = strlen(payload1); + memcpy(data, payload1, payload_len); + tcp_send->tcp_flags = TCP_ACK; + } else if (ntohl(tcp->tcp_seq) == 2) { + tcp_send->tcp_seq = htonl(ntohl(tcp->tcp_ack)); + tcp_send->tcp_ack = htonl(ntohl(tcp->tcp_seq) + 1); + payload_len = 0; + tcp_send->tcp_flags = TCP_ACK | TCP_FIN; + } + + tcp_send->tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE)); + tcp_send->tcp_win = htons(PKTBUFSRX * TCP_MSS >> TCP_SCALE); + tcp_send->tcp_xsum = 0; + tcp_send->tcp_ugr = 0; + pkt_len = IP_TCP_HDR_SIZE + payload_len; + tcp_send->tcp_xsum = tcp_set_pseudo_header((uchar *)tcp_send, + tcp->ip_src, + tcp->ip_dst, + pkt_len - IP_HDR_SIZE, + pkt_len); + net_set_ip_header((uchar *)tcp_send, + tcp->ip_src, + tcp->ip_dst, + pkt_len, + IPPROTO_TCP); + + if (ntohl(tcp->tcp_seq) == 1 || ntohl(tcp->tcp_seq) == 2) { + priv->recv_packet_length[priv->recv_packets] = + ETHER_HDR_SIZE + IP_TCP_HDR_SIZE + payload_len; + ++priv->recv_packets; + } + + return 0; +} + +static int sb_http_handler(struct udevice *dev, void *packet, + unsigned int len) +{ + struct ethernet_hdr *eth = packet; + struct ip_hdr *ip; + struct ip_tcp_hdr *tcp; + + if (ntohs(eth->et_protlen) == PROT_ARP) { + return sb_arp_handler(dev, packet, len); + } else if (ntohs(eth->et_protlen) == PROT_IP) { + ip = packet + ETHER_HDR_SIZE; + if (ip->ip_p == IPPROTO_TCP) { + tcp = packet + ETHER_HDR_SIZE; + if (tcp->tcp_flags == TCP_SYN) + return sb_syn_handler(dev, packet, len); + else if (tcp->tcp_flags & TCP_ACK && !(tcp->tcp_flags & TCP_SYN)) + return sb_ack_handler(dev, packet, len); + return 0; + } + return -EPROTONOSUPPORT; + } + + return -EPROTONOSUPPORT; +} + +static int net_test_wget(struct unit_test_state *uts) +{ + sandbox_eth_set_tx_handler(0, sb_http_handler); + sandbox_eth_set_priv(0, uts); + + env_set("ethact", "eth@10002000"); + env_set("ethrotate", "no"); + env_set("loadaddr", "0x20000"); + ut_assertok(run_command("wget ${loadaddr} 1.1.2.2:/index.html", 0)); + + sandbox_eth_set_tx_handler(0, NULL); + + ut_assertok(console_record_reset_enable()); + run_command("md5sum ${loadaddr} ${filesize}", 0); + ut_assert_nextline("md5 for 00020000 ... 0002001f ==> 234af48e94b0085060249ecb5942ab57"); + ut_assertok(ut_check_console_end(uts)); + + return 0; +} + +LIB_TEST(net_test_wget, 0);