<commit_before># -*- coding: utf-8 -*-

import unittest
import logging

from dyndnsc.detector.base import AF_INET6


def is_netifaces_available():
    try:
        import netifaces
    except ImportError:
        return False
    else:
        return True


def give_me_an_interface_ipv6():
    import netifaces
    for interface in netifaces.interfaces():
        if netifaces.AF_INET6 in netifaces.ifaddresses(interface):
            return interface
    return None


def give_me_an_interface_ipv4():
    import netifaces
    for interface in netifaces.interfaces():
        if netifaces.AF_INET in netifaces.ifaddresses(interface):
            return interface
    return None


class IfaceDetectorTest(unittest.TestCase):

    def setUp(self):
        logging.basicConfig(level=logging.INFO)
        unittest.TestCase.setUp(self)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def test_iface_detector(self):
        # py3: can user unittest.skipIf(condition, reason)
        if not is_netifaces_available():
            return
        import dyndnsc.detector.iface as iface
        self.assertTrue("iface" in iface.IPDetector_Iface.names())
        # auto-detect an interface:
        interface = give_me_an_interface_ipv4()
        self.assertNotEqual(None, interface)
        detector = iface.IPDetector_Iface(iface=interface)
        self.assertTrue(detector.can_detect_offline())
        self.assertEqual(None, detector.get_current_value())
        self.assertTrue(type(detector.detect()) in (type(None), str))
        # empty interface name must not fail construction
        # broken in python2.6
        # self.assertIsInstance(iface.IPDetector_Iface(iface=None), iface.IPDetector_Iface)
        self.assertEqual(type(iface.IPDetector_Iface(iface=None)), iface.IPDetector_Iface)
        # invalid netmask must fail construction
        self.assertRaises(ValueError, iface.IPDetector_Iface, netmask='fubar')
        # unknown address family  must fail construction
        self.assertRaises(ValueError, iface.IPDetector_Iface, family='bla')

    def test_teredo_detector(self):
        # py3: can user unittest.skipIf(condition, reason)
        if not is_netifaces_available():
            return
        import dyndnsc.detector.teredo as teredo
        self.assertTrue("teredo" in teredo.IPDetector_Teredo.names())
        # auto-detect an interface:
        interface = give_me_an_interface_ipv6()
        self.assertNotEqual(None, interface)
        detector = teredo.IPDetector_Teredo(iface=interface)
        self.assertTrue(detector.can_detect_offline())
        self.assertEqual(AF_INET6, detector.af())
        self.assertEqual(None, detector.get_current_value())
        self.assertTrue(type(detector.detect()) in (type(None), str))
        # self.assertNotEqual(None, detector.netmask)<commit_msg>Only run certain tests if there is ipv6 support<commit_after># -*- coding: utf-8 -*-

import unittest
import logging

from dyndnsc.detector.base import AF_INET6


def is_netifaces_available():
    try:
        import netifaces
    except ImportError:
        return False
    else:
        return True


def give_me_an_interface_ipv6():
    import netifaces
    for interface in netifaces.interfaces():
        if netifaces.AF_INET6 in netifaces.ifaddresses(interface):
            return interface
    return None


def give_me_an_interface_ipv4():
    import netifaces
    for interface in netifaces.interfaces():
        if netifaces.AF_INET in netifaces.ifaddresses(interface):
            return interface
    return None


class IfaceDetectorTest(unittest.TestCase):

    def setUp(self):
        logging.basicConfig(level=logging.INFO)
        unittest.TestCase.setUp(self)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def test_iface_detector(self):
        # py3: can user unittest.skipIf(condition, reason)
        if not is_netifaces_available():
            return
        import dyndnsc.detector.iface as iface
        self.assertTrue("iface" in iface.IPDetector_Iface.names())
        # auto-detect an interface:
        interface = give_me_an_interface_ipv4()
        self.assertNotEqual(None, interface)
        detector = iface.IPDetector_Iface(iface=interface)
        self.assertTrue(detector.can_detect_offline())
        self.assertEqual(None, detector.get_current_value())
        self.assertTrue(type(detector.detect()) in (type(None), str))
        # empty interface name must not fail construction
        # broken in python2.6
        # self.assertIsInstance(iface.IPDetector_Iface(iface=None), iface.IPDetector_Iface)
        self.assertEqual(type(iface.IPDetector_Iface(iface=None)), iface.IPDetector_Iface)
        # invalid netmask must fail construction
        self.assertRaises(ValueError, iface.IPDetector_Iface, netmask='fubar')
        # unknown address family  must fail construction
        self.assertRaises(ValueError, iface.IPDetector_Iface, family='bla')

    def test_teredo_detector(self):
        # py3: can user unittest.skipIf(condition, reason)
        if not is_netifaces_available():
            return
        import dyndnsc.detector.teredo as teredo
        self.assertTrue("teredo" in teredo.IPDetector_Teredo.names())
        # auto-detect an interface:
        interface = give_me_an_interface_ipv6()
        if interface is not None:  # we have ip6 support
            detector = teredo.IPDetector_Teredo(iface=interface)
            self.assertTrue(detector.can_detect_offline())
            self.assertEqual(AF_INET6, detector.af())
            self.assertEqual(None, detector.get_current_value())
            self.assertTrue(type(detector.detect()) in (type(None), str))
            # self.assertNotEqual(None, detector.netmask)
{'fraction_non_alphanumeric': 0.08724365658672228, 'fraction_numerical': 0.0045185957594716716, 'mean_word_length': 3.096085409252669, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7571792', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1736, 'n_words': 348}