From 455214e67aaef3c8004faf502734ed94891bec41 Mon Sep 17 00:00:00 2001 From: Tobias Bossert Date: Fri, 17 Mar 2023 19:48:07 +0100 Subject: [PATCH] Fixed test command, added tests for the ip manager module Signed-off-by: Tobias Bossert --- Makefile.am | 2 +- t/IPManager_v4.t | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ t/IPManager_v6.t | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 t/IPManager_v4.t create mode 100644 t/IPManager_v6.t diff --git a/Makefile.am b/Makefile.am index 4c5ccf93..85ab8726 100644 --- a/Makefile.am +++ b/Makefile.am @@ -38,4 +38,4 @@ share/messages.pot: $(PM) $(CALLBACKERY_PM) for lang in $(LANGUAGES); do ( test -f $$lang || cp share/messages.pot $$lang ); $(MSGMERGE) -s --force-po -U $$lang share/messages.pot; done test: - $(MJ_CLASS)_CONFIG=t/etc/wgwrangler.yaml prove -w + $(MJ_CLASS)_CONFIG_HOME=t/etc prove -w diff --git a/t/IPManager_v4.t b/t/IPManager_v4.t new file mode 100644 index 00000000..cdefa4e3 --- /dev/null +++ b/t/IPManager_v4.t @@ -0,0 +1,64 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More; + +use experimental 'signatures'; +use FindBin; +use lib $FindBin::Bin.'/../thirdparty/lib/perl5'; +use lib $FindBin::Bin.'/../lib'; +use Net::IPManager; +use Net::IPManager::Constants; + +my $IPManager = Net::IPManager->new(); +use constant TEST_INTERFACE => 'test-interface'; + +$IPManager->populate_range(TEST_INTERFACE, '192.168.0.0/24'); + +# acquire single ip and check if the suggestions are updated accordingly +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '192.168.0.1/32', "Single ip suggestion 1"; +$IPManager->acquire_single(TEST_INTERFACE, '192.168.0.1/32'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '192.168.0.2/32', "Single ip suggestion 2"; + +# Release ip again and check if suggestions are updated accordingly +$IPManager->release_ip(TEST_INTERFACE, '192.168.0.1/32'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '192.168.0.1/32', "Single ip successfully released"; + +# acquire other (higher) ip and check if suggestions are still returning the lowest possible address +$IPManager->acquire_single(TEST_INTERFACE, '192.168.0.10/32'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '192.168.0.1/32', "Acquire arbitrary and suggest"; + +# Checks +# Try to required already occupied address +ok $IPManager->acquire_single(TEST_INTERFACE, '192.168.0.10/32') eq E_ALREADY_ACQUIRED, 'Already acquired'; + +# Try to acquire/release on unknown interface +ok $IPManager->acquire_single('DOES NOT EXIST', '192.168.0.10/32') eq E_UNKNOWN_INTERFACE, 'Unknown interface (acquire)'; +ok $IPManager->release_ip('DOES NOT EXIST', '192.168.0.10/32') eq E_UNKNOWN_INTERFACE, 'Unknown interface (acquire)'; + +# Wrong subnet +ok $IPManager->acquire_single(TEST_INTERFACE, '192.168.2.10/32') eq E_SUBNET_NOT_MATCHING, 'Non matching subnet (acquire)'; + +# Entire networks (192.168.0.0 - 192.168.0.7) +$IPManager->acquire_single(TEST_INTERFACE, '192.168.0.0/29'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '192.168.0.8/32', 'IP suggestions after network acquire'; +ok $IPManager->acquire_single(TEST_INTERFACE, '192.168.0.6/32') eq E_ALREADY_ACQUIRED, 'Try acquiring ip inside acquired subnet'; +ok $IPManager->acquire_single(TEST_INTERFACE, '192.168.0.8/29') eq E_ALREADY_ACQUIRED, 'Try acquiring with overlapping subnet'; + +# Multi acquire +$IPManager->acquire_multiple(TEST_INTERFACE, '192.168.0.8/32,192.168.0.9/32'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '192.168.0.11/32', 'IP suggestion after multi-acquire'; + +# try release ip inside acquired subnet +$IPManager->release_ip(TEST_INTERFACE, '192.168.0.4/32'); +ok $IPManager->acquire_single(TEST_INTERFACE, '192.168.0.4/32') eq E_ALREADY_ACQUIRED, 'Try release ip inside subnet'; + +# try acquire with overlapping subnet +ok $IPManager->acquire_single(TEST_INTERFACE, '192.168.0.8/29') eq E_ALREADY_ACQUIRED, 'Try acquire with overlapping address'; + +# try acquire first and last address of interface subnet +ok $IPManager->acquire_single(TEST_INTERFACE, '192.168.0.0/32') eq E_CANNOT_ACQUIRE_FIRST_OR_LAST, 'Try acquire first'; +ok $IPManager->acquire_single(TEST_INTERFACE, '192.168.0.255/32') eq E_CANNOT_ACQUIRE_FIRST_OR_LAST, 'Try acquire last'; + +done_testing(); + diff --git a/t/IPManager_v6.t b/t/IPManager_v6.t new file mode 100644 index 00000000..720babef --- /dev/null +++ b/t/IPManager_v6.t @@ -0,0 +1,64 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More; + +use experimental 'signatures'; +use FindBin; +use lib $FindBin::Bin.'/../thirdparty/lib/perl5'; +use lib $FindBin::Bin.'/../lib'; +use Net::IPManager; +use Net::IPManager::Constants; + +my $IPManager = Net::IPManager->new(); +use constant TEST_INTERFACE => 'test-interface'; + +$IPManager->populate_range(TEST_INTERFACE, '2001:db8::/64'); + +# acquire single ip and check if the suggestions are updated accordingly +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '2001:0db8:0000:0000:0000:0000:0000:0001/128', "Single ip suggestion 1"; +$IPManager->acquire_single(TEST_INTERFACE, '2001:db8::1/128'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '2001:0db8:0000:0000:0000:0000:0000:0002/128', "Single ip suggestion 2"; + +# Release ip again and check if suggestions are updated accordingly +$IPManager->release_ip(TEST_INTERFACE, '2001:db8::1/128'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '2001:0db8:0000:0000:0000:0000:0000:0001/128', "Single ip successfully released"; + +# acquire other (higher) ip and check if suggestions are still returning the lowest possible address +$IPManager->acquire_single(TEST_INTERFACE, '2001:db8::83/128'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '2001:0db8:0000:0000:0000:0000:0000:0001/128', "Acquire arbitrary and suggest"; + +# Checks +# Try to required already occupied address +ok $IPManager->acquire_single(TEST_INTERFACE, '2001:db8::83/128') eq E_ALREADY_ACQUIRED, 'Already acquired'; + +# Try to acquire/release on unknown interface +ok $IPManager->acquire_single('DOES NOT EXIST', '2001:db8::a/128') eq E_UNKNOWN_INTERFACE, 'Unknown interface (acquire)'; +ok $IPManager->release_ip('DOES NOT EXIST', '2001:0db8:0000:0000:0000:0000:0000:000a/128') eq E_UNKNOWN_INTERFACE, 'Unknown interface (acquire)'; + +# Wrong subnet +ok $IPManager->acquire_single(TEST_INTERFACE, '2001:db9::a/128') eq E_SUBNET_NOT_MATCHING, 'Non matching subnet (acquire)'; + +# Entire networks (2001:db8:: - 2001:db8::7f) +$IPManager->acquire_single(TEST_INTERFACE, '2001:db8::2/128'); +ok $IPManager->acquire_single(TEST_INTERFACE, '2001:db8::/121') eq E_ALREADY_ACQUIRED, 'Try acquiring overlapping subnet'; +# release ::2 and try again +$IPManager->release_ip(TEST_INTERFACE, '2001:db8::2/128'); +$IPManager->acquire_single(TEST_INTERFACE, '2001:db8::/121'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '2001:0db8:0000:0000:0000:0000:0000:0080/128', 'IP suggestions after network acquire'; +ok $IPManager->acquire_single(TEST_INTERFACE, '2001:db8::6f') eq E_ALREADY_ACQUIRED, 'Try acquiring ip inside acquired subnet'; + +# Multi acquire +$IPManager->acquire_single(TEST_INTERFACE, '2001:0db8:0000:0000:0000:0000:0000:0080/128'); +ok $IPManager->suggest_ip(TEST_INTERFACE) eq '2001:0db8:0000:0000:0000:0000:0000:0081/128', 'IP suggestion after multi-acquire'; + +# try release ip inside acquired subnet +$IPManager->release_ip(TEST_INTERFACE, '2001:db8::12/128'); +ok $IPManager->acquire_single(TEST_INTERFACE, '2001:db8::12/128') eq E_ALREADY_ACQUIRED, 'Try release ip inside subnet'; + +# try acquire first and last address of interface subnet +ok $IPManager->acquire_single(TEST_INTERFACE, '2001:db8::/128') eq E_CANNOT_ACQUIRE_FIRST_OR_LAST, 'Try acquire first'; +ok $IPManager->acquire_single(TEST_INTERFACE, '2001:0db8:0000:0000:ffff:ffff:ffff:ffff/128') eq E_CANNOT_ACQUIRE_FIRST_OR_LAST, 'Try acquire last'; + +done_testing(); +