public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 6654b6da0590241e72b73c1e86aa7bb9db879b4e 6718 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
 
#! /usr/bin/env python3

# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright Red Hat
# Author: David Gibson <david@gibson.dropbear.id.au>

"""
Test A Simple Socket Transport

transfer.py - Helpers for testing data transfers
"""

import dataclasses
import ipaddress
import time
from typing import Iterator, Optional

import exeter

from . import cmdsite, ip, unshare

# HACK: how long to wait for the server to be ready and listening (s)
SERVER_READY_DELAY = 0.1  # 1/10th of a second


# socat needs IPv6 addresses in square brackets
def socat_ip(ip: ip.Addr) -> str:
    if isinstance(ip, ipaddress.IPv6Address):
        return f'[{ip}]'
    elif isinstance(ip, ipaddress.IPv4Address):
        return f'{ip}'
    raise TypeError


def socat_upload(datafile: str, csite: cmdsite.CmdSite,
                 ssite: cmdsite.CmdSite, connect: str, listen: str) -> None:
    srcdata = csite.output('cat', f'{datafile}')
    with ssite.bg('socat', '-u', f'{listen}', 'STDOUT',
                  capture=cmdsite.Capture.STDOUT) as server:
        time.sleep(SERVER_READY_DELAY)

        # Can't use csite.fg() here, because while we wait for the
        # client to complete we won't be reading from the output pipe
        # of the server, meaning it will freeze once the buffers fill
        with csite.bg('socat', '-u', f'OPEN:{datafile}', f'{connect}') \
             as client:
            res = server.run()
            client.run()
    exeter.assert_eq(srcdata, res.stdout)


def socat_download(datafile: str, csite: cmdsite.CmdSite,
                   ssite: cmdsite.CmdSite,
                   connect: str, listen: str) -> None:
    srcdata = ssite.output('cat', f'{datafile}')
    with ssite.bg('socat', '-u', f'OPEN:{datafile}', f'{listen}'):
        time.sleep(SERVER_READY_DELAY)
        dstdata = csite.output('socat', '-u', f'{connect}', 'STDOUT')
    exeter.assert_eq(srcdata, dstdata)


def _tcp_socat(connectip: ip.Addr, connectport: int,
               listenip: Optional[ip.Addr], listenport: Optional[int],
               fromip: Optional[ip.Addr]) -> tuple[str, str]:
    v6 = isinstance(connectip, ipaddress.IPv6Address)
    if listenport is None:
        listenport = connectport
    if v6:
        connect = f'TCP6:[{connectip}]:{connectport},ipv6only'
        listen = f'TCP6-LISTEN:{listenport},ipv6only'
    else:
        connect = f'TCP4:{connectip}:{connectport}'
        listen = f'TCP4-LISTEN:{listenport}'
    if listenip is not None:
        listen += f',bind={socat_ip(listenip)}'
    if fromip is not None:
        connect += f',bind={socat_ip(fromip)}'
    return (connect, listen)


def tcp_upload(datafile: str, cs: cmdsite.CmdSite, ss: cmdsite.CmdSite,
               connectip: ip.Addr, connectport: int,
               listenip: Optional[ip.Addr] = None,
               listenport: Optional[int] = None,
               fromip: Optional[ip.Addr] = None) -> None:
    connect, listen = _tcp_socat(connectip, connectport, listenip, listenport,
                                 fromip)
    socat_upload(datafile, cs, ss, connect, listen)


def tcp_download(datafile: str, cs: cmdsite.CmdSite, ss: cmdsite.CmdSite,
                 connectip: ip.Addr, connectport: int,
                 listenip: Optional[ip.Addr] = None,
                 listenport: Optional[int] = None,
                 fromip: Optional[ip.Addr] = None) -> None:
    connect, listen = _tcp_socat(connectip, connectport, listenip, listenport,
                                 fromip)
    socat_download(datafile, cs, ss, connect, listen)


