<commit_before>import unittest
import libvirt


class TestLibvirtConn(unittest.TestCase):
    def setUp(self):
        self.conn = libvirt.open("test:///default")

    def tearDown(self):
        self.conn = None

    def testConnDomainList(self):
        doms = self.conn.listAllDomains()
        self.assertEquals(len(doms), 1)
        self.assertEquals(type(doms[0]), libvirt.virDomain)
        self.assertEquals(doms[0].name(), "test")<commit_msg>Add unit tests for openAuth method<commit_after>import unittest
import libvirt
import tempfile
import contextlib
import os


class TestLibvirtConn(unittest.TestCase):
    def setUp(self):
        self.conn = libvirt.open("test:///default")

    def tearDown(self):
        self.conn = None

    def testConnDomainList(self):
        doms = self.conn.listAllDomains()
        self.assertEquals(len(doms), 1)
        self.assertEquals(type(doms[0]), libvirt.virDomain)
        self.assertEquals(doms[0].name(), "test")

class TestLibvirtConnAuth(unittest.TestCase):
    connXML = """
<node>
  <auth>
    <user password="<PASSWORD>"><PASSWORD></user>
    <user password="<PASSWORD>"><PASSWORD></user>
  </auth>
</node>"""
    def setUp(self):
        def noop(msg, opaque):
            pass
        libvirt.registerErrorHandler(noop, None)

    @contextlib.contextmanager
    def tempxmlfile(content):
        try:
            fp = tempfile.NamedTemporaryFile(delete=False,
                                             prefix="libvirt-python-test",
                                             suffix=".xml")
            fname = fp.name
            fp.write(content.encode("utf8"))
            fp.close()
            yield fname
        finally:
            os.unlink(fname)

    def authHelper(self, username, password):
        with TestLibvirtConnAuth.tempxmlfile(self.connXML) as fname:
            magic = 142857
            def authCB(creds, opaque):
                if opaque != magic:
                    return -1

                for cred in creds:
                    if (cred[0] == libvirt.VIR_CRED_AUTHNAME and
                        username is not None):
                        cred[4] = username
                        return 0
                    elif (cred[0] == libvirt.VIR_CRED_PASSPHRASE and
                          password is not None):
                        cred[4] = password
                        return 0
                    return -1
                return 0

            auth = [[libvirt.VIR_CRED_AUTHNAME,
                     libvirt.VIR_CRED_ECHOPROMPT,
                     libvirt.VIR_CRED_REALM,
                     libvirt.VIR_CRED_PASSPHRASE,
                     libvirt.VIR_CRED_NOECHOPROMPT,
                     libvirt.VIR_CRED_EXTERNAL],
                    authCB, magic]

            return libvirt.openAuth("test://" + fname,
                                    auth, 0)

    def testOpenAuthGood(self):
        conn = self.authHelper("srinivasa", "87539319")

    def testOpenAuthBad(self):
        try:
            conn = self.authHelper("srinivasa", "2147483647")
            raise Exception("Unexpected open success")
        except libvirt.libvirtError as ex:
            pass

    def testOpenAuthNone(self):
        try:
            conn = self.authHelper(None, None)
            raise Exception("Unexpected open success")
        except libvirt.libvirtError as ex:
            pass
{'fraction_non_alphanumeric': 0.09814323607427056, 'fraction_numerical': 0.012083701738874153, 'mean_word_length': 1.6787687450670876, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2313299', 'n_tokens_mistral': 990, 'n_tokens_neox': 926, 'n_words': 192}