Skip to content

Commit

Permalink
Merge pull request #2832 from lunkwill42/test/fix-broken-statemon-tests
Browse files Browse the repository at this point in the history
Fix broken statemon tests
  • Loading branch information
lunkwill42 authored Feb 28, 2024
2 parents e535d5f + 0f31258 commit 0c15d1a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 57 deletions.
10 changes: 10 additions & 0 deletions tests/unittests/statemon/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import pytest


@pytest.fixture
def modulo_pid():
"""Returns this process' PID number, modulo 2^16. Because that's what pping uses
to produce ping packet IDs
"""
yield os.getpid() % 2**16
70 changes: 31 additions & 39 deletions tests/unittests/statemon/host_test.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,50 @@
from unittest import TestCase
import os
from nav.statemon.megaping import Host
from nav.statemon.icmppacket import PacketV4, PacketV6


class HostTestcase(TestCase):
def test_make_v4_packet(self):
"""
Test to make a v4 packet
"""
class TestHost:
"""Tests for the Host class"""

def test_make_v4_packet(self, modulo_pid):
"""Test to make a v4 packet"""
host = Host('127.0.0.1')
pid = os.getpid()
self.assertFalse(host.is_v6())
assert not host.is_v6()

self.assertTrue(host.packet)
self.assertTrue(isinstance(host.packet, PacketV4))
assert host.packet
assert isinstance(host.packet, PacketV4)

packet, cookie = host.make_packet(64)

self.assertTrue(packet)
self.assertTrue(cookie)
self.assertEqual(len(packet), 64)
self.assertEqual(len(cookie), 16)
assert packet
assert cookie
assert len(packet) == 64
assert len(cookie) == 16

self.assertEqual(host.packet.sequence, 0)
self.assertEqual(host.packet.id, pid)
assert host.packet.sequence == 0
assert host.packet.id, modulo_pid

def test_make_v6_packet(self):
"""
Test to make a v6 packet
"""
def test_make_v6_packet(self, modulo_pid):
"""Test to make a v6 packet"""
host = Host('2001:701::FFFF')
pid = os.getpid()
self.assertTrue(host.is_v6())
assert host.is_v6()

self.assertTrue(host.packet)
self.assertTrue(isinstance(host.packet, PacketV6))
assert host.packet
assert isinstance(host.packet, PacketV6)

packet, cookie = host.make_packet(64)

self.assertTrue(packet)
self.assertTrue(cookie)
self.assertEqual(len(packet), 64)
self.assertEqual(len(cookie), 16)
assert packet
assert cookie
assert len(packet) == 64
assert len(cookie) == 16

self.assertEqual(host.packet.sequence, 0)
self.assertEqual(host.packet.id, pid)
assert host.packet.sequence == 0
assert host.packet.id == modulo_pid

def test_ip_validation(self):
"""
Test the IP valdidation helper methods for both v6 & v4
"""
self.assertTrue(Host('129.241.105.210').is_valid_ipv4())
self.assertFalse(Host('129.241.105.256').is_valid_ipv4())

self.assertTrue(Host('2001:701::FFFF').is_valid_ipv6())
self.assertFalse(Host('127.0.0.1').is_valid_ipv6())
"""Test the IP valdidation helper methods for both v6 & v4"""
assert Host('129.241.105.210').is_valid_ipv4()
assert not Host('129.241.105.256').is_valid_ipv4()

assert Host('2001:701::FFFF').is_valid_ipv6()
assert not Host('127.0.0.1').is_valid_ipv6()
33 changes: 15 additions & 18 deletions tests/unittests/statemon/icmp_test.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
from nav.statemon.icmppacket import PacketV6, PacketV4, inet_checksum
from unittest import TestCase
import os


class ICMPPacketTestcase(TestCase):
def test_assemble_v6_packet_echo(self):

class TestICMPPacket:
def test_assemble_v6_packet_echo(self, modulo_pid):
# Make packet
packet = PacketV6()
packet.data = b'Testing'
packet.id = os.getpid()
packet.id = modulo_pid
packet.sequence = 3
packet = packet.assemble()

# Make Packet object which disassembles the raw packet
v6_packet = PacketV6(packet, False)

# Check if ICMP_ECHO
self.assertEqual(v6_packet.type, PacketV6.ICMP_ECHO)
assert v6_packet.type == PacketV6.ICMP_ECHO

# Check sequence number
self.assertEqual(v6_packet.sequence, 3)
assert v6_packet.sequence == 3

# Check payload
self.assertEqual(v6_packet.data, b'Testing')
assert v6_packet.data == b'Testing'

# Check if Id of the packet is process id
self.assertEqual(os.getpid(), v6_packet.id)
assert modulo_pid == v6_packet.id

# Check if the checksum is correct
unpacked_packet = packet[v6_packet.packet_slice]
self.assertEqual(inet_checksum(unpacked_packet), 0)

def test_assemble_v4_packet_echo(self):
assert inet_checksum(unpacked_packet) == 0

def test_assemble_v4_packet_echo(self, modulo_pid):
# Make packet
packet = PacketV4()
packet.data = b'Testing'
packet.id = os.getpid()
packet.id = modulo_pid
packet.sequence = 3
packet = packet.assemble()

Expand All @@ -49,17 +46,17 @@ def test_assemble_v4_packet_echo(self):
v4_packet = PacketV4(packet, False)

# Check if ICMP_ECHO
self.assertEqual(v4_packet.type, PacketV4.ICMP_ECHO)
assert v4_packet.type == PacketV4.ICMP_ECHO

# Check sequence number
self.assertEqual(v4_packet.sequence, 3)
assert v4_packet.sequence == 3

# Check if Id of the packet is process id
self.assertEqual(os.getpid(), v4_packet.id)
assert modulo_pid == v4_packet.id

# Check payload
self.assertEqual(v4_packet.data, b'Testing')
assert v4_packet.data == b'Testing'

# Check if the checksum is correct
unpacked_packet = packet[v4_packet.packet_slice]
self.assertEqual(inet_checksum(unpacked_packet), 0)
assert inet_checksum(unpacked_packet) == 0

0 comments on commit 0c15d1a

Please sign in to comment.