This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
/
test-uuid.rb
142 lines (114 loc) · 3.71 KB
/
test-uuid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# encoding: UTF-8
# Author:: Assaf Arkin assaf@labnotes.org
# Eric Hodel drbrain@segment7.net
# Copyright:: Copyright (c) 2005-2008 Assaf Arkin, Eric Hodel
# License:: MIT and/or Creative Commons Attribution-ShareAlike
require 'test/unit'
require 'rubygems'
require 'uuid'
require 'mocha'
class TestUUID < Test::Unit::TestCase
def test_state_file_creation
path = UUID.state_file
File.delete path if File.exist?(path)
UUID.new.generate
File.exist?(path)
end
def test_state_file_creation_mode
UUID.class_eval{ @state_file = nil; @mode = nil }
UUID.state_file 0666
path = UUID.state_file
File.delete path if File.exist?(path)
old_umask = File.umask(0022)
UUID.new.generate
File.umask(old_umask)
assert_equal '0666', sprintf('%04o', File.stat(path).mode & 0777)
end
def test_state_file_specify
path = File.join("path", "to", "ruby-uuid")
UUID.state_file = path
assert_equal path, UUID.state_file
end
def test_with_no_state_file
UUID.state_file = false
assert !UUID.state_file
uuid = UUID.new
assert_match(/\A[\da-f]{32}\z/i, uuid.generate(:compact))
seq = uuid.next_sequence
assert_equal seq + 1, uuid.next_sequence
assert !UUID.state_file
end
def validate_uuid_generator(uuid)
assert_match(/\A[\da-f]{32}\z/i, uuid.generate(:compact))
assert_match(/\A[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
uuid.generate(:default))
assert_match(/^urn:uuid:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
uuid.generate(:urn))
e = assert_raise ArgumentError do
uuid.generate :unknown
end
assert_equal 'invalid UUID format :unknown', e.message
end
def test_instance_generate
uuid = UUID.new
validate_uuid_generator(uuid)
end
def test_class_generate
assert_match(/\A[\da-f]{32}\z/i, UUID.generate(:compact))
assert_match(/\A[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
UUID.generate(:default))
assert_match(/^urn:uuid:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
UUID.generate(:urn))
e = assert_raise ArgumentError do
UUID.generate :unknown
end
assert_equal 'invalid UUID format :unknown', e.message
end
def test_class_validate
assert !UUID.validate('')
assert UUID.validate('01234567abcd8901efab234567890123'), 'compact'
assert UUID.validate('01234567-abcd-8901-efab-234567890123'), 'default'
assert UUID.validate('urn:uuid:01234567-abcd-8901-efab-234567890123'),
'urn'
assert UUID.validate('01234567ABCD8901EFAB234567890123'), 'compact'
assert UUID.validate('01234567-ABCD-8901-EFAB-234567890123'), 'default'
assert UUID.validate('urn:uuid:01234567-ABCD-8901-EFAB-234567890123'),
'urn'
end
def test_monotonic
seen = {}
uuid_gen = UUID.new
20_000.times do
uuid = uuid_gen.generate
assert !seen.has_key?(uuid), "UUID repeated"
seen[uuid] = true
end
end
def test_same_mac
class << foo = UUID.new
attr_reader :mac
end
class << bar = UUID.new
attr_reader :mac
end
assert_equal foo.mac, bar.mac
end
def test_increasing_sequence
class << foo = UUID.new
attr_reader :sequence
end
class << bar = UUID.new
attr_reader :sequence
end
assert_equal foo.sequence + 1, bar.sequence
end
def test_pseudo_random_mac_address
uuid_gen = UUID.new
Mac.stubs(:addr).returns "00:00:00:00:00:00"
assert uuid_gen.iee_mac_address == 0
[:compact, :default, :urn].each do |format|
assert UUID.validate(uuid_gen.generate(format)), format.to_s
end
validate_uuid_generator(uuid_gen)
end
end