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
32 from functools import partial
40 parser = argparse.ArgumentParser(description='divert socket tester')
41 parser.add_argument('--sip', type=str, default='', help='IP to listen on')
42 parser.add_argument('--family', type=str, help='inet/inet6')
43 parser.add_argument('--ports', type=str, help='packet ports 1,2,3')
44 parser.add_argument('--match_str', type=str, help='match string to use')
45 parser.add_argument('--count', type=int, default=1,
46 help='Number of messages to receive')
47 parser.add_argument('--test_name', type=str, required=True,
48 help='test name to run')
49 return parser.parse_args()
52 def test_listen_tcp(args):
53 if args.family == 'inet6':
58 ports = [int(port) for port in args.ports.split(',')]
60 s = socket.socket(fam, socket.SOCK_STREAM)
61 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
63 s.bind((args.sip, port))
64 print('binding on {}:{}'.format(args.sip, port))
69 while count < args.count:
70 readable, writable, exceptional = select.select(inputs, [], inputs)
72 (c, address) = s.accept()
73 print('C: {}'.format(address))
75 if args.match_str and args.match_str.encode('utf-8') != data:
76 raise Exception('Expected "{}" but got "{}"'.format(
77 args.match_str, data.decode('utf-8')))
82 def test_listen_udp(args):
83 if args.family == 'inet6':
88 ports = [int(port) for port in args.ports.split(',')]
90 s = socket.socket(fam, socket.SOCK_DGRAM)
91 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
93 s.bind((args.sip, port))
94 print('binding on {}:{}'.format(args.sip, port))
98 while count < args.count:
99 readable, writable, exceptional = select.select(inputs, [], inputs)
101 (data, address) = s.recvfrom(9000)
102 print('C: {}'.format(address))
103 if args.match_str and args.match_str.encode('utf-8') != data:
104 raise Exception('Expected "{}" but got "{}"'.format(
105 args.match_str, data.decode('utf-8')))
111 test_ptr = globals()[args.test_name]
115 if __name__ == '__main__':