C0 code coverage information generated on Wed Mar 15 10:52:01 MST 2006
Marked code looks like this.
This line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this.
Finally, here's a line marked as not executed.
1 # This library can be used to perform operations on IP information
2 # from the configuration database. The classes defined here are
3 # representative of objects in the database:
4 #
5 # Network object => row in the network table
6 # NetworkPrefix object => row in the network_prefix table
7 # NetworkPrefixes object => the entire (or a subset of the) network_prefix table
8 #
9 # These classes add upon the data by adding operations that can be performed.
10 #
11 # TODO: Write code to dump network information from the database into one or
12 # more PSV files
13 # TODO: Write code to read those PSV files and create these objects
14
15 require 'ipaddr'
16 require 'test/unit'
17
18
19 class Network
20 attr_reader :name, :mask
21
22 def initialize(name = '', mask = '0.0.0.0/24')
23 @name = name
24 @mask = mask
25 end
26
27 def mask_complement_as_string
28 (~IPAddr.new(@mask)).to_s
29 end
30
31 end
32
33 class NetworkPrefix
34 attr_reader :type, :partition
35
36 def initialize(network, type, partition, prefix)
37 @network = network
38 @type = type
39 @partition = partition
40 @prefix = IPAddr.new(prefix)
41 end
42
43 def network_name
44 @network.name
45 end
46
47 def contains_ip?(ip)
48 @prefix.mask(@network.mask) == IPAddr.new(ip).mask(@network.mask)
49 end
50
51 def get_ip(base_ip)
52 # The network mask (@network.mask) defines which part of the IP address
53 # comes from the prefix, and which part comes from the base IP. So we mask (&) the prefix with the
54 # mask to get the first half, mask the base_ip with the complement of the mask to get the second
55 # half, then or the two halves together. We return the result as a string.
56 first_half = @prefix.mask(@network.mask)
57 second_half = IPAddr.new(base_ip).mask(@network.mask_complement_as_string)
58 return (first_half | second_half).to_s
59 end
60 end
61
62 class NetworkPrefixes
63 def initialize
64 @prefixes = []
65 end
66
67 def add_prefix( new_prefix )
68 @prefixes.push( new_prefix )
69 end
70
71 def prefix_for_ip( ip )
72 @prefixes.detect { |prefix| prefix.contains_ip?(ip) }
73 end
74
75 def prefix_for_network(name, type, partition)
76 @prefixes.detect { |prefix| prefix.network_name == name and
77 prefix.type == type and
78 prefix.partition == partition }
79 end
80 end

