diff mbox series

[ovs-dev,1/3] tests: sendpkt: Allow different input formats.

Message ID 20240531214635.2084937-2-i.maximets@ovn.org
State Accepted
Commit 40f0ac48ffee7936e3c87c122171f5a1143fbd2e
Delegated to: Ilya Maximets
Headers show
Series tests: Use compose-packet for sendpkt calls. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Ilya Maximets May 31, 2024, 9:45 p.m. UTC
We require python 3, so instead of manually parsing bytes on input we
can use built-in bytes.fromhex().  This function ignores whitespaces,
so we can use different input formats - the old style space-separated
bytes as well as pure hex strings provided by ovs-ofctl compose-packet
and ovs-pcap.

Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
---
 tests/sendpkt.py | 26 ++++----------------------
 1 file changed, 4 insertions(+), 22 deletions(-)

Comments

Simon Horman June 3, 2024, 3:06 p.m. UTC | #1
On Fri, May 31, 2024 at 11:45:10PM +0200, Ilya Maximets wrote:
> We require python 3, so instead of manually parsing bytes on input we
> can use built-in bytes.fromhex().  This function ignores whitespaces,
> so we can use different input formats - the old style space-separated
> bytes as well as pure hex strings provided by ovs-ofctl compose-packet
> and ovs-pcap.
> 
> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>

Acked-by: Simon Horman <horms@ovn.org>
Eelco Chaudron June 5, 2024, 1:44 p.m. UTC | #2
On 31 May 2024, at 23:45, Ilya Maximets wrote:

> We require python 3, so instead of manually parsing bytes on input we
> can use built-in bytes.fromhex().  This function ignores whitespaces,
> so we can use different input formats - the old style space-separated
> bytes as well as pure hex strings provided by ovs-ofctl compose-packet
> and ovs-pcap.
>
> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>

Thanks for cleaning this up! This patch looks good to me.

Acked-by: Eelco Chaudron <echaudro@redhat.com>
diff mbox series

Patch

diff --git a/tests/sendpkt.py b/tests/sendpkt.py
index 49ac45275..7cbea5165 100755
--- a/tests/sendpkt.py
+++ b/tests/sendpkt.py
@@ -48,28 +48,10 @@  if len(args) < 2:
 if options.packet_type != "eth":
     parser.error('invalid argument to "-t"/"--type". Allowed value is "eth".')
 
-# store the hex bytes with 0x appended at the beginning
-# if not present in the user input and validate the hex bytes
-hex_list = []
-for a in args[1:]:
-    if a[:2] != "0x":
-        hex_byte = "0x" + a
-    else:
-        hex_byte = a
-    try:
-        temp = int(hex_byte, 0)
-    except:
-        parser.error("invalid hex byte " + a)
-
-    if temp > 0xff:
-        parser.error("hex byte " + a + " cannot be greater than 0xff!")
-
-    hex_list.append(temp)
-
-if sys.version_info < (3, 0):
-    pkt = "".join(map(chr, hex_list))
-else:
-    pkt = bytes(hex_list)
+# Strip '0x' prefixes from hex input, combine into a single string and
+# convert to bytes.
+hex_str = "".join([a[2:] if a.startswith("0x") else a for a in args[1:]])
+pkt = bytes.fromhex(hex_str)
 
 try:
     sockfd = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)