def test_b3270_nvt_xml_smoke(self): # Start 'nc' to read b3270's output. nc = cti.copyserver() # Start b3270. b3270 = Popen(cti.vgwrap(['b3270']), stdin=PIPE, stdout=DEVNULL) self.children.append(b3270) # Feed b3270 some actions. top = ET.Element('b3270-in') ET.SubElement( top, 'run', { 'actions': f'Open(a:c:t:127.0.0.1:{nc.port}) String(abc) Enter() Disconnect()' }) *first, _, _ = cti.xml_prettify(top).split(b'\n') b3270.stdin.write(b'\n'.join(first) + b'\n') b3270.stdin.flush() # Make sure they are passed through. out = nc.data() self.assertEqual(b"abc\r\n", out) # Wait for the processes to exit. b3270.stdin.close() self.vgwait(b3270)
def test_b3270_xml_syntax_error(self): b3270 = Popen(cti.vgwrap(['b3270']), stdin=PIPE, stdout=PIPE, stderr=DEVNULL) self.children.append(b3270) # Feed b3270 junk. top = ET.Element('b3270-in') *first, _, _ = cti.xml_prettify(top).split(b'\n') b3270.stdin.write(b'\n'.join(first) + b'<<>' b'\n') out = b3270.communicate(timeout=2)[0].decode('utf8').split(os.linesep) self.vgwait(b3270, assertOnFailure=False) self.assertTrue(b3270.wait() != 0) # Get the result. out_err = out[-3] et_err = ET.fromstring(out_err) # Wait for the process to exit. b3270.stdin.close() # Check. self.assertEqual('ui-error', et_err.tag) attr = et_err.attrib self.assertEqual('true', attr['fatal']) self.assertTrue(attr['text'].startswith('XML parsing error'))
def test_b3270_xml_semantic_error(self): b3270 = Popen(cti.vgwrap(['b3270']), stdin=PIPE, stdout=PIPE) self.children.append(b3270) # Feed b3270 an action. top = ET.Element('b3270-in') ET.SubElement(top, 'run', {'foo': 'bar'}) ET.SubElement(top, 'run', {'actions': 'Wait(0.1,seconds) Quit()'}) *first, _, _ = cti.xml_prettify(top).split(b'\n') b3270.stdin.write(b'\n'.join(first) + b'\n') out = b3270.communicate(timeout=2)[0].decode('utf8').split(os.linesep) self.vgwait(b3270) # Get the result. out_err = out[-4] et_err = ET.fromstring(out_err) # Check. self.assertEqual('ui-error', et_err.tag) attr = et_err.attrib self.assertEqual('false', attr['fatal']) self.assertEqual('missing attribute', attr['text']) self.assertEqual('run', attr['element']) self.assertEqual('actions', attr['attribute'])
def test_b3270_passthru_xml(self): b3270 = Popen(cti.vgwrap(['b3270']), stdin=PIPE, stdout=PIPE) self.children.append(b3270) # Get the initial dump. pq = pipeq.pipeq(self, b3270.stdout) while True: line = pq.get(2, 'b3270 did not initialize').decode('utf8') if '</initialize>' in line: break # Register the Foo action and run it. top = ET.Element('b3270-in') ET.SubElement(top, 'register', {'name': 'Foo'}) ET.SubElement(top, 'run', {'r-tag': 'abc', 'actions': "Foo(a,b)"}) *first, _, _ = cti.xml_prettify(top).split(b'\n') b3270.stdin.write(b'\n'.join(first) + b'\n') b3270.stdin.flush() # Get the result. s = pq.get(2, 'b3270 did not produce expected output').decode('utf8') out = ET.fromstring(s) self.assertEqual('passthru', out.tag) attr = out.attrib tag = attr['p-tag'] self.assertEqual('abc', attr['parent-r-tag']) self.assertEqual('a', attr['arg1']) self.assertEqual('b', attr['arg2']) self.assertFalse('arg3' in attr) # Make the action succeed. succeed = ET.Element('succeed', {'p-tag': tag, 'text': 'hello\nthere'}) b3270.stdin.write(ET.tostring(succeed) + b'\n') b3270.stdin.flush() # Get the result of that. s = pq.get(2, 'b3270 did not produce expected output').decode('utf8') out = ET.fromstring(s) self.assertEqual('run-result', out.tag) attr = out.attrib self.assertEqual('abc', attr['r-tag']) self.assertEqual('true', attr['success']) self.assertEqual('hello\nthere', attr['text']) # Wait for the process to exit. b3270.stdin.write(b'</b3270-in>\n') b3270.stdin.close() b3270.stdout.close() self.vgwait(b3270) pq.close()
def test_b3270_xml_multiple(self): b3270 = Popen(cti.vgwrap(['b3270']), stdin=PIPE, stdout=PIPE) self.children.append(b3270) # Feed b3270 two actions, which it will run concurrently and complete # in reverse order. top = ET.Element('b3270-in') ET.SubElement(top, 'run', { 'actions': 'Wait(0.1,seconds) Set(startTls) Quit()', 'r-tag': 'tls' }) ET.SubElement(top, 'run', { 'actions': 'Set(insertMode)', 'r-tag': 'ins' }) *first, _, _ = cti.xml_prettify(top).split(b'\n') b3270.stdin.write(b'\n'.join(first) + b'\n') b3270.stdin.flush() # Get the result. errmsg = 'b3270 did not produce the expected output' pq = pipeq.pipeq(self, b3270.stdout) pq.get(2, errmsg) pq.get(2, errmsg) pq.get(2, errmsg) out = pq.get(2, errmsg).decode('utf8') et_ins = ET.fromstring(out) out = pq.get(2, errmsg).decode('utf8') et_tls = ET.fromstring(out) self.vgwait(b3270) pq.close() b3270.stdin.close() b3270.stdout.close() # Check. self.assertEqual('run-result', et_ins.tag) att_ins = et_ins.attrib self.assertEqual('ins', att_ins['r-tag']) self.assertEqual('true', att_ins['success']) self.assertEqual('false', att_ins['text']) self.assertEqual('run-result', et_tls.tag) att_tls = et_tls.attrib self.assertEqual('tls', att_tls['r-tag']) self.assertEqual('true', att_tls['success']) self.assertEqual('true', att_tls['text'])
def test_b3270_xml_seconds(self): b3270 = Popen(cti.vgwrap(['b3270']), stdin=PIPE, stdout=PIPE) self.children.append(b3270) # Feed b3270 an action. top = ET.Element('b3270-in') ET.SubElement(top, 'run', {'actions': "Wait(0.2,Seconds)"}) *first, _, _ = cti.xml_prettify(top).split(b'\n') b3270.stdin.write(b'\n'.join(first) + b'\n') b3270.stdin.flush() # Get the result. pq = pipeq.pipeq(self, b3270.stdout) output = b'' while True: line = pq.get(2, 'b3270 did not produce expected output') self.assertNotEqual(b'', line) output += line if b'run-result' in line: break output += b'</b3270-out>' # a white lie out = ET.fromstring(output.decode('utf8')) # Wait for the processes to exit. b3270.stdin.write(b'</b3270-in>\n') b3270.stdin.close() b3270.stdout.close() self.vgwait(b3270) pq.close() # Check. a = out.find('./run-result') self.assertTrue(a != None) att = a.attrib self.assertTrue(att['success']) self.assertTrue(float(att['time']) >= 0.1)
def test_b3270_xml_single(self): b3270 = Popen(cti.vgwrap(['b3270']), stdin=PIPE, stdout=PIPE) self.children.append(b3270) # Feed b3270 an action. top = ET.Element('b3270-in') ET.SubElement(top, 'run', {'actions': 'Set(startTls)'}) *first, _, _ = cti.xml_prettify(top).split(b'\n') b3270.stdin.write(b'\n'.join(first) + b'\n') # Get the result. out = ET.fromstring(b3270.communicate(timeout=2)[0].decode('utf8')) # Wait for the process to exit. b3270.stdin.close() self.vgwait(b3270) # Check. a = out.find('./run-result') self.assertTrue(a != None) att = a.attrib self.assertTrue(att['success']) self.assertEqual('true', att['text'])