File: test_nonce.rb

package info (click to toggle)
ruby-openid 2.1.8debian-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,676 kB
  • sloc: ruby: 16,506; xml: 219; sh: 24; makefile: 2
file content (89 lines) | stat: -rw-r--r-- 2,318 bytes parent folder | download | duplicates (4)
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
require 'test/unit'
require 'openid/store/nonce'

module OpenID
  class NonceTestCase < Test::Unit::TestCase

     NONCE_RE = /\A\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/

    def test_mk_nonce
      nonce = Nonce::mk_nonce
      assert(nonce.match(NONCE_RE))
      assert(nonce.size == 26)
    end

    def test_mk_nonce_time
      nonce = Nonce::mk_nonce(0)
      assert(nonce.match(NONCE_RE))
      assert(nonce.size == 26)
      assert(nonce.match(/^1970-01-01T00:00:00Z/))
    end

    def test_split
      s = '1970-01-01T00:00:00Z'
      expected_t = 0
      expected_salt = ''
      actual_t, actual_salt = Nonce::split_nonce(s)
      assert_equal(expected_t, actual_t)
      assert_equal(expected_salt, actual_salt)
    end

    def test_mk_split
      t = 42
      nonce_str = Nonce::mk_nonce(t)
      assert(nonce_str.match(NONCE_RE))
      at, salt = Nonce::split_nonce(nonce_str)
      assert_equal(6, salt.size)
      assert_equal(t, at)
    end

    def test_bad_split
      cases = [
        '',
        '1970-01-01T00:00:00+1:00',
        '1969-01-01T00:00:00Z',
        '1970-00-01T00:00:00Z',
        '1970.01-01T00:00:00Z',
        'Thu Sep  7 13:29:31 PDT 2006',
        'monkeys',
        ]
      cases.each{|c|
        assert_raises(ArgumentError, c.inspect) { Nonce::split_nonce(c) }
      }
    end

    def test_check_timestamp
      cases = [
        # exact, no allowed skew
        ['1970-01-01T00:00:00Z', 0, 0, true],

        # exact, large skew
        ['1970-01-01T00:00:00Z', 1000, 0, true],

        # no allowed skew, one second old
        ['1970-01-01T00:00:00Z', 0, 1, false],

        # many seconds old, outside of skew
        ['1970-01-01T00:00:00Z', 10, 50, false],

        # one second old, one second skew allowed
        ['1970-01-01T00:00:00Z', 1, 1, true],

        # One second in the future, one second skew allowed
        ['1970-01-01T00:00:02Z', 1, 1, true],

        # two seconds in the future, one second skew allowed
        ['1970-01-01T00:00:02Z', 1, 0, false],

        # malformed nonce string
        ['monkeys', 0, 0, false],
      ]
      
      cases.each{|c|
        (nonce_str, allowed_skew, now, expected) = c
        actual = Nonce::check_timestamp(nonce_str, allowed_skew, now)
        assert_equal(expected, actual, c.inspect)
      }
    end
  end
end