macaddr 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/macaddr.rb CHANGED
@@ -23,7 +23,7 @@ end
23
23
  require 'systemu'
24
24
 
25
25
  module Mac
26
- VERSION = '1.2.1'
26
+ VERSION = '1.3.0'
27
27
 
28
28
  def Mac.version
29
29
  ::Mac::VERSION
@@ -58,25 +58,30 @@ module Mac
58
58
 
59
59
  null = test(?e, '/dev/null') ? '/dev/null' : 'NUL'
60
60
 
61
- lines = nil
61
+ output = nil
62
62
  cmds.each do |cmd|
63
63
  status, stdout, stderr = systemu(cmd) rescue next
64
64
  next unless stdout and stdout.size > 0
65
- lines = stdout.split(/\n/) and break
65
+ output = stdout and break
66
66
  end
67
- raise "all of #{ cmds.join ' ' } failed" unless lines
67
+ raise "all of #{ cmds.join ' ' } failed" unless output
68
68
 
69
- candidates = lines.select{|line| line =~ re}
69
+ @mac_address = parse(output)
70
+ end
71
+
72
+ def parse(output)
73
+ lines = output.split(/\n/)
74
+
75
+ candidates = lines.select{|line| line =~ RE}
70
76
  raise 'no mac address candidates' unless candidates.first
71
- candidates.map!{|c| c[re].strip}
77
+ candidates.map!{|c| c[RE].strip}
72
78
 
73
79
  maddr = candidates.first
74
80
  raise 'no mac address found' unless maddr
75
81
 
76
82
  maddr.strip!
77
83
  maddr.instance_eval{ @list = candidates; def list() @list end }
78
-
79
- @mac_address = maddr
84
+ maddr
80
85
  end
81
86
 
82
87
  ##
@@ -84,6 +89,8 @@ module Mac
84
89
 
85
90
  alias_method "addr", "address"
86
91
  end
92
+
93
+ RE = %r/(?:[^:\-]|\A)(?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F](?:[^:\-]|\Z)/io
87
94
  end
88
95
 
89
- Macaddr = Mac
96
+ MacAddr = Macaddr = Mac
data/macaddr.gemspec CHANGED
@@ -3,13 +3,23 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "macaddr"
6
- spec.version = "1.2.1"
6
+ spec.version = "1.3.0"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "macaddr"
9
9
  spec.description = "description: macaddr kicks the ass"
10
10
 
11
11
  spec.files =
12
- ["LICENSE", "README", "Rakefile", "lib", "lib/macaddr.rb", "macaddr.gemspec"]
12
+ ["LICENSE",
13
+ "README",
14
+ "Rakefile",
15
+ "lib",
16
+ "lib/macaddr.rb",
17
+ "macaddr.gemspec",
18
+ "test",
19
+ "test/data",
20
+ "test/data/c8:bc:c8:9b:28:b1",
21
+ "test/mac_test.rb",
22
+ "test/testing.rb"]
13
23
 
14
24
  spec.executables = []
15
25
 
