uuidtools 2.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ $VERBOSE=true
2
+
1
3
  spec_dir = File.expand_path(File.dirname(__FILE__))
2
4
  lib_dir = File.expand_path(File.join(spec_dir, "../lib"))
3
5
 
@@ -1,4 +1,238 @@
1
- require File.join(File.dirname(__FILE__), "../spec_helper.rb")
1
+ require File.expand_path("../../spec_helper.rb", __FILE__)
2
+
3
+ def pending_if_root_required
4
+ if @mac_address == nil
5
+ output = `ifconfig -a 2>&1`
6
+ if output =~ /inet/ &&
7
+ output =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ &&
8
+ output =~ /Warning: cannot open/
9
+ pending("Cannot get MAC address without root?")
10
+ end
11
+ end
12
+ end
13
+
14
+ # ===========================================================================
15
+ #
16
+ # Samples of ifconfig -a output
17
+ #
18
+ # ===========================================================================
19
+
20
+ #
21
+ # solaris
22
+ #
23
+ solaris_sample = <<EOF
24
+ lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
25
+ inet 127.0.0.1 netmask ff000000
26
+ igb1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
27
+ inet 10.51.0.18 netmask ffffff00 broadcast 10.51.0.255
28
+ ether 0:21:28:fa:c6:65
29
+ igb2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
30
+ inet 10.99.0.12 netmask ffffff00 broadcast 10.99.0.255
31
+ ether 0:21:28:fa:c6:66
32
+ EOF
33
+
34
+ solaris_mac = "00:21:28:fa:c6:65"
35
+
36
+ #
37
+ # windows
38
+ #
39
+ windows_sample = <<EOF
40
+ Windows IP Configuration
41
+
42
+ Host Name . . . . . . . . . . . . : OFFICE
43
+ Primary Dns Suffix . . . . . . . :
44
+ Node Type . . . . . . . . . . . . : Unknown
45
+ IP Routing Enabled. . . . . . . . : No
46
+ WINS Proxy Enabled. . . . . . . . : No
47
+
48
+ Ethernet adapter Local Area Connection:
49
+
50
+ Connection-specific DNS Suffix . :
51
+ Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller
52
+ Physical Address. . . . . . . . . : E0-CB-4E-5D-BC-E9
53
+ Dhcp Enabled. . . . . . . . . . . : No
54
+ IP Address. . . . . . . . . . . . : 192.168.1.10
55
+ Subnet Mask . . . . . . . . . . . : 255.255.255.0
56
+ Default Gateway . . . . . . . . . : 192.168.1.1
57
+ DNS Servers . . . . . . . . . . . : 192.168.1.1
58
+ 4.2.2.1
59
+ EOF
60
+
61
+ windows_mac = "e0:cb:4e:5d:bc:e9"
62
+
63
+ #
64
+ # linux
65
+ #
66
+ linux_sample = <<EOF
67
+ eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
68
+ inet 10.16.187.71 netmask 255.255.252.0 broadcast 10.16.187.255
69
+ inet6 fe80::226:2dff:fef6:b94 prefixlen 64 scopeid 0x20<link>
70
+ ether 00:26:2d:f6:0b:94 txqueuelen 1000 (Ethernet)
71
+ RX packets 172074 bytes 82784684 (78.9 MiB)
72
+ RX errors 0 dropped 0 overruns 0 frame 0
73
+ TX packets 99075 bytes 23551085 (22.4 MiB)
74
+ TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
75
+ device interrupt 20 memory 0xf2600000-f2620000
76
+
77
+ lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
78
+ inet 127.0.0.1 netmask 255.0.0.0
79
+ inet6 ::1 prefixlen 128 scopeid 0x10<host>
80
+ loop txqueuelen 0 (Local Loopback)
81
+ RX packets 20 bytes 2148 (2.0 KiB)
82
+ RX errors 0 dropped 0 overruns 0 frame 0
83
+ TX packets 20 bytes 2148 (2.0 KiB)
84
+ TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
85
+ EOF
86
+
87
+ linux_mac = "00:26:2d:f6:0b:94"
88
+
89
+ #
90
+ # alternate linux
91
+ #
92
+ linux_sample_2 = <<EOF
93
+ eth0 Link encap:Ethernet HWaddr 00:80:C8:F8:4A:51
94
+ inet addr:192.168.99.35 Bcast:192.168.99.255 Mask:255.255.255.0
95
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
96
+ RX packets:190312 errors:0 dropped:0 overruns:0 frame:0
97
+ TX packets:86955 errors:0 dropped:0 overruns:0 carrier:0
98
+ collisions:0 txqueuelen:100
99
+ RX bytes:30701229 (29.2 Mb) TX bytes:7878951 (7.5 Mb)
100
+ Interrupt:9 Base address:0x5000
101
+
102
+ lo Link encap:Local Loopback
103
+ inet addr:127.0.0.1 Mask:255.0.0.0
104
+ UP LOOPBACK RUNNING MTU:16436 Metric:1
105
+ RX packets:306 errors:0 dropped:0 overruns:0 frame:0
106
+ TX packets:306 errors:0 dropped:0 overruns:0 carrier:0
107
+ collisions:0 txqueuelen:0
108
+ RX bytes:29504 (28.8 Kb) TX bytes:29504 (28.8 Kb)
109
+ EOF
110
+
111
+ linux_mac_2 = "00:80:c8:f8:4a:51"
112
+
113
+ linux_ip_sample = <<EOF
114
+ 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
115
+ link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
116
+ inet 127.0.0.1/8 scope host lo
117
+ inet6 ::1/128 scope host
118
+ valid_lft forever preferred_lft forever
119
+ 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
120
+ link/ether 00:26:2d:f6:0b:94 brd ff:ff:ff:ff:ff:ff
121
+ inet 10.16.187.125/22 brd 10.16.187.255 scope global eth0
122
+ inet6 fe80::226:2dff:fef6:b94/64 scope link
123
+ valid_lft forever preferred_lft forever
124
+ 3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
125
+ link/ether 00:26:c6:c6:1a:b4 brd ff:ff:ff:ff:ff:ff
126
+ 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
127
+ link/ether 52:54:00:e3:cf:d3 brd ff:ff:ff:ff:ff:ff
128
+ inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
129
+ 5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500
130
+ link/ether 52:54:00:e3:cf:d3 brd ff:ff:ff:ff:ff:ff
131
+ EOF
132
+
133
+ linux_ip_mac = "00:26:2d:f6:0b:94"
134
+
135
+ #
136
+ # freebsd
137
+ #
138
+ freebsd_sample = <<EOF
139
+ igb0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
140
+ options=401bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,VLAN_HWTSO>
141
+ ether 00:25:90:2b:81:32
142
+ inet6 fe80::225:90ff:fe2b:8132%igb0 prefixlen 64 scopeid 0x1
143
+ nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
144
+ media: Ethernet autoselect (1000baseT <full-duplex>)
145
+ status: active
146
+ igb1: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
147
+ options=401bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,VLAN_HWTSO>
148
+ ether 00:25:90:2b:81:32
149
+ inet6 fe80::225:90ff:fe2b:8133%igb1 prefixlen 64 scopeid 0x2
150
+ nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
151
+ media: Ethernet autoselect (1000baseT <full-duplex>)
152
+ EOF
153
+
154
+ freebsd_mac = "00:25:90:2b:81:32"
155
+
156
+ #
157
+ # openbsd
158
+ #
159
+ openbsd_sample = <<EOF
160
+ lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33200
161
+ priority: 0
162
+ groups: lo
163
+ inet 127.0.0.1 netmask 0xff000000
164
+ inet6 ::1 prefixlen 128
165
+ inet6 fe80::1%lo0 prefixlen 64 scopeid 0x5
166
+ vr0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
167
+ lladdr 00:0d:b9:28:ab:44
168
+ priority: 0
169
+ media: Ethernet autoselect (100baseTX full-duplex)
170
+ status: active
171
+ inet 192.168.7.2 netmask 0xffffff00 broadcast 192.168.7.255
172
+ inet6 fe80::20d:b9ff:fe28:ab44%vr0 prefixlen 64 scopeid 0x1
173
+ EOF
174
+
175
+ openbsd_mac = "00:0d:b9:28:ab:44"
176
+
177
+ #
178
+ # MacOS 10
179
+ #
180
+ macos_sample = <<EOF
181
+ lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
182
+ options=3<RXCSUM,TXCSUM>
183
+ inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
184
+ inet 127.0.0.1 netmask 0xff000000
185
+ inet6 ::1 prefixlen 128
186
+ gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
187
+ stf0: flags=0<> mtu 1280
188
+ en0: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
189
+ options=27<RXCSUM,TXCSUM,VLAN_MTU,TSO4>
190
+ ether 58:b0:35:a4:cd:0c
191
+ inet6 fe80::5ab0:35ff:fea4:cd0c%en0 prefixlen 64 scopeid 0x4
192
+ inet 192.168.142.136 netmask 0xfffffff0 broadcast 192.168.142.143
193
+ media: autoselect (1000baseT <full-duplex,flow-control>)
194
+ status: active
195
+ en1: flags=8823<UP,BROADCAST,SMART,SIMPLEX,MULTICAST> mtu 1500
196
+ ether d8:30:62:51:dd:3d
197
+ media: autoselect (<unknown type>)
198
+ status: inactive
199
+ p2p0: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 2304
200
+ ether 0a:30:62:51:dd:3d
201
+ media: autoselect
202
+ status: inactive
203
+ EOF
204
+
205
+ macos_mac = "58:b0:35:a4:cd:0c"
206
+
207
+ # Gather the samples and MAC addresses for simple access
208
+ samples = {
209
+ :macos => macos_sample,
210
+ :windows => windows_sample,
211
+ :solaris => solaris_sample,
212
+ :freebsd => freebsd_sample,
213
+ :openbsd => openbsd_sample,
214
+ :linux => linux_sample,
215
+ :linux2 => linux_sample_2,
216
+ :linuxip => linux_ip_sample,
217
+ :docker => nil
218
+ }
219
+
220
+ macs = {
221
+ :macos => macos_mac,
222
+ :windows => windows_mac,
223
+ :solaris => solaris_mac,
224
+ :freebsd => freebsd_mac,
225
+ :openbsd => openbsd_mac,
226
+ :linux => linux_mac,
227
+ :linux2 => linux_mac_2,
228
+ :linuxip => linux_ip_mac
229
+ }
230
+
231
+ # --------------------------------------------------------------------------
232
+ #
233
+ # TESTS
234
+ #
235
+ # --------------------------------------------------------------------------
2
236
 
