From 2d3854b37b08f6da3d3e1ddcd584a1b572dc7906 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 23 Nov 2012 15:06:49 +0000 Subject: Moved the code to detect whether an IP-address is inside/outside the Bytemark range into its own class & added _simple_ test-cases. --- lib/custodian/util/bytemark.rb | 43 ++++++++++++++++++++++++++ t/test-custodian-util-bytemark.rb | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 lib/custodian/util/bytemark.rb create mode 100755 t/test-custodian-util-bytemark.rb diff --git a/lib/custodian/util/bytemark.rb b/lib/custodian/util/bytemark.rb new file mode 100644 index 0000000..8143c4c --- /dev/null +++ b/lib/custodian/util/bytemark.rb @@ -0,0 +1,43 @@ + + +require 'ipaddr' + + + +# +# This class contains some Bytemark-specific tests/code. +# +module Custodian + + module Util + + class Bytemark + + + # + # The currently allocated IP-ranges which belong to Bytemark. + # + # These are used to test if an alert refers to a machine outwith our + # network. + # + BYTEMARK_RANGES = %w(80.68.80.0/20 89.16.160.0/19 212.110.160.0/19 46.43.0.0/18 91.223.58.0/24 213.138.96.0/19 5.153.224.0/21 2001:41c8::/32).collect{|i| IPAddr.new(i)} + + + # + # Is the named target inside the Bytemark IP-range? + # + def Bytemark.inside?( target ) + inside = false + + if ( BYTEMARK_RANGES.any?{|range| range.include?(IPAddr.new(target))} ) + inside = true + end + + inside + end + + + + end + end +end diff --git a/t/test-custodian-util-bytemark.rb b/t/test-custodian-util-bytemark.rb new file mode 100755 index 0000000..95222f0 --- /dev/null +++ b/t/test-custodian-util-bytemark.rb @@ -0,0 +1,65 @@ +#!/usr/bin/ruby1.8 -I./lib/ -I../lib/ + + +require 'custodian/util/bytemark' +require 'test/unit' + + + +# +# Unit test for our Bytemark utility class. +# +# +class TestBytemarkUtil < Test::Unit::TestCase + + # + # Create the test suite environment: NOP. + # + def setup + end + + # + # Destroy the test suite environment: NOP. + # + def teardown + end + + + # + # Test that we receive sensible results from the static inside? method + # + def test_ranges + + # + # Hash of hostnames and the expected result. + # + to_test = { + + # + # Hosts inside the Bytemark network + # + "80.68.85.48" => true, + "2001:41c8:125:46::10" => true, + + # + # Hosts outside the Bytemark network + # + "127.0.0.1" => false, + "192.168.1.1" => false, + "2a00:1450:400c:c00::93" => false, + } + + + to_test.each do |name,inside| + + if ( inside ) + assert( Custodian::Util::Bytemark.inside?( name ) == true ) + else + assert( Custodian::Util::Bytemark.inside?( name ) == false ) + end + end + + end + + +end -- cgit v1.2.1