uuid 2.3.1 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGELOG +4 -0
  2. data/lib/uuid.rb +56 -6
  3. data/test/test-uuid.rb +19 -3
  4. data/uuid.gemspec +13 -14
  5. metadata +9 -18
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 2.3.2 (2011-04-11)
2
+ * Allow usage on environments that don't have MAC address accessible (mikezter)
3
+ * Fix for JRuby expand_path interpretation on windows (pinnymz)
4
+
1
5
  2.3.1 (2010-05-07)
2
6
  * Fix: Open state file in binary mode (http://github.com/assaf/uuid/issues#issue/8)
3
7
 
@@ -11,6 +11,7 @@ require 'thread'
11
11
  require 'tmpdir'
12
12
  require 'socket'
13
13
  require 'macaddr'
14
+ require 'digest/sha1'
14
15
 
15
16
 
16
17
  ##
@@ -169,7 +170,7 @@ class UUID
169
170
  if File.writable?(state_dir) then
170
171
  @state_file = File.join(state_dir, 'ruby-uuid')
171
172
  else
172
- @state_file = File.expand_path(File.join('~', '.ruby-uuid'))
173
+ @state_file = File.expand_path('.ruby-uuid', '~')
173
174
  end
174
175
 
175
176
  @state_file
@@ -194,6 +195,55 @@ class UUID
194
195
  uuid =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i
195
196
  end
196
197
 
198
+ ##
199
+ # Generate a pseudo MAC address because we have no pure-ruby way
200
+ # to know the MAC address of the NIC this system uses. Note
201
+ # that cheating with pseudo arresses here is completely legal:
202
+ # see Section 4.5 of RFC4122 for details.
203
+ #
204
+ # This implementation is shamelessly stolen from
205
+ # https://github.com/spectra/ruby-uuid/blob/master/uuid.rb
206
+ # Thanks spectra.
207
+ #
208
+ def pseudo_mac_address
209
+ sha1 = ::Digest::SHA1.new
210
+ 256.times do
211
+ r = [rand(0x100000000)].pack "N"
212
+ sha1.update r
213
+ end
214
+ str = sha1.digest
215
+ r = rand 14 # 20-6
216
+ node = str[r, 6] || str
217
+ if RUBY_VERSION >= "1.9.0"
218
+ nnode = node.bytes.to_a
219
+ nnode[0] |= 0x01
220
+ node = ''
221
+ nnode.each { |s| node << s.chr }
222
+ else
223
+ node[0] |= 0x01 # multicast bit
224
+ end
225
+ node.bytes.collect{|b|b.to_s(16)}.join.hex & 0x7FFFFFFFFFFF
226
+ end
227
+
228
+ ##
229
+ # Uses system calls to get a mac address
230
+ #
231
+ def iee_mac_address
232
+ begin
233
+ Mac.addr.gsub(/:|-/, '').hex & 0x7FFFFFFFFFFF
234
+ rescue
235
+ 0
236
+ end
237
+ end
238
+
239
+ ##
240
+ # return iee_mac_address if available, pseudo_mac_address otherwise
241
+ #
242
+ def mac_address
243
+ return iee_mac_address unless iee_mac_address == 0
244
+ return pseudo_mac_address
245
+ end
246
+
197
247
  ##
198
248
  # Create a new UUID generator. You really only need to do this once.
199
249
  def initialize
@@ -205,8 +255,8 @@ class UUID
205
255
  if state_file && File.size?(state_file) then
206
256
  next_sequence
207
257
  else
208
- @mac = Mac.addr.gsub(/:|-/, '').hex & 0x7FFFFFFFFFFF
209
- fail "Cannot determine MAC address from any available interface, tried with #{Mac.addr}" if @mac == 0
258
+ @mac = mac_address
259
+ fail "Cannot determine MAC address from any available interface, tried with #{mac_address}" if @mac == 0
210
260
  @sequence = rand 0x10000
211
261
 
212
262
  if state_file
@@ -358,9 +408,9 @@ protected
358
408
  def listen(address)
359
409
  sock = bind(address)
360
410
  while client = sock.accept
361
- Thread.start(client) do |client|
362
- while client.read 1
363
- client.write @generator.generate
411
+ Thread.start(client) do |socket|
412
+ while socket.read 1
413
+ socket.write @generator.generate
364
414
  end
365
415
  end
366
416
  end
@@ -4,7 +4,9 @@
4
4
  # License:: MIT and/or Creative Commons Attribution-ShareAlike
5
5
 
6
6
  require 'test/unit'
7
+ require 'rubygems'
7
8
  require 'uuid'
9
+ require 'mocha'
8
10
 
9
11
  class TestUUID < Test::Unit::TestCase
10
12
 
@@ -31,8 +33,7 @@ class TestUUID < Test::Unit::TestCase
31
33
  assert !UUID.state_file
32
34
  end
33
35
 
34
- def test_instance_generate
35
- uuid = UUID.new
36
+ def validate_uuid_generator(uuid)
36
37
  assert_match(/\A[\da-f]{32}\z/i, uuid.generate(:compact))
37
38
 
38
39
  assert_match(/\A[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
@@ -44,8 +45,13 @@ class TestUUID < Test::Unit::TestCase
44
45
  e = assert_raise ArgumentError do
45
46
  uuid.generate :unknown
46
47
  end
47
-
48
48
  assert_equal 'invalid UUID format :unknown', e.message
49
+
50
+ end
51
+
52
+ def test_instance_generate
53
+ uuid = UUID.new
54
+ validate_uuid_generator(uuid)
49
55
  end
50
56
 
51
57
  def test_class_generate
@@ -108,5 +114,15 @@ class TestUUID < Test::Unit::TestCase
108
114
  assert_equal foo.sequence + 1, bar.sequence
109
115
  end
110
116
 
117
+ def test_pseudo_random_mac_address
118
+ uuid_gen = UUID.new
119
+ Mac.stubs(:addr).returns "00:00:00:00:00:00"
120
+ assert uuid_gen.iee_mac_address == 0
121
+ [:compact, :default, :urn].each do |format|
122
+ assert UUID.validate(uuid_gen.generate(format)), format.to_s
123
+ end
124
+ validate_uuid_generator(uuid_gen)
125
+ end
126
+
111
127
  end
112
128
 
@@ -1,23 +1,22 @@
1
- spec = Gem::Specification.new do |spec|
2
- spec.name = 'uuid'
3
- spec.version = '2.3.1'
4
- spec.summary = "UUID generator"
5
- spec.description = <<-EOF
1
+ Gem::Specification.new do |s|
2
+ s.name = 'uuid'
3
+ s.version = '2.3.2'
4
+ s.summary = "UUID generator"
5
+ s.description = <<-EOF
6
6
  UUID generator for producing universally unique identifiers based on RFC 4122
7
7
  (http://www.ietf.org/rfc/rfc4122.txt).
8
8
  EOF
9
9
 
10
- spec.authors << 'Assaf Arkin' << 'Eric Hodel'
11
- spec.email = 'assaf@labnotes.org'
12
- spec.homepage = 'http://github.com/assaf/uuid'
10
+ s.authors << 'Assaf Arkin' << 'Eric Hodel'
11
+ s.email = 'assaf@labnotes.org'
12
+ s.homepage = 'http://github.com/assaf/uuid'
13
13
 
14
- spec.files = Dir['{bin,test,lib,docs}/**/*'] + ['README.rdoc', 'MIT-LICENSE', 'Rakefile', 'CHANGELOG', 'uuid.gemspec']
15
- spec.executables = "uuid"
14
+ s.files = Dir['{bin,test,lib,docs}/**/*'] + ['README.rdoc', 'MIT-LICENSE', 'Rakefile', 'CHANGELOG', 'uuid.gemspec']
15
+ s.executables = "uuid"
16
16
 
17
- spec.has_rdoc = true
18
- spec.rdoc_options << '--main' << 'README.rdoc' << '--title' << 'UUID generator' << '--line-numbers'
17
+ s.rdoc_options << '--main' << 'README.rdoc' << '--title' << 'UUID generator' << '--line-numbers'
19
18
  '--webcvs' << 'http://github.com/assaf/uuid'
20
- spec.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
19
+ s.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
21
20
 
22
- spec.add_dependency 'macaddr', ['~>1.0']
21
+ s.add_dependency 'macaddr', ['~>1.0']
23
22
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuid
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 2
7
- - 3
8
- - 1
9
- version: 2.3.1
4
+ prerelease:
5
+ version: 2.3.2
10
6
  platform: ruby
11
7
  authors:
12
8
  - Assaf Arkin
@@ -15,21 +11,18 @@ autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
13
 
18
- date: 2010-05-07 00:00:00 -07:00
19
- default_executable:
14
+ date: 2011-04-11 00:00:00 Z
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
22
17
  name: macaddr
18
+ prerelease: false
23
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
24
21
  requirements:
25
22
  - - ~>
26
23
  - !ruby/object:Gem::Version
27
- segments:
28
- - 1
29
- - 0
30
24
  version: "1.0"
31
25
  type: :runtime
32
- prerelease: false
33
26
  version_requirements: *id001
34
27
  description: |
35
28
  UUID generator for producing universally unique identifiers based on RFC 4122
@@ -52,7 +45,6 @@ files:
52
45
  - Rakefile
53
46
  - CHANGELOG
54
47
  - uuid.gemspec
55
- has_rdoc: true
56
48
  homepage: http://github.com/assaf/uuid
57
49
  licenses: []
58
50
 
@@ -66,25 +58,24 @@ rdoc_options:
66
58
  require_paths:
67
59
  - lib
68
60
  required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
69
62
  requirements:
70
63
  - - ">="
71
64
  - !ruby/object:Gem::Version
72
- segments:
73
- - 0
74
65
  version: "0"
75
66
  required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
76
68
  requirements:
77
69
  - - ">="
78
70
  - !ruby/object:Gem::Version
79
- segments:
80
- - 0
81
71
  version: "0"
82
72
  requirements: []
83
73
 
84
74
  rubyforge_project:
85
- rubygems_version: 1.3.6
75
+ rubygems_version: 1.7.2
86
76
  signing_key:
87
77
  specification_version: 3
88
78
  summary: UUID generator
89
79
  test_files: []
90
80
 
81
+ has_rdoc: