Skip to content

Commit

Permalink
Fix broken tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
pmattes committed Jan 1, 2025
1 parent 441d2b4 commit df05e74
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 33 deletions.
18 changes: 11 additions & 7 deletions run_windows_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ def run_gcc_tests():
if build and os.system('make windows') != 0:
exit(1)

# Run the library tests.
if 'lib' in dirs:
dirs.remove('lib')
if os.system('make windows-lib-test') != 0:
exit(1)

# Set the path.
if os.path.exists('obj\\win64'):
obj = 'obj\\win64'
Expand All @@ -56,8 +50,18 @@ def run_gcc_tests():
exit(1)
os.environ['PATH'] = ';'.join([os.getcwd() + '\\' + obj + '\\' + dir for dir in dirs] + [os.environ['PATH']])

if 'PYTESTS' in os.environ:
cmd = sys.executable + ' -m unittest ' + verbose_flag + ' ' + os.environ['PYTESTS']
else:
# Run the library tests.
if 'lib' in dirs:
dirs.remove('lib')
if os.system('make windows-lib-test') != 0:
exit(1)

cmd = sys.executable + ' -m unittest ' + verbose_flag + ' ' + ' '.join([' '.join(glob.glob(dir + '\\Test\\test*.py')) for dir in dirs])

# Run the tests.
cmd = sys.executable + ' -m unittest ' + verbose_flag + ' ' + ' '.join([' '.join(glob.glob(dir + '\\Test\\test*.py')) for dir in dirs])
os.system(cmd)

# Run Visual Studio tests.
Expand Down
37 changes: 26 additions & 11 deletions s3270/Test/testCookie.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021-2024 Paul Mattes.
# Copyright (c) 2021-2025 Paul Mattes.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -75,7 +75,9 @@ class TestS3270Cookie(cti.cti):
def test_s3270_s3270_cookie(self):

port, ts = cti.unused_port()
with tempfile.NamedTemporaryFile() as tf:
with tempfile.NamedTemporaryFile(delete=False) as tf:

tf_name = tf.name

# Start s3270.
s3270 = Popen(cti.vgwrap(['s3270', '-scriptport', str(port), '-cookiefile', tf.name]), stdin=DEVNULL, stdout=DEVNULL)
Expand Down Expand Up @@ -110,12 +112,15 @@ def test_s3270_s3270_cookie(self):

# Wait for the processes to exit.
self.vgwait(s3270)
os.unlink(tf_name)

# s3270 HTTP cookie test
def test_s3270_http_cookie(self):

port, ts = cti.unused_port()
with tempfile.NamedTemporaryFile() as tf:
with tempfile.NamedTemporaryFile(delete=False) as tf:

tf_name = tf.name

# Start s3270.
s3270 = Popen(cti.vgwrap(['s3270', '-httpd', str(port), '-cookiefile', tf.name]), stdin=DEVNULL, stdout=DEVNULL)
Expand Down Expand Up @@ -150,28 +155,31 @@ def test_s3270_http_cookie(self):
# Stop.
r = requests.get(f'http://127.0.0.1:{port}/3270/rest/json/Quit()', cookies={"x3270-security": cookie})

os.unlink(tf_name)

# Wait for the processes to exit.
self.vgwait(s3270)


# s3270 s3270-mode cookie test with a file that does not exist yet.
def test_s3270_s3270_cookie_create_file(self):

port, ts = cti.unused_port()
tf = tempfile.mktemp()
tf = tempfile.NamedTemporaryFile(delete=False)

# Start s3270.
s3270 = Popen(cti.vgwrap(['s3270', '-scriptport', str(port), '-cookiefile', tf]), stdin=DEVNULL, stdout=DEVNULL)
s3270 = Popen(cti.vgwrap(['s3270', '-scriptport', str(port), '-cookiefile', tf.name]), stdin=DEVNULL, stdout=DEVNULL)
self.children.append(s3270)
ts.close()

# Make sure s3270 creates and puts something in the file. Then read the cookie from it.
self.try_until(lambda: os.path.exists(tf) and os.path.getsize(tf) > 0, 2, 's3270 did not write to the cookie file')
with open(tf, 'r') as cookiefile:
self.try_until(lambda: os.path.exists(tf.name) and os.path.getsize(tf.name) > 0, 2, 's3270 did not write to the cookie file')
with open(tf.name, 'r') as cookiefile:
cookie = cookiefile.read()

