Skip to content

Commit

Permalink
Log used nc command in testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
aswaving committed Nov 30, 2024
1 parent 7523ad1 commit 41aebb7
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions tests/nc-test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
import os
import platform
import shutil
import time
import unittest

Expand All @@ -24,10 +24,10 @@ class NetcatClientTests(unittest.TestCase):
def test_stdin_redirect_tcp_filesize_larger_buffer(self):
fname = generate_random_file(32000)
out_fd, out_filename = mkstemp()
srv = Popen([NC_PATH, '-l', '12340'], stdout=out_fd)
srv = Popen([SYSTEM_NC, '-l', '12340'], stdout=out_fd)
time.sleep(0.1)
infd = os.open(fname, os.O_RDONLY)
clt = Popen(['target/' + build + '/nc', 'localhost', '12340'], stdin=infd)
clt = Popen([TARGET_NC, 'localhost', '12340'], stdin=infd)
self.assertEqual(0, clt.wait())
self.assertEqual(0, srv.wait())
diff = Popen(['diff', fname, out_filename])
Expand All @@ -36,10 +36,10 @@ def test_stdin_redirect_tcp_filesize_larger_buffer(self):
def test_stdin_redirect_tcp_zero_sized_file(self):
fname = generate_random_file(0)
out_fd, out_filename = mkstemp()
srv = Popen([NC_PATH, '-l', '12340'], stdout=out_fd)
srv = Popen([SYSTEM_NC, '-l', '12340'], stdout=out_fd)
time.sleep(0.1)
infd = os.open(fname, os.O_RDONLY)
clt = Popen(['target/' + build + '/nc', 'localhost', '12340'], stdin=infd)
clt = Popen([TARGET_NC, 'localhost', '12340'], stdin=infd)
self.assertEqual(0, clt.wait())
self.assertEqual(0, srv.wait())
diff = Popen(['diff', fname, out_filename])
Expand All @@ -48,10 +48,10 @@ def test_stdin_redirect_tcp_zero_sized_file(self):
def test_stdin_redirect_tcp_small_filesize(self):
fname = generate_random_file(1)
out_fd, out_filename = mkstemp()
srv = Popen([NC_PATH, '-l', '12340'], stdout=out_fd)
srv = Popen([SYSTEM_NC, '-l', '12340'], stdout=out_fd)
time.sleep(0.1)
infd = os.open(fname, os.O_RDONLY)
clt = Popen(['target/' + build + '/nc', 'localhost', '12340'], stdin=infd)
clt = Popen([TARGET_NC, 'localhost', '12340'], stdin=infd)
self.assertEqual(0, clt.wait())
self.assertEqual(0, srv.wait())
diff = Popen(['diff', fname, out_filename])
Expand All @@ -60,11 +60,11 @@ def test_stdin_redirect_tcp_small_filesize(self):
def test_stdin_redirect_udp4(self):
fname = generate_random_file(32000)
out_fd, out_filename = mkstemp()
srv = Popen([NC_PATH, '-l', '-u', '-4', '-d',
srv = Popen([SYSTEM_NC, '-l', '-u', '-4', '-d',
'127.0.0.1', '12340'], stdout=out_fd)
time.sleep(0.1)
infd = os.open(fname, os.O_RDONLY)
clt = Popen(['target/' + build + '/nc', '-u', '-4', '-w',
clt = Popen([TARGET_NC, '-u', '-4', '-w',
'1', '127.0.0.1', '12340'], stdin=infd)
self.assertEqual(0, clt.wait())
self.assertEqual(0, srv.wait())
Expand All @@ -74,11 +74,11 @@ def test_stdin_redirect_udp4(self):
def test_stdin_redirect_udp46(self):
fname = generate_random_file(32000)
out_fd, out_filename = mkstemp()
srv = Popen([NC_PATH, '-l', '-u', '-d',
srv = Popen([SYSTEM_NC, '-l', '-u', '-d',
'::1', '12340'], stdout=out_fd)
time.sleep(0.1)
infd = os.open(fname, os.O_RDONLY)
clt = Popen(['target/' + build + '/nc', '-v', '-u', '-w',
clt = Popen([TARGET_NC, '-v', '-u', '-w',
'1', '::1', '12340'], stdin=infd)
self.assertEqual(0, clt.wait())
self.assertEqual(0, srv.wait())
Expand All @@ -88,11 +88,11 @@ def test_stdin_redirect_udp46(self):
def test_stdin_redirect_udp6(self):
fname = generate_random_file(32000)
out_fd, out_filename = mkstemp()
srv = Popen([NC_PATH, '-l', '-u', '-d', '-6',
srv = Popen([SYSTEM_NC, '-l', '-u', '-d', '-6',
'12340'], stdout=out_fd)
time.sleep(0.1)
infd = os.open(fname, os.O_RDONLY)
clt = Popen(['target/' + build + '/nc', '-v', '-u', '-6', '-w',
clt = Popen([TARGET_NC, '-v', '-u', '-6', '-w',
'1', '::1', '12340'], stdin=infd)
self.assertEqual(0, clt.wait())
self.assertEqual(0, srv.wait())
Expand All @@ -101,7 +101,7 @@ def test_stdin_redirect_udp6(self):

def test_stdin_pipe_tcp(self):
outfd, outfilename = mkstemp()
srv = Popen([NC_PATH, '-l', '12340'], stdout=PIPE)
srv = Popen([SYSTEM_NC, '-l', '12340'], stdout=PIPE)
time.sleep(0.1)
clt = Popen('echo bla | target/' + build + '/nc localhost 12340', shell=True)
out, _ = srv.communicate()
Expand All @@ -116,15 +116,14 @@ def test_stdin_pipe_tcp(self):
if len(sys.argv) > 1:
build = sys.argv[1]
del sys.argv[1]

print('testing ' + build + ' build')
if not os.path.exists('target/' + build + '/nc'):

TARGET_NC = 'target/' + build +'/nc'
print('Testing', TARGET_NC)
if not os.path.exists(TARGET_NC):
print('Error: target executable `nc` does not exits in `target/' + build + '`')
exit(1)
sys.exit(1)

if platform.system() == 'Linux':
NC_PATH = '/bin/nc'
else:
NC_PATH = '/usr/bin/nc'
SYSTEM_NC = shutil.which('nc')
print('Using this system netcat command as counter party: ', SYSTEM_NC)

unittest.main(verbosity=2)

0 comments on commit 41aebb7

Please sign in to comment.