# Path: xclib/saslauthd_io.py
# class saslauthd_io:
#     @classmethod
#     def read_request(cls, infd, outfd):
#         try:
#             infd = infd.buffer
#         except AttributeError:
#             pass
#         field_no = 0
#         fields = [None, None, None, None]
#         length_field = infd.read(2)
#         while len(length_field) == 2:
#             (size,) = unpack('>H', length_field)
#             val = infd.read(size)
#             if len(val) != size:
#                logging.warn('premature EOF while reading field %d: %d != %d' % (field_no, len(val), size))
#                return
#             fields[field_no] = val
#             field_no = (field_no + 1) % 4
#             if field_no == 0:
#                 logging.debug('from_saslauthd got %r, %r, %r, %r' % tuple(fields))
#                 yield ('auth', unutf8(fields[0], 'illegal'), unutf8(fields[3], 'illegal'), unutf8(fields[1], 'illegal'))
#             length_field = infd.read(2)
# 
#     @classmethod
#     def write_response(cls, flag, outfd):
#         try:
#             outfd = outfd.buffer
#         except AttributeError:
#             pass
#         answer = b'NO xcauth authentication failure'
#         if flag:
#             answer = b'OK success'
#         token = pack('>H', len(answer)) + 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.saslauthd_io import saslauthd_io
from xclib.tests.iostub import iostub
# Checks that saslauthd_io works as it should
class TestSaslAuthD(unittest.TestCase, iostub):

    def test_input(self):
        self.stub_stdin(b'\000\005login\000\004pass\000\000\000\006domain' +
                        b'\000\005login\000\004pass\000\006ignore\000\006domain',
                        ioclass=io.BytesIO)
        tester = iter(saslauthd_io.read_request(sys.stdin, sys.stdout))
        output = next(tester)
        self.assertEqual(output, ('auth', 'login', 'domain', 'pass'))
        output = next(tester)
        self.assertEqual(output, ('auth', 'login', 'domain', 'pass'))
        self.assertRaises(StopIteration, next, tester)

    def test_input_short(self):
        self.stub_stdin(b'\001\005login\000\004pass\000\000\000\006domain',
                        ioclass=io.BytesIO)

# Path: xclib/saslauthd_io.py
# class saslauthd_io:
#     @classmethod
#     def read_request(cls, infd, outfd):
#         try:
#             infd = infd.buffer
#         except AttributeError:
#             pass
#         field_no = 0
#         fields = [None, None, None, None]
#         length_field = infd.read(2)
#         while len(length_field) == 2:
#             (size,) = unpack('>H', length_field)
#             val = infd.read(size)
#             if len(val) != size:
#                logging.warn('premature EOF while reading field %d: %d != %d' % (field_no, len(val), size))
#                return
#             fields[field_no] = val
#             field_no = (field_no + 1) % 4
#             if field_no == 0:
#                 logging.debug('from_saslauthd got %r, %r, %r, %r' % tuple(fields))
#                 yield ('auth', unutf8(fields[0], 'illegal'), unutf8(fields[3], 'illegal'), unutf8(fields[1], 'illegal'))
#             length_field = infd.read(2)
# 
#     @classmethod
#     def write_response(cls, flag, outfd):
#         try:
#             outfd = outfd.buffer
#         except AttributeError:
#             pass
#         answer = b'NO xcauth authentication failure'
#         if flag:
#             answer = b'OK success'
#         token = pack('>H', len(answer)) + 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/22_saslauthd_test.py
import sys
import io
import unittest
from xclib.saslauthd_io import saslauthd_io
from xclib.tests.iostub import iostub

# Checks that saslauthd_io works as it should
class TestSaslAuthD(unittest.TestCase, iostub):

    def test_input(self):
        self.stub_stdin(b'\000\005login\000\004pass\000\000\000\006domain' +
                        b'\000\005login\000\004pass\000\006ignore\000\006domain',
                        ioclass=io.BytesIO)
        tester = iter(saslauthd_io.read_request(sys.stdin, sys.stdout))
        output = next(tester)
        self.assertEqual(output, ('auth', 'login', 'domain', 'pass'))
        output = next(tester)
        self.assertEqual(output, ('auth', 'login', 'domain', 'pass'))
        self.assertRaises(StopIteration, next, tester)

    def test_input_short(self):
        self.stub_stdin(b'\001\005login\000\004pass\000\000\000\006domain',
                        ioclass=io.BytesIO)

        tester = iter(saslauthd_io.read_request(sys.stdin, sys.stdout))