# Except on Windows, make sure the file has 0400 permissions now.
if not sys.platform.startswith('win'):
s = os.stat(tf)
s = os.stat(tf.name)
self.assertEqual(0o400, (s.st_mode & 0o777))

# Try an s3270 protocol command without specifying a cookie.
Expand All @@ -193,13 +201,16 @@ def test_s3270_s3270_cookie_create_file(self):
# Wait for the processes to exit.
self.vgwait(s3270)

os.unlink(tf)
tf.close()
os.unlink(tf.name)

# s3270 s3270-mode cookie test with a file that we create and fill in
def test_s3270_s3270_cookie_filled(self):

port, ts = cti.unused_port()
with tempfile.NamedTemporaryFile() as tf:
with tempfile.NamedTemporaryFile(delete=False) as tf:

tf_name = tf.name

# Generate a cookie.
cookie = random_word(10)
Expand Down Expand Up @@ -238,12 +249,15 @@ def test_s3270_s3270_cookie_filled(self):

# Wait for the processes to exit.
self.vgwait(s3270)
os.unlink(tf_name)

# s3270 s3270-mode cookie test with a file that we create and fill in with a bad cookie value.
def s3270_s3270_cookie_filled_wrong(self, contents: str):

port, ts = cti.unused_port()
with tempfile.NamedTemporaryFile() as tf:
with tempfile.NamedTemporaryFile(delete=False) as tf:

tf_name = tf.name

# Write it into the temporary file.
fd = os.open(tf.name, os.O_WRONLY)
Expand All @@ -261,6 +275,7 @@ def s3270_s3270_cookie_filled_wrong(self, contents: str):
errmsg = s3270.stderr.readlines()
s3270.stderr.close()
self.assertTrue(b'invalid cookie' in errmsg[0])
os.unlink(tf_name)

def test_s3270_s3270_cookie_filled_wrong_char(self):
self.s3270_s3270_cookie_filled_wrong(',')
Expand Down
12 changes: 9 additions & 3 deletions s3270/Test/testMultiLine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021-2024 Paul Mattes.
# Copyright (c) 2021-2025 Paul Mattes.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -110,7 +110,10 @@ def test_s3270_multiline_scriptport(self):
while True:
r, _, _ = select.select([s], [], [], 5)
self.assertNotEqual([], r)
blob = s.recv(1024)
try:
blob = s.recv(1024)
except ConnectionResetError:
blob = b''
if len(blob) == 0:
break
output += blob
Expand Down Expand Up @@ -151,7 +154,10 @@ def test_s3270_multiline_scriptport_json(self):
while True:
r, _, _ = select.select([s], [], [], 5)
self.assertNotEqual([], r)
blob = s.recv(1024)
try:
blob = s.recv(1024)
except ConnectionResetError:
blob = b''
if len(blob) == 0:
break
output += blob
Expand Down
23 changes: 18 additions & 5 deletions s3270/Test/testPrintText.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021-2024 Paul Mattes.
# Copyright (c) 2021-2025 Paul Mattes.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand All @@ -27,7 +27,6 @@
#
# s3270 PrintText() tests

import filecmp
import os
import requests
from subprocess import Popen, PIPE, DEVNULL
Expand All @@ -36,6 +35,17 @@
import Common.Test.playback as playback
import Common.Test.cti as cti

# Line-ending-independent file comparison.
def cmp_lines(file1, file2):
l1 = l2 = True
with open(file1, 'r') as f1, open(file2, 'r') as f2:
while l1 and l2:
l1 = f1.readline()
l2 = f2.readline()
if l1 != l2:
return False
return True

class TestS3270PrintText(cti.cti):

# s3270 PrintText(html) test
Expand All @@ -58,15 +68,18 @@ def s3270_PrintText(self, type: str):
p.send_records(1)

# Generate an image file.
image_file = tempfile.NamedTemporaryFile(suffix='.' + type)
image_file = tempfile.NamedTemporaryFile(suffix='.' + type, delete=False)
if_name = image_file.name
tparam = (type + ',') if type != 'txt' else ''
r = requests.get(f'http://127.0.0.1:{hport}/3270/rest/json/PrintText({tparam}file,{image_file.name})')
r = requests.get(f'http://127.0.0.1:{hport}/3270/rest/json/PrintText({tparam}file,{if_name})')
self.assertTrue(r.ok, 'Expected PrintText()) to succeed')
self.assertTrue(filecmp.cmp(image_file.name, f's3270/Test/login.{type}'), f'Expected correct {type} output')
self.assertTrue(cmp_lines(image_file.name, f's3270/Test/login.{type}'), f'Expected correct {type} output')