3
237
  describe UUIDTools::UUID, "when obtaining a MAC address" do
4
238
  before do
@@ -6,10 +240,229 @@ describe UUIDTools::UUID, "when obtaining a MAC address" do
6
240
  end
7
241
 
8
242
  it "should obtain a MAC address" do
9
- @mac_address.should_not be_nil
243
+ pending_if_root_required()
244
+ expect(@mac_address).not_to be_nil
10
245
  end
11
246
 
12
247
  it "should cache the MAC address" do
13
- @mac_address.object_id.should == UUIDTools::UUID.mac_address.object_id
248
+ pending_if_root_required()
249
+ expect(@mac_address.object_id).to eql(UUIDTools::UUID.mac_address.object_id)
250
+ end
251
+ end
252
+
253
+ describe UUIDTools::UUID, "before obtaining a MAC address" do
254
+
255
+ before do
256
+ module UUIDTools
257
+ class UUID
258
+ remove_class_variable(:@@mac_address) if defined?(@@mac_address)
259
+ end
260
+ end
261
+ end
262
+
263
+ it "should parse windows MAC addresses" do
264
+ # mock os_class() so that we don't get different mock behavior on windows
265
+ allow(UUIDTools::UUID).to receive(:os_class) { :windows }
266
+ allow(UUIDTools::UUID).to receive(:ipconfig) { samples[:windows] }
267
+ mac = UUIDTools::UUID.mac_address
268
+ expect(mac).to eql(macs[:windows])
269
+ end
270
+
271
+ it "should parse solaris MAC addresses" do
272
+ allow(UUIDTools::UUID).to receive(:os_class) { :solaris }
273
+ allow(UUIDTools::UUID).to receive(:ifconfig) { samples[:solaris] }
274
+ mac = UUIDTools::UUID.mac_address
275
+ expect(mac).to eql(macs[:solaris])
276
+ end
277
+
278
+ it "should parse freebsd MAC addresses" do
279
+ allow(UUIDTools::UUID).to receive(:os_class) { nil }
280
+ allow(UUIDTools::UUID).to receive(:ifconfig) { samples[:freebsd] }
281
+ mac = UUIDTools::UUID.mac_address
282
+ expect(mac).to eql(macs[:freebsd])
283
+ end
284
+
285
+ it "should parse openbsd MAC addresses" do
286
+ allow(UUIDTools::UUID).to receive(:os_class) { :openbsd }
287
+ allow(UUIDTools::UUID).to receive(:ifconfig) { samples[:openbsd] }
288
+ mac = UUIDTools::UUID.mac_address
289
+ expect(mac).to eql(macs[:openbsd])
290
+ end
291
+
292
+ it "should parse linux MAC addresses with ifconfig" do
293
+ allow(UUIDTools::UUID).to receive(:os_class) { nil }
294
+ allow(UUIDTools::UUID).to receive(:ifconfig) { samples[:linux] }
295
+ mac = UUIDTools::UUID.mac_address
296
+ expect(mac).to eql(macs[:linux])
297
+ end
298
+
299
+ it "should parse a linux HWaddr address with ifconfig" do
300
+ allow(UUIDTools::UUID).to receive(:os_class) { nil }
301
+ allow(UUIDTools::UUID).to receive(:ifconfig) { samples[:linux2] }
302
+ mac = UUIDTools::UUID.mac_address
303
+ expect(mac).to eql(macs[:linux2])
304
+ end
305
+
306
+ it "should parse macos MAC addresses with ifconfig" do
307
+ allow(UUIDTools::UUID).to receive(:os_class) { nil }
308
+ allow(UUIDTools::UUID).to receive(:ifconfig) { samples[:macos] }
309
+ mac = UUIDTools::UUID.mac_address
310
+ expect(mac).to eql(macs[:macos])
311
+ end
312
+
313
+ it "should parse linux MAC addresses with ip" do
314
+ allow(UUIDTools::UUID).to receive(:os_class) { nil }
315
+ allow(UUIDTools::UUID).to receive(:ifconfig) { samples[:linuxip] }
316
+ mac = UUIDTools::UUID.mac_address
317
+ expect(mac).to eql(macs[:linuxip])
318
+ end
319
+
320
+ it "should identify the default os classes" do
321
+ module RbConfig
322
+ CONFIG['target_os'] = nil
323
+ end
324
+
325
+ os_class = UUIDTools::UUID.os_class
326
+ expect(os_class).to be_nil
327
+
328
+ RbConfig::CONFIG['target_os'] = 'linux'
329
+ os_class = UUIDTools::UUID.os_class
330
+ expect(os_class).to be_nil
331
+
332
+ RbConfig::CONFIG['target_os'] = 'darwin'
333
+ os_class = UUIDTools::UUID.os_class
334
+ expect(os_class).to be_nil
335
+ end
336
+
337
+ it "should identify the solaris os classes" do
338
+ module RbConfig
339
+ CONFIG['target_os'] = "solaris"
340
+ end
341
+
342
+ os_class = UUIDTools::UUID.os_class
343
+ expect(os_class).to eql(:solaris)
344
+ end
345
+
346
+ it "should identify the BSD os classes" do
347
+ module RbConfig
348
+ CONFIG['target_os'] = "netbsd"
349
+ end
350
+
351
+ os_class = UUIDTools::UUID.os_class
352
+ expect(os_class).to eql(:netbsd)
353
+
354
+ RbConfig::CONFIG['target_os'] = "openbsd"
355
+ os_class = UUIDTools::UUID.os_class
356
+ expect(os_class).to eql(:openbsd)
357
+
358
+ end
359
+
360
+ it "should identify the Windows os classes" do
361
+ module RbConfig
362
+ CONFIG['target_os'] = "win"
363
+ end
364
+ os_class = UUIDTools::UUID.os_class
365
+ expect(os_class).to eql(:windows)
366
+
367
+ RbConfig::CONFIG['target_os'] = "w32"
368
+ os_class = UUIDTools::UUID.os_class
369
+ expect(os_class).to eql(:windows)
370
+
371
+ RbConfig::CONFIG['target_os'] = "darwin"
372
+ os_class = UUIDTools::UUID.os_class
373
+ expect(os_class).to be_nil
374
+
375
+ end
376
+
377
+ it "should find the ifconfig program" do
378
+ save_ifconfig_command = UUIDTools::UUID.ifconfig_command
379
+ save_ifconfig_path = UUIDTools::UUID.ifconfig_path_default
380
+
381
+ # this should always exist
382
+ UUIDTools::UUID.ifconfig_command="sh"
383
+ UUIDTools::UUID.ifconfig_path_default="notfound"
384
+ ifconfig_path = UUIDTools::UUID.ifconfig_path
385
+ # expect(ifconfig_path).to eql("/usr/bin/sh")
386
+
387
+ # Test what happens if it does not
388
+ UUIDTools::UUID.ifconfig_command="nosuchthing"
389
+ UUIDTools::UUID.ifconfig_path_default="default"
390
+
391
+ # ifconfig_path checks if the IFCONFIG_PATH command file exists
392
+ allow(File).to receive(:exist?) { true }
393
+
394
+ ifconfig_path = UUIDTools::UUID.ifconfig_path
395
+ # expect(ifconfig_path).to eql("default")
396
+
397
+ UUIDTools::UUID.ifconfig_command=save_ifconfig_command
398
+ UUIDTools::UUID.ifconfig_path_default=save_ifconfig_path
399
+
400
+ end
401
+
402
+ it "should find the ip program" do
403
+ save_ip_command = UUIDTools::UUID.ip_command
404
+ save_ip_path = UUIDTools::UUID.ip_path_default
405
+
406
+ # this should always exist
407
+ UUIDTools::UUID.ip_command="sh"
408
+ UUIDTools::UUID.ip_path_default="notfound"
409
+ ip_path = UUIDTools::UUID.ip_path
410
+ # expect(ip_path).to eql("/usr/bin/sh")
411
+
412
+ # Test what happens if it does not
413
+ UUIDTools::UUID.ip_command="nosuchthing"
414
+ UUIDTools::UUID.ip_path_default="default"
415
+
416
+ # ifconfig_path checks if the IP_PATH command file exists
417
+ allow(File).to receive(:exist?) { true }
418
+
419
+ ifconfig_path = UUIDTools::UUID.ip_path
420
+ # expect(ifconfig_path).to eql("default")
421
+
422
+ UUIDTools::UUID.ip_command=save_ip_command
423
+ UUIDTools::UUID.ip_path_default=save_ip_path
424
+ end
425
+
426
+ it "should be able to parse windows ipconfig output" do
427
+ mac = UUIDTools::UUID.first_mac samples[:windows]
428
+ expect(mac).to eql(macs[:windows])
429
+ end
430
+
431
+ it "should be able to parse solaris ifconfig output" do
432
+ mac = UUIDTools::UUID.first_mac samples[:solaris]
433
+ expect(mac).to eql(macs[:solaris])
434
+ end
435
+
436
+ it "should be able to parse freebsd ifconfig output" do
437
+ mac = UUIDTools::UUID.first_mac samples[:freebsd]
438
+ expect(mac).to eql(macs[:freebsd])
439
+ end
440
+
441
+ it "should be able to parse openbsd ifconfig output" do
442
+ mac = UUIDTools::UUID.first_mac samples[:openbsd]
443
+ expect(mac).to eql(macs[:openbsd])
444
+ end
445
+
446
+ it "should be able to parse linux ifconfig output" do
447
+ mac = UUIDTools::UUID.first_mac samples[:linux]
448
+ expect(mac).to eql(macs[:linux])
449
+
450
+ mac2 = UUIDTools::UUID.first_mac samples[:linux2]
451
+ expect(mac2).to eql(macs[:linux2])
452
+ end
453
+
454
+ it "should be able to parse macos ifconfig output" do
455
+ mac = UUIDTools::UUID.first_mac samples[:macos]
456
+ expect(mac).to eql(macs[:macos])
457
+ end
458
+
459
+ it "should be able to parse ip addr output" do
460
+ mac = UUIDTools::UUID.first_mac samples[:linuxip]
461
+ expect(mac).to eql(macs[:linuxip])
462
+ end
463
+
464
+ it "should return nil when unable to identify the mac address" do
465
+ mac = UUIDTools::UUID.first_mac nil
466
+ expect(mac).to be_nil
14
467
  end