@@ -0,0 +1,39 @@
1
+ lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
2
+ inet6 ::1 prefixlen 128
3
+ inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
4
+ inet 127.0.0.1 netmask 0xff000000
5
+ inet6 fdb1:49bc:790c:b10:cabc:c8ff:fe9b:28b1 prefixlen 128
6
+ gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
7
+ stf0: flags=0<> mtu 1280
8
+ en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
9
+ ether c8:bc:c8:9b:28:b1
10
+ media: autoselect (none)
11
+ status: inactive
12
+ en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
13
+ ether c8:bc:c8:dc:c9:44
14
+ inet 10.0.1.7 netmask 0xffffff00 broadcast 10.0.1.255
15
+ media: autoselect
16
+ status: active
17
+ en2: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
18
+ ether 0b:0b:0b:0b:0b:0b
19
+ inet6 fe80::cabc:c8ff:fe9b:28b1%en2 prefixlen 64 scopeid 0x6
20
+ inet 169.254.67.37 netmask 0xffff0000 broadcast 169.254.255.255
21
+ media: autoselect (10baseT/UTP <full-duplex>)
22
+ status: active
23
+ fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
24
+ lladdr 78:ca:39:ff:fe:37:4f:44
25
+ media: autoselect <full-duplex>
26
+ status: inactive
27
+ vmnet1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
28
+ ether 00:50:56:c0:00:01
29
+ inet 172.16.139.1 netmask 0xffffff00 broadcast 172.16.139.255
30
+ vmnet8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
31
+ ether 00:50:56:c0:00:08
32
+ inet 192.168.33.1 netmask 0xffffff00 broadcast 192.168.33.255
33
+ en4: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
34
+ ether c8:bc:c8:dc:ba:8a
35
+ media: autoselect
36
+ status: inactive
37
+ utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1500
38
+ inet6 fe80::cabc:c8ff:fe9b:28b1%utun0 prefixlen 64 scopeid 0x9
39
+ inet6 fd00:6587:52d7:f8f2:cabc:c8ff:fe9b:28b1 prefixlen 64
data/test/mac_test.rb ADDED
@@ -0,0 +1,37 @@
1
+ Testing Mac do
2
+ $data.each do |basename, output|
3
+ testing "#{ basename } output" do
4
+ expected = basename
5
+
6
+ macaddr = assert{ Mac.parse(output) }
7
+
8
+ assert{ macaddr.is_a?(String) }
9
+ assert{ macaddr =~ Mac::RE }
10
+ assert{ macaddr.list.is_a?(Array) }
11
+ assert{ macaddr == expected }
12
+ end
13
+ end
14
+
15
+
16
+ testing ".addr" do
17
+ assert{ Mac.addr }
18
+ end
19
+ end
20
+
21
+
22
+ BEGIN {
23
+ $testdir = File.dirname(File.expand_path(__FILE__))
24
+ $rootdir = File.dirname($testdir)
25
+ $libdir = File.join($rootdir, 'lib')
26
+ require File.join($libdir, 'macaddr')
27
+ require File.join($testdir, 'testing')
28
+
29
+ $datadir = File.join($testdir, 'data')
30
+ $data = {}
31
+ glob = File.join($datadir, '**/**')
32
+ Dir.glob(glob) do |entry|
33
+ key = File.basename(entry)
34
+ val = IO.read(entry)
35
+ $data[key] = val
36
+ end
37
+ }
data/test/testing.rb ADDED
@@ -0,0 +1,146 @@
1
+ require 'test/unit'
2
+
3
+ testdir = File.expand_path(File.dirname(__FILE__))
4
+ rootdir = File.dirname(testdir)
5
+ libdir = File.join(rootdir, 'lib')
6
+
7
+ STDOUT.sync = true
8
+
9
+ $:.unshift(testdir) unless $:.include?(testdir)
10
+ $:.unshift(libdir) unless $:.include?(libdir)
11
+ $:.unshift(rootdir) unless $:.include?(rootdir)
12
+
13
+ def Testing(*args, &block)
14
+ Class.new(Test::Unit::TestCase) do
15
+
16
+ ## class methods
17
+ #
18
+ class << self
19
+ def context(*args, &block)
20
+ @context = args unless args.empty?
21
+ block.call(*@context) if block
22
+ @context
23
+ end
24
+
25
+ def slug_for(*args)
26
+ string = [@context, args].flatten.compact.join('-')
27
+ words = string.to_s.scan(%r/\w+/)
28
+ words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
29
+ words.delete_if{|word| word.nil? or word.strip.empty?}
30
+ words.join('-').downcase
31
+ end
32
+
33
+ def name() const_get(:Name) end
34
+
35
+ def testno()
36
+ '%05d' % (@testno ||= 0)
37
+ ensure
38
+ @testno += 1
39
+ end
40
+
41
+ def testing(*args, &block)
42
+ method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_')
43
+ define_method("test_#{ testno }_#{ slug_for(*args) }", &block)
44
+ end
45
+
46
+ def setup(&block)
47
+ define_method(:setup, &block) if block
48
+ end
49
+
50
+ def teardown(&block)
51
+ define_method(:teardown, &block) if block
52
+ end
53
+
54
+ def prepare(&block)
55
+ @prepare ||= []
56
+ @prepare.push(block) if block
57
+ @prepare
58
+ end
59
+
60
+ def cleanup(&block)
61
+ @cleanup ||= []
62
+ @cleanup.push(block) if block
63
+ @cleanup
64
+ end
65
+ end
66
+
67
+ ## configure the subclass!
68
+ #
69
+ const_set(:Testno, '0')
70
+ slug = slug_for(*args).gsub(%r/-/,'_')
71
+ name = ['TESTING', '%03d' % const_get(:Testno), slug].delete_if{|part| part.empty?}.join('_')
72
+ name = name.upcase!
73
+ const_set(:Name, name)
74
+ const_set(:Missing, Object.new.freeze)
75
+
76
+ ## instance methods
77
+ #
78
+ alias_method('__assert__', 'assert')
79
+
80
+ def assert(*args, &block)
81
+ if args.size == 1 and args.first.is_a?(Hash)
82
+ options = args.first
83
+ expected = getopt(:expected, options){ missing }
84
+ actual = getopt(:actual, options){ missing }
85
+ if expected == missing and actual == missing
86
+ actual, expected, *ignored = options.to_a.flatten
87
+ end
88
+ expected = expected.call() if expected.respond_to?(:call)
89
+ actual = actual.call() if actual.respond_to?(:call)
90
+ assert_equal(expected, actual)
91
+ end
92
+
93
+ if block
94
+ label = "assert(#{ args.join(' ') })"
95
+ result = nil
96
+ assert_nothing_raised{ result = block.call }
97
+ __assert__(result, label)
98
+ result
99
+ else
100
+ result = args.shift
101
+ label = "assert(#{ args.join(' ') })"
102
+ __assert__(result, label)
103
+ result
104
+ end
105
+ end
106
+
107
+ def missing
108
+ self.class.const_get(:Missing)
109
+ end
110
+
111
+ def getopt(opt, hash, options = nil, &block)
112
+ [opt.to_s, opt.to_s.to_sym].each do |key|
113
+ return hash[key] if hash.has_key?(key)
114
+ end
115
+ default =
116
+ if block
117
+ block.call
118
+ else
119
+ options.is_a?(Hash) ? options[:default] : nil
120
+ end
121
+ return default
122
+ end
123
+
124
+ def subclass_of exception
125
+ class << exception
126
+ def ==(other) super or self > other end
127
+ end
128
+ exception
129
+ end
130
+
131
+ ##
132
+ #
133
+ module_eval(&block)
134
+
135
+ self.setup()
136
+ self.prepare.each{|b| b.call()}
137
+
138
+ at_exit{
139
+ self.teardown()
140
+ self.cleanup.each{|b| b.call()}
141
+ }
142
+
143
+
144
+ self
145
+ end
146
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: macaddr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 2
9
- - 1
10
- version: 1.2.1
8
+ - 3
9
+ - 0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ara T. Howard
@@ -48,6 +48,9 @@ files:
48
48
  - Rakefile
49
49
  - lib/macaddr.rb
50
50
  - macaddr.gemspec
51
+ - test/data/c8:bc:c8:9b:28:b1
52
+ - test/mac_test.rb
53
+ - test/testing.rb
51
54
  has_rdoc: true
52
55
  homepage: https://github.com/ahoward/macaddr
53
56
  licenses: []