# Wait for s3270 to exit.
r = requests.get(f'http://127.0.0.1:{hport}/3270/rest/json/Quit()')
self.vgwait(s3270)
image_file.close()
os.unlink(if_name)

def test_s3270_html(self):
self.s3270_PrintText('html')
Expand Down
17 changes: 11 additions & 6 deletions s3270/Test/testRpq.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021-2024 Paul Mattes.
# Copyright (c) 2021-2025 Paul Mattes.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -30,6 +30,7 @@
import os
from subprocess import Popen, PIPE, DEVNULL
import requests
import sys
import time
import unittest

Expand All @@ -55,9 +56,13 @@ def split_list(input_list, match):
if accum != None:
result.append(accum)
return result


# Define the AF value for IPv6.
v6family = '17' if sys.platform.startswith('win') else '0a'

class TestS3270RpqNames(cti.cti):


# s3270 RPQNAMES test, multi-session
def s3270_rpqnames_multi_session(self, rpq: str, sessions):
'''Test with multiple sessions, dicts provide reply, v6, stderr_count, twice_same_session, set_value'''
Expand Down Expand Up @@ -128,7 +133,7 @@ def s3270_rpqnames_multi_session(self, rpq: str, sessions):

# Split the errors.
if len(sessions) > 1:
split_stderr = split_list(stderr, delimiter + b'\n')
split_stderr = split_list(stderr, delimiter + os.linesep.encode())
# print('sessions', sessions, 'stderr', stderr, 'split_stderr', split_stderr)
self.assertEqual(len(sessions), len(split_stderr))
self.assertSequenceEqual([t['stderr_count'] for t in sessions], [len(t) for t in split_stderr])
Expand Down Expand Up @@ -172,7 +177,7 @@ def get_timezone(self) -> str:

def get_address(self, ipv6=False) -> str:
'''Get the address'''
return rpq.add_len(rpq.RpqName.Address.encode() + ('000a00000000000000000000000000000001' if ipv6 else '00027f000001'))
return rpq.add_len(rpq.RpqName.Address.encode() + ('00' + v6family + '00000000000000000000000000000001' if ipv6 else '00027f000001'))

# Default, no fields
def test_s3270_rpqnames(self):
Expand All @@ -192,7 +197,7 @@ def test_s3270_rpqnames_address_override(self):

# IPv6 address override
def test_s3270_rpqnames_address_override_ipv6(self):
self.s3270_rpqnames(rpq.make_rpq(rpq.add_len(rpq.RpqName.Address.encode() + '000a00010002000300000000000000000004')), rpq=r'ADDRESS=1\:2\:3\:\:4')
self.s3270_rpqnames(rpq.make_rpq(rpq.add_len(rpq.RpqName.Address.encode() + '00' + v6family + '00010002000300000000000000000004')), rpq=r'ADDRESS=1\:2\:3\:\:4')

# Program version
def test_s3270_rpqnames_version(self):
Expand Down Expand Up @@ -394,7 +399,7 @@ def test_s3270_rpqnames_set_see(self):
# Check stdout and stderr.
stdout = s3270.stdout.readlines()
result = [x for x in stdout if x.startswith(b'data: ')]
self.assertEqual([b'data: foo\n', b'data: bar\n'], result)
self.assertEqual([b'data: foo' + os.linesep.encode(), b'data: bar' + os.linesep.encode()], result)
s3270.stdout.close()

stderr = s3270.stderr.readlines()
Expand Down
4 changes: 3 additions & 1 deletion s3270/Test/testTls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021-2024 Paul Mattes.
# Copyright (c) 2021-2025 Paul Mattes.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -393,6 +393,7 @@ def test_s3270_tls_992_disable(self):
self.vgwait(s3270)

# s3270 TLS security level test
@unittest.skipIf(sys.platform.startswith('win'), 'security level is POSIX-specific')
def test_s3270_tls_security_level(self):

# Start a server to read s3270's output.
Expand Down Expand Up @@ -430,6 +431,7 @@ def test_s3270_tls_security_level(self):
self.vgwait(s3270)

# s3270 TLS security level test failure
@unittest.skipIf(sys.platform.startswith('win'), 'security level is POSIX-specific')
def test_s3270_tls_security_level_fail(self):

# Start a server to read s3270's output.
Expand Down

0 comments on commit df05e74

Please sign in to comment.