# Path: xclib/ejabberd_io.py
# class ejabberd_io:
#     @classmethod
#     def read_request(cls, infd, outfd):
#         try:
#             infd = infd.buffer
#         except AttributeError:
#             pass
#         length_field = infd.read(2)
#         while len(length_field) == 2:
#             (size,) = unpack('>H', length_field)
#             if size == 0:
#                logging.info('command length 0, treating as logical EOF')
#                return
#             cmd = infd.read(size)
#             if len(cmd) != size:
#                logging.warn('premature EOF while reading cmd: %d != %d' % (len(cmd), size))
#                return
#             x = unutf8(cmd).split(':', 3)
#             yield tuple(x)
#             length_field = infd.read(2)
# 
#     @classmethod
#     def write_response(cls, flag, outfd):
#         try:
#             outfd = outfd.buffer
#         except AttributeError:
#             pass
#         answer = 0
#         if flag:
#             answer = 1
#         token = pack('>HH', 2, answer)
#         outfd.write(token)
#         outfd.flush()
# 
# Path: xclib/tests/iostub.py
# class iostub:
#     def stub_stdin(testcase_inst, inputs, ioclass=io.StringIO):
#         stdin = sys.stdin
# 
#         def cleanup():
#             sys.stdin = stdin
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdin = ioclass(inputs)
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdin.buffer = sys.stdin
# 
#     def stub_stdout(testcase_inst, ioclass=io.StringIO):
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
# 
#     def stub_stdouts(testcase_inst, ioclass=io.StringIO):
#         stderr = sys.stderr
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stderr = stderr
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stderr = ioclass()
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
#             sys.stderr.buffer = sys.stderr


import sys
import io
import unittest
from xclib.ejabberd_io import ejabberd_io
from xclib.tests.iostub import iostub
        tester = iter(ejabberd_io.read_request(sys.stdin, sys.stdout))
        output = next(tester)
        self.assertEqual(output, ('isuser', 'login', ''))
        output = next(tester)
        self.assertEqual(output, ('auth', 'log', 'dom', 'pass'))
        self.assertRaises(StopIteration, next, tester)

    def test_input_fake_eof(self):
        self.stub_stdin(b'\000\000', ioclass=io.BytesIO)
        tester = iter(ejabberd_io.read_request(sys.stdin, sys.stdout))
        self.assertRaises(StopIteration, next, tester)

    def test_input_short(self):
        self.stub_stdin(b'\001\000', ioclass=io.BytesIO)
        tester = iter(ejabberd_io.read_request(sys.stdin, sys.stdout))
        self.assertRaises(StopIteration, next, tester)

    def test_input_negative(self):
        self.stub_stdin(b'\377\377', ioclass=io.BytesIO)
        tester = iter(ejabberd_io.read_request(sys.stdin, sys.stdout))
        self.assertRaises(StopIteration, next, tester)

    def test_output_false(self):
        self.stub_stdout(ioclass=io.BytesIO)
        ejabberd_io.write_response(False, sys.stdout)
        self.assertEqual(sys.stdout.getvalue(), b'\000\002\000\000')

    # Cannot be merged, as getvalue() returns the aggregate value
    def test_output_true(self):
        self.stub_stdout(ioclass=io.BytesIO)

# Path: xclib/ejabberd_io.py
# class ejabberd_io:
#     @classmethod
#     def read_request(cls, infd, outfd):
#         try:
#             infd = infd.buffer
#         except AttributeError:
#             pass
#         length_field = infd.read(2)
#         while len(length_field) == 2:
#             (size,) = unpack('>H', length_field)
#             if size == 0:
#                logging.info('command length 0, treating as logical EOF')
#                return
#             cmd = infd.read(size)
#             if len(cmd) != size:
#                logging.warn('premature EOF while reading cmd: %d != %d' % (len(cmd), size))
#                return
#             x = unutf8(cmd).split(':', 3)
#             yield tuple(x)
#             length_field = infd.read(2)
# 
#     @classmethod
#     def write_response(cls, flag, outfd):
#         try:
#             outfd = outfd.buffer
#         except AttributeError:
#             pass
#         answer = 0
#         if flag:
#             answer = 1
#         token = pack('>HH', 2, answer)
#         outfd.write(token)
#         outfd.flush()
# 
# Path: xclib/tests/iostub.py
# class iostub:
#     def stub_stdin(testcase_inst, inputs, ioclass=io.StringIO):
#         stdin = sys.stdin
# 
#         def cleanup():
#             sys.stdin = stdin
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdin = ioclass(inputs)
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdin.buffer = sys.stdin
# 
#     def stub_stdout(testcase_inst, ioclass=io.StringIO):
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
# 
#     def stub_stdouts(testcase_inst, ioclass=io.StringIO):
#         stderr = sys.stderr
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stderr = stderr
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stderr = ioclass()
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
#             sys.stderr.buffer = sys.stderr


# Path: xclib/tests/21_ejabberd_test.py
import sys
import io
import unittest
from xclib.ejabberd_io import ejabberd_io
from xclib.tests.iostub import iostub

        tester = iter(ejabberd_io.read_request(sys.stdin, sys.stdout))
        output = next(tester)
        self.assertEqual(output, ('isuser', 'login', ''))
        output = next(tester)
        self.assertEqual(output, ('auth', 'log', 'dom', 'pass'))
        self.assertRaises(StopIteration, next, tester)

    def test_input_fake_eof(self):
        self.stub_stdin(b'\000\000', ioclass=io.BytesIO)
        tester = iter(ejabberd_io.read_request(sys.stdin, sys.stdout))
        self.assertRaises(StopIteration, next, tester)

    def test_input_short(self):
        self.stub_stdin(b'\001\000', ioclass=io.BytesIO)
        tester = iter(ejabberd_io.read_request(sys.stdin, sys.stdout))
        self.assertRaises(StopIteration, next, tester)

    def test_input_negative(self):
        self.stub_stdin(b'\377\377', ioclass=io.BytesIO)
        tester = iter(ejabberd_io.read_request(sys.stdin, sys.stdout))
        self.assertRaises(StopIteration, next, tester)

    def test_output_false(self):
        self.stub_stdout(ioclass=io.BytesIO)
        ejabberd_io.write_response(False, sys.stdout)
        self.assertEqual(sys.stdout.getvalue(), b'\000\002\000\000')

    # Cannot be merged, as getvalue() returns the aggregate value
    def test_output_true(self):
        self.stub_stdout(ioclass=io.BytesIO)

        ejabberd_io.write_response(True, sys.stdout)