def udp_transfer(datafile: str, cs: cmdsite.CmdSite, ss: cmdsite.CmdSite,
                 connectip: ip.Addr, connectport: int,
                 listenip: Optional[ip.Addr] = None,
                 listenport: Optional[int] = None,
                 fromip: Optional[ip.Addr] = None) -> None:
    v6 = isinstance(connectip, ipaddress.IPv6Address)
    if listenport is None:
        listenport = connectport
    if v6:
        connect = f'UDP6:[{connectip}]:{connectport},ipv6only,shut-null'
        listen = f'UDP6-LISTEN:{listenport},ipv6only,null-eof'
    else:
        connect = f'UDP4:{connectip}:{connectport},shut-null'
        listen = f'UDP4-LISTEN:{listenport},null-eof'
    if listenip is not None:
        listen += f',bind={socat_ip(listenip)}'
    if fromip is not None:
        connect += f',bind={socat_ip(fromip)}'

    socat_upload(datafile, cs, ss, connect, listen)


SMALL_DATA = 'small.bin'
BIG_DATA = 'big.bin'
UDP_DATA = 'medium.bin'


@dataclasses.dataclass
class TransferScenario(exeter.Scenario):
    client: cmdsite.CmdSite
    server: cmdsite.CmdSite
    connect_ip: ip.Addr
    connect_port: int
    listen_ip: Optional[ip.Addr] = None
    from_ip: Optional[ip.Addr] = None
    listen_port: Optional[int] = None

    def tcp_upload(self, datafile: str) -> None:
        tcp_upload(datafile, self.client, self.server,
                   self.connect_ip, self.connect_port,
                   listenip=self.listen_ip, listenport=self.listen_port,
                   fromip=self.from_ip)

    @exeter.scenariotest
    def tcp_small_upload(self) -> None:
        self.tcp_upload(SMALL_DATA)

    @exeter.scenariotest
    def tcp_big_upload(self) -> None:
        self.tcp_upload(BIG_DATA)

    def tcp_download(self, datafile: str) -> None:
        tcp_download(datafile, self.client, self.server,
                     self.connect_ip, self.connect_port,
                     listenip=self.listen_ip, listenport=self.listen_port,
                     fromip=self.from_ip)

    @exeter.scenariotest
    def tcp_small_download(self) -> None:
        self.tcp_download(SMALL_DATA)

    @exeter.scenariotest
    def tcp_big_download(self) -> None:
        self.tcp_download(BIG_DATA)

    @exeter.scenariotest
    def udp_transfer(self, datafile: str = UDP_DATA) -> None:
        udp_transfer(datafile, self.client, self.server,
                     self.connect_ip, self.connect_port,
                     listenip=self.listen_ip, listenport=self.listen_port,
                     fromip=self.from_ip)


def local4() -> Iterator[TransferScenario]:
    with unshare.unshare('ns', '-Un') as ns:
        ip.ifup(ns, 'lo')
        yield TransferScenario(client=ns, server=ns,
                               connect_ip=ip.LOOPBACK4,
                               connect_port=10000)


def local6() -> Iterator[TransferScenario]:
    with unshare.unshare('ns', '-Un') as ns:
        ip.ifup(ns, 'lo')
        yield TransferScenario(client=ns, server=ns,
                               connect_ip=ip.LOOPBACK6,
                               connect_port=10000)


def selftests() -> None:
    TransferScenario.test(local4)
    TransferScenario.test(local6)

debug log:

solving 6654b6da ...
found 6654b6da in https://archives.passt.top/passt-dev/20240826020942.545155-12-david@gibson.dropbear.id.au/

applying [1/1] https://archives.passt.top/passt-dev/20240826020942.545155-12-david@gibson.dropbear.id.au/
diff --git a/test/tasst/transfer.py b/test/tasst/transfer.py
new file mode 100644
index 00000000..6654b6da

Checking patch test/tasst/transfer.py...
Applied patch test/tasst/transfer.py cleanly.

index at:
100644 6654b6da0590241e72b73c1e86aa7bb9db879b4e	test/tasst/transfer.py

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).