15
468
  end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), "../spec_helper.rb")
1
+ require File.expand_path("../../spec_helper.rb", __FILE__)
2
2
 
3
3
  describe SecureRandom do
4
4
  it "should correctly obtain random bits" do
@@ -7,15 +7,15 @@ describe SecureRandom do
7
7
  bits << SecureRandom.random_bytes(16)
8
8
  end
9
9
  # Check to make sure that none of the 10,000 strings were duplicates
10
- (bits.map {|x| x.to_s}).uniq.size.should == bits.size
10
+ expect((bits.map {|x| x.to_s}).uniq.size).to eql(bits.size)
11
11
  end
12
12
 
13
13
  it "should return the correct number of random bits" do
14
- SecureRandom.random_bytes(16).size.should == 16
15
- SecureRandom.random_bytes(6).size.should == 6
14
+ expect(SecureRandom.random_bytes(16).size).to eql(16)
15
+ expect(SecureRandom.random_bytes(6).size).to eql(6)
16
16
  end
17
17
 
18
18
  it "should return a sane random number" do
19
- SecureRandom.random_number(5000).should < 5000
19
+ expect(SecureRandom.random_number(5000)).to be < 5000
20
20
  end
21
21
  end
@@ -1,37 +1,40 @@
1
- require File.join(File.dirname(__FILE__), "../spec_helper.rb")
1
+ require File.expand_path("../../spec_helper.rb", __FILE__)
2
2
 
