-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UUID continuation #4453
UUID continuation #4453
Changes from 56 commits
3b1bae7
f0bb4d8
a9019b6
cbfd749
a2e23a3
3d69d69
133e84d
c6a9604
0ca59d6
2d881d9
e48a7d5
a48e4fb
64e64f7
df3fd11
55ad17a
c1cf14d
8295cfa
b7c17f3
c6b781c
8fee1b6
f72b57b
7139655
f3fce3d
dfbc0c1
a71f569
41302ca
0f7d36f
9242f73
baa9277
e34308e
5dce117
ad9a37f
509ff75
e55718d
4cfd90e
79f5b7d
7b4b9ad
2cae97a
f369a34
b325f3d
ac2d176
9290bf7
6044754
627a8e1
cc14710
8ff542e
8639668
8d25980
8062232
8f47d38
d79b51f
9828bd7
0366f78
409fe5b
e3bee29
49a6372
17ddbbf
f282061
a80ff09
b4f1340
4669f9a
1c3e811
3addefc
30db4ad
eec5ece
494d20b
6a25156
264f729
6a143fa
1c8db2b
3712820
f6f6070
122f84b
c496177
9c49e7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
require "spec" | ||
require "uuid" | ||
|
||
describe "UUID" do | ||
describe "default initialize" do | ||
end | ||
|
||
describe "initialize with slice" do | ||
it "initialize with slice only" do | ||
subject = UUID.new(StaticArray(UInt8, 16).new(0_u8)) | ||
subject.to_s.should eq "00000000-0000-4000-8000-000000000000" | ||
subject.variant.should eq UUID::Variant::RFC4122 | ||
subject.version.should eq UUID::Version::V4 | ||
end | ||
|
||
it "initialize with slice and variant" do | ||
subject = UUID.new(StaticArray(UInt8, 16).new(0_u8), variant: UUID::Variant::NCS) | ||
subject.to_s.should eq "00000000-0000-4000-0000-000000000000" | ||
subject.variant.should eq UUID::Variant::NCS | ||
subject.version.should eq UUID::Version::V4 | ||
end | ||
|
||
it "initialize with slice and version" do | ||
subject = UUID.new(StaticArray(UInt8, 16).new(0_u8), version: UUID::Version::V3) | ||
subject.to_s.should eq "00000000-0000-3000-8000-000000000000" | ||
subject.variant.should eq UUID::Variant::RFC4122 | ||
subject.version.should eq UUID::Version::V3 | ||
end | ||
end | ||
|
||
describe "initialize with static array" do | ||
it "initialize with static array only" do | ||
subject = UUID.new(StaticArray(UInt8, 16).new(0_u8)) | ||
subject.to_s.should eq "00000000-0000-4000-8000-000000000000" | ||
subject.variant.should eq UUID::Variant::RFC4122 | ||
subject.version.should eq UUID::Version::V4 | ||
end | ||
|
||
it "initialize with static array and variant" do | ||
subject = UUID.new(StaticArray(UInt8, 16).new(0_u8), variant: UUID::Variant::NCS) | ||
subject.to_s.should eq "00000000-0000-4000-0000-000000000000" | ||
subject.variant.should eq UUID::Variant::NCS | ||
subject.version.should eq UUID::Version::V4 | ||
end | ||
|
||
it "initialize with static array and version" do | ||
subject = UUID.new(StaticArray(UInt8, 16).new(0_u8), version: UUID::Version::V3) | ||
subject.to_s.should eq "00000000-0000-3000-8000-000000000000" | ||
subject.variant.should eq UUID::Variant::RFC4122 | ||
subject.version.should eq UUID::Version::V3 | ||
end | ||
end | ||
|
||
describe "initialize with String" do | ||
it "initialize with static array only" do | ||
subject = UUID.new("00000000-0000-0000-0000-000000000000") | ||
subject.to_s.should eq "00000000-0000-4000-8000-000000000000" | ||
subject.variant.should eq UUID::Variant::RFC4122 | ||
subject.version.should eq UUID::Version::V4 | ||
end | ||
|
||
it "initialize with static array and variant" do | ||
subject = UUID.new("00000000-0000-0000-0000-000000000000", variant: UUID::Variant::NCS) | ||
subject.to_s.should eq "00000000-0000-4000-0000-000000000000" | ||
subject.variant.should eq UUID::Variant::NCS | ||
subject.version.should eq UUID::Version::V4 | ||
end | ||
|
||
it "initialize with static array and version" do | ||
subject = UUID.new("00000000-0000-0000-0000-000000000000", version: UUID::Version::V3) | ||
subject.to_s.should eq "00000000-0000-3000-8000-000000000000" | ||
subject.variant.should eq UUID::Variant::RFC4122 | ||
subject.version.should eq UUID::Version::V3 | ||
end | ||
|
||
it "can be built from strings" do | ||
UUID.new("c20335c3-7f46-4126-aae9-f665434ad12b").should eq("c20335c3-7f46-4126-aae9-f665434ad12b") | ||
UUID.new("c20335c37f464126aae9f665434ad12b").should eq("c20335c3-7f46-4126-aae9-f665434ad12b") | ||
UUID.new("C20335C3-7F46-4126-AAE9-F665434AD12B").should eq("c20335c3-7f46-4126-aae9-f665434ad12b") | ||
UUID.new("C20335C37F464126AAE9F665434AD12B").should eq("c20335c3-7f46-4126-aae9-f665434ad12b") | ||
end | ||
end | ||
|
||
it "initializes zeroed UUID" do | ||
UUID.empty.should eq UUID.new(StaticArray(UInt8, 16).new(0_u8), UUID::Variant::NCS, UUID::Version::V4) | ||
UUID.empty.to_s.should eq "00000000-0000-4000-0000-000000000000" | ||
UUID.empty.variant.should eq UUID::Variant::NCS | ||
UUID.empty.version.should eq UUID::Version::V4 | ||
end | ||
|
||
describe "supports different string formats" do | ||
it "normal output" do | ||
UUID.new("ee843b2656d8472bb3430b94ed9077ff").to_s.should eq "ee843b26-56d8-472b-b343-0b94ed9077ff" | ||
end | ||
|
||
it "hexstring" do | ||
UUID.new("3e806983-eca4-4fc5-b581-f30fb03ec9e5").hexstring.should eq "3e806983eca44fc5b581f30fb03ec9e5" | ||
end | ||
|
||
it "urn" do | ||
UUID.new("1ed1ee2f-ef9a-4f9c-9615-ab14d8ef2892").urn.should eq "urn:uuid:1ed1ee2f-ef9a-4f9c-9615-ab14d8ef2892" | ||
end | ||
|
||
it "compares to strings" do | ||
uuid = UUID.new "c3b46146eb794e18877b4d46a10d1517" | ||
uuid.should eq("c3b46146eb794e18877b4d46a10d1517") | ||
uuid.should eq("c3b46146-eb79-4e18-877b-4d46a10d1517") | ||
uuid.should eq("C3B46146-EB79-4E18-877B-4D46A10D1517") | ||
uuid.should eq("urn:uuid:C3B46146-EB79-4E18-877B-4D46A10D1517") | ||
uuid.should eq("urn:uuid:c3b46146-eb79-4e18-877b-4d46a10d1517") | ||
(UUID.new).should_not eq("C3B46146-EB79-4E18-877B-4D46A10D1517") | ||
end | ||
end | ||
|
||
it "fails on invalid arguments when creating" do | ||
expect_raises(ArgumentError) { UUID.new "" } | ||
expect_raises(ArgumentError) { UUID.new "25d6f843?cf8e-44fb-9f84-6062419c4330" } | ||
expect_raises(ArgumentError) { UUID.new "67dc9e24-0865 474b-9fe7-61445bfea3b5" } | ||
expect_raises(ArgumentError) { UUID.new "5942cde5-10d1-416b+85c4-9fc473fa1037" } | ||
expect_raises(ArgumentError) { UUID.new "0f02a229-4898-4029-926f=94be5628a7fd" } | ||
expect_raises(ArgumentError) { UUID.new "cda08c86-6413-474f-8822-a6646e0fb19G" } | ||
expect_raises(ArgumentError) { UUID.new "2b1bfW06368947e59ac07c3ffdaf514c" } | ||
end | ||
|
||
it "fails when comparing to invalid strings" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That looks odd, are you sure calling |
||
expect_raises(ArgumentError) { UUID.new == "" } | ||
expect_raises(ArgumentError) { UUID.new == "d1fb9189-7013-4915-a8b1-07cfc83bca3U" } | ||
expect_raises(ArgumentError) { UUID.new == "2ab8ffc8f58749e197eda3e3d14e0 6c" } | ||
expect_raises(ArgumentError) { UUID.new == "2ab8ffc8f58749e197eda3e3d14e 06c" } | ||
expect_raises(ArgumentError) { UUID.new == "2ab8ffc8f58749e197eda3e3d14e-76c" } | ||
end | ||
|
||
it "should handle variant" do | ||
uuid = UUID.new | ||
expect_raises(ArgumentError) { uuid.variant = UUID::Variant::Unknown } | ||
{% for variant in %w(NCS RFC4122 Microsoft Future) %} | ||
uuid.variant = UUID::Variant::{{ variant.id }} | ||
uuid.variant.should eq UUID::Variant::{{ variant.id }} | ||
{% end %} | ||
end | ||
|
||
it "should handle version" do | ||
uuid = UUID.new | ||
expect_raises(ArgumentError) { uuid.version = UUID::Version::Unknown } | ||
{% for version in %w(1 2 3 4 5) %} | ||
uuid.version = UUID::Version::V{{ version.id }} | ||
uuid.version.should eq UUID::Version::V{{ version.id }} | ||
{% end %} | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -373,34 +373,6 @@ module Random | |
random_bytes(n).hexstring | ||
end | ||
|
||
# Generates a UUID (Universally Unique Identifier). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why there's no replacement of # It generates a random v4 UUID (Universally Unique Identifier).
#
# See `UUID.new`
def uuid : UUID
UUID.new
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...because |
||
# | ||
# It generates a random v4 UUID. See | ||
# [RFC 4122 Section 4.4](https://tools.ietf.org/html/rfc4122#section-4.4) | ||
# for the used algorithm and its implications. | ||
# | ||
# ``` | ||
# Random::Secure.uuid # => "a4e319dd-a778-4a51-804e-66a07bc63358" | ||
# ``` | ||
# | ||
# It is recommended to use the secure `Random::Secure` as a source or another | ||
# cryptographically quality PRNG such as `Random::ISAAC` or ChaCha20. | ||
def uuid : String | ||
bytes = random_bytes(16) | ||
bytes[6] = (bytes[6] & 0x0f) | 0x40 | ||
bytes[8] = (bytes[8] & 0x3f) | 0x80 | ||
|
||
String.new(36) do |buffer| | ||
buffer[8] = buffer[13] = buffer[18] = buffer[23] = 45_u8 | ||
bytes[0, 4].hexstring(buffer + 0) | ||
bytes[4, 2].hexstring(buffer + 9) | ||
bytes[6, 2].hexstring(buffer + 14) | ||
bytes[8, 2].hexstring(buffer + 19) | ||
bytes[10, 6].hexstring(buffer + 24) | ||
{36, 36} | ||
end | ||
end | ||
|
||
# See `#rand`. | ||
def self.rand : Float64 | ||
DEFAULT.rand | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
describe
block is empty. Was this intentional?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out.
I have updated it.