summaryrefslogtreecommitdiff
path: root/test/test_securerandom.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-12-05 21:53:35 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-12-09 20:26:44 +0900
commit12a0a89e22fbc312e4a95a7749bc153532daa855 (patch)
treec02bab74641503e5a9cdb4ce1d2db02241e9fb43 /test/test_securerandom.rb
parent4258c8df867b497369a815e92f741aebf1469b0d (diff)
[ruby/securerandom] Split Random::Formatter from SecureRandom [Feature #18190]
https://github.com/ruby/securerandom/commit/1e57277b9e
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5237
Diffstat (limited to 'test/test_securerandom.rb')
-rw-r--r--test/test_securerandom.rb102
1 files changed, 4 insertions, 98 deletions
diff --git a/test/test_securerandom.rb b/test/test_securerandom.rb
index f9130ab81e..e4a0e17365 100644
--- a/test/test_securerandom.rb
+++ b/test/test_securerandom.rb
@@ -1,21 +1,17 @@
# frozen_string_literal: false
require 'test/unit'
require 'securerandom'
+require_relative 'ruby/test_random_formatter'
# This testcase does NOT aim to test cryptographically strongness and randomness.
class TestSecureRandom < Test::Unit::TestCase
+ include Random::Formatter::FormatterTest
+ include Random::Formatter::NotDefaultTest
+
def setup
@it = SecureRandom
end
- def test_s_random_bytes
- assert_equal(16, @it.random_bytes.size)
- assert_equal(Encoding::ASCII_8BIT, @it.random_bytes.encoding)
- 65.times do |idx|
- assert_equal(idx, @it.random_bytes(idx).size)
- end
- end
-
# This test took 2 minutes on my machine.
# And 65536 times loop could not be enough for forcing PID recycle.
if false
@@ -69,96 +65,6 @@ if false
end
end
- def test_s_hex
- s = @it.hex
- assert_equal(16 * 2, s.size)
- assert_match(/\A\h+\z/, s)
- 33.times do |idx|
- s = @it.hex(idx)
- assert_equal(idx * 2, s.size)
- assert_match(/\A\h*\z/, s)
- end
- end
-
- def test_hex_encoding
- assert_equal(Encoding::US_ASCII, @it.hex.encoding)
- end
-
- def test_s_base64
- assert_equal(16, @it.base64.unpack('m*')[0].size)
- 17.times do |idx|
- assert_equal(idx, @it.base64(idx).unpack('m*')[0].size)
- end
- end
-
- def test_s_urlsafe_base64
- safe = /[\n+\/]/
- 65.times do |idx|
- assert_not_match(safe, @it.urlsafe_base64(idx))
- end
- # base64 can include unsafe byte
- assert((0..10000).any? {|idx| safe =~ @it.base64(idx)}, "None of base64(0..10000) is url-safe")
- end
-
- def test_s_random_number_float
- 101.times do
- v = @it.random_number
- assert_in_range(0.0...1.0, v)
- end
- end
-
- def test_s_random_number_float_by_zero
- 101.times do
- v = @it.random_number(0)
- assert_in_range(0.0...1.0, v)
- end
- end
-
- def test_s_random_number_int
- 101.times do |idx|
- next if idx.zero?
- v = @it.random_number(idx)
- assert_in_range(0...idx, v)
- end
- end
-
- def test_s_random_number_not_default
- msg = "SecureRandom#random_number should not be affected by srand"
- seed = srand(0)
- x = @it.random_number(1000)
- 10.times do|i|
- srand(0)
- return unless @it.random_number(1000) == x
- end
- srand(0)
- assert_not_equal(x, @it.random_number(1000), msg)
- ensure
- srand(seed) if seed
- end
-
- def test_uuid
- uuid = @it.uuid
- assert_equal(36, uuid.size)
-
- # Check time_hi_and_version and clock_seq_hi_res bits (RFC 4122 4.4)
- assert_equal('4', uuid[14])
- assert_include(%w'8 9 a b', uuid[19])
-
- assert_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/, uuid)
- end
-
- def test_alphanumeric
- 65.times do |n|
- an = @it.alphanumeric(n)
- assert_match(/\A[0-9a-zA-Z]*\z/, an)
- assert_equal(n, an.length)
- end
- end
-
- def assert_in_range(range, result, mesg = nil)
- assert(range.cover?(result), build_message(mesg, "Expected #{result} to be in #{range}"))
- end
-
def test_with_openssl
begin
require 'openssl'