3
3
  describe UUIDTools::UUID, "when generating" do
4
4
  it "should correctly generate SHA1 variant UUIDs" do
5
- UUIDTools::UUID.sha1_create(
5
+ expect(UUIDTools::UUID.sha1_create(
6
6
  UUIDTools::UUID_URL_NAMESPACE, 'http://sporkmonger.com'
7
- ).to_s.should == "f2d04685-b787-55da-8644-9bd28a6f5a53"
7
+ ).to_s).to eql('f2d04685-b787-55da-8644-9bd28a6f5a53')
8
8
  end
9
9
 
10
10
  it "should correctly generate MD5 variant UUIDs" do
11
- UUIDTools::UUID.md5_create(
11
+ expect(UUIDTools::UUID.md5_create(
12
12
  UUIDTools::UUID_URL_NAMESPACE, 'http://sporkmonger.com'
13
- ).to_s.should == "15074785-9071-3fe3-89bd-876e4b9e919b"
13
+ ).to_s).to eql('15074785-9071-3fe3-89bd-876e4b9e919b')
14
14
  end
15
15
 
16
16
  it "should correctly generate timestamp variant UUIDs" do
17
- UUIDTools::UUID.timestamp_create.should_not be_random_node_id
18
- UUIDTools::UUID.timestamp_create.to_s.should_not ==
17
+ allow(UUIDTools::UUID).to receive(:mac_address) { "aa:bb:cc:dd:ee:ff" }
18
+ expect(UUIDTools::UUID.timestamp_create).not_to be_random_node_id
19
+ expect(UUIDTools::UUID.timestamp_create.to_s).not_to eql(
19
20
  UUIDTools::UUID.timestamp_create.to_s
21
+ )
20
22
  current_time = Time.now
