3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2020 Alexander V. Chernikov
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 from functools import partial
39 parser = argparse.ArgumentParser(description='divert socket tester')
40 parser.add_argument('--sip', type=str, default='', help='IP to listen on')
41 parser.add_argument('--family', type=str, help='inet/inet6')
42 parser.add_argument('--ports', type=str, help='packet ports 1,2,3')
43 parser.add_argument('--match_str', type=str, help='match string to use')
44 parser.add_argument('--count', type=int, default=1,
45 help='Number of messages to receive')
46 parser.add_argument('--test_name', type=str, required=True,
47 help='test name to run')
48 return parser.parse_args()
51 def test_listen_tcp(args):
52 if args.family == 'inet6':
57 ports = [int(port) for port in args.ports.split(',')]
59 s = socket.socket(fam, socket.SOCK_STREAM)
60 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
62 s.bind((args.sip, port))
63 print('binding on {}:{}'.format(args.sip, port))
68 while count < args.count:
69 readable, writable, exceptional = select.select(inputs, [], inputs)
71 (c, address) = s.accept()
72 print('C: {}'.format(address))
74 if args.match_str and args.match_str.encode('utf-8') != data:
75 raise Exception('Expected "{}" but got "{}"'.format(
76 args.match_str, data.decode('utf-8')))
81 def test_listen_udp(args):
82 if args.family == 'inet6':
87 ports = [int(port) for port in args.ports.split(',')]
89 s = socket.socket(fam, socket.SOCK_DGRAM)
90 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
92 s.bind((args.sip, port))
93 print('binding on {}:{}'.format(args.sip, port))
97 while count < args.count:
98 readable, writable, exceptional = select.select(inputs, [], inputs)
100 (data, address) = s.recvfrom(9000)
101 print('C: {}'.format(address))
102 if args.match_str and args.match_str.encode('utf-8') != data:
103 raise Exception('Expected "{}" but got "{}"'.format(
104 args.match_str, data.decode('utf-8')))
110 test_ptr = globals()[args.test_name]
114 if __name__ == '__main__':