| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright (c) 2021-2024 Paul Mattes. |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# * Redistributions of source code must retain the above copyright |
| 9 |
# notice, this list of conditions and the following disclaimer. |
| 10 |
# * Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# * Neither the names of Paul Mattes nor the names of his contributors |
| 14 |
# may be used to endorse or promote products derived from this software |
| 15 |
# without specific prior written permission. |
| 16 |
# |
| 17 |
# THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 |
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 |
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 20 |
# EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 23 |
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 |
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 25 |
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 26 |
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
# |
| 28 |
# wc3270 smoke tests |
| 29 |
|
| 30 |
import unittest |
| 31 |
from subprocess import Popen, PIPE, DEVNULL |
| 32 |
import os |
| 33 |
import tempfile |
| 34 |
import sys |
| 35 |
import requests |
| 36 |
import filecmp |
| 37 |
import time |
| 38 |
import Common.Test.playback as playback |
| 39 |
import Common.Test.cti as cti |
| 40 |
|
| 41 |
@unittest.skipUnless(sys.platform.startswith("win"), "Only works on native Windows") |
| 42 |
class TestWc3270Smoke(cti.cti): |
| 43 |
|
| 44 |
def find_in_path(self, exe): |
| 45 |
'''Find an executable in $PATH''' |
| 46 |
for dir in os.environ['PATH'].split(';'): |
| 47 |
cand = dir + '\\' + exe |
| 48 |
if os.path.exists(cand): |
| 49 |
return (dir, cand) |
| 50 |
self.assertTrue(False, f'Could not find {exe} in PATH') |
| 51 |
|
| 52 |
# wc3270 smoke test |
| 53 |
def test_wc3270_smoke(self): |
| 54 |
|
| 55 |
# Start 'playback' to feed wc3270. |
| 56 |
playback_port, ts = cti.unused_port() |
| 57 |
with playback.playback(self, 's3270/Test/ibmlink.trc', port=playback_port) as p: |
| 58 |
ts.close() |
| 59 |
|
| 60 |
# Create a session file. |
| 61 |
wc3270_port, ts = cti.unused_port() |
| 62 |
(handle, sname) = tempfile.mkstemp(suffix='.wc3270') |
| 63 |
os.write(handle, f'wc3270.title: wc3270\n'.encode('utf8')) |
| 64 |
os.write(handle, f'wc3270.httpd: 127.0.0.1:{wc3270_port}\n'.encode('utf8')) |
| 65 |
os.write(handle, f'wc3270.hostname: 127.0.0.1:{playback_port}\n'.encode('utf8')) |
| 66 |
os.close(handle) |
| 67 |
|
| 68 |
# Create a shortcut. |
| 69 |
(handle, lname) = tempfile.mkstemp(suffix='.lnk') |
| 70 |
os.close(handle) |
| 71 |
wc3270_dir, wc3270_path = self.find_in_path('wc3270.exe') |
| 72 |
cmd = f'mkshort {wc3270_dir} wc3270.exe {lname} {sname}' |
| 73 |
self.assertEqual(0, os.system(cmd)) |
| 74 |
|
| 75 |
# Start wc3270 in its own window by starting the link. |
| 76 |
self.assertEqual(0, os.system(f'start {lname}')) |
| 77 |
self.check_listen(wc3270_port) |
| 78 |
ts.close() |
| 79 |
os.unlink(sname) |
| 80 |
os.unlink(lname) |
| 81 |
|
| 82 |
# Feed wc3270 some data. |
| 83 |
p.send_records(4) |
| 84 |
|
| 85 |
# Dump the window contents. |
| 86 |
time.sleep(0.5) |
| 87 |
(handle, name) = tempfile.mkstemp(suffix='.bmp') |
| 88 |
os.close(handle) |
| 89 |
requests.get(f'http://127.0.0.1:{wc3270_port}/3270/rest/json/SnapScreen({name})') |
| 90 |
|
| 91 |
# Make sure the image is correct. |
| 92 |
self.assertTrue(filecmp.cmp(name, 'wc3270/Test/ibmlink.bmp') or filecmp.cmp(name, 'wc3270/Test/ibmlink-notfront.bmp'), |
| 93 |
f'{name} does not match wc3270/Test/ibmlink.bmp or wc3270/Test/ibmlink-notfront.tmp') |
| 94 |
os.unlink(name) |
| 95 |
|
| 96 |
if __name__ == '__main__': |
| 97 |
unittest.main() |