21
- UUIDTools::UUID.timestamp_create(current_time).to_s.should_not ==
23
+ expect(UUIDTools::UUID.timestamp_create(current_time).to_s).not_to eql(
22
24
  UUIDTools::UUID.timestamp_create(current_time).to_s
25
+ )
23
26
  uuids = []
24
27
  1000.times do
25
28
  uuids << UUIDTools::UUID.timestamp_create
26
29
  end
27
30
  # Check to make sure that none of the 1,000 UUIDs were duplicates
28
- (uuids.map {|x| x.to_s}).uniq.size.should == uuids.size
31
+ expect((uuids.map {|x| x.to_s}).uniq.size).to eql(uuids.size)
29
32
  end
30
33
 
31
34
  it "should correctly generate UUIDs without a MAC address" do
32
35
  mac_address = UUIDTools::UUID.mac_address
33
36
  UUIDTools::UUID.mac_address = nil
34
- UUIDTools::UUID.timestamp_create.should be_random_node_id
37
+ expect(UUIDTools::UUID.timestamp_create).to be_random_node_id
35
38
  UUIDTools::UUID.mac_address = mac_address
36
39
  end
37
40
 
@@ -41,81 +44,88 @@ describe UUIDTools::UUID, "when generating" do
41
44
  uuids << UUIDTools::UUID.random_create
