File: stringprep_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 (191 lines) | stat: -rw-r--r-- 4,237 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe Mongo::Auth::StringPrep do
  include Mongo::Auth::StringPrep

  describe '#prepare' do
    let(:prepared_data) do
      prepare(data, mappings, prohibited, options)
    end

    context 'with no options' do
      let(:mappings) do
        []
      end

      let(:prohibited) do
        []
      end

      let(:options) do
        {}
      end

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

        it 'does not raise an error' do
          expect(prepared_data).to eq("\u0627\u0031")
        end
      end

      context 'when the data has unicode codes' do
        let(:data) do
          "ua\u030Aer"
        end

        it 'does not normalize the data' do
          expect(prepared_data).to eq("ua\u030Aer")
        end
      end
    end

    context 'with options specified' do
      let (:mappings) do
        [Mongo::Auth::StringPrep::Tables::B1, Mongo::Auth::StringPrep::Tables::B2]
      end

      let (:prohibited) do
        [
          Mongo::Auth::StringPrep::Tables::C1_1,
          Mongo::Auth::StringPrep::Tables::C1_2,
          Mongo::Auth::StringPrep::Tables::C6,
        ]
      end

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

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

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

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

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

      context 'when the input contains zero-width spaces' do
        let(:data) do
          "u\u200Ber"
        end

        it 'removes the zero-width spaces' do
          expect(prepared_data).to eq('uer')
        end
      end

      context 'when the input contains non-ASCII characters' do
        let(:data) do
          "u\u00DFer"
        end

        it 'maps the non-ASCII characters to ASCII' do
          expect(prepared_data).to eq('usser')
        end
      end

      context 'when the input contains unicode codes' do
        let(:data) do
          "ua\u030Aer"
        end

        it 'unicode normalizes the input' do
          expect(prepared_data).to eq("u\u00e5er")
        end
      end

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

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

      context 'when the data is proper bidi' do
        let(:data) do
          "\u0627\u0031\u0628"
        end

        it 'does not raise an error' do
          expect(
            prepared_data
          ).to eq("\u0627\u0031\u0628")
        end
      end

      context 'when bidi input contains prohibited bidi characters' do
        let(:data) do
          "\u0627\u0589\u0628"
        end

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

      context 'when bidi input has an invalid first bidi character' do
        let(:data) do
          "\u0031\u0627"
        end

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

      context 'when bidi input has an invalid last bidi character' do
        let(:data) do
          "\u0627\u0031"
        end

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

      context 'when bidi input has a bad character' do
        let(:data) do
          "\u206D"
        end

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