aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/secure_random_test.rb
blob: 4b732339712a50049d6638e3e9adb8805d4e1714 (plain) (blame)
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
# frozen_string_literal: true

require "abstract_unit"
require "active_support/core_ext/securerandom"

class SecureRandomTest < ActiveSupport::TestCase
  def test_base58
    s1 = SecureRandom.base58
    s2 = SecureRandom.base58

    assert_not_equal s1, s2
    assert_equal 16, s1.length
  end

  def test_base58_with_length
    s1 = SecureRandom.base58(24)
    s2 = SecureRandom.base58(24)

    assert_not_equal s1, s2
    assert_equal 24, s1.length
  end

  def test_base36
    s1 = SecureRandom.base36
    s2 = SecureRandom.base36

    assert_not_equal s1, s2
    assert_equal 16, s1.length
    assert_match(/^[a-z0-9]+$/, s1)
    assert_match(/^[a-z0-9]+$/, s2)
  end

  def test_base36_with_length
    s1 = SecureRandom.base36(24)
    s2 = SecureRandom.base36(24)

    assert_not_equal s1, s2
    assert_equal 24, s1.length
    assert_match(/^[a-z0-9]+$/, s1)
    assert_match(/^[a-z0-9]+$/, s2)
  end
end