42
45
  end
43
46
  # Check to make sure that none of the 1,000 UUIDs were duplicates
44
- (uuids.map {|x| x.to_s}).uniq.size.should == uuids.size
47
+ expect((uuids.map {|x| x.to_s}).uniq.size).to eql(uuids.size)
48
+ end
49
+
50
+ it "should not have internal state used in string representations" do
51
+ uuid = UUIDTools::UUID.random_create
52
+ uuid_string = uuid.to_s.dup
53
+ uuid.to_s.gsub!("-", "/")
54
+ expect(uuid.to_s).to eql(uuid_string)
45
55
  end
46
56
 
47
57
  it "should throw an exception if a segment has an invalid value" do
48
- (lambda do
58
+ expect do
49
59
  UUIDTools::UUID.new(-1, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
50
- end).should raise_error(ArgumentError)
51
- (lambda do
60
+ end.to raise_error(ArgumentError)
61
+ expect do
52
62
  UUIDTools::UUID.new(4294967296, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
53
- end).should raise_error(ArgumentError)
63
+ end.to raise_error(ArgumentError)
54
64
  end
55
65
 
56
66
  it "should throw an exception if a segment has an invalid value" do
57
- (lambda do
67
+ expect do
58
68
  UUIDTools::UUID.new(0, -1, 0, 0, 0, [0, 0, 0, 0, 0, 0])
59
- end).should raise_error(ArgumentError)
60
- (lambda do
69
+ end.to raise_error(ArgumentError)
70
+ expect do
61
71
  UUIDTools::UUID.new(0, 65536, 0, 0, 0, [0, 0, 0, 0, 0, 0])
62
- end).should raise_error(ArgumentError)
72
+ end.to raise_error(ArgumentError)
63
73
  end
64
74
 
65
75
  it "should throw an exception if a segment has an invalid value" do
66
- (lambda do
76
+ expect do
67
77
  UUIDTools::UUID.new(0, 0, -1, 0, 0, [0, 0, 0, 0, 0, 0])
68
- end).should raise_error(ArgumentError)
69
- (lambda do
78
+ end.to raise_error(ArgumentError)
79
+ expect do
70
80
  UUIDTools::UUID.new(0, 0, 65536, 0, 0, [0, 0, 0, 0, 0, 0])
71
- end).should raise_error(ArgumentError)
81
+ end.to raise_error(ArgumentError)
72
82
  end
73
83
 
74
84
  it "should throw an exception if a segment has an invalid value" do
75
- (lambda do
85
+ expect do
76
86
  UUIDTools::UUID.new(0, 0, 0, -1, 0, [0, 0, 0, 0, 0, 0])
77
- end).should raise_error(ArgumentError)
78
- (lambda do
87
+ end.to raise_error(ArgumentError)
88
+ expect do
79
89
  UUIDTools::UUID.new(0, 0, 0, 256, 0, [0, 0, 0, 0, 0, 0])
80
- end).should raise_error(ArgumentError)
90
+ end.to raise_error(ArgumentError)
81
91
  end
82
92
 
83
93
  it "should throw an exception if a segment has an invalid value" do
84
- (lambda do
94
+ expect do
85
95
  UUIDTools::UUID.new(0, 0, 0, 0, -1, [0, 0, 0, 0, 0, 0])
86
- end).should raise_error(ArgumentError)
87
- (lambda do
96
+ end.to raise_error(ArgumentError)
97
+ expect do
88
98
  UUIDTools::UUID.new(0, 0, 0, 0, 256, [0, 0, 0, 0, 0, 0])
89
- end).should raise_error(ArgumentError)
99
+ end.to raise_error(ArgumentError)
90
100
  end
91
101
 
92
102
  it "should throw an exception if nodes are not a collection" do
93
- (lambda do
103
+ expect do
94
104
  UUIDTools::UUID.new(0, 0, 0, 0, 0, :bogus)
95
- end).should raise_error(TypeError)
105
+ end.to raise_error(TypeError)
96
106
  end
97
107
 
98
108
  it "should throw an exception if nodes are the wrong size" do
99
- (lambda do
109
+ expect do
100
110
  UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0])
101
- end).should raise_error(ArgumentError)
111
+ end.to raise_error(ArgumentError)
102
112
  end
103
113
 
104
114
  it "should throw an exception if any nodes have invalid values" do
105
- (lambda do
115
+ expect do
106
116
  UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 256])
107
- end).should raise_error(ArgumentError)
117
+ end.to raise_error(ArgumentError)
108
118
  end
109
119
 
110
120
  it "should throw an exception if parsing anything but a String" do
111
- (lambda do
121
+ expect do
112
122
  UUIDTools::UUID.parse(:bogus)
113
- end).should raise_error(TypeError)
123
+ end.to raise_error(TypeError)
114
124
  end
115
125
 
116
126
  it "should throw an exception if raw parsing anything but a String" do
117
- (lambda do
127
+ expect do
118
128
  UUIDTools::UUID.parse_raw(:bogus)
119
- end).should raise_error(TypeError)
129
+ end.to raise_error(TypeError)
120
130
  end
121
131
  end