File: sasl_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (116 lines) | stat: -rw-r--r-- 2,290 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe Mongo::Auth::StringPrep::Profiles::SASL do
  let(:prepared_data) do
    Mongo::Auth::StringPrep.prepare(
      data,
      mappings,
      prohibited,
      options
    )
  end

  let(:mappings) do
    Mongo::Auth::StringPrep::Profiles::SASL::MAPPINGS
  end

  let(:prohibited) do
    Mongo::Auth::StringPrep::Profiles::SASL::PROHIBITED
  end

  let(:options) do
    {
      normalize: true,
      bidi: true
    }
  end

  describe 'StringPrep#prepare' do
    context 'when there is unnecessary punctuation' do
      let(:data) do
        "I\u00ADX"
      end

      it 'removes the punctuation' do
        expect(prepared_data).to eq('IX')
      end
    end

    context 'when there are non-ASCII spaces' do
      let(:data) do
        "I\u2000X"
      end

      it 'replaces them with ASCII spaces' do
        expect(prepared_data).to eq('I X')
      end
    end

    context 'when the input is ASCII' do
      let(:data) do
        'user'
      end

      it 'returns the same string' do
        expect(prepared_data).to eq('user')
      end
    end

    context 'when the data contains uppercase characters' do
      let(:data) do
        'USER'
      end

      it 'preserves case' do
        expect(prepared_data).to eq('USER')
      end
    end

    context 'when the data contains single-character codes' do
      let(:data) do
        "\u00AA"
      end

      it 'normalizes the codes' do
        expect(prepared_data).to eq('a')
      end
    end

    context 'when the data contains multi-character codes' do
      let(:data) do
        "\u2168"
      end

      it 'normalizes the codes' do
        expect(prepared_data).to eq('IX')
      end
    end

    context 'when the data contains prohibited input' do
      let(:data) do
        "\u0007"
      end

      it 'raises an error' do
        expect {
          prepared_data
        }.to raise_error(Mongo::Error::FailedStringPrepValidation)
      end
    end

    context 'when the data contains invalid bidi input' do
      let(:data) do
        "\u0627\u0031"
      end

      it 'raises an error' do
        expect {
          prepared_data
        }.to raise_error(Mongo::Error::FailedStringPrepValidation)
      end
    end
  end
end