# Path: topology/sniffer/sniffing_service.py
# class PacketExporter(Component):
#     """
#     Component used to export packets.
# 
#     The packets that are taken from the list of shared packets are then
#     discarded from the list of shared packets.
#     """
#     def __init__(self, shared_packets, lock):
#         self.shared_packets = shared_packets
#         self.lock = lock
# 
#     def process(self, unused=None):
#         """
#         :return: returns a list of packets as a dictionary
#         """
#         self.lock.acquire()
# 
#         packets = self.shared_packets[:]
#         self.shared_packets[:] = []
# 
#         self.lock.release()
#         return packets
# 
# Path: topology/sniffer/daemon.py
# class SniffingDaemon():
#     """
#     A daemon that itertes through a series of device connections
#     and gets sniffed devices.
#     """
#     def __init__(self, shared_packets, lock, connections=discover_devices):
#         """
#         :param shared_packets: a list of packets discovered by the sniffer
#         :param lock: the lock that protects the shared_packets list
#         :connections: the connections used to pool packets
#         """
#         self.packets = shared_packets
#         self.lock = lock
# 
#         # If the default argument was given, then we call discover_devices
#         if connections == discover_devices:
#             self.connections = discover_devices()
#         else:
#             self.connections = connections
# 
#     def get_new_packets(self, filter_mask="10.1.1.1/32"):
#         """
#         Updates the list of shared packets via new packests provided through
#         scans.
# 
#         For the moment we ask each device for a packet. If this proves to
#         be a problem, we need to group the reads.
# 
#         :param filter_mask: the filter mask of form `IP/bits` used to filter
#             the GOOD packets
#         :return: returns nothing
#         """
#         new_packets = []
#         for connection in self.connections:
#             try:
#                 (header, packet) = connection.next()
#                 packet = parse_packet(packet)
#                 packet = filter_packet(packet, filter_mask)
#                 if packet is not None:
#                     new_packets.append(packet)
#                     logger.debug(packet)
#             except Exception as e:
#                 # Non-eth packets received
#                 pass
# 
#         self.lock.acquire()
# 
#         for packet in new_packets:
#             self.packets.append(packet)
# 
#         self.lock.release()
# 
#     def run(self, filter_mask="10.1.1.1/32"):
#         while True:
#             self.get_new_packets(filter_mask)


from unittest import TestCase
from topology.sniffer.sniffing_service import PacketExporter
from topology.sniffer.daemon import SniffingDaemon
from unittest.mock import MagicMock
        if self.acqd:
            self.acqd = False
        self.rels += 1

class TestPacketExporter(TestCase):
    def build_daemon(self, shared_list):
        lock = GiveUpLock()
        return PacketExporter(shared_list, lock)

    def test_no_new_packets(self):
        self.assertListEqual([], self.build_daemon([]).process())

    def test_all_packets_are_retreived(self):
        lst = [1, {"x" : "y"}]
        self.assertListEqual(lst[:], self.build_daemon(lst).process())

    def test_shared_list_becomes_empty(self):
        lst = [1, 2, 3]
        self.build_daemon(lst).process()
        self.assertListEqual(lst, [])

    def test_daemon_waits_for_lock_release(self):
        lock = GiveUpLock()
        daemon = PacketExporter([1, 2, 3], lock)

        lock.acquire()
        self.assertRaises(Exception, daemon.process)

    def test_lock_used_and_released(self):
        lock = GiveUpLock()

# Path: topology/sniffer/sniffing_service.py
# class PacketExporter(Component):
#     """
#     Component used to export packets.
# 
#     The packets that are taken from the list of shared packets are then
#     discarded from the list of shared packets.
#     """
#     def __init__(self, shared_packets, lock):
#         self.shared_packets = shared_packets
#         self.lock = lock
# 
#     def process(self, unused=None):
#         """
#         :return: returns a list of packets as a dictionary
#         """
#         self.lock.acquire()
# 
#         packets = self.shared_packets[:]
#         self.shared_packets[:] = []
# 
#         self.lock.release()
#         return packets
# 
# Path: topology/sniffer/daemon.py
# class SniffingDaemon():
#     """
#     A daemon that itertes through a series of device connections
#     and gets sniffed devices.
#     """
#     def __init__(self, shared_packets, lock, connections=discover_devices):
#         """
#         :param shared_packets: a list of packets discovered by the sniffer
#         :param lock: the lock that protects the shared_packets list
#         :connections: the connections used to pool packets
#         """
#         self.packets = shared_packets
#         self.lock = lock
# 
#         # If the default argument was given, then we call discover_devices
#         if connections == discover_devices:
#             self.connections = discover_devices()
#         else:
#             self.connections = connections
# 
#     def get_new_packets(self, filter_mask="10.1.1.1/32"):
#         """
#         Updates the list of shared packets via new packests provided through
#         scans.
# 
#         For the moment we ask each device for a packet. If this proves to
#         be a problem, we need to group the reads.
# 
#         :param filter_mask: the filter mask of form `IP/bits` used to filter
#             the GOOD packets
#         :return: returns nothing
#         """
#         new_packets = []
#         for connection in self.connections:
#             try:
#                 (header, packet) = connection.next()
#                 packet = parse_packet(packet)
#                 packet = filter_packet(packet, filter_mask)
#                 if packet is not None:
#                     new_packets.append(packet)
#                     logger.debug(packet)
#             except Exception as e:
#                 # Non-eth packets received
#                 pass
# 
#         self.lock.acquire()
# 
#         for packet in new_packets:
#             self.packets.append(packet)
# 
#         self.lock.release()
# 
#     def run(self, filter_mask="10.1.1.1/32"):
#         while True:
#             self.get_new_packets(filter_mask)


# Path: tests/topology/sniffer/test_daemon.py
from unittest import TestCase
from topology.sniffer.sniffing_service import PacketExporter
from topology.sniffer.daemon import SniffingDaemon
from unittest.mock import MagicMock

        if self.acqd:
            self.acqd = False
        self.rels += 1

class TestPacketExporter(TestCase):
    def build_daemon(self, shared_list):
        lock = GiveUpLock()
        return PacketExporter(shared_list, lock)

    def test_no_new_packets(self):
        self.assertListEqual([], self.build_daemon([]).process())

    def test_all_packets_are_retreived(self):
        lst = [1, {"x" : "y"}]
        self.assertListEqual(lst[:], self.build_daemon(lst).process())

    def test_shared_list_becomes_empty(self):
        lst = [1, 2, 3]
        self.build_daemon(lst).process()
        self.assertListEqual(lst, [])

    def test_daemon_waits_for_lock_release(self):
        lock = GiveUpLock()
        daemon = PacketExporter([1, 2, 3], lock)

        lock.acquire()
        self.assertRaises(Exception, daemon.process)

    def test_lock_used_and_released(self):
        lock = GiveUpLock()

        daemon = PacketExporter([1, 2